Commit 0f1cd15370ecfab54312fbea77a9a97e91440a8e

Thomas de Grivel 2022-05-31T11:54:54

use git nif for log1

diff --git a/c_src/git_nif.c b/c_src/git_nif.c
index 0038b29..716d363 100644
--- a/c_src/git_nif.c
+++ b/c_src/git_nif.c
@@ -499,7 +499,8 @@ static ERL_NIF_TERM log_nif (ErlNifEnv *env, int argc,
   ERL_NIF_TERM log;
   ERL_NIF_TERM res;
   struct log_state s;
-  if (argc != 3 || !argv || !argv[0] || !argv[1] || !argv[2]) {
+  if (argc != 5 || !argv || !argv[0] || !argv[1] ||
+      !argv[2] || !argv[3] || !argv[4]) {
     res = enif_make_atom(env, "badarg");
     goto error;
   }
@@ -518,12 +519,11 @@ static ERL_NIF_TERM log_nif (ErlNifEnv *env, int argc,
     res = enif_make_atom(env, "path_missing");
     goto error;
   }
-  if (git_repository_open_bare(&r, repo_dir)) {
+  bzero(&s, sizeof(s));
+  if (git_repository_open_bare(&s.repo, repo_dir)) {
     res = enif_make_atom(env, "git_repository_open_bare");
     goto error;
   }
-  bzero(&s, sizeof(s));
-  s.repo = r;
   s.sorting = GIT_SORT_TIME;
   if (log_add_revision(&s, branch_name)) {
     res = enif_make_atom(env, "bad_branch");
@@ -531,7 +531,10 @@ static ERL_NIF_TERM log_nif (ErlNifEnv *env, int argc,
   }
   bzero(&opt, sizeof(opt));
   opt.max_parents = -1;
-  opt.limit = -1;
+  enif_get_int(env, argv[3], &i);
+  opt.skip = i;
+  enif_get_int(env, argv[4], &i);
+  opt.limit = i;
   if (path[0]) {
     diffopts.pathspec.strings = &path;
     diffopts.pathspec.count = 1;
@@ -622,7 +625,7 @@ static ErlNifFunc funcs[] = {
   {"content_nif",  2, content_nif,  0},
   {"create_nif",   1, create_nif,   0},
   {"files_nif",    3, files_nif,    0},
-  {"log_nif",      3, log_nif,      0},
+  {"log_nif",      5, log_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 ba77dce..40d5de9 100644
--- a/lib/kmxgit/git.ex
+++ b/lib/kmxgit/git.ex
@@ -66,14 +66,14 @@ defmodule Kmxgit.Git do
     :ok = :erlang.load_nif(path, 0)
   end
 
-  def log(repo, tree \\ "HEAD", path \\ "") do
+  def log(repo, tree \\ "HEAD", path \\ "", skip \\ 0, limit \\ 100) do
     tree = tree || "HEAD"
     dir = git_dir(repo)
     # [%{author: author, author_email: email, hash: hash, date: date, message: msg}]
-    log_nif(dir, tree, path)
+    log_nif(dir, tree, path, skip, limit)
   end
 
-  def log_nif(_repo, _tree, _path) do
+  def log_nif(_repo, _tree, _path, _skip, _limit) 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 51ccaac..1036a86 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -327,8 +327,8 @@ defmodule KmxgitWeb.RepositoryController do
 
   defp git_put_log1(git = %{valid: true}, repo, tree, path) do
     slug = Repository.full_slug(repo)
-    log1 = case if path, do: GitManager.log1_file(slug, path, tree), else: GitManager.log1(slug, tree) do
-             {:ok, log1} -> log1
+    log1 = case Git.log(slug, tree, path || "", 0, 1) do
+             {:ok, [log1]} -> log1
              {:error, err} ->
                IO.inspect(err)
                nil
diff --git a/lib/kmxgit_web/templates/repository/show_commit_message.html.heex b/lib/kmxgit_web/templates/repository/show_commit_message.html.heex
index 26071c1..4e042f3 100644
--- a/lib/kmxgit_web/templates/repository/show_commit_message.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_commit_message.html.heex
@@ -4,7 +4,7 @@
     <%= link String.slice(@git.log1.hash, 0..8), to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo, ["_log", @tree] ++ (if @path, do: String.split(@path, "/"), else: []))) <> "##{@git.log1.hash}" %>
   </span>
   <span class="date">
-    <%= @git.log1.date |> String.replace("T", " ") |> String.replace("+", " +") %>
+    <%= NaiveDateTime.add(~N[1970-01-01 00:00:00], @git.log1.date) %>
   </span>
   <br/>
   <span class="message" %>