I ended up using a custom Makefile to insert the revision info.
When creating a Makefile instead of using the --enable-write18
compiler flag, you require people to use a specific compilation method. That sucks. But the same thing counts for write18, as people have to configure their compiler to allow possibly insecure access to their system. By using a Makefile, the security risk of write18 is reduced, while providing an almost cross-platform method of compiling your document (Make
is available when using cygwin on Windows; cygwin usually comes with git anyways). To (intelligently) compile the document itself, you can use latexmk on Linux and OS X (comes with TeXLive) and texify on Windows (comes with MiKTeX).
This is the Makefile:
TARGET=main.pdf
LL=latexmk -pdf
CLEAN=latexmk -C
all: revision.tex $(TARGET)
pdf: revision.tex $(TARGET)
.PHONY : clean revision.tex $(TARGET)
revision.tex:
echo "% Autogenerated, do not edit" > revision.tex
echo "\\newcommand{\\revisiondate}{`git log -1 --format=\"%ad\" --date=short`}" >> revision.tex
echo "\\newcommand{\\revision}{`git log -1 --format=\"%h\"`}" >> revision.tex
$(TARGET): $(TARGET:%.pdf=%.tex) $(SRC)
$(LL) $<
clean:
$(CLEAN)
rm -f revision.tex
If people want to build the PDF directly from their favorite IDE, they can tell the IDE to trigger "make". This should work with most software. When you use vim for example, you can use a mapping like this:
map <leader>m :w<CR> :!make<CR><CR>
The Makefile creates a file called revision.tex
which looks like this:
% Autogenerated, do not edit
\newcommand{\revisiondate}{2012-10-17}
\newcommand{\revision}{e8e5238}
To insert it into the main document, simply use \input{revision}
and then insert \revisiondate
or \revision
at the desired place.