Commit 248f4f3e2a79251c03604efaecf5dffb2e6b53af

Thomas de Grivel 2022-01-25T21:48:35

optimize git commands, refactor setup_git, git_show_content without properties

diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index ba86a1d..319eb74 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -50,7 +50,7 @@ defmodule Kmxgit.GitManager do
 
   def files(repo, tree, path, parent \\ ".") do
     dir = git_dir(repo)
-    path1 = if path == "" do "." else path end
+    path1 = if path == "", do: ".", else: path
     {out, status} = System.cmd("git", ["-C", dir, "ls-tree", tree, path1], stderr_to_stdout: true)
     case status do
       0 ->
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index b5e3464..a66c4c5 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -104,7 +104,7 @@ defmodule KmxgitWeb.RepositoryController do
     if repo && repo.public_access || Repository.member?(repo, current_user) do
       org = repo.organisation
       user = repo.user
-      git = setup_git(repo, op_params.tree || "master", op_params.path, conn, op)
+      git = setup_git(repo, conn, op, op_params)
       first_tree = case Enum.at(git.trees, 0) do
                        {_, first_tree, _} -> first_tree
                        nil -> nil
@@ -178,7 +178,7 @@ defmodule KmxgitWeb.RepositoryController do
     %{tree: tree, from: nil, git: nil, org: nil, path: path3, repo: nil, to: nil, user: nil}
   end
 
-  defp setup_git(repo, tree, path, conn, op) do
+  defp setup_git(repo, conn, op, %{path: path}) do
     %{trees: [],
       content: nil,
       content_html: nil,
@@ -192,13 +192,7 @@ defmodule KmxgitWeb.RepositoryController do
       status: "",
       tags: [],
       valid: true}
-    |> git_put_branches(repo, conn, op, path)
-    |> git_put_files(repo, tree, path, conn)
-    |> git_put_content(repo, path)
-    |> git_put_readme(repo)
-    |> git_put_log1(repo, tree, path)
-    |> git_put_tags(repo, conn, op, path)
-    |> git_put_commit(repo, conn, op, tree, path)
+      |> git_put_branches(repo, conn, op, path)
   end
 
   defp git_put_branches(git = %{valid: true}, repo, conn, op, path) do
@@ -348,7 +342,9 @@ defmodule KmxgitWeb.RepositoryController do
     log
   end
 
-  defp show_op(conn, :blob, %{git: git}) do
+  defp show_op(conn, :blob, %{git: git, path: path, repo: repo}) do
+    git = git
+    |> git_put_content(repo, path)
     if (git.content) do
       conn
       |> put_resp_content_type("application/octet-stream")
@@ -358,7 +354,9 @@ defmodule KmxgitWeb.RepositoryController do
       not_found(conn)
     end
   end
-  defp show_op(conn, :commit, %{git: git, org: org, repo: repo}) do
+  defp show_op(conn, :commit = op, %{git: git, org: org, path: path, repo: repo, tree: tree}) do
+    git = git
+    |> git_put_commit(repo, conn, op, tree, path)
     IO.inspect(git)
     diff = case GitManager.diff(Repository.full_slug(repo), "#{git.log1.hash}~1", git.log1.hash) do
              {:ok, diff} -> diff
@@ -396,6 +394,7 @@ defmodule KmxgitWeb.RepositoryController do
     end
   end
   defp show_op(conn, :log, %{tree: tree, git: git, org: org, path: path, repo: repo}) do
+    git = git
     log = git_log(repo, tree, path)
     conn
     |> assign(:tree, tree)
@@ -408,7 +407,9 @@ defmodule KmxgitWeb.RepositoryController do
     |> assign(:repo, repo)
     |> render("log.html")
   end
-  defp show_op(conn, :tag, %{tree: tree, git: git, org: org, repo: repo}) do
+  defp show_op(conn, :tag = op, %{tree: tree, git: git, org: org, path: path, repo: repo}) do
+    git = git
+    |> git_put_tags(repo, conn, op, path)
     tag = Enum.find(git.tags, fn tag -> tag.tag == tree end)
     conn
     |> assign_current_organisation(org)
@@ -417,7 +418,13 @@ defmodule KmxgitWeb.RepositoryController do
     |> assign(:tag, tag)
     |> render("tag.html")
   end
-  defp show_op(conn, :tree, %{tree: tree, git: git, org: org, path: path, repo: repo, user: user}) do
+  defp show_op(conn, :tree = op, %{tree: tree, git: git, org: org, path: path, repo: repo, user: user}) do
+    git = git
+    |> git_put_files(repo, tree, path, conn)
+    |> git_put_content(repo, path)
+    |> git_put_readme(repo)
+    |> git_put_log1(repo, tree, path)
+    |> git_put_tags(repo, conn, op, path)
     conn
     |> assign_current_organisation(org)
     |> assign(:current_repository, repo)
diff --git a/lib/kmxgit_web/templates/repository/show.html.heex b/lib/kmxgit_web/templates/repository/show.html.heex
index 61d0ad3..9ae685e 100644
--- a/lib/kmxgit_web/templates/repository/show.html.heex
+++ b/lib/kmxgit_web/templates/repository/show.html.heex
@@ -1,43 +1,16 @@
 <div class="container-fluid">
-
-  <div class="row">
-    <div class="col col-12 col-md-7">
-      <%= render("show_title.html", assigns) %>
+  <%= if @git.content do %>
+    <%= render("show_title.html", assigns) %>
+    <%= render("show_git_content.html", assigns) %>
+  <% else %>
+    <div class="row">
+      <div class="col col-12 col-md-7">
+        <%= render("show_title.html", assigns) %>
+      </div>
+      <div class="col col-12 col-md-5">
+        <%= render("show_actions.html", assigns) %>
+      </div>
     </div>
-    <div class="col col-12 col-md-5">
-      <%= render("show_actions.html", assigns) %>
-    </div>
-  </div>
-
-  <div class="row">
-    <div class="col col-12 col-md-7">
-      <%= if @path do %>
-        <hr/>
-        <h2><%= @path %></h2>
-      <% end %>
-
-      <%= render("show_branch.html", assigns) %>
-
-      <hr/>
-      <%= if @git.log1 do %>
-        <%= render("show_commit_message.html", assigns) %>
-      <% end %>
-
-      <%= if @git.content do %>
-        <hr/>
-        <%= render("show_git_content.html", assigns) %>
-      <% else %>
-        <hr/>
-        <%= render("show_files.html", assigns) %>
-      <% end %>
-    </div>
-    <div class="col col-12 col-md-5">
-      <hr/>
-      <%= render("show_properties.html", assigns) %>
-    </div>
-  </div>
-
-  <div class="row">
-    <%= render("show_readmes.html", assigns) %>
-  </div>
+    <%= render("show_files.html", assigns) %>
+  <% end %>
 </div>
diff --git a/lib/kmxgit_web/templates/repository/show_files.html.heex b/lib/kmxgit_web/templates/repository/show_files.html.heex
index a96fef2..f9ac847 100644
--- a/lib/kmxgit_web/templates/repository/show_files.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_files.html.heex
@@ -1,15 +1,40 @@
-<h2><%= gettext("Files") %></h2>
-<ul>
-  <%= for file <- @git.files do %>
-    <li>
-      <%= case file.type do %>
-        <% "blob" -> %>
-          <%= link file.name, to: file.url %>
-        <% "tree" -> %>
-          <%= link "#{file.name}/", to: file.url %>
-        <% _ -> %>
-          <%= "#{file.type} #{file.name}" %>
-      <% end %>
-    </li>
-  <% end %>
-</ul>
+<div class="row">
+  <div class="col col-12 col-md-7">
+    <%= render("show_branch.html", assigns) %>
+
+    <hr/>
+    <%= if @git.log1 do %>
+      <%= render("show_commit_message.html", assigns) %>
+    <% end %>
+
+    <%= if @git.content do %>
+      <hr/>
+      <%= render("show_git_content.html", assigns) %>
+    <% else %>
+      <hr/>
+      <h2><%= gettext("Files") %></h2>
+      <ul>
+        <%= for file <- @git.files do %>
+          <li>
+            <%= case file.type do %>
+              <% "blob" -> %>
+                <%= link file.name, to: file.url %>
+              <% "tree" -> %>
+                <%= link "#{file.name}/", to: file.url %>
+              <% _ -> %>
+                <%= "#{file.type} #{file.name}" %>
+            <% end %>
+          </li>
+        <% end %>
+      </ul>
+    <% end %>
+  </div>
+  <div class="col col-12 col-md-5">
+    <hr/>
+    <%= render("show_properties.html", assigns) %>
+  </div>
+</div>
+
+<div class="row">
+  <%= render("show_readmes.html", assigns) %>
+</div>
diff --git a/lib/kmxgit_web/templates/repository/show_git_content.html.heex b/lib/kmxgit_web/templates/repository/show_git_content.html.heex
index c0f9a38..3c18686 100644
--- a/lib/kmxgit_web/templates/repository/show_git_content.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_git_content.html.heex
@@ -1,28 +1,18 @@
+<%= render("show_branch.html", assigns) %>
+<%= if @git.log1 do %>
+  <hr/>
+  <%= render("show_commit_message.html", assigns) %>
+<% end %>
+<hr/>
 <%= if String.match?(@git.content_type, ~r(^text/)) do %>
   <%= if @git.markdown_html do %>
-    <ul class="nav nav-tabs" id="contentTab" role="tablist">
-      <li class="nav-item" role="presentation">
-        <button class="nav-link active" id="contentTabMarkdown" data-bs-toggle="tab" data-bs-target="#contentMarkdown" type="button" role="tab" aria-selected="true"><%= gettext "Markdown" %></button>
-      </li>
-      <li class="nav-item" role="presentation">
-        <button class="nav-link" id="contentTabSource" data-bs-toggle="tab" data-bs-target="#contentSource" type="button" role="tab" aria-selected="false"><%= gettext "Source" %></button>
-      </li>
-    </ul>
-    <div class="tab-content" id="contentTabContent">
-      <div class="tab-pane fade show active" id="contentMarkdown" role="tabpanel" aria-labelledby="contentTabMarkdown">
-        <div class="file_content">
-          <div class="content_html">
-            <%= raw @git.markdown_html %>
-          </div>
-        </div>
-      </div>
-      <div class="tab-pane fade" id="contentSource" role="tabpanel" aria-labelledby="contentTabSource">
-        <%= render("show_git_content_text.html", assigns) %>
-      </div>
+    <div class="content_html">
+      <%= raw @git.markdown_html %>
     </div>
-  <% else %>
-    <%= render("show_git_content_text.html", assigns) %>
+    <hr/>
+    <h3><%= gettext "Source" %></h3>
   <% end %>
+  <%= render("show_git_content_text.html", assigns) %>
 <% end %>
 <%= if String.match?(@git.content_type, ~r(^image/)) do %>
   <img src={"data:#{@git.content_type};base64,#{Base.encode64(@git.content)}"}/>
diff --git a/lib/kmxgit_web/templates/repository/show_git_content_full.html.heex b/lib/kmxgit_web/templates/repository/show_git_content_full.html.heex
new file mode 100644
index 0000000..5f3609c
--- /dev/null
+++ b/lib/kmxgit_web/templates/repository/show_git_content_full.html.heex
@@ -0,0 +1,7 @@
+<%= if String.match?(@git.content_type, ~r(^text/)) do %>
+  <hr/>
+  <h3><%= gettext "Source" %></h3>
+  <%= if @git.markdown_html do %>
+    <%= render("show_git_content_text.html", assigns) %>
+  <% end %>
+<% end %>
diff --git a/lib/kmxgit_web/templates/repository/show_readmes.html.heex b/lib/kmxgit_web/templates/repository/show_readmes.html.heex
index 384c17b..3187806 100644
--- a/lib/kmxgit_web/templates/repository/show_readmes.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_readmes.html.heex
@@ -3,12 +3,14 @@
     <hr/>
     <h3><%= readme.name %></h3>
     <div class="container-fluid">
-      <div class="container-fluid file-content">
-        <%= if readme.html do %>
-          <%= raw readme.html %>
-        <% else %>
-          <pre><%= readme.txt %></pre>
-        <% end %>
+      <div class="container-fluid file_content">
+        <div class="content_html">
+          <%= if readme.html do %>
+            <%= raw readme.html %>
+          <% else %>
+            <pre><%= readme.txt %></pre>
+          <% end %>
+        </div>
       </div>
     </div>
   <% end %>
diff --git a/lib/kmxgit_web/templates/repository/show_title.html.heex b/lib/kmxgit_web/templates/repository/show_title.html.heex
index 72e7fd7..2eaf828 100644
--- a/lib/kmxgit_web/templates/repository/show_title.html.heex
+++ b/lib/kmxgit_web/templates/repository/show_title.html.heex
@@ -1,4 +1,4 @@
-<h1 id="repo_title"><%= link Repository.owner_slug(@repo), to: Routes.slug_path(@conn, :show, Repository.owner_slug(@repo)) %>/<%= link @repo.slug, to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo)) %></h1>
+<h1 id="repo_title"><%= link Repository.owner_slug(@repo), to: Routes.slug_path(@conn, :show, Repository.owner_slug(@repo)) %>/<%= link @repo.slug, to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo)) %><%= if @path do %>/<%= link @path, to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo, ["_tree", @tree] ++ String.split(@path, "/"))) %><% end %></h1>
 <%= if @repo.forked_from do %>
   <%= gettext("Forked from") %>
   <%= link Repository.full_slug(@repo.forked_from), to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo.forked_from), Repository.splat(@repo.forked_from)) %>