Commit 17fd535777870f766fe2cae99bb2be522beb9fa2

Thomas de Grivel 2022-01-26T14:42:40

optimize git, fix empty diff

diff --git a/README.md b/README.md
index f7da621..0fb2043 100644
--- a/README.md
+++ b/README.md
@@ -127,9 +127,9 @@ location ~ ^(.*/info/refs|.*/git-upload-pack)$ {
      - DONE git push -u kmx branch
      - DONE git pull
      - install git
- - Optimizations
-   - useless git commands (`git_put_log1`)
-   - disk usage calculation
+ - DONE Optimizations
+   - DONE useless git commands (`git_put_log1`)
+   - DONE disk usage calculation
  - Admin
    - logo
    - disk usage (repos, orgs, users)
diff --git a/lib/kmxgit/git_manager.ex b/lib/kmxgit/git_manager.ex
index 319eb74..b94d4c8 100644
--- a/lib/kmxgit/git_manager.ex
+++ b/lib/kmxgit/git_manager.ex
@@ -287,12 +287,24 @@ defmodule Kmxgit.GitManager do
     end
   end
 
-  def disk_usage(repo) do
-    dir = git_dir(repo)
-    {out, status} = System.cmd("du", ["-ks", dir], stderr_to_stdout: true)
+  defp du_ks(path) do
+    {out, status} = System.cmd("du", ["-ks", path], stderr_to_stdout: true)
     case status do
-      0 -> {:ok, Integer.parse(out)}
-      _ -> {:error, out}
+      0 ->
+        {k, _} = Integer.parse(out)
+        k
+      x ->
+        IO.inspect(x)
+        -1
     end
   end
+
+  def dir_disk_usage(dir) do
+    du_ks("#{@git_root}/#{dir}")
+  end
+
+  def disk_usage(repo) do
+    git_dir(repo)
+    |> du_ks()
+  end
 end
diff --git a/lib/kmxgit/organisation_manager/organisation.ex b/lib/kmxgit/organisation_manager/organisation.ex
index 180a0d2..87525be 100644
--- a/lib/kmxgit/organisation_manager/organisation.ex
+++ b/lib/kmxgit/organisation_manager/organisation.ex
@@ -3,6 +3,7 @@ defmodule Kmxgit.OrganisationManager.Organisation do
   use Ecto.Schema
   import Ecto.Changeset
 
+  alias Kmxgit.GitManager
   alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.SlugManager.Slug
   alias Kmxgit.UserManager.User
@@ -37,4 +38,8 @@ defmodule Kmxgit.OrganisationManager.Organisation do
     org.owned_repositories
     |> Enum.sort_by(&Repository.full_slug/1)
   end
+
+  def disk_usage(org) do
+    GitManager.dir_disk_usage(org.slug.slug)
+  end
 end
diff --git a/lib/kmxgit/repository_manager/repository.ex b/lib/kmxgit/repository_manager/repository.ex
index d83d44b..2fd0271 100644
--- a/lib/kmxgit/repository_manager/repository.ex
+++ b/lib/kmxgit/repository_manager/repository.ex
@@ -206,12 +206,7 @@ defmodule Kmxgit.RepositoryManager.Repository do
   end
 
   def disk_usage(repo = %__MODULE__{}) do
-    case GitManager.disk_usage(full_slug(repo)) do
-      {:ok, {du, _}} -> du
-      x ->
-        IO.inspect(x)
-        0
-    end
+    GitManager.disk_usage(full_slug(repo))
   end
   def disk_usage(repos) when is_list(repos) do
     repos
diff --git a/lib/kmxgit/user_manager/user.ex b/lib/kmxgit/user_manager/user.ex
index 41af245..bc2cd4c 100644
--- a/lib/kmxgit/user_manager/user.ex
+++ b/lib/kmxgit/user_manager/user.ex
@@ -2,6 +2,7 @@ defmodule Kmxgit.UserManager.User do
   use Ecto.Schema
   import Ecto.Changeset
 
+  alias Kmxgit.GitManager
   alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.SlugManager.Slug
@@ -269,4 +270,8 @@ defmodule Kmxgit.UserManager.User do
       |> add_error(:totp_last, "invalid token")
     end
   end
+
+  def disk_usage(user) do
+    GitManager.dir_disk_usage(login(user))
+  end
 end
diff --git a/lib/kmxgit_web/controllers/slug_controller.ex b/lib/kmxgit_web/controllers/slug_controller.ex
index 698469a..6a68868 100644
--- a/lib/kmxgit_web/controllers/slug_controller.ex
+++ b/lib/kmxgit_web/controllers/slug_controller.ex
@@ -1,6 +1,8 @@
 defmodule KmxgitWeb.SlugController do
   use KmxgitWeb, :controller
 
+  alias Kmxgit.GitManager
+  alias Kmxgit.OrganisationManager.Organisation
   alias Kmxgit.RepositoryManager
   alias Kmxgit.RepositoryManager.Repository
   alias Kmxgit.SlugManager
@@ -20,7 +22,7 @@ defmodule KmxgitWeb.SlugController do
         contributor_repos = RepositoryManager.list_contributor_repositories(user)
         repos = owned_repos ++ contributor_repos
         conn
-        |> assign(:disk_usage, Repository.disk_usage(owned_repos))
+        |> assign(:disk_usage, User.disk_usage(user))
         |> assign(:disk_usage_all, Repository.disk_usage(repos))
         |> assign(:repos, repos)
         |> assign(:page_title, gettext("User %{login}", login: User.login(user)))
@@ -32,7 +34,7 @@ defmodule KmxgitWeb.SlugController do
         if org do
           conn
           |> assign(:current_organisation, org)
-          |> assign(:disk_usage, Repository.disk_usage(org.owned_repositories))
+          |> assign(:disk_usage, Organisation.disk_usage(org))
           |> assign(:org, org)
           |> assign(:page_title, org.name || org.slug.slug)
           |> put_view(OrganisationView)