Hash :
970b811c
Author :
Thomas de Grivel
Date :
2021-12-07T11:44:43
fork without tracking
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
defmodule Kmxgit.GitManager do
@git_root "priv/git"
def git_dir(repo) do
"#{@git_root}/#{repo}.git"
end
def branches(repo) do
dir = git_dir(repo)
{out, status} = System.cmd("git", ["-C", dir, "branch", "--list"], stderr_to_stdout: true)
case status do
0 ->
b = out
|> String.split("\n")
|> filter_branches()
|> Enum.reject(&(!&1 || &1 == ""))
{:ok, b}
_ -> {:error, out}
end
end
defp filter_branches(lines) do
filter_branches(lines, [], nil)
end
defp filter_branches([], acc, nil) do
Enum.reverse(acc)
end
defp filter_branches([], acc, main) do
[main | Enum.reverse(acc)]
end
defp filter_branches([line | rest], acc, main) do
case Regex.run(~r/^(\* )? *([^ ].*)$/, line) do
[_, "* ", branch] -> filter_branches(rest, acc, branch)
[_, _, branch] -> filter_branches(rest, [branch | acc], main)
_ -> filter_branches(rest, acc, main)
end
end
def content(repo, path) do
dir = git_dir(repo)
path = if path == "" do "." else path end
{out, status} = System.cmd("git", ["-C", dir, "cat-file", "blob", path], stderr_to_stdout: true)
case status do
0 -> {:ok, out}
_ -> {:error, out}
end
end
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], stderr_to_stdout: true)
IO.inspect {out, status}
case status do
0 ->
list = out
|> String.split("\n")
|> Enum.reject(&(&1 == ""))
|> Enum.map(fn line ->
[stat, name] = String.split(line, "\t")
[mode, type, sha1] = String.split(stat, " ")
url = "#{parent}/#{name}"
%{mode: mode, name: name, sha1: sha1, type: type, url: url}
end)
case list do
[%{name: ^path1, sha1: sha1, type: "tree"}] ->
files(repo, sha1, "", "#{parent}/#{path1}")
_ -> {:ok, list}
end
_ ->
if Regex.match?(~r(^fatal: Not a valid object name ), out) do
{:ok, []}
else
{:error, String.split(out, "\n")}
end
end
end
def update_auth do
{out, status} = System.cmd("update_etc_git_auth", [], stderr_to_stdout: true)
case status do
0 ->
{out, status} = System.cmd("update_git_ssh_authorized_keys", [], stderr_to_stdout: true)
case status do
0 -> :ok
_ -> {:error, out}
end
_ -> {:error, out}
end
end
def rename(from, to) do
dir_from = git_dir(from)
dir_to = git_dir(to)
if File.exists?(dir_to) 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}
end
end
end
def rename_dir(from, to) do
dir_from = "#{@git_root}/#{from}/"
dir_to = "#{@git_root}/#{to}/"
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
end
end
def create(repo) do
dir = "#{@git_root}/#{Path.dirname(repo)}"
name = "#{Path.basename(repo)}.git"
:ok = File.mkdir_p(dir)
{out, status} = System.cmd("git", ["-C", dir, "init", "--bare", name], stderr_to_stdout: true)
case status do
0 -> {:ok, out}
_ -> {:error, out}
end
end
defp rm_rf(dir) do
case System.cmd("rm", ["-rf", dir], stderr_to_stdout: true) do
{"", 0} -> :ok
{err, _} -> {:error, err}
end
end
def delete(repo) do
repo |> git_dir() |> rm_rf()
end
def delete_dir(dir) do
"#{@git_root}/#{dir}/"
|> rm_rf()
end
def fork(from, to) do
dir_from = git_dir(from)
dir_to = git_dir(to)
if File.exists?(dir_to) do
{:error, "file exists"}
else
dir = Path.dirname(dir_to)
:ok = File.mkdir_p(dir)
{out, status} = System.cmd("cp", ["-PRp", dir_from, dir_to], stderr_to_stdout: true)
case status do
0 -> :ok
_ -> {:error, out}
end
end
end
end