Hash :
f91b358b
Author :
Thomas de Grivel
Date :
2022-01-26T20:34:40
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
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
{out, status} = System.cmd("git", ["-C", dir, "ls-tree", tree, path1], stderr_to_stdout: true)
case status do
0 ->
list = out
|> String.split("\n")
|> Enum.reject(&(&1 == ""))
|> Enum.map(fn line ->
case String.split(line, "\t") do
[stat, name] ->
[mode, type, sha1] = String.split(stat, " ")
url = "#{parent}/#{name}"
%{mode: mode, name: name, sha1: sha1, type: type, url: url}
_ -> nil
end
end)
|> Enum.reject(&(&1 == nil))
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
def public_access(repo, true) do
dir = git_dir(repo)
if ! File.exists?(export = "#{dir}/git-daemon-export-ok") 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
File.rm!(export)
end
:ok
end
def log(repo, tree \\ nil) do
if tree do
log_(repo, [tree])
else
log_(repo, [])
end
end
def log_file(repo, path, tree \\ nil) do
if tree do
log_(repo, [tree, "--", path])
else
log_(repo, ["--", path])
end
end
defp ok_hd({:ok, [first | _]}), do: {:ok, first}
defp ok_hd({:ok, []}), do: {:ok, nil}
defp ok_hd(x), do: x
def log1(repo, tree \\ nil) do
if tree do
log_(repo, ["-1", tree])
else
log_(repo, ["-1"])
end
|> ok_hd()
end
def log1_file(repo, file, tree \\ nil) do
if tree do
log_(repo, ["-1", tree, "--", file])
else
log_(repo, ["-1", "--", file])
end
|> ok_hd()
end
defp log_(repo, args) do
dir = git_dir(repo)
args = ["-C", dir, "log", "--format=%H %aI \"%an <%ae>\" %s"] ++ args
{out, status} = System.cmd("git", args, stderr_to_stdout: true)
case status do
0 ->
entries = out
|> String.split("\n")
|> Enum.map(fn line ->
case Regex.run(~r/^([^ ]+) ([^ ]+) "(.+) <([^>]+)>" (.*)$/, line) do
[_ , hash, date, author, email, msg] -> %{author: author, author_email: email, hash: hash, date: date, message: msg}
_ -> nil
end
end)
|> Enum.filter(& &1)
{:ok, entries}
_ -> {:error, out}
end
end
def tag(repo, tag) do
dir = git_dir(repo)
args = ["-C", dir, "log", "-1", "--format=%H %aI \"%an <%ae>\"", tag]
{out, status} = System.cmd("git", args, stderr_to_stdout: true)
case status do
0 ->
case Regex.run(~r/^([^ ]+) ([^ ]+) "([^"]+)"$/, out) do
[_ , hash, date, author] -> {:ok, %{author: author, date: date, hash: hash}}
_ -> {:error, nil}
end
_ -> {:error, out}
end
end
def tags(repo) do
dir = git_dir(repo)
{out, status} = System.cmd("git", ["-C", dir, "tag", "-l"], stderr_to_stdout: true)
case status do
0 ->
tags = out
|> String.split("\n")
|> Enum.filter(& &1 && &1 != "")
|> Enum.map(& tag_info(repo, &1))
{:ok, tags}
_ ->
{:error, out}
end
end
def tag_info(repo, tag) do
{:ok, commit} = log1(repo, tag)
commit
|> Map.put(:tag, tag)
end
def diff(repo, from, to) do
dir = git_dir(repo)
IO.inspect("git -C #{dir} diff #{from} #{to}")
{out, status} = System.cmd("git", ["-C", dir, "diff", from, to], stderr_to_stdout: true)
case status do
0 -> {:ok, out}
_ -> {:error, out}
end
end
def du_ks(path) do
{out, status} = System.cmd("du", ["-ks", path], stderr_to_stdout: true)
case status do
0 ->
{k, _} = Integer.parse(out)
k
x ->
IO.inspect(x)
0
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