%% gitinfo-lua.tex %% Copyright 2023 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-cmd.lua and gitinfo-lua.lua \documentclass{ltxdoc} \usepackage[english]{babel} \usepackage[titlepage,authors,rootdir]{gitinfo-lua} \usepackage{listings} \lstset{ columns=fullflexible, basicstyle=\ttfamily\lst@ifdisplaystyle\small\fi, commentstyle={\slshape}, showspaces=false, showstringspaces=false, breaklines=true, breakatwhitespace=true, breakindent=1em, prebreak=\raisebox{0ex}[0ex][0ex] { \ensuremath{_{\kern-2.2pt\hookleftarrow}}} } \usepackage{calc} \usepackage{multicol} \usepackage{tabularx} \usepackage{xcolor} \usepackage{textcomp} \usepackage[orig,english]{isodate} \def\projecturl{https://github.com/Xerdi/gitinfo-lua} \title{\textbf{Gitinfo Lua} package\thanks{This document corresponds to package \texttt{gitinfo-lua} version \gitversion{} written on \gitdate{}.}} \def\cmd{\lstinline[language={[LaTeX]TeX},keywordsprefix={\\}]} \usepackage{hyperref} \usepackage[nonewpage]{imakeidx} \CodelineIndex \EnableCrossrefs \makeindex[columns=1] \newlength\xample \newlength\xamplesep \setlength\xample{6cm-5pt} \setlength\xamplesep{5pt} \begin{document} \RecordChanges \maketitle \begin{abstract} This project aims to display git project information in PDF documents. It's mostly written in Lua for executing the \texttt{git} commands, therefore making this package only applicable for \texttt{lualatex}. If \texttt{lualatex} isn't working for you, you could try \href{https://ctan.org/pkg/gitinfo2}{gitinfo2} instead. For \LaTeX{} it provides a set of standard macros for displaying basic information or setting the project directory, and a set of advanced macros for formatting commits and tags. \end{abstract} \begin{multicols}{2} \tableofcontents % \lstlistoflistings \printindex \end{multicols} \clearpage \section{Usage} For the package to work one should work, and only work, with Lua\TeX{}. Another prerequisite is that there is an available git repository either in the working directory, or somewhere else on your machine (see section~\ref{sub:tex-basic}). \subsection{Git} For this package to work at a minimum, there has to be an initialized Git repository, and preferably, at least with one commit. For example, the following minimal example should do the trick already: \begin{lstlisting}[language=bash,frame=single,caption={Minimal Git setup},morekeywords={mkdir,git}] mkdir my_project cd my_project echo "# My Project" > README.md git init && git commit -am "Init" \end{lstlisting} Then in order for the changelog to work, the project needs to contain either `lightweight-' or `annotated' tags. The main difference is that a lightweight tag takes no extra options, for example: \texttt{git tag 0.1}. See listing~\ref{lst:scenario} for more examples on authoring and versioning with \texttt{git}. \subsection{Lua\LaTeX{}} For generating the document with \LaTeX{} one must make use of \texttt{lualatex}. For example, when having the main file `\texttt{main.tex}': \begin{lstlisting}[language=bash,frame=single,caption={Generating the document with \LaTeX{}},morekeywords={lualatex,latexmk}] # Generate once lualatex -shell-escape main # Generate and keep watching with LaTeXMK latexmk -pvc -lualatex -shell-escape main \end{lstlisting} Note that in both cases option \texttt{--shell-escape} is required. This is required for issuing \texttt{git} via the commandline. If using \texttt{--shell-restricted} mode, which is the default, make sure to add \texttt{git} to the CSV variable \texttt{shell\_escape\_commands} in either your \texttt{texmf.cnf} or using a Lua initialization script, like: \lstinputlisting[language={[5.3]Lua},caption={Lua initialization script},frame=single]{gitinfo-lua-init.lua} \noindent The Lua initialization script can be used as follows: \begin{lstlisting}[language=bash,frame=single,caption={With Lua initialization script},morekeywords=lualatex] lualatex --lua=gitinfo-lua-init.lua main \end{lstlisting} For using the script with \texttt{latexmk}, this can be achieved with the \texttt{-lualatex="COMMAND"} option or specifying the \texttt{\$lualatex} command using a \texttt{latexmkrc} configuration file: \begin{lstlisting}[language=bash,frame=single,caption={Overriding Lua\LaTeX\ on commandline},morekeywords=latexmk] latexmk --lualatex --lualatex="lualatex --lua=gitinfo-lua-init.lua %O %S" main \end{lstlisting} \begin{lstlisting}[language=perl,frame=single,caption={Overriding Lua\LaTeX\ in \texttt{latexmkrc}}] $lualatex = "lualatex --lua=gitinfo-lua-init.lua %O %S"; \end{lstlisting} Keep in mind that both the Lua initialization script and \texttt{latexmkrc} need to be placed within the same directory as the main file.\\ When utilizing the continuous compilation option \texttt{-pvc} with \texttt{latexmk}, it's important to note that only committed changes will be detected, while tag changes, unfortunately, won't be recognized. \section{LaTeX Interface} \subsection{Package Options} \cmd{\usepackage}% \oarg{opts...}% \texttt{\textbraceleft gitinfo-lua\textbraceright}\index[pkgopts]{gitinfo-lua(.sty)} This package provides some options for default formatting purposes. The author sorting is one of them. If the options contain \texttt{contrib} the authors will be sorted based on their contributions; otherwise, the authors will be sorted alphabetically, which is the default option \texttt{alpha}. Another option is the \texttt{titlepage} option, which sets the \cmd{\author} and \cmd{\date} macros accordingly. By default, it sets the local git author, equivalent to option \texttt{author}. Pass the option \texttt{authors} to set all git authors of the project based on commit history instead. Another option, more concerning directory management, \texttt{rootdir}, sets the current working directory to the root directory of the current project for all \texttt{git} commands that are executed, similar to what \cmd{\gitdirectory} does. If you're using recording of files, this option comes in handy when the main file is in a subdirectory of the project. Otherwise, if the root directory isn't set appropriately, you'll receive the warning `\texttt{Warning: couldn't read HEAD from git project directory}'. \subsection{Basic macros}\label{sub:tex-basic} By default, the main file's directory is used as git project directory. This directory can be manipulated with \DescribeMacro{\gitdirectory}\cmd{\gitdirectory}\marg{path}. The foremost difference between using the \texttt{rootdir} option and the \cmd{gitdirectory} macro, is that the macro can specify a git directory which is part of another project. The main reason for this macro to exist is its usage in the project example in section~\ref{sec:project}. \DescribeMacro{\gitunsetdirectory} To undo an operation done with \cmd{\gitdirectory} and switch back to the main file's directory, use \cmd{\gitunsetdirectory}.\\ \DescribeMacro{\gitversion} The current version can be display by using \cmd{\gitversion} and is equivalent to \texttt{git describe --tags --always}, working for both lightweight and annotated tags. For this project \cmd{\gitversion} results in \gitversion. When the version is dirty it will be post fixed with \texttt{--}. For example, when this paragraph was written, the version was displaying 0.0.1-14-gcc2bc30.\\ \DescribeMacro{\ifgitdirty} To test whether there are any pending changes in the local Git repository, use the \cmd{\ifgitdirty}\marg{true code}\marg{false code} macro. For this macro to work properly, be sure to add a \texttt{.gitignore}\footnote{A proper example of a \texttt{.gitignore} file for \LaTeX: \url{https://raw.githubusercontent.com/github/gitignore/main/TeX.gitignore}} file and include all \TeX\ based generated files.\\ The \DescribeMacro{\gitdate}\cmd{\gitdate} macro gets the most recent date from the git log. Meaning, the last `short date' variant is picked from the last commit. This short date is formatted ISO based and is already suitable for use in packages like \texttt{isodate} for more advanced date formatting.\\ The author's name and email can be accessed using \cmd{\gitauthor} and \cmd{\gitemail}\DescribeMacro{\gitauthor}\DescribeMacro{\gitemail}. These values are based on \texttt{git config user.name} and \texttt{git config user.email}. \subsection{Multiple Authors} When projects having multiple authors, this package can help with the \DescribeMacro{\dogitauthors}\cmd{\dogitauthors}\oarg{conj} and \DescribeMacro{\forgitauthors}\cmd{\forgitauthors}\oarg{conj}\marg{csname} macro. Where \cmd{\dogitauthors} executes a default formatting implementation of \cmd{\git@format@author} and \cmd{\forgitauthors} executes the given \meta{csname} for every author available. The optional \meta{conj} conjunction makes it possible to even integrate it further. For example, when setting the authors in pdfx, the conjunction would be \texttt{[\textbackslash\textbackslash sep ]}, so that the authors are properly separated in the document properties\footnote{See package documentation of \texttt{pdfx}: \url{https://ctan.org/pkg/pdfx}}. \gitdirectory{../../git-test-project}% \setlength\xample{4.6cm-5pt}% \setlength\xamplesep{5pt}% \noindent% \begin{minipage}[t]{\linewidth-\xample-\xamplesep}% \begin{lstlisting}[language={[LaTeX]TeX},numbers=left,caption={Formatting authors},captionpos=t,morekeywords={myauthorformat,href,forgitauthors,dogitauthors}] \newcommand{\myauthorformat}[2]{#1 \href{mailto:#2}{#2}} \forgitauthors[\\]{myauthorformat} % Or using standard format \dogitauthors[\\] \end{lstlisting} \end{minipage}\hfill% \begin{minipage}[t]{\xample} \noindent \begin{center} Results in \end{center} \noindent \setlength{\fboxsep}{5pt}% \fbox{\parbox{\linewidth-2\fboxsep}{\dogitauthors[\\\\]}} \end{minipage}\\ This example is generated with the history of the \texttt{git-test-project} (see section~\ref{sec:project}) and is alphabetically sorted with package option \texttt{alpha}. \gitunsetdirectory \subsection{Commits} For this section the git project of this document is used due to the fact that there are references to revisions. The test project's revisions change for every user, since they get recreated every time \texttt{test-scenario.sh} is executed (see section~\ref{sec:project}).\\ \noindent \DescribeMacro{\gitcommit}\oarg{format}\marg{csname}\marg{revision}\\ For displaying commit data \cmd{\gitcommit} can be used. The optional \texttt{format} takes variables separated by a comma. The default \texttt{format} is \texttt{h,an,ae,as,s,b}. The \texttt{csname} is a user defined command accepting every variable as argument.\\ \setlength\xample{3.5cm}% \setlength\xamplesep{0pt}% \noindent% \begin{minipage}[t]{\linewidth-\xample-\xamplesep} \begin{lstlisting}[language={[LaTeX]TeX},numbers=left,caption={Formatting a commit},captionpos=t,morekeywords={formatcommit,printdate,gitcommit}] \newcommand{\formatcommit}[3]{#1, by #2 on \printdate{#3}} \gitcommit[s,an,as]{formatcommit}{75dc036} \end{lstlisting} \end{minipage}\hfill% \begin{minipage}[t]{\xample} \noindent \begin{center} Results in \end{center} \noindent \setlength{\fboxsep}{5pt}% \newcommand\formatcommit[3]{#1, by #2 on \printdate{#3}}% \fbox{\parbox{\linewidth-2\fboxsep}{\gitcommit[s,an,as]{formatcommit}{75dc036}\footnotemark}} \end{minipage}\\ \footnotetext{\cs{printdate} from \texttt{isodate}: \url{https://www.ctan.org/pkg/isodate}} Consult \texttt{man git-log} for possible format variables and omit the \% for every variable.\\ \noindent \DescribeMacro{\forgitcommit}\oarg{format}\marg{csname}\marg{rev\_spec, files=\{...\}, flags=\{...\}, cwd=...}\\ For displaying multiple commits the~\cmd{\forgitcommit} is used, which has the same arguments as \cmd{\gitcommit}, but only this time the \texttt{csname} is executed for every commit. The last argument, which originally only took a \texttt{rev\_spec}, now also supports some additional `named' arguments. The argument \meta{files} takes a list of file names relative from the root of the git project. When \meta{files} is given, all commits will be filtered out accordingly. Currently, for \meta{flags}, only \texttt{merges} and \texttt{no-merges} are supported, which includes or excludes merge commits. The \meta{cwd} option is like \cmd{\gitdirectory}, but only for this call. \noindent\setlength\xample{4.5cm}\setlength\xamplesep{0pt} \begin{minipage}[t]{\linewidth-\xample-\xamplesep} \noindent \begin{lstlisting}[language={[LaTeX]TeX},numbers=left,captionpos=t,caption={Formatting commits},morekeywords={formatcommits,forgitcommit}] \newcommand{\formatcommits}[2]{\item #1\\\quad —#2} \begin{itemize} \forgitcommit[s,an]{formatcommits}{75dc036...e51c481} \end{itemize} \end{lstlisting} \end{minipage}\hfill% \begin{minipage}[t]{\xample} \noindent \begin{center} Results in \end{center} \noindent \newcommand\formatcommits[2]{\item #1\\\quad —#2}% \setlength{\fboxsep}{0pt}% \fbox{\parbox{\linewidth}{% \begin{itemize} \forgitcommit[s,an] {formatcommits} {75dc036...e51c481} \end{itemize} }} \end{minipage}\\ \gitdirectory{../../git-test-project} % \clearpage \subsection{Tags} In this section the \texttt{git-test-project} is used. The tags are mostly useful for generating changelogs. For formatting tags, there's a \DescribeMacro{\forgittag}\cmd{\forgittag}\oarg{format}\marg{csname}. Again, like \cmd{\forgitcommit} it takes a format, however, this time more complex, since the formatting options differ between \texttt{git log} and \texttt{git for-each-ref}. For more info regarding these formatting options consult the man page of \texttt{git-for-each-ref}. \noindent\setlength\xample{3.5cm}\setlength\xamplesep{0pt} \begin{minipage}[t]{\linewidth-\xample-\xamplesep} \noindent \begin{lstlisting}[language={[LaTeX]TeX},numbers=left,captionpos=t,caption={Formatting tags},morekeywords={formattags,forgittag}] \newcommand{\formattags}[2]{\item Version #1\\type: #2} \begin{itemize} \forgittag[refname:short,objecttype]{formattags} \end{itemize} \end{lstlisting} \end{minipage}\hfill% \begin{minipage}[t]{\xample} \noindent \begin{center} Results in \end{center} \noindent \newcommand\formattags[2]{\item Version #1\\type: #2}% \setlength{\fboxsep}{0pt}% \fbox{\parbox{\linewidth}{% \begin{itemize} \forgittag[refname:short,objecttype] {formattags} \end{itemize} }} \end{minipage}\\[1em] This example shows that the versions used are mixed. This is, of course, a horrible way to manage a project's version, though, we'll continue on with this hard objective. For example, if we wish to display the author of the lightweight and annotated tag, we can do so by specifying a format using the if-then-else feature of the format specification. The format would be: \texttt{(taggername)(taggername)(authorname)}. Here the \texttt{taggername} will show up, or if not present, the \texttt{authorname} will be shown instead. The default format specification is like the \cmd{\forgitcommit} format, but then again, some bit more complex:\\ \hfill\parbox{\linewidth-\parindent}{\texttt{refname:short,(taggername)(taggername,taggeremail,taggerdate:short)\\(authorname,authoremail,authordate:short),subject,body}}\\ \noindent This is a robust example of getting all information, being it either a lightweight- or annotated tag.\\ For displaying commits in between tags, there's a \DescribeMacro{\forgittagseq}\cmd{\forgittagseq}\marg{csname}. The \meta{csname} takes exactly three arguments, namely, the \meta{current}, \meta{next tag} and \meta{rev spec}. The last iteration gives an empty value for \meta{next tag} and the \meta{rev spec} is identical to \meta{current}.\\ Afterward tag info can be fetched using the \DescribeMacro{\gittag} \cmd{\gittag}\oarg{format}\marg{csname}\marg{tag}. This macro takes the same formatting specification as \cmd{\fotgittag}. Beware of using \cmd{\gittag} for the \meta{next tag} parameter in \cmd{\forgittagseq}. All these macros put together are demonstrated in listing~\ref{lst:changelog} (see next page). % \clearpage \subsection{Changelog} This example demonstrates the generation of a changelog. For simplicity’s sake, every tag is displayed in a \texttt{description} environment's item and within an \texttt{enumerate} environment displaying commits in between. \begin{lstlisting}[language={[LaTeX]TeX},numbers=left,captionpos=t,caption={Formatting a changelog},label={lst:changelog},morekeywords={commitline,formatversion,gittag,forgitcommit,forgittagseq,printdate}] \section*{Change History} \newcommand{\commitline}[1]{\item #1} \newcommand{\formatversion}[3]{% \item[#1] \gittag[(taggerdate)(taggerdate:short)(authordate:short)]{printdate}{#1} \begin{itemize} \forgitcommit[s]{commitline}{#3} \end{itemize} }% \begin{description} \forgittagseq{formatversion} \end{description} \end{lstlisting} \noindent \newcommand\commitline[2]{\item #1 #2} \newcommand\formatversion[3]{% \item[#1] \gittag[(taggerdate)(taggerdate:short)(authordate:short)]{printdate}{#1} \begin{itemize} \forgitcommit[s,b]{commitline}{#3} \end{itemize}% }% \setlength{\fboxsep}{16pt}% \begin{center}Results in\end{center} \noindent \fbox{ \parbox{\linewidth-8pt-2\fboxsep}{ {\bfseries\Large Change History} \begin{description} \forgittagseq{formatversion}% \end{description} } }\\[1em] \noindent For displaying the tagline (see line 5) we use the existing \cmd{\printdate} macro of package \texttt{isodate}, which also takes exactly one argument For every version sequence the commits in between are displayed (see line 7), where the last sequence having the initial commit as second argument plays well with the \cmd{\forgitcommit} macro and makes it possible to show the whole sequence of history. \section{Project Example}\label{sec:project} This documentation uses an example \texttt{project} which gets created by the \texttt{git-scenario.sh} script (see listing~\ref{lst:scenario}). It creates some commits having dates in the past and different authors set. Lastly it creates a `lightweight-' and `annotated' tag. To set up this scenario either do \texttt{make scenario} or execute \texttt{bash git-scenario.sh} in an initialized \texttt{git} repository. Keep in mind that when executing with Bash directly, you may need to specify the path to the Bash file. \lstinputlisting[language=bash,numbers=left,frame=single,label={lst:scenario},caption={git-scenario.sh},captionpos=t,morekeywords={git,alice,bob,charlie,mkdir,rm,curl,set\_author}]{git-scenario.sh} \end{document}