Hash :
4d8f6ed5
Author :
Thomas de Grivel
Date :
2022-11-02T18:24:08
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## kmxgit
## Copyright 2022 kmx.io <contact@kmx.io>
##
## Permission is hereby granted to use this software granted
## the above copyright notice and this permission paragraph
## are included in all copies and substantial portions of this
## software.
##
## THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
## PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
## AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
## THIS SOFTWARE.
defmodule Kmxgit.GitManager do
@git_root "priv/git"
def git_dir(repo) do
if String.match?(repo, ~r/(^|\/)\.\.($|\/)/), do: raise "invalid git dir"
"#{@git_root}/#{repo}.git"
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)
with :ok <- File.mkdir_p(dir),
:ok <- File.rename(dir_from, dir_to) do
:ok
end
end
end
def rename_dir(from, to) do
dir_from = "#{@git_root}/#{from}/"
if File.exists?(dir_from) do
dir_to = "#{@git_root}/#{to}/"
if File.exists?(dir_to) do
{:error, "file exists"}
else
File.rename(dir_from, dir_to)
end
else
:ok
end
end
defp rm_rf(dir) do
with {:ok, _} <- File.rm_rf(dir) do
:ok
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)
with :ok <- File.mkdir_p(dir),
{:ok, _} <- File.cp_r(dir_from, dir_to) do
:ok
end
end
end
def public_access(repo, true) do
dir = git_dir(repo)
export = "#{dir}/git-daemon-export-ok"
if ! File.exists?(export) do
File.write!(export, "")
end
:ok
end
def public_access(repo, false) do
dir = git_dir(repo)
export = "#{dir}/git-daemon-export-ok"
if File.exists?(export) do
File.rm!(export)
end
:ok
end
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
defp filter_branches(lines) do
filter_branches(lines, [], nil)
end
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
def log(repo, tree \\ nil) do
if tree do
log_(repo, [tree])
else
log_(repo, [])
end
end
@deprecated "use Kmxgit.Git instead"
def log_file(repo, path, tree \\ nil) do
if tree do
log_(repo, [tree, "--", path])
else
log_(repo, ["--", path])
end
end
@deprecated "use Kmxgit.Git instead"
defp ok_hd({:ok, [first | _]}), do: {:ok, first}
defp ok_hd({:ok, []}), do: {:ok, nil}
@deprecated "use Kmxgit.Git instead"
defp ok_hd(x), do: x
@deprecated "use Kmxgit.Git instead"
def log1(repo, tree \\ nil) do
if tree do
log_(repo, ["-1", tree])
else
log_(repo, ["-1"])
end
|> ok_hd()
end
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
def tag_info(repo, tag) do
{:ok, commit} = log1(repo, tag)
commit
|> Map.put(:tag, tag)
end
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
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
@deprecated "use Kmxgit.Git instead"
def dir_disk_usage(dir) do
du_ks("#{@git_root}/#{dir}")
end
@deprecated "use Kmxgit.Git instead"
def disk_usage(repo) do
git_dir(repo)
|> du_ks()
end
end