diff options
author | Karl Berry <karl@freefriends.org> | 2024-04-12 21:46:10 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2024-04-12 21:46:10 +0000 |
commit | 11d36f56f8f5115d58d3a97c78b8a8945383c5fb (patch) | |
tree | 7599d7502ff7f794234b065df54cae7150aa29cc /Master/texmf-dist/scripts/gitinfo-lua | |
parent | e5759d48525423bdcb3c90b3fb73a2357740d18b (diff) |
gitinfo-lua (12apr24)
git-svn-id: svn://tug.org/texlive/trunk@70931 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/gitinfo-lua')
3 files changed, 99 insertions, 35 deletions
diff --git a/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-cmd.lua b/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-cmd.lua index 68ac3c09a9b..32d59de1809 100644 --- a/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-cmd.lua +++ b/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-cmd.lua @@ -38,12 +38,25 @@ function api.trim(s) return (s:gsub("^%s*(.-)%s*$", "%1")) end -function api:exec(command, do_caching, target_dir) - local cmd = self.executable .. ' ' .. command +function api:exec(command, do_caching, target_dir, no_recording, path_spec) local cwd = target_dir or self.cwd - api.recorder.record_head(cwd) + local cmd = self.executable if cwd then - cmd = 'cd ' .. cwd .. ' && ' .. cmd + cmd = cmd .. ' -C ' .. cwd + end + cmd = cmd .. ' ' .. command + if not no_recording then + api.recorder.record_head(cwd) + end + if path_spec then + if type(path_spec) == 'table' then + cmd = cmd .. ' --' + for _,path in ipairs(path_spec) do + cmd = cmd .. ' ' .. path + end + elseif type(path_spec) == 'string' then + cmd = cmd .. ' -- ' .. path_spec + end end if do_caching then local found, result = cache:seek(cmd) @@ -53,7 +66,7 @@ function api:exec(command, do_caching, target_dir) end local f = io.popen(cmd) if f == nil then - return nil, "Couldn't execute git command.\n\tIs option '-shell-escape' turned on?" + return nil, "Couldn't execute git command.\n\tIs option '--shell-escape' turned on?" end local s = f:read('*a') if f:close() then @@ -62,7 +75,7 @@ function api:exec(command, do_caching, target_dir) end return s else - return nil, 'Error executing git command' + return nil, 'Error executing git command\n\t"' .. cmd .. '"' end end @@ -142,7 +155,7 @@ function api:parse_response(buffer) return results end -function api:log(format_spec, revision, options, target_dir) +function api:log(format_spec, revision, options, target_dir, path_spec) local format, err = self:parse_format_spec(format_spec) if err then return nil, err @@ -155,7 +168,7 @@ function api:log(format_spec, revision, options, target_dir) if revision and revision ~= '' then cmd = cmd .. ' ' .. revision end - local response, err = self:exec(cmd, true, target_dir) + local response, err = self:exec(cmd, true, target_dir, false, path_spec) if not response then return nil, err end diff --git a/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-recorder.lua b/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-recorder.lua index 8d4aa78af23..4fc798ca69e 100644 --- a/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-recorder.lua +++ b/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua-recorder.lua @@ -30,7 +30,7 @@ local api = { function api.record_head(git_directory) local head_path = '.git/HEAD' if git_directory then - head_path = git_directory .. head_path + head_path = git_directory .. '/' .. head_path end if not api.record_list[head_path] then api.record_list[head_path] = true @@ -45,20 +45,24 @@ function api.record_head(git_directory) 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) + if i and j then + 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('Warning: couldn\'t read ref file: ' .. ref_path) + texio.write_nl('Warning: didn\'t find any ref in .git/HEAD') end else texio.write_nl('Couldn\'t open input file ' .. head_path) diff --git a/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua.lua b/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua.lua index cba19b19d78..8c346ec3bed 100644 --- a/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua.lua +++ b/Master/texmf-dist/scripts/gitinfo-lua/gitinfo-lua.lua @@ -23,8 +23,8 @@ end local module = { name = 'gitinfo-lua', info = { - version = '1.0.3', --TAGVERSION - date = '2024/04/02', --TAGDATE + version = '1.1.0', --TAGVERSION + date = '2024/04/11', --TAGDATE comment = "Git info Lua — Git integration with LaTeX", author = "Erik Nijenhuis", license = "free" @@ -56,6 +56,8 @@ local mt = { local gitinfo = {} setmetatable(gitinfo, mt) +local luakeys = require('luakeys')() + function api.trim(s) return s and (s:gsub("^%s*(.-)%s*$", "%1")) or 'nil' end @@ -85,6 +87,15 @@ function api:dir(path) self.cmd.cwd = path end +function api:dir_to_root() + local toplevel, err = self.cmd:exec('rev-parse --show-toplevel', false, nil, true) + if toplevel then + self.cmd.cwd = self.cmd.trim(toplevel) + else + tex.error(err) + end +end + function api:version() return self.trim(self.cmd:exec('describe --tags --always', true)) end @@ -167,7 +178,7 @@ function api:cs_for_authors(csname, conjunction, sort_by_contrib) tex.error(err) end else - tex.error('ERROR: \\' .. csname .. ' not defined') + tex.error('ERROR: ' .. csname .. ' not defined') end end @@ -188,7 +199,7 @@ function api:cs_commit(csname, rev, format) tex.error('ERROR: ' .. (err or 'nil')) end else - tex.error('ERROR: \\' .. csname .. ' not defined') + tex.error('ERROR: ' .. csname .. ' not defined') end end @@ -196,10 +207,46 @@ function api:cs_last_commit(csname, format) return self:cs_commit(csname, '-1', format) end -function api:cs_for_commit(csname, rev_spec, format) +local parse_commit_opts = luakeys.define({ + rev_spec = { pick = 'string' }, + files = { data_type = 'list' }, + cwd = { data_type = 'string' }, + flags = { + sub_keys = { + merges = { data_type='boolean', exclusive_group='merges' }, + ['no-merges'] = { data_type='boolean', exclusive_group='merges' } + } + } +}) +local function parse_flags(flags_table) + local t = {} + if flags_table then + for k,v in pairs(flags_table) do + if v then + table.insert(t, k) + end + end + end + return t +end +function api:cs_for_commit(csname, args, format) if token.is_defined(csname) then local tok = token.create(csname) - local log, err = self.cmd:log(format, rev_spec) + local opts = parse_commit_opts(args) + -- Something is going wrong with the parsing of rev_spec with pick, which ends up to be missing. + -- This is a workaround to ensure the old API would still work. + -- This will be fixed after luakeys version >0.13.0 + if type(opts.rev_spec) ~= 'string' then + local i = string.find(args, ',') + if i then + opts.rev_spec = string.sub(args, 1, i-1) + else + opts.rev_spec = args + end + else + opts.rev_spec = string.gsub(opts.rev_spec, '[\'"]', '') + end + local log, err = self.cmd:log(format, opts.rev_spec, parse_flags(opts.flags), opts.cwd, opts['files']) if log then for _, commit in ipairs(log) do tex.print(tok) @@ -208,10 +255,10 @@ function api:cs_for_commit(csname, rev_spec, format) end end else - tex.error('ERROR:\\' .. err) + tex.error('ERROR: ' .. err) end else - tex.error('ERROR: \\' .. csname .. ' not defined') + tex.error('ERROR: ' .. csname .. ' not defined') end end @@ -249,7 +296,7 @@ function api:cs_tag(csname, format_spec, tag, target_dir) end end else - tex.error('ERROR:\\' .. csname .. ' not defined') + tex.error('ERROR: ' .. csname .. ' not defined') end end @@ -265,10 +312,10 @@ function api:cs_for_tag(csname, format_spec, target_dir) end end else - tex.error('ERROR:\\' .. err) + tex.error('ERROR: ' .. err) end else - tex.error('ERROR:\\' .. csname .. ' not defined') + tex.error('ERROR: ' .. csname .. ' not defined') end end @@ -286,10 +333,10 @@ function api:cs_for_tag_sequence(csname, target_dir) end end else - tex.error('ERROR:\\' .. (err or 'Unknown error')) + tex.error('ERROR: ' .. (err or 'Unknown error')) end else - tex.error('ERROR:\\' .. csname .. ' not defined') + tex.error('ERROR: ' .. csname .. ' not defined') end end |