Skip to content

Commit 09e0062

Browse files
committed
Add HEAD recording
Ensure that a recompilation is triggered when the HEAD or its ref is changed.
1 parent 6f2c377 commit 09e0062

File tree

4 files changed

+88
-59
lines changed

4 files changed

+88
-59
lines changed

scripts/gitinfo-lua-cmd.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- gitinfo-lua-cmd.lua
2-
-- Copyright 2023 E. Nijenhuis
2+
-- Copyright 2024 E. Nijenhuis
33
--
44
-- This work may be distributed and/or modified under the
55
-- conditions of the LaTeX Project Public License, either version 1.3c
@@ -14,14 +14,15 @@
1414
-- The Current Maintainer of this work is E. Nijenhuis.
1515
--
1616
-- This work consists of the files gitinfo-lua.sty gitinfo-lua.pdf
17-
-- gitinfo-cmd.lua and gitinfo-lua.lua
17+
-- gitinfo-lua-cmd.lua, gitinfo-lua-recorder.lua and gitinfo-lua.lua
1818

1919
local api = {
2020
cwd = nil,
2121
executable = 'git',
2222
default_sort = '',
2323
attribute_separator = '\\pop',
24-
record_separator = '\\end'
24+
record_separator = '\\end',
25+
recorder = require('gitinfo-lua-recorder')
2526
}
2627
local cache = {}
2728
function cache:seek(_key)
@@ -40,6 +41,7 @@ end
4041
function api:exec(command, do_caching, target_dir)
4142
local cmd = self.executable .. ' ' .. command
4243
local cwd = target_dir or self.cwd
44+
api.recorder.record_head(cwd)
4345
if cwd then
4446
cmd = 'cd ' .. cwd .. ' && ' .. cmd
4547
end

scripts/gitinfo-lua-recorder.lua

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-- gitinfo-lua-recorder.lua
2+
-- Copyright 2024 E. Nijenhuis
3+
--
4+
-- This work may be distributed and/or modified under the
5+
-- conditions of the LaTeX Project Public License, either version 1.3c
6+
-- of this license or (at your option) any later version.
7+
-- The latest version of this license is in
8+
-- http://www.latex-project.org/lppl.txt
9+
-- and version 1.3c or later is part of all distributions of LaTeX
10+
-- version 2005/12/01 or later.
11+
--
12+
-- This work has the LPPL maintenance status ‘maintained’.
13+
--
14+
-- The Current Maintainer of this work is E. Nijenhuis.
15+
--
16+
-- This work consists of the files gitinfo-lua.sty gitinfo-lua.pdf
17+
-- gitinfo-lua-cmd.lua, gitinfo-lua-recorder.lua and gitinfo-lua.lua
18+
19+
local kpse = kpse or require('kpse')
20+
local texio = texio or require('texio')
21+
22+
local api = {
23+
record_list = {}
24+
}
25+
26+
---record_head
27+
---Records .git/HEAD and .git/refs/heads/<branch> respectively,
28+
---in order to trigger a rebuild in LaTeX.
29+
---@param git_directory string
30+
function api.record_head(git_directory)
31+
local head_path = '.git/HEAD'
32+
if git_directory then
33+
head_path = git_directory .. head_path
34+
end
35+
if not api.record_list[head_path] then
36+
api.record_list[head_path] = true
37+
if kpse.in_name_ok(head_path) then
38+
local head_file = io.open(head_path, 'rb')
39+
if not head_file then
40+
texio.write_nl('Warning: couldn\'t read HEAD from git project directory')
41+
return
42+
end
43+
kpse.record_input_file(head_path)
44+
texio.write_nl('Info: recording input file ' .. head_path)
45+
local head_info = head_file:read('*a')
46+
head_file:close()
47+
local i, j = string.find(head_info, '^ref: .+\n$')
48+
local ref_path = string.sub(head_info, i + 5, j-1)
49+
if not ref_path then
50+
texio.write_nl('Warning: couldn\'t find ref of HEAD')
51+
return
52+
end
53+
ref_path = '.git/' .. ref_path
54+
if git_directory then
55+
ref_path = git_directory .. ref_path
56+
end
57+
if kpse.in_name_ok(ref_path) then
58+
kpse.record_input_file(ref_path)
59+
texio.write_nl('Info: recording input file ' .. ref_path)
60+
else
61+
texio.write_nl('Warning: couldn\'t read ref file: ' .. ref_path)
62+
end
63+
else
64+
texio.write_nl('Couldn\'t open input file ' .. head_path)
65+
end
66+
end
67+
end
68+
69+
70+
71+
local gitinfo_recorder = {}
72+
local gitinfo_recorder_mt = {
73+
__index = api,
74+
__newindex = nil
75+
}
76+
77+
setmetatable(gitinfo_recorder, gitinfo_recorder_mt)
78+
79+
return gitinfo_recorder

scripts/gitinfo-lua.lua

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- gitinfo-lua.lua
2-
-- Copyright 2023 E. Nijenhuis
2+
-- Copyright 2024 E. Nijenhuis
33
--
44
-- This work may be distributed and/or modified under the
55
-- conditions of the LaTeX Project Public License, either version 1.3c
@@ -14,7 +14,7 @@
1414
-- The Current Maintainer of this work is E. Nijenhuis.
1515
--
1616
-- This work consists of the files gitinfo-lua.sty gitinfo-lua.pdf
17-
-- gitinfo-cmd.lua and gitinfo-lua.lua
17+
-- gitinfo-lua-cmd.lua, gitinfo-lua-recorder.lua and gitinfo-lua.lua
1818

1919
if not modules then
2020
modules = {}
@@ -81,58 +81,6 @@ function api:escape_str(value)
8181
return buf
8282
end
8383

84-
-- experimental
85-
function api:get_tok()
86-
if self.cur_tok == nil then
87-
self.cur_tok = token.get_next()
88-
end
89-
return self.cur_tok
90-
end
91-
92-
-- experimental
93-
function api:parse_opts()
94-
local tok = self:get_tok()
95-
if tok.cmdname == 'other_char' then
96-
--token.put_next(tok)
97-
local opts = token.scan_word()
98-
self.cur_tok = nil
99-
-- todo: parse []
100-
return opts
101-
end
102-
end
103-
104-
-- experimental
105-
function api:parse_arguments(argc)
106-
local result_list = {}
107-
for _ = 1, argc do
108-
local tok = self:get_tok()
109-
if tok.cmdname == 'left_brace' then
110-
token.put_next(tok)
111-
table.insert(result_list, token.scan_argument())
112-
self.cur_tok = nil
113-
else
114-
tex.error("Expected left brace")
115-
return
116-
end
117-
end
118-
return table.unpack(result_list)
119-
end
120-
121-
-- experimental
122-
function api:parse_macro()
123-
--tex.print('\\noexpand')
124-
local tok = self:get_tok()
125-
if (tok.cmdname == 'call') or tok.cmdname == 'long_call' then
126-
self.cur_tok = nil
127-
return tok
128-
else
129-
tex.error("Expected Macro")
130-
for i = 1, 5 do
131-
local _tok = token.get_next()
132-
end
133-
end
134-
end
135-
13684
function api:dir(path)
13785
self.cmd.cwd = path
13886
end

tex/gitinfo-lua.sty

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%% gitinfo-lua.sty
2-
%% Copyright 2023 E. Nijenhuis
2+
%% Copyright 2024 E. Nijenhuis
33
%
44
% This work may be distributed and/or modified under the
55
% conditions of the LaTeX Project Public License, either version 1.3c
@@ -14,7 +14,7 @@
1414
% The Current Maintainer of this work is E. Nijenhuis.
1515
%
1616
% This work consists of the files gitinfo-lua.sty gitinfo-lua.pdf
17-
% gitinfo-cmd.lua and gitinfo-lua.lua
17+
% gitinfo-lua-cmd.lua, gitinfo-lua-recorder.lua and gitinfo-lua.lua
1818
\NeedsTeXFormat{LaTeX2e}
1919
\ProvidesPackage{gitinfo-lua}[2024/02/23 1.0.2 Xerdi's Git Package]
2020

0 commit comments

Comments
 (0)