1 | -- git_commands.lua |
2 | -- Index of git commands and parser functions used for the git HTTP site |
3 |
|
4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin |
5 | -- <https://joshstock.in> |
6 |
|
7 | local utils = require("utils") |
8 |
|
9 | local _M = {} |
10 |
|
11 | local git = function(repo_dir, command) |
12 | local formatted_command = string.format( |
13 | "git --git-dir=%s %s", |
14 | repo_dir, command |
15 | ) |
16 | return utils.process(formatted_command) |
17 | end |
18 |
|
19 | _M.show_file = function(repo_dir, hash, filename) |
20 | hash = hash or "@" |
21 | filename = filename or "" |
22 | local output = git(repo_dir, "show "..hash..":"..filename) |
23 | return output |
24 | end |
25 |
|
26 | _M.get_head = function(repo_dir, head_name) |
27 | head_name = head_name or "HEAD" |
28 | local head = {} |
29 | local name = string.trim(git(repo_dir, "rev-parse --abbrev-ref "..head_name)) |
30 | local output = git(repo_dir, "show-ref --heads --tags "..name) |
31 | local a = string.split(string.trim(output), " ") |
32 | head.hash = a[1] |
33 | head.shorthash = string.sub(a[1], 1, 7) |
34 | head.full = a[2] |
35 | head.name = name |
36 | return head |
37 | end |
38 |
|
39 | _M.count = function(repo_dir, hash) |
40 | hash = hash or "@" |
41 | local output = git(repo_dir, "rev-list --count "..hash.." --") |
42 | return tonumber(string.trim(output)) |
43 | end |
44 |
|
45 | _M.log = function(repo_dir, hash, file, number, skip, gpg) |
46 | hash = hash or "@" |
47 | file = file or "" |
48 | number = tostring(number or 25) |
49 | skip = tostring(skip or 0) |
50 | gpg = gpg or false |
51 | local output |
52 | if gpg then |
53 | output = git(repo_dir, "log --pretty=tformat:'%x00%x01%H%x00%cI%x00%cn%x00%ce%x00%s%x00%b%x00%G?%x00%GK%x00%GG%x00' --numstat -n "..number.." --skip "..skip.." "..hash.." -- "..file) |
54 | else |
55 | output = git(repo_dir, "log --pretty=tformat:'%x00%x01%H%x00%cI%x00%cn%x00%ce%x00%s%x00%b%x00' --numstat -n "..number.." --skip "..skip.." "..hash.." -- "..file) |
56 | end |
57 | local commits = {} |
58 | local a = string.split(output,"\0\1") |
59 | local f = false |
60 | for i,v in pairs(a) do |
61 | if f == true then |
62 | local commit = {} |
63 | local c = string.split(v, "\0") |
64 | commit.hash = c[1] |
65 | commit.shorthash = string.sub(c[1], 1,7) |
66 | commit.timestamp = c[2] |
67 | commit.author = c[3] |
68 | commit.email = c[4] |
69 | commit.subject = c[5] |
70 | commit.body = string.trim(c[6]) |
71 | local diffs |
72 | if gpg then |
73 | commit.gpggood = c[7] |
74 | commit.gpgkey = c[8] |
75 | commit.gpgfull = string.trim(c[9]) |
76 | diffs = string.trim(c[10]) |
77 | else |
78 | diffs = string.trim(c[7]) |
79 | end |
80 | commit.diff = {} |
81 | local b = string.split(diffs, "\n") |
82 | commit.diff.plus = 0 |
83 | commit.diff.minus = 0 |
84 | commit.diff.num = 0 |
85 | commit.diff.files = {} |
86 | for i,v in pairs(b) do |
87 | local d = string.split(v,"\t") |
88 | local x = {} |
89 | x.plus = tonumber(d[1]) or 0 |
90 | commit.diff.plus = commit.diff.plus + x.plus |
91 | x.minus = tonumber(d[2]) or 0 |
92 | commit.diff.minus = commit.diff.minus + x.minus |
93 | commit.diff.files[d[3]] = x |
94 | commit.diff.num = commit.diff.num + 1 |
95 | end |
96 | table.insert(commits, commit) |
97 | else |
98 | f = true |
99 | end |
100 | end |
101 | return commits |
102 | end |
103 |
|
104 | _M.number = function(repo_dir, hash) |
105 | hash = hash or "@" |
106 | local output = git(repo_dir, "rev-list --count "..hash.." --") |
107 | end |
108 |
|
109 | _M.commit = function(repo_dir, hash) |
110 | local commit = _M.log(repo_dir, hash, 1, 0)[1] |
111 | commit.number = _M.number(repo_dir, hash) |
112 | return commit |
113 | end |
114 |
|
115 | _M.heads = function(repo_dir) |
116 | local output = git(repo_dir, "show-ref --heads") |
117 | local a = string.split(output, "\n") |
118 | table.remove(a,#a) |
119 | local heads = {} |
120 | for i,v in pairs(a) do |
121 | local b = string.split(v, " ") |
122 | local head = {} |
123 | head.hash = b[1] |
124 | head.shorthash = string.sub(b[1], 1, 7) |
125 | head.full = b[2] |
126 | head.name = string.split(b[2], "/")[3] |
127 | table.insert(heads, head) |
128 | end |
129 | return heads |
130 | end |
131 |
|
132 | _M.tags = function(repo_dir) |
133 | local output = git(repo_dir, "show-ref --tags") |
134 | local a = string.split(output, "\n") |
135 | table.remove(a,#a) |
136 | local tags = {} |
137 | for i,v in pairs(a) do |
138 | local b = string.split(v, " ") |
139 | local tag = {} |
140 | tag.hash = b[1] |
141 | tag.shorthash = string.sub(b[1], 1, 7) |
142 | tag.full = b[2] |
143 | tag.name = string.split(b[2], "/")[3] |
144 | table.insert(tags, tag) |
145 | end |
146 | return tags |
147 | end |
148 |
|
149 | _M.list_refs = function(repo_dir) |
150 | local refs = {} |
151 | refs.heads = _M.heads(repo_dir) |
152 | refs.tags = _M.tags(repo_dir) |
153 | return refs |
154 | end |
155 |
|
156 | _M.list_dirs = function(repo_dir, hash, path) |
157 | hash = hash or "@" |
158 | path = path or "" |
159 | local output = git(repo_dir, "ls-tree -d --name-only "..hash.." -- "..path) |
160 | local dirs = string.split(output, "\n") |
161 | table.remove(dirs, #dirs) -- remove trailing \n |
162 | return dirs |
163 | end |
164 |
|
165 | _M.list_all = function(repo_dir, hash, path) |
166 | hash = hash or "@" |
167 | path = path or "" |
168 | local output = git(repo_dir, "ls-tree --name-only "..hash.." -- "..path) |
169 | local all = string.split(output, "\n") |
170 | table.remove(all, #all) -- remove trailing \n |
171 | return all |
172 | end |
173 |
|
174 | _M.list_tree = function(repo_dir, hash, path) |
175 | hash = hash or "@" |
176 | path = path or "" |
177 | local files = _M.list_all(repo_dir, hash, path) |
178 | local dirs = _M.list_dirs(repo_dir, hash, path) |
179 | local ret = {} |
180 | ret.dirs = {} |
181 | ret.files = {} |
182 | for i,v in pairs(files) do -- iterate over all objects, separate directories from files |
183 | local not_dir = true |
184 | for _,d in pairs(dirs) do -- check if object is directory |
185 | if v == d then |
186 | not_dir = false |
187 | break |
188 | end |
189 | end |
190 | if not_dir then |
191 | table.insert(ret.files, v) |
192 | else |
193 | table.insert(ret.dirs, v) |
194 | end |
195 | end |
196 | return ret |
197 | end |
198 |
|
199 | return _M |
200 |
|