29

Is there a way to insert the last git commit date into a document without having to add git hooks (like the gitinfo package does it)?

I'm not 100% sure how LaTeX packages work, but if they can access the current directory during compile-time and if they can execute external commands, that should easily be possible...

0

5 Answers 5

26

enter image description here

This is svn but any command line is the same.

Depending on your configuration you probably need

pdflatex --enable-write18 filename

to enable \write18 to be a hook to execute shell commands.

this writes to a temporary file using the shell escape, then inputs it

\documentclass{article}

\begin{document}

\immediate\write18{svn info \jobname.tex | grep "Changed Date" > \jobname.info }
\fbox{\input{\jobname.info}}

\end{document}
3
  • Thank you! This works (although the configuration might be a little annoying depending on the software used) :) Oct 4, 2012 at 23:18
  • 1
    Corresponding git write18 command could be: \write18{git log -1 --format="\@percentchar ad" --date=short > \jobname.info} Oct 7, 2012 at 21:35
  • 1
    \immediate\write18{git log -1 --pretty=format:"%h%x3b%ad" --date=short > \jobname.info} this is the line I'm using for git at the moment.
    – Dror
    May 23, 2013 at 20:35
14

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.

3
  • 1
    I would say you would be better of using the gitinfo2 package. Even when you don‘t want to use git hooks you still can call the post-xxx-sample.txt script from your makefile.
    – ypid
    Feb 6, 2015 at 15:47
  • it looks like the make file needs more escaping of \\
    – Gus
    Feb 13, 2016 at 16:25
  • 1
    A better command for the revision might git describe --always --tags.
    – qznc
    Jul 4, 2016 at 11:35
6

Here is an alternative solution that runs with gitinfo2. Hence I really like gitinfo2; the hooks mechanism proposed never satisfied me.

I ended up doing Makefile and shell scripts, but they weren't properly handled by my LaTeX editor and were not working on all platforms.

Finally, I ended up writing a kind of plugin for latexmk.

This plugin is named gitinfo2-latexmk and is available on GitHub

Disclaimer: I'm the author of that plugin.

The setup is pretty easy :

  • download the plugin gitinfo2.pm (Perl module)
  • add one line in your .latexmkrc file

Once these two small steps are done, no more boilerplate is needed. Each time you, or your LaTeX editor, run latexmk, the git information is ready for the package gitinfo2.

1

here is my solution: use \write18 to write a temp.dat file which contains 2 commands:

\gdef\GITHash{...}
\gedf\GITDate{...}

then input that file, the \GITxxx will get defined. We can use it anywhere, e.g. running title.

\makeatletter
\immediate\write18{git log -1 --pretty=format:"\@backslashchar gdef\@backslashchar GITHash{\@percentchar h}\@percentchar n \@backslashchar gdef\@backslashchar GITDate{\@percentchar ad}" --date=short > /tmp/temp.dat }
\makeatother
\input{/tmp/temp.dat}
\begin{document}
0

I used Danilo's approach, but I'm using a makefile on windows, so it took some tweaks to get the revision file to be created.

I'm sharing it for anyone's benefit and to improve upon:

revision.tex:
    powershell "echo '\newcommand{\revisiondate}{' | Out-File revision.tex -NoNewLine -Encoding UTF8"
    powershell 'git log -1 --format="%ad" --date=short | Out-File revision.tex -Append -NoNewLine -Encoding UTF8'
    powershell "echo '}' | Out-File source\_revision.md -Append -Encoding UTF8"
    powershell "echo '\newcommand{\revision}{' | Out-File revision.tex -Append -NoNewLine -Encoding UTF8"
    powershell "git describe --always --tags | Out-File revision.tex -Append -NoNewLine -Encoding UTF8"
    powershell "echo '}' | Out-File revision.tex -Append -Encoding UTF8"

You must log in to answer this question.

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