Commit e293360476876cd35fd683ce3c90eb70312cc306

Thomas de Grivel 2022-05-31T12:37:15

use git nif for tags

diff --git a/c_src/git_nif.c b/c_src/git_nif.c
index 716d363..c1ea4d9 100644
--- a/c_src/git_nif.c
+++ b/c_src/git_nif.c
@@ -601,7 +601,51 @@ static ERL_NIF_TERM log_nif (ErlNifEnv *env, int argc,
   free(repo_dir);
   free(branch_name);
   free(path);
+  return res;
+}
 
+static ERL_NIF_TERM tags_nif (ErlNifEnv *env, int argc,
+                              const ERL_NIF_TERM argv[])
+{
+  git_repository *r = NULL;
+  char *repo_dir = NULL;
+  git_strarray tags = {0};
+  int i = 0;
+  ERL_NIF_TERM acc;
+  ERL_NIF_TERM tag;
+  ERL_NIF_TERM ok;
+  ERL_NIF_TERM res;
+  if (argc != 1 || !argv || !argv[0]) {
+    res = enif_make_atom(env, "badarg");
+    goto error;
+  }
+  repo_dir = enif_term_to_string(env, argv[0]);
+  if (!repo_dir || !repo_dir[0]) {
+    res = enif_make_atom(env, "repo_dir_missing");
+    goto error;
+  }
+  if (git_repository_open_bare(&r, repo_dir)) {
+    res = enif_make_atom(env, "git_repository_open_bare");
+    goto error;
+  }
+  if (git_tag_list(&tags, r)) {
+    res = enif_make_atom(env, "git_tag_list");
+    goto error;
+  }
+  acc = enif_make_list(env, 0);
+  for (i = tags.count - 1; i >= 0; i--) {
+    tag = enif_string_to_term(env, tags.strings[i]);
+    acc = enif_make_list_cell(env, tag, acc);
+  }
+  ok = enif_make_atom(env, "ok");
+  git_repository_free(r);
+  free(repo_dir);
+  return enif_make_tuple2(env, ok, acc);
+ error:
+  res = enif_make_tuple2(env, enif_make_atom(env, "error"), res);
+  enif_fprintf(stderr, "%T\n", res);
+  git_repository_free(r);
+  free(repo_dir);
   return res;
 }
 
@@ -626,6 +670,7 @@ static ErlNifFunc funcs[] = {
   {"create_nif",   1, create_nif,   0},
   {"files_nif",    3, files_nif,    0},
   {"log_nif",      5, log_nif,      0},
+  {"tags_nif",     1, tags_nif,     0}
 };
 
 ERL_NIF_INIT(Elixir.Kmxgit.Git, funcs, load, NULL, NULL, unload);
diff --git a/lib/kmxgit/git.ex b/lib/kmxgit/git.ex
index 40d5de9..2281239 100644
--- a/lib/kmxgit/git.ex
+++ b/lib/kmxgit/git.ex
@@ -76,4 +76,13 @@ defmodule Kmxgit.Git do
   def log_nif(_repo, _tree, _path, _skip, _limit) do
     exit(:nif_not_loaded)
   end
+
+  def tags(repo) do
+    dir = git_dir(repo)
+    tags_nif(dir)
+  end
+
+  def tags_nif(_repo) do
+    exit(:nif_not_loaded)
+  end
 end
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 1036a86..d277a03 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -340,10 +340,10 @@ defmodule KmxgitWeb.RepositoryController do
   end
 
   defp git_put_tags(git = %{valid: true}, repo, conn, op, path) do
-    case GitManager.tags(Repository.full_slug(repo)) do
+    case Git.tags(Repository.full_slug(repo)) do
       {:ok, tags} ->
         tag_trees = tags
-        |> Enum.map(fn %{tag: tag} ->
+        |> Enum.map(fn tag ->
           url = Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo) ++ ["_#{op || :tree}", tag] ++ (if path, do: String.split(path, "/"), else: []))
           {:tag, tag, url}
         end)
diff --git a/lib/kmxgit_web/templates/repository/show_properties.html.heex b/lib/kmxgit_web/templates/repository/show_properties.html.heex
index fdb26b3..8940434 100644
--- a/lib/kmxgit_web/templates/repository/show_properties.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_properties.html.heex
@@ -53,7 +53,7 @@
         <ul>
           <%= for tag <- @git.tags |> Enum.reverse() do %>
             <li>
-              <%= link tag.tag, to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo, ["_tag", tag.tag])) %>
+              <%= link tag, to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo, ["_tag", tag])) %>
             </li>
           <% end %>
         </ul>