1 | -- git_commands.lua |
2 | -- Index of git commands used for the git status site |
3 |
|
4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin |
5 | -- <https://github.com/JoshuaS3/joshstock.in> |
6 | -- <https://joshstock.in> |
7 |
|
8 |
|
9 | local utils = require("utils") |
10 |
|
11 | local _M = {} |
12 |
|
13 | local git = function(repo_dir, command) |
14 | local formatted_command = string.format( |
15 | "/usr/bin/git --git-dir=%s/.git --work-tree=%s %s", |
16 | repo_dir, repo_dir, command |
17 | ) |
18 | return utils.process(formatted_command) |
19 | end |
20 |
|
21 | _M.get_head = function(repo_dir) |
22 | local head = {} |
23 | local name = string.trim(git(repo_dir, "rev-parse --abbrev-ref HEAD")) |
24 | local output = git(repo_dir, "show-ref --heads "..name) |
25 | local a = string.split(string.trim(output), " ") |
26 | head.hash = a[1] |
27 | head.shorthash = string.sub(a[1], 1, 7) |
28 | head.full = a[2] |
29 | head.name = name |
30 | return head |
31 | end |
32 |
|
33 | _M.diffstat = function(repo_dir, hash) |
34 | hash = hash or "@" |
35 | local output = git(repo_dir, "diff --numstat --shortstat "..hash.."^ --") |
36 | local stat = {} |
37 | local a = string.split(output, "\n") |
38 | table.remove(a,#a) |
39 | stat.shortstat = a[#a] |
40 | table.remove(a,#a) |
41 | stat.plus = 0 |
42 | stat.minus = 0 |
43 | stat.files = {} |
44 | for i,v in pairs(a) do |
45 | local b = string.split(v,"\t") |
46 | local f = {} |
47 | f.plus = tonumber(b[1]) |
48 | stat.plus = stat.plus + f.plus |
49 | f.minus = tonumber(b[2]) |
50 | stat.minus = stat.minus + f.minus |
51 | stat.files[b[3]] = f |
52 | end |
53 | return stat |
54 | end |
55 |
|
56 | _M.count = function(repo_dir, hash) |
57 | hash = hash or "@" |
58 | local output = git(repo_dir, "rev-list --count "..hash.." --") |
59 | return tonumber(string.trim(output)) |
60 | end |
61 |
|
62 | _M.log = function(repo_dir, hash, number, skip, gpg) |
63 | hash = hash or "@" |
64 | number = tostring(number or 25) |
65 | skip = tostring(skip or 0) |
66 | gpg = gpg or false |
67 | local output |
68 | if gpg then |
69 | 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.." --") |
70 | else |
71 | 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.." --") |
72 | end |
73 | local commits = {} |
74 | local a = string.split(output,"\0\1") |
75 | local f = false |
76 | for i,v in pairs(a) do |
77 | if f == true then |
78 | local commit = {} |
79 | local c = string.split(v, "\0") |
80 | commit.hash = c[1] |
81 | commit.shorthash = string.sub(c[1], 1,7) |
82 | commit.timestamp = c[2] |
83 | commit.author = c[3] |
84 | commit.email = c[4] |
85 | commit.subject = c[5] |
86 | commit.body = string.trim(c[6]) |
87 | local diffs |
88 | if gpg then |
89 | commit.gpggood = c[7] |
90 | commit.gpgkey = c[8] |
91 | commit.gpgfull = string.trim(c[9]) |
92 | diffs = string.trim(c[10]) |
93 | else |
94 | diffs = string.trim(c[7]) |
95 | end |
96 | commit.diff = {} |
97 | local b = string.split(diffs, "\n") |
98 | commit.diff.plus = 0 |
99 | commit.diff.minus = 0 |
100 | commit.diff.files = {} |
101 | for i,v in pairs(b) do |
102 | local d = string.split(v,"\t") |
103 | local x = {} |
104 | x.plus = tonumber(d[1]) or 0 |
105 | commit.diff.plus = commit.diff.plus + x.plus |
106 | x.minus = tonumber(d[2]) or 0 |
107 | commit.diff.minus = commit.diff.minus + x.minus |
108 | commit.diff.files[d[3]] = x |
109 | end |
110 | table.insert(commits, commit) |
111 | else |
112 | f = true |
113 | end |
114 | end |
115 | return commits |
116 | end |
117 |
|
118 | _M.number = function(repo_dir, hash) |
119 | hash = hash or "@" |
120 | local output = git(repo_dir, "rev-list --count "..hash.." --") |
121 | end |
122 |
|
123 | _M.commit = function(repo_dir, hash) |
124 | local commit = _M.log(repo_dir, hash, 1, 0)[1] |
125 | commit.number = _M.number(repo_dir, hash) |
126 | return commit |
127 | end |
128 |
|
129 | _M.heads = function(repo_dir) |
130 | local output = git(repo_dir, "show-ref --heads") |
131 | local a = string.split(output, "\n") |
132 | table.remove(a,#a) |
133 | local heads = {} |
134 | for i,v in pairs(a) do |
135 | local b = string.split(v, " ") |
136 | local head = {} |
137 | head.hash = b[1] |
138 | head.shorthash = string.sub(b[1], 1, 7) |
139 | head.full = b[2] |
140 | head.name = string.split(b[2], "/")[3] |
141 | table.insert(heads, head) |
142 | end |
143 | return heads |
144 | end |
145 |
|
146 | _M.tags = function(repo_dir) |
147 | local output = git(repo_dir, "show-ref --tags") |
148 | local a = string.split(output, "\n") |
149 | table.remove(a,#a) |
150 | local tags = {} |
151 | for i,v in pairs(a) do |
152 | local b = string.split(v, " ") |
153 | local tag = {} |
154 | tag.hash = b[1] |
155 | tag.shorthash = string.sub(b[1], 1, 7) |
156 | tag.full = b[2] |
157 | tag.name = string.split(b[2], "/")[3] |
158 | table.insert(tags, tag) |
159 | end |
160 | return tags |
161 | end |
162 |
|
163 | _M.list_refs = function(repo_dir) |
164 | local refs = {} |
165 | refs.heads = _M.heads(repo_dir) |
166 | refs.tags = _M.tags(repo_dir) |
167 | return refs |
168 | end |
169 |
|
170 | return _M |
171 |
|