Commit ffaab2b05d6305b67502844eb17effb9d677e53b

Thomas de Grivel 2022-05-12T12:01:21

refactor GitManager file operations, use Git.create

diff --git a/c_src/git_nif.c b/c_src/git_nif.c
index 7fe07c9..b92d923 100644
--- a/c_src/git_nif.c
+++ b/c_src/git_nif.c
@@ -275,10 +275,43 @@ static ERL_NIF_TERM files_nif (ErlNifEnv *env, int argc,
   return res;
 }
 
+static ERL_NIF_TERM create_nif (ErlNifEnv *env, int argc,
+                                const ERL_NIF_TERM argv[])
+{
+  ERL_NIF_TERM ok;
+  git_repository *r = NULL;
+  char *repo_dir = NULL;
+  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_init(&r, repo_dir, 1)) {
+    res = enif_make_atom(env, "git_repository_init");
+    goto error;
+  }
+  git_repository_free(r);
+  free(repo_dir);
+  ok = enif_make_atom(env, "ok");
+  return ok;
+ 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;
+}
+
 static ErlNifFunc funcs[] = {
   {"branches_nif", 1, branches_nif, 0},
-  {"content_nif", 2, content_nif, 0},
-  {"files_nif", 3, files_nif, 0},
+  {"content_nif",  2, content_nif,  0},
+  {"create_nif",   1, create_nif,   0},
+  {"files_nif",    3, files_nif,    0},
 };
 
 int load (ErlNifEnv *env, void **a, ERL_NIF_TERM b)
diff --git a/lib/kmxgit/git.ex b/lib/kmxgit/git.ex
index abd0632..0fa85f2 100644
--- a/lib/kmxgit/git.ex
+++ b/lib/kmxgit/git.ex
@@ -9,7 +9,7 @@ defmodule Kmxgit.Git do
     :ok = :erlang.load_nif(path, 0)
   end
 
-  # Functions
+  # NIFs
 
   def branches(repo) do
     repo
@@ -31,6 +31,17 @@ defmodule Kmxgit.Git do
     exit(:nif_not_loaded)
   end
 
+  def create(repo) do
+    dir = "#{@git_root}/#{Path.dirname(repo)}"
+    name = "#{Path.basename(repo)}.git"
+    :ok = File.mkdir_p(dir)
+    create_nif("#{dir}/#{name}")
+  end
+
+  def create_nif(_repo) do
+    exit(:nif_not_loaded)
+  end
+
   def files(repo, tree, path, parent \\ ".") do
     dir = git_dir(repo)
     case files_nif(dir, tree, path) do
diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index fbeeb6b..ca81e53 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -102,11 +102,9 @@ defmodule Kmxgit.GitManager do
       {:error, "file exists"}
     else
       dir = Path.dirname(dir_to)
-      :ok = File.mkdir_p(dir)
-      {out, status} = System.cmd("mv", [dir_from, dir_to], stderr_to_stdout: true)
-      case status do
-        0 -> :ok
-        _ -> {:error, out}
+      with :ok <- File.mkdir_p(dir),
+           :ok <- File.rename(dir_from, dir_to) do
+        :ok
       end
     end
   end
@@ -117,11 +115,7 @@ defmodule Kmxgit.GitManager do
     if File.exists?(dir_to) do
       {:error, "file exists"}
     else
-      {out, status} = System.cmd("mv", [dir_from, dir_to], stderr_to_stdout: true)
-      case status do
-        0 -> :ok
-        _ -> {:error, out}
-      end
+      File.rename(dir_from, dir_to)
     end
   end
 
@@ -170,14 +164,16 @@ defmodule Kmxgit.GitManager do
 
   def public_access(repo, true) do
     dir = git_dir(repo)
-    if ! File.exists?(export = "#{dir}/git-daemon-export-ok") do
+    export = "#{dir}/git-daemon-export-ok"
+    if ! File.exists?(export) do
       File.write!(export, "")
     end
     :ok
   end
   def public_access(repo, false) do
     dir = git_dir(repo)
-    if File.exists?(export = "#{dir}/git-daemon-export-ok") do
+    export = "#{dir}/git-daemon-export-ok"
+    if File.exists?(export) do
       File.rm!(export)
     end
     :ok
diff --git a/lib/kmxgit_web/controllers/admin/repository_controller.ex b/lib/kmxgit_web/controllers/admin/repository_controller.ex
index de31cfd..f4c6a54 100644
--- a/lib/kmxgit_web/controllers/admin/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/repository_controller.ex
@@ -2,6 +2,7 @@ defmodule KmxgitWeb.Admin.RepositoryController do
   use KmxgitWeb, :controller
 
   alias Kmxgit.IndexParams
+  alias Kmxgit.Git
   alias Kmxgit.GitAuth
   alias Kmxgit.GitManager
   alias Kmxgit.RepositoryManager
@@ -61,8 +62,8 @@ defmodule KmxgitWeb.Admin.RepositoryController do
     case Repo.transaction(fn ->
           case RepositoryManager.create_repository(owner, params) do
             {:ok, repo} ->
-              case GitManager.create(Repository.full_slug(repo)) do
-                {:ok, _} -> repo
+              case Git.create(Repository.full_slug(repo)) do
+                :ok -> repo
                 {:error, e} ->
                   repo
                   |> Repository.changeset(params)
diff --git a/lib/kmxgit_web/controllers/admin/user_controller.ex b/lib/kmxgit_web/controllers/admin/user_controller.ex
index 350ce5e..41be5b4 100644
--- a/lib/kmxgit_web/controllers/admin/user_controller.ex
+++ b/lib/kmxgit_web/controllers/admin/user_controller.ex
@@ -3,7 +3,6 @@ defmodule KmxgitWeb.Admin.UserController do
 
   alias Kmxgit.IndexParams
   alias Kmxgit.GitAuth
-  alias Kmxgit.GitManager
   alias Kmxgit.RepositoryManager
   alias Kmxgit.UserManager
   alias Kmxgit.UserManager.User
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index ea5fde6..ebfde53 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -3,6 +3,7 @@ defmodule KmxgitWeb.RepositoryController do
   require Logger
 
   alias Kmxgit.Git
+  alias Kmxgit.GitAuth
   alias Kmxgit.GitManager
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.RepositoryManager
@@ -67,8 +68,8 @@ defmodule KmxgitWeb.RepositoryController do
     case Repo.transaction(fn ->
           case RepositoryManager.create_repository(params, owner) do
             {:ok, repo} ->
-              case GitManager.create(Repository.full_slug(repo)) do
-                {:ok, _} -> repo
+              case Git.create(Repository.full_slug(repo)) do
+                :ok -> repo
                 {:error, e} ->
                   repo
                   |> Repository.changeset(params)
diff --git a/lib/kmxgit_web/controllers/user_controller.ex b/lib/kmxgit_web/controllers/user_controller.ex
index 13c0b7a..d254dce 100644
--- a/lib/kmxgit_web/controllers/user_controller.ex
+++ b/lib/kmxgit_web/controllers/user_controller.ex
@@ -162,7 +162,7 @@ defmodule KmxgitWeb.UserController do
             end
           end) do
         {:ok, _} ->
-          GitAuth.update() do
+          GitAuth.update()
           conn
           |> redirect(to: "/")
         {:error, changeset} ->