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
343
344
345
346
347
348
|
-- gitinfo-lua.lua
-- Copyright 2023 E. Nijenhuis
--
-- This work may be distributed and/or modified under the
-- conditions of the LaTeX Project Public License, either version 1.3c
-- of this license or (at your option) any later version.
-- The latest version of this license is in
-- http://www.latex-project.org/lppl.txt
-- and version 1.3c or later is part of all distributions of LaTeX
-- version 2005/12/01 or later.
--
-- This work has the LPPL maintenance status ‘maintained’.
--
-- The Current Maintainer of this work is E. Nijenhuis.
--
-- This work consists of the files gitinfo-lua.sty gitinfo-lua.pdf
-- gitinfo-cmd.lua and gitinfo-lua.lua
if not modules then
modules = {}
end
local module = {
name = 'gitinfo-lua',
info = {
version = '1.0.2', --TAGVERSION
date = '2024/02/23', --TAGDATE
comment = "Git info Lua — Git integration with LaTeX",
author = "Erik Nijenhuis",
license = "free"
}
}
modules[module.name] = module.info
local api = {
cur_tok = nil,
cmd = require('gitinfo-lua-cmd'),
escape_chars = {
['&'] = '\\&',
['%%'] = '\\%%',
['%$'] = '\\$',
['#'] = '\\#',
['_'] = '\\_',
['{'] = '\\{',
['}'] = '\\}',
['~'] = '\\textasciitilde ',
['%^'] = '\\textasciicircum ',
['\n'] = ' '
}
}
local mt = {
__index = api,
__newindex = nil
}
local gitinfo = {}
setmetatable(gitinfo, mt)
function api.trim(s)
return s and (s:gsub("^%s*(.-)%s*$", "%1")) or 'nil'
end
function api:set_date()
local date, err = self.cmd:log('cs', '-1', { 'max-count=1' })
if date and #date == 1 then
local _, _, year, month, day = date[1][1]:find('(%d+)[-/](%d+)[-/](%d+)')
tex.year = tonumber(year)
tex.month = tonumber(month)
tex.day = tonumber(day)
else
return nil, (err or 'Length of output doesn\'t match one (attempt to set git date)')
end
end
function api:escape_str(value)
local buf = string.gsub(value, '\\', '\\textbackslash ')
buf = string.gsub(buf, "\n%s*\n+", "\\par ")
for search, replace in pairs(self.escape_chars) do
buf = string.gsub(buf, search, replace)
end
return buf
end
-- experimental
function api:get_tok()
if self.cur_tok == nil then
self.cur_tok = token.get_next()
end
return self.cur_tok
end
-- experimental
function api:parse_opts()
local tok = self:get_tok()
if tok.cmdname == 'other_char' then
--token.put_next(tok)
local opts = token.scan_word()
self.cur_tok = nil
-- todo: parse []
return opts
end
end
-- experimental
function api:parse_arguments(argc)
local result_list = {}
for _ = 1, argc do
local tok = self:get_tok()
if tok.cmdname == 'left_brace' then
token.put_next(tok)
table.insert(result_list, token.scan_argument())
self.cur_tok = nil
else
tex.error("Expected left brace")
return
end
end
return table.unpack(result_list)
end
-- experimental
function api:parse_macro()
--tex.print('\\noexpand')
local tok = self:get_tok()
if (tok.cmdname == 'call') or tok.cmdname == 'long_call' then
self.cur_tok = nil
return tok
else
tex.error("Expected Macro")
for i = 1, 5 do
local _tok = token.get_next()
end
end
end
function api:dir(path)
self.cmd.cwd = path
end
function api:version()
return self.trim(self.cmd:exec('describe --tags --always', true))
end
function api:write_version()
local version, err = self:version()
if version then
tex.write(version)
else
tex.error(err)
end
end
-- todo: prevent output to stderr
-- todo: add write variant
-- experimental
function api:is_dirty()
local ok, _ = self.cmd:exec('describe --tags --exact-match')
return ok == nil
end
function api:local_author()
return self.trim(self.cmd:exec('config user.name', true))
end
function api:write_local_author()
local name, err = self:local_author()
if name then
tex.write(name)
else
tex.error(err)
end
end
function api:local_email()
return self.trim(self.cmd:exec('config user.email', true))
end
function api:write_local_email()
local name, err = self:local_email()
if name then
tex.write(name)
else
tex.error(err)
end
end
function api:authors(sort_by_contrib)
local authors, err = self.cmd:shortlog(sort_by_contrib, true)
if authors then
local author_list = {}
for line in authors:gmatch('(.-)\n') do
local contributions, name, email = line:match("^%s-(%d+)%s-(.-)%s-<(.-)>%s-$")
table.insert(author_list, {
contributions = contributions,
name = name,
email = email
})
end
return author_list
else
return nil, err
end
end
function api:cs_for_authors(csname, conjunction, sort_by_contrib)
if token.is_defined(csname) then
local tok = token.create(csname)
local authors, err = self:authors(sort_by_contrib)
if authors then
local next_conj
for _, author in ipairs(authors) do
if next_conj then
tex.print(next_conj)
end
next_conj = conjunction
tex.print(tok, '{' .. self:escape_str(self.trim(author.name)) .. '}', '{' .. self:escape_str(self.trim(author.email)) .. '}')
end
else
tex.error(err)
end
else
tex.error('ERROR: \\' .. csname .. ' not defined')
end
end
function api:cs_commit(csname, rev, format)
if token.is_defined(csname) then
local tok = token.create(csname)
local log, err = self.cmd:log(format, rev, { 'max-count=1' })
if log then
if #log == 1 then
tex.print(tok)
for _, value in ipairs(log[1]) do
tex.print('{' .. self:escape_str(value) .. '}')
end
else
texio.write_nl('Warning: commit returned none')
end
else
tex.error('ERROR: ' .. (err or 'nil'))
end
else
tex.error('ERROR: \\' .. csname .. ' not defined')
end
end
function api:cs_last_commit(csname, format)
return self:cs_commit(csname, '-1', format)
end
function api:cs_for_commit(csname, rev_spec, format)
if token.is_defined(csname) then
local tok = token.create(csname)
local log, err = self.cmd:log(format, rev_spec)
if log then
for _, commit in ipairs(log) do
tex.print(tok)
for _, value in ipairs(commit) do
tex.print('{' .. self:escape_str(value) .. '}')
end
end
else
tex.error('ERROR:\\' .. err)
end
else
tex.error('ERROR: \\' .. csname .. ' not defined')
end
end
function api:tag_info(format_spec, tag, target_dir)
local err, info
info, err = self.cmd:for_each_ref(format_spec, 'refs/tags', { 'count=1', 'contains=' .. tag }, target_dir)
if info and #info == 1 then
return info[1]
else
tex.error(err or 'Result count didn\'t match. (in tag_info)')
end
end
function api:tags(target_dir)
local tag_list = {}
local tags, err = self.cmd:exec('tag -l --sort=-v:refname', true, target_dir)
if tags then
for tag in tags:gmatch('(.-)\n') do
table.insert(tag_list, self.trim(tag))
end
else
return nil, err
end
return tag_list
end
function api:cs_tag(csname, format_spec, tag, target_dir)
if token.is_defined(csname) then
local tok = token.create(csname)
local info = self:tag_info(format_spec, tag, target_dir)
if info then
tex.print(tok)
for _, value in ipairs(info) do
tex.print('{' .. self:escape_str(value) .. '}')
end
end
else
tex.error('ERROR:\\' .. csname .. ' not defined')
end
end
function api:cs_for_tag(csname, format_spec, target_dir)
if token.is_defined(csname) then
local tok = token.create(csname)
local tags, err = self.cmd:for_each_ref(format_spec, 'refs/tags', { 'sort=-authordate' }, target_dir)
if tags then
for _, info in ipairs(tags) do
tex.print(tok)
for _, value in ipairs(info) do
tex.print('{' .. self:escape_str(value) .. '}')
end
end
else
tex.error('ERROR:\\' .. err)
end
else
tex.error('ERROR:\\' .. csname .. ' not defined')
end
end
function api:cs_for_tag_sequence(csname, target_dir)
if token.is_defined(csname) then
local tok = token.create(csname)
local seq, err = self:tags(target_dir)
if seq then
for idx, tag in ipairs(seq) do
if idx < #seq then
local next = seq[idx + 1]
tex.print(tok, '{' .. tag .. '}{' .. next .. '}{' .. tag .. '...' .. next .. '}')
else
tex.print(tok, '{' .. tag .. '}{}{' .. tag .. '}')
end
end
else
tex.error('ERROR:\\' .. (err or 'Unknown error'))
end
else
tex.error('ERROR:\\' .. csname .. ' not defined')
end
end
return gitinfo
|