Commit ffed68d412f7dc670fb3df65ec8d841e6aacdf18

Thomas de Grivel 2022-05-08T14:10:36

use Kmxgit.Git and git_nif.so for branches

diff --git a/c_src/git_nif.c b/c_src/git_nif.c
index e556e8a..22f3f7f 100644
--- a/c_src/git_nif.c
+++ b/c_src/git_nif.c
@@ -48,34 +48,32 @@ static ERL_NIF_TERM branches (ErlNifEnv *env, int argc,
   git_branch_iterator *i = NULL;
   git_reference *ref = NULL;
   git_branch_t ref_type = 0;
-  char *branch = NULL;
+  const char *branch = NULL;
   char *path = NULL;
+  ERL_NIF_TERM acc;
   ERL_NIF_TERM branches;
-  fprintf(stderr, "branches %d\n", argc);
+  ERL_NIF_TERM ok;
   if (argc != 1 || !argv || !argv[0])
     goto error;
   path = enif_term_to_string(env, argv[0]);
-  fprintf(stderr, "path: '%s'\n", path);
   if (!path || !path[0])
     goto error;
   if (git_repository_open(&r, path))
     goto error;
   git_branch_iterator_new(&i, r, GIT_BRANCH_ALL);
-  branches = enif_make_list(env, 0);
+  acc = enif_make_list(env, 0);
   while (!git_branch_next(&ref, &ref_type, i)) {
-    git_branch_name((const char **) &branch, ref);
-    fprintf(stderr, " %s", branch);
-    branches = push_string(env, branch, branches);
+    git_branch_name(&branch, ref);
+    acc = push_string(env, branch, acc);
   }
   git_branch_iterator_free(i);
+  ok = enif_make_atom(env, "ok");
+  if (! enif_make_reverse_list(env, acc, &branches))
+    goto error;
   free(path);
-  //free(branch);
-  fprintf(stderr, "\n");
-  return branches;
+  return enif_make_tuple2(env, ok, branches);
  error:
-  fprintf(stderr, "error\n");
   free(path);
-  //free(branch);
   return enif_make_atom(env, "error");
 }
 
diff --git a/lib/kmxgit/git.ex b/lib/kmxgit/git.ex
index baf3f7a..c194ac9 100644
--- a/lib/kmxgit/git.ex
+++ b/lib/kmxgit/git.ex
@@ -14,7 +14,6 @@ defmodule Kmxgit.Git do
   def branches(repo) do
     repo
     |> git_dir()
-    |> IO.inspect()
     |> branches_nif()
   end
 
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 64497a5..92d90e8 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -2,6 +2,7 @@ defmodule KmxgitWeb.RepositoryController do
   use KmxgitWeb, :controller
   require Logger
 
+  alias Kmxgit.Git
   alias Kmxgit.GitManager
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.RepositoryManager
@@ -204,7 +205,7 @@ defmodule KmxgitWeb.RepositoryController do
   end
 
   defp git_put_branches(git = %{valid: true}, repo, conn, op, path) do
-    case GitManager.branches(Repository.full_slug(repo)) do
+    case Git.branches(Repository.full_slug(repo)) do
       {:ok, branches} ->
         branch_trees = branches
         |> Enum.map(fn branch ->