# Parse git commit date e.g. for datetime

I am working on a LaTeX document with LuaLaTeX and try to include git information in the document to make it easier to find the correct version once I stumble over a printed copy.

With the help of some posts here, I was already able to get most of the git information in the document: commit hash and the timestamp. This is following the \input|"git log -1 --format=\@percentchar ci ./" approach.

But now I would like to use the timestamp also as the date for e.g. \maketitle. So I came across this post: Is it possible to parse a date string in LaTeX to convert it to a different date format? The solution provided there works great for me, as long as I use a test date created with \newcommand{\testDate}{2021-05-27 ...}. But as soon as I want to use the date retrieved from git, it stops working.

Down below you find a MWE, which produces various kinds of errors, depending on which line you comment out

\documentclass{article}

\usepackage[ngerman]{babel}

% define a parser
\def\parsegitdate#1-#2-#3 #4\endparse{#3.#2.#1}

\begin{document}
\makeatletter
% first definiton
\newcommand{\gitdatex}{\input|"git log -1 --format=\@percentchar ci"}
% second definition
\def\gitdatey{\@@input|"git log -1 --format=\@percentchar ci"}
% third definition
\edef\gitdatez{\@@input|"git log -1 --format=\@percentchar ci"}
\makeatother

% print date for reference
\gitdatex

% version 1
\expandafter\parsegitdate\gitdatex\endparse

% version 2
\expandafter\parsegitdate\gitdatey\endparse

% version 3
\expandafter\parsegitdate\gitdatez\endparse

\end{document}


First and second definition with version 1 and 2 give me the same error: ! Paragraph ended before \parsegitdate was complete. Apparently the definition is not properly expanded and the actual git command is handed to the parser rather than its result. Thus, I tried \edef.

The third definition with \edef and version 3 produce the desired result, as long as I do not include \usepackage[ngerman]{babel}. Once I include that package (with the option ngerman), I get an ! Argument of \language@active@arg" has an extra }.

How can I get this to work in combination?

I am also aware of the following answers, which are kind of similar. But I actually do not want to use external scripts. Thus, gitinfo2 or vsc bundle are not my desired solution.

By the way: Can someone explain the \endparse to me as well? I was not able to find any information what this is actually meaning and doing.

EDIT:

So I did some digging into why the closing brace is passed through to \edef, and found this helpful post here. Apparently, the TeX input primitive looks for an implicit \space to signal the end of what needs to be read, so we have to add this. Additionally, \input adds a new empty line, which is why the code then errors out if you don't suppress it via \everyeof{\noexpand}. Last, we also need to define \gitdate now globally via \xdef.

Your MWE is thus changed to:

\documentclass{article}

\usepackage[ngerman]{babel}

% define a parser
\def\parsegitdate#1-#2-#3 #4\endparse{#3.#2.#1}

\makeatletter
\begingroup\everyeof{\noexpand}
\xdef\gitdate{\@@input|"git log -1 --format=\@percentchar ci ."\space}
\endgroup
\makeatother

\begin{document}

% print date for reference
\gitdate

\expandafter\parsegitdate\gitdate\endparse

\end{document}


ORIGINAL POST

sigh

After trying various solutions for quite some time, I seem to have lost my head. Reading over the finished post here, one more idea came to my mind: Just move the definitions outside of \begin{document}. This simply works, now.

\documentclass{article}

\usepackage[ngerman]{babel}

% define a parser
\def\parsegitdate#1-#2-#3 #4\endparse{#3.#2.#1}

\makeatletter
\edef\gitdate{\@@input|"git log -1 --format=\@percentchar ci"}
\makeatother

\begin{document}

% print date for reference
\gitdate

\expandafter\parsegitdate\gitdate\endparse

\end{document}

• To my surprise the closing brace } of the \edef is also handed to the command line. This does not cause any issues with the current example. But it causes the command to fail, if one tries to restrict the command to the current directory: \edef\gitdate{\@@input|"git log -1 --format=\@percentchar ci ."} May 27, 2021 at 12:45
• I'm glad that you found a solution
– hola
May 28, 2021 at 7:26