Commit 091232c7fcd2faa4978c585fb806de6efcd2af7d

Thomas de Grivel 2021-11-24T17:17:36

fix repository

diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index daff2d6..41e55b3 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -8,7 +8,7 @@ defmodule Kmxgit.GitManager do
 
   def status(repo) do
     dir = git_dir(repo)
-    {out, status} = System.cmd("git", ["-C", dir, "status"])
+    {out, status} = System.cmd("git", ["-C", dir, "status"], stderr_to_stdout: true)
     case status do
       0 -> {:ok, out}
       _ -> {:error, out}
@@ -17,8 +17,7 @@ defmodule Kmxgit.GitManager do
 
   def branches(repo) do
     dir = git_dir(repo)
-    {out, status} = System.cmd("git", ["branch", "-a"])
-    IO.inspect {out, status}
+    {out, status} = System.cmd("git", ["branch", "--list"], stderr_to_stdout: true)
     case status do
       0 ->
         b = out
@@ -51,8 +50,7 @@ defmodule Kmxgit.GitManager do
   def content(repo, branch, path) do
     dir = git_dir(repo)
     path = if path == "" do "." else path end
-    {out, status} = System.cmd("git", ["-C", dir, "cat-file", "blob", path])
-    IO.inspect {out, status}
+    {out, status} = System.cmd("git", ["-C", dir, "cat-file", "blob", path], stderr_to_stdout: true)
     case status do
       0 -> {:ok, out}
       _ -> {:error, out}
@@ -62,7 +60,8 @@ defmodule Kmxgit.GitManager do
   def files(repo, tree, path, parent \\ ".") do
     dir = git_dir(repo)
     path1 = if path == "" do "." else path end
-    {out, status} = System.cmd("git", ["-C", dir, "ls-tree", tree, path1])
+    {out, status} = System.cmd("git", ["-C", dir, "ls-tree", tree, path1], stderr_to_stdout: true)
+    IO.inspect {out, status}
     case status do
       0 ->
         list = out
@@ -79,7 +78,12 @@ defmodule Kmxgit.GitManager do
             files(repo, sha1, "", "#{parent}/#{path1}")
           _ -> {:ok, list}
         end
-      _ -> {:error, String.split(out, "\n")}
+      _ ->
+        if Regex.match?(~r(^fatal: Not a valid object name ), out) do
+          {:ok, []}
+        else
+          {:error, String.split(out, "\n")}
+        end
     end
   end
 
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 5c875ff..a8473c3 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -131,7 +131,7 @@ defmodule KmxgitWeb.RepositoryController do
       {path1 |> Enum.at(1),
        path2 ++ rest1 |> Enum.reject(&(!&1 || &1 == "")) |> Enum.join("/")}
     else
-      {"master", ""}
+      {nil, ""}
     end
     IO.inspect([path: path, slug: slug, branch: branch, path1: path1])
     repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
@@ -140,23 +140,29 @@ defmodule KmxgitWeb.RepositoryController do
       user = repo.user
       git = %{branches: [], content: nil, files: [], status: "", valid: true}
       |> git_put_branches(repo, conn)
-      |> git_put_files(repo, branch, path1, conn)
-      |> git_put_content(repo, branch, path1)
+      |> git_put_files(repo, branch || "master", path1, conn)
+      |> git_put_content(repo, branch || "master", path1)
       IO.inspect(git)
-      if git.valid do
+      if !branch do
+        {b, _} = Enum.at(git.branches, 0)
         conn
-        |> assign(:branch, branch)
-        |> assign(:branch_url, branch_url(git.branches, branch))
-        |> assign_current_organisation(org)
-        |> assign(:current_repository, repo)
-        |> assign(:git, git)
-        |> assign(:repo, repo)
-        |> assign(:members, Repository.members(repo))
-        |> assign(:owner, org || user)
-        |> assign(:path, path1)
-        |> render("show.html")
+        |> redirect(to: Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo) ++ ["_branch", b]))
       else
-        not_found(conn)
+        if git.valid do
+          conn
+          |> assign(:branch, branch)
+          |> assign(:branch_url, branch_url(git.branches, branch))
+          |> assign_current_organisation(org)
+          |> assign(:current_repository, repo)
+          |> assign(:git, git)
+          |> assign(:repo, repo)
+          |> assign(:members, Repository.members(repo))
+          |> assign(:owner, org || user)
+          |> assign(:path, path1)
+          |> render("show.html")
+        else
+          not_found(conn)
+        end
       end
     else
       not_found(conn)
@@ -200,7 +206,7 @@ defmodule KmxgitWeb.RepositoryController do
 
   defp git_put_files(git = %{valid: true}, repo, branch, subdir, conn) do
     case GitManager.files(Repository.full_slug(repo), branch, subdir) do
-      {:ok, []} -> %{git | valid: false}
+      {:ok, []} -> git
       {:ok, files} ->
         files = files
         |> Enum.map(fn f = %{url: url} ->