summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-recorder.lua
blob: 8d4aa78af2364e4bce18bb61be94a66896387adb (plain)
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
-- gitinfo-lua-recorder.lua
-- Copyright 2024 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-lua-cmd.lua, gitinfo-lua-recorder.lua and gitinfo-lua.lua

local kpse = kpse or require('kpse')
local texio = texio or require('texio')

local api = {
    record_list = {}
}

---record_head
---Records .git/HEAD and .git/refs/heads/<branch> respectively,
---in order to trigger a rebuild in LaTeX.
---@param git_directory string
function api.record_head(git_directory)
    local head_path = '.git/HEAD'
    if git_directory then
        head_path = git_directory .. head_path
    end
    if not api.record_list[head_path] then
        api.record_list[head_path] = true
        if kpse.in_name_ok(head_path) then
            local head_file = io.open(head_path, 'rb')
            if not head_file then
                texio.write_nl('Warning: couldn\'t read HEAD from git project directory')
                return
            end
            kpse.record_input_file(head_path)
            texio.write_nl('Info: recording input file ' .. head_path)
            local head_info = head_file:read('*a')
            head_file:close()
            local i, j = string.find(head_info, '^ref: .+\n$')
            local ref_path = string.sub(head_info, i + 5, j-1)
            if not ref_path then
                texio.write_nl('Warning: couldn\'t find ref of HEAD')
                return
            end
            ref_path = '.git/' .. ref_path
            if git_directory then
                ref_path = git_directory .. ref_path
            end
            if kpse.in_name_ok(ref_path) then
                kpse.record_input_file(ref_path)
                texio.write_nl('Info: recording input file ' .. ref_path)
            else
                texio.write_nl('Warning: couldn\'t read ref file: ' .. ref_path)
            end
        else
            texio.write_nl('Couldn\'t open input file ' .. head_path)
        end
    end
end



local gitinfo_recorder = {}
local gitinfo_recorder_mt = {
    __index = api,
    __newindex = nil
}

setmetatable(gitinfo_recorder, gitinfo_recorder_mt)

return gitinfo_recorder