Skip to content

Instantly share code, notes, and snippets.

@mstankie
Last active October 3, 2017 10:56
Show Gist options
  • Save mstankie/188fe7051d0c02932b65558cc14d8cfe to your computer and use it in GitHub Desktop.
Save mstankie/188fe7051d0c02932b65558cc14d8cfe to your computer and use it in GitHub Desktop.
Obtaining `git describe` directly from LaTeX document without any intermediate files.
#!/bin/bash
# obtaining the name of either the remote origin repo or the local directory if no origin defined
DIRNAME=${PWD##*/}
JOBNAME=$(git config --get remote.origin.url | xargs basename || echo $DIRNAME".loc")
# compiling
latexmk -pdf -pdflatex="pdflatex --enable-pipes --shell-escape --enable-write18 %O %S" -jobname=$JOBNAME -output-directory="./build/" document.tex
latexmk -c -jobname=$JOBNAME -output-directory="./build/"
% The git command is interpreted as LaTeX source
% The command formats output to be later converted by the csvsimple package
% as CSV file contents
\usepackage{newverbs}[2011/07/24] %(for Verbdef)
\Verbdef\perc{%}
\newcommand{\revhist}{
% \usepackage{newverbs}[2011/07/24] %(for Verbdef)
%\Verbdef\perc{%} % percent character must be used as-is to the shell command
% Verbdef must be declared outside the function!
% other characters may be used aspercent-encoding characters (like in URLs):
% %26 - ampersand sign, %5C - backslash
\begin{tabular}{|r|c|c|l|}
\hline
ver. & date & author & description \\
\hline
\input|"git for-each-ref --sort=taggerdate --format=\perc(refname:short)\perc26\perc(taggerdate:short)\perc26\perc(taggername)\perc26\perc(subject)\perc5C\perc5C refs/tags/"
\hline
\end{tabular}
}
\section*{Document revision history}
\revhist
\newcommand{\version}{\input|"git describe --always --dirty='*'"}
% Obtaining `git describe` directly from LaTeX document without any intermediate files.
\newcommand{\gitdescribe}{
% If there is any tag defined -print the result of git describe
% if there are no tags -print the shoart hash of current commit
% if some of the changes are not commited -print appropriate message
% Sell pipeline access is required, so depending on the LaTeX distro
% one has to compile with special command arguments:
% MiKTeX (@win): --enable-pipes
% other may require (and/or?): --enable-pipes --shell-escape --enable-write18
\IfFileExists{|"git status || echo got"} % check if there is access to any shell cmd (or \usepackage{pdftexcmds})
{
\jobname{} ver. \version
}
{
\errmessage{No access to shell to run Git commands!
Compile with --enable-pipes --shell-escape --enable-write18
(pdf)latex options.}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment