-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluagit.lua
More file actions
126 lines (111 loc) · 3.54 KB
/
luagit.lua
File metadata and controls
126 lines (111 loc) · 3.54 KB
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
-- luagit.lua
--
--[[
Sets the following TeX variables:
\ifingit = true if in a git repository
\gitbranch = name of (local) git branch
\lastgitcommitref = git commit reference
\lastgitcommitdate = git commit date
\lastgitcommitname = Author name
\lastgitcommitemail = Author email
\ifchangedsincegitcommit = true if changed files in working area since commit
Example git commands
-------------------
$ git log -s
commit 7c750c507d6c68c2360335c23f41346586032a41
Author: Earthspike <nospam@nowhere.net>
Date: Sat Feb 17 17:46:29 2018 +0000
Initial commit
--
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
luagit.aux
luagit.log
luagit.lua
luagit.lua~
luagit.pdf
luagit.tex
luagit.tex~
nothing added to commit but untracked files present (use "git add" to track)
--
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
--
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: luagit.tex
Untracked files:
(use "git add <file>..." to include in what will be committed)
luagit.aux
luagit.log
luagit.lua
luagit.lua~
luagit.pdf
luagit.tex~
--
$ git status
On branch master
nothing to commit, working directory clean
--]]
-- Define and default all TeX variables as if not in a git repo
tex.print("\\newif\\ifingit\\ingitfalse")
tex.print("\\def\\gitbranch{}\\def\\lastgitcommitref{}\\def\\lastgitcommitdate{}")
tex.print("\\def\\lastgitcommitname{}\\def\\lastgitcommitemail{}")
tex.print("\\newif\\ifchangedsincegitcommit\\changedsincegitcommittrue")
local handle = io.popen("git status")
local result = handle:read("*a")
local success, err = handle:close()
local gitrepo = success
local gitbranch = ""
local gitcommit = ""
local gitauthorname = ""
local gitauthoremail = ""
local gitcommitdate = ""
local gitchanged = false
-- local result=lua.version
-- tex.print(result)
if success then
-- We're in a git repository
tex.print("\\ingittrue")
-- Break result into lines
local lines = {}
result:gsub("[^\10]+", function(l)
lines[#lines + 1] = l
end)
-- Extract the git branch name
gitbranch = lines[1]:match("On branch (.+)")
tex.print("\\def\\gitbranch{" .. gitbranch .. "}")
-- Check that working area is still clean
local test_phrase = "nothing to commit, working directory clean"
if lines[#lines]:sub(1,string.len(test_phrase)) == test_phrase then
tex.print("\\changedsincegitcommitfalse")
end
-- Now get details of the last commit from `git log -s`
local handle = io.popen("git log -s")
local result = handle:read("*a")
local success, err = handle:close()
if success then
-- Break into lines
lines = {}
result:gsub("[^\10]+", function(l)
lines[#lines + 1] = l
end)
-- Get the commit reference
gitcommit = lines[1]:match("commit (%x+)")
tex.print("\\def\\lastgitcommitref{" .. gitcommit .. "}")
-- Get the author name and email
gitauthorname, gitauthoremail = lines[2]:match("Author: (.+) <(.+)>")
tex.print("\\def\\lastgitcommitname{" .. gitauthorname .. "}\\def\\lastgitcommitemail{" .. gitauthoremail .. "}")
-- Get the date
gitcommitdate = lines[3]:match("Date:%s+(.+)")
tex.print("\\def\\lastgitcommitdate{" .. gitcommitdate .. "}")
else
-- No git log (empty repo)
tex.print("\\def\\lastgitcommitref{(Empty repository)}")
end
end