summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/latexgit/latexgit.sty
blob: d01af9bdb37241fe360ecf1f03e37ca09f3baa2f (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
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
%%
%% This is file `latexgit.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latexgit.dtx  (with options: `package')
%% latexgit
%% Author: Camil Staps <info@camilstaps.nl>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{latexgit}[2020/04/20]

\RequirePackage{pgfkeys}
\RequirePackage{datetime}
\newcommand{\latexgit}[0]{\LaTeX{}git}
\newcommand{\git@result}[0]{\git@rawresult\unskip}
\newif\ifgit@opt@FormatDate
\newif\ifgit@opt@FormatTime
\newif\ifgit@opt@ShortHash
\newif\ifgit@opt@ShowTimeZone
\pgfkeys{
  /git/.is family, /git,
  default/.style={
    directory=.,
    file=,
    formatDate=false,
    formatInterDateTime=\space{}at\space{},
    formatTime=false,
    revision=HEAD,
    showTimeZone=false,
    shortHash=true,
  },
  directory/.estore in=\git@opt@Directory,
  file/.estore in=\git@opt@File,
  formatDate/.is if=git@opt@FormatDate,
  formatInterDateTime/.estore in=\git@opt@FormatInterDateTime,
  formatTime/.is if=git@opt@FormatTime,
  revision/.estore in=\git@opt@Revision,
  showTimeZone/.is if=git@opt@ShowTimeZone,
  shortHash/.is if=git@opt@ShortHash,
}
\newread\git@stream%
\newcommand{\git@command}[1]{%
  \def\git@rawresult{}%
  \openin\git@stream|"cd \git@opt@Directory; #1"
  \ifeof\git@stream%
    \PackageError{latexgit}%
      {invoke LaTeX with the -shell-escape flag}%
      {invoke LaTeX with the -shell-escape flag}%
  \else%
    \begingroup%
    \catcode`\^^M9%
    \endlinechar=-1%
    \readline\git@stream to \git@streamoutput%
    \global\let\git@rawresult\git@streamoutput%
    \endgroup%
  \fi%
}
\newcommand{\gitcommand}[2][]{%
  \pgfkeys{/git,default,#1}%
  \git@command{git #2}%
  \git@result}
\catcode`\&=14\catcode`\%=11
\def\git@space{ }
\newcommand{\gitcommithash}[1][]{&
  \git@commithash[#1]\git@result}
\newcommand{\git@commithash}[1][]{&
  \pgfkeys{/git,default,#1}&
  \ifgit@opt@ShortHash&
    \git@command{git log -n 1 --format=%h
      \git@opt@Revision\git@space -- \git@opt@File}&
  \else&
    \git@command{git log -n 1 --format=%H
      \git@opt@Revision\git@space -- \git@opt@File}&
  \fi&
}
\newcommand{\gitcommitmsg}[1][]{&
  \git@commitmsg[#1]\git@result}
\newcommand{\git@commitmsg}[1][]{&
  \pgfkeys{/git,default,#1}&
  \git@command{git log -n 1 --format=%B
    \git@opt@Revision\git@space -- \git@opt@File}&
}
\def\git@formatCommitDate#1-#2-#3 #4:#5:#6 +#7\relax{&
  \formatdate{#3}{#2}{#1}&
}
\def\git@formatCommitTime#1-#2-#3 #4:#5:#6 +#7\relax{&
  \formattime{#4}{#5}{#6}&
  \ifgit@opt@ShowTimeZone&
    \space(+#7\unskip)&
  \fi&
}
\newcommand{\gitcommitdate}[1][]{&
  \git@commitdate[#1]&
  \ifgit@opt@FormatDate&
    \expandafter\git@formatCommitDate\git@rawresult\relax&
    \ifgit@opt@FormatTime&
      \git@opt@FormatInterDateTime&
      \expandafter\git@formatCommitTime\git@rawresult\relax&
    \fi
  \else\ifgit@opt@FormatTime&
    \expandafter\git@formatCommitTime\git@rawresult\relax&
  \else
    \git@result&
  \fi\fi&
}
\newcommand{\git@commitdate}[1][]{&
  \pgfkeys{/git,default,#1}&
  \git@command{git log -n 1 --format=%ai
    \git@opt@Revision\git@space -- \git@opt@File}&
}
\newcommand{\gitcommitauthor}[1][]{&
  \git@commitauthor[#1]\git@result}
\newcommand{\git@commitauthor}[1][]{&
  \pgfkeys{/git,default,#1}&
  \git@command{git log -n 1 --format='%an <%ae>'
    \git@opt@Revision\git@space -- \git@opt@File}&
}
\newcommand{\gitcommitauthorname}[1][]{&
  \git@commitauthorname[#1]\git@result}
\newcommand{\git@commitauthorname}[1][]{&
  \pgfkeys{/git,default,#1}&
  \git@command{git log -n 1 --format=%an
    \git@opt@Revision\git@space -- \git@opt@File}&
}
\newcommand{\gitcommitauthoremail}[1][]{&
  \git@commitauthoremail[#1]\git@result}
\newcommand{\git@commitauthoremail}[1][]{&
  \pgfkeys{/git,default,#1}&
  \git@command{git log -n 1 --format=%ae
    \git@opt@Revision\git@space -- \git@opt@File}&
}
\newcommand{\git@commitparent}[1][]{&
  \pgfkeys{/git,default,#1}&
  \git@command{git log -n 1 --format=%p
    \git@opt@Revision\git@space -- \git@opt@File
    | cut -d' ' -f2}&
}
\newcommand{\gitchanges}[1][]{&
  \git@changes[#1]{HEAD}
}
\newcommand{\git@changes}[2][]{&
  \edef\git@@revision{#2}&
  \git@commithash[revision=\git@@revision]&
  \edef\git@@thishash{\git@rawresult}&
  \git@command{git log -n 1 --format=%ad --date=short \git@opt@Revision}&
  \edef\git@@thisdate{\git@rawresult}&
  \git@commitmsg[revision=\git@@revision]&
  & TODO: this removes '=' characters because they break \changes, but the real
  & solution would be to put something back that restores these characters.
  \StrSubstitute[0]{\git@rawresult}{=}{}[\git@@thismsg]&
  \changes{\git@@thisdate\unskip: \git@@thishash}{\git@@thisdate}{\git@@thismsg}&
  \git@commitparent[revision=\git@@revision]&
  \let\git@@parent\git@rawresult&
  \setbox0=\hbox{\git@@parent\unskip}\ifdim\wd0=0pt
  \else&
    \git@changes{\git@@parent}&
  \fi&
}
\catcode`\&=4\catcode`\%=14
%% Copyright (c) 2016-2020 Camil Staps <info@camilstaps.nl>
%% Licensed under GPL v3.
%%
%% End of file `latexgit.sty'.