2

I am currently using the gitinfo2 package in my document to display the current git revision, branch, and date. However, I would only like to display the branch if I am not on the master branch. This will require some conditional wrapping around \gitBranch but I couldn't figure this out.

MWE:

\documentclass{scrbook}

\usepackage{gitinfo2}
% additional install is required. See package documentation for details.

\begin{document}
\begin{titlepage}
\centering
\ \\
\vspace{5cm}
{\Huge Document title}
\vspace{5cm}

Revision: \gitAbbrevHash on branch \gitBranch \ \gitCommitterDate

\end{titlepage}

Some text.

\end{document}

So far, I've tried this, with no success:

\documentclass{scrbook}

\usepackage{gitinfo2}
% additional install is required. See package documentation for details.
\newcommand{\masterBranch}{master}

\begin{document}
\begin{titlepage}
\centering
\ \\
\vspace{5cm}
{\Huge Document title}
\vspace{5cm}

Revision: \gitAbbrevHash

\if\gitBranch\masterBranch
\else
    on branch \gitBranch
\fi

\ \gitCommitterDate

\end{titlepage}

Some text.

\end{document}

1 Answer 1

2

There are two issues:

  • The test \if expands the next tokens until it finds two unexpandable tokens for comparison. Therefore, it cannot be used to compare macro definitions. The switch \ifx is better suited here, because it compares the next two tokens without expanding.

  • \gitBranch does not contain the name of the branch directly, instead is uses expandable (I hope always) macros to get the name. Also \detokenize is used to convert the characters to tokens with category code 12 (other), like digits and punctuation characters.

That means:

  • Use of \ifx instead of \if.
  • \edef\gitBranch{\gitBranch} to get a macro with the expanded branch name.
  • \edef\branchMaster{\detokenize{master}} to get the correct category codes.

Result:

\documentclass{scrbook}

\usepackage{gitinfo2}

\edef\masterBranch{\detokenize{master}}
\edef\gitBranch{\gitBranch}

\begin{document}
Revision: \gitAbbrevHash\ %
%
\ifx\gitBranch\masterBranch
\else
    on branch \gitBranch\ %
\fi
%
\gitCommitterDate
\end{document}

Result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .