# How can I produce the history graph of a Git repository in LaTeX?

Is there a relatively simple way to produce the history graph of a git repository in LaTeX? I'm looking for something nicer-looking than the verbatim output of

git log --graph --oneline

-
Not a graphical solution but is something like latex-git-log sufficient? –  Alexander Jul 24 '13 at 10:49
It I am not mistaken, latex-git-log just tabulates the history. Not exactly what I am looking for. –  xvtk Jul 24 '13 at 18:50

I have begun an attempt at an answer here. It would involve piping the output of $git log --graph --oneline to a text file and having LaTeX parse that for information. We'd need to determine the nest level of the asterisk (branch level), which is what I'm not sure how to accomplish. Here is a MWE using a section of the tree from one of my projects: \documentclass{standalone} \usepackage{tikz} \usepackage{filecontents} % gitgraph.txt contains raw output of:$ git log --graph --oneline
\begin{filecontents}{gitgraph.txt}
* d764b48 added plaintext version in markdown
* 54ba4b2 release 2014-01-25
*   c589395 Merge branch 'master'
|\
| * 9f9c652 Remove holdover from kjh gh-pages branch
* | b3bd158 exclude font files
|/
* 63268c1 micro-typography
\end{filecontents}

\newcommand\commit[2]{\node[commit] (#1) {}; \node[clabel] at (#1) {\texttt{#1}: #2};}
\newcommand\ghost[1]{\coordinate (#1);}
\newcommand\connect[2]{\path (#1) to[out=90,in=-90] (#2);}

\begin{document}
\begin{tikzpicture}
\tikzstyle{commit}=[draw,circle,fill=white,inner sep=0pt,minimum size=5pt]
\tikzstyle{clabel}=[right,outer sep=1em]
\tikzstyle{every path}=[draw]
\matrix [column sep={1em,between origins},row sep=\lineskip]
{
\commit{d764b48}{added plaintext version in markdown} & \\
\commit{54ba4b2}{release 2014-01-25} & \\
\commit{c589395}{Merge branch master'} & \\
& \commit{9f9c652}{Remove holdover from kjh gh-pages branch} \\
\commit{b3bd158}{exclude font files} & \ghost{branch1} \\
\commit{63268c1}{micro-typography} & \\
};
\connect{63268c1}{b3bd158};
\connect{63268c1}{branch1};
\connect{branch1}{9f9c652};
\connect{b3bd158}{c589395};
\connect{9f9c652}{c589395};
\connect{c589395}{54ba4b2};
\connect{54ba4b2}{d764b48};
\end{tikzpicture}
\end{document}


Which produces the output:

This could be easily stylized in whatever way you wish. Of course the main issue is going to be finding a solution to parse the commit tree. Maybe pgfplotstable could be used?

-
This looks very nice! As you pointed out, the trick is going to be parsing the git output. –  xvtk Jan 30 '14 at 12:23
Man, this looks sweet. It just lacks automation. Have you considered writing a package to generate the tikz code based on the git log? I sure would be interested in such a package, and I'm sure others would too. –  Jubobs Jan 30 '14 at 15:45
@Jubobs, I've looked into this and I think this is the best route. One could write a hook to write this tree (\$ git log --graph --oneline --all) or a pre-parsed version of it into a file to be read/processed by TeX/TikZ. This is the approach used by the gitinfo package. –  Paul Gessler Jan 30 '14 at 15:58
For a linear history (i.e., without branches and merges), git log --format="\commit{%h}{%s} & \\" can be used at least to write the matrix part of the code. More info in the git-log manual. –  giordano Aug 31 '14 at 13:33

As I'm working on a Git tutorial that includes several history graphs, I have, for convenience, written a little package (built on top of TikZ v3.0) called gitdags.

Automatic generation of history graphs of (possibly large) repositories is not the purpose of gitdags. Rather, the package is meant to allow you to effortlessly draw nice graphs for demonstrating the effects of Git commands such as checkout, reset, merge, rebase, etc.

The package itself is little more than a bunch of TikZ styles and a couple of macros but, because some Stack-Overflow users have expressed an interest in it, I've made the code available on GitHub, and I have plans to submit the package to CTAN in the near future...

\documentclass{article}

\usepackage{subcaption}
\usepackage{gitdags}

\begin{document}

\begin{figure}
\begin{subfigure}[b]{\textwidth}
\centering
\begin{tikzpicture}
% Commit DAG
\gitDAG[grow right sep = 2em]{
A -- B -- {
C,
D -- E,
}
};
% Tag reference
\gittag
[v0p1]       % node name
{v0.1}       % node text
{above=of A} % node placement
{A}          % target
% Remote branch
\gitremotebranch
[origmaster]    % node name
{origin/master} % node text
{above=of C}    % node placement
{C}             % target
% Branch
\gitbranch
{master}     % node name and text
{above=of E} % node placement
{E}          % target
{above=of master} % node placement
{master}          % target
\end{tikzpicture}
\subcaption{Before\ldots}
\end{subfigure}

\begin{subfigure}[b]{\textwidth}
\centering
\begin{tikzpicture}
\gitDAG[grow right sep = 2em]{
A -- B -- {
C -- D' -- E',
{[nodes=unreachable] D -- E },
}
};
% Tag reference
\gittag
[v0p1]       % node name
{v0.1}       % node text
{above=of A} % node placement
{A}          % target
% Remote branch
\gitremotebranch
[origmaster]    % node name
{origin/master} % node text
{above=of C}    % node placement
{C}             % target
% Branch
\gitbranch
{master}      % node name and text
{above=of E'} % node placement
{E'}          % target
{above=of master} % node placement
{master}          % target
\SAandWT
\end{tikzpicture}
\subcaption{\ldots{} and after \texttt{git rebase origin/master}}
\end{subfigure}
\caption{Demonstrating a typical \texttt{rebase}}
\end{figure}

\end{document}

-
Very nice! You've also reminded me that I need to revisit my own answer... :p :-) –  Paul Gessler Aug 28 '14 at 14:56
@PaulGessler Yes. I've been thinking about it (automatic generation) too. I've got a few ideas. Perhaps you should start a project on GitHub on which we could both hack... –  Jubobs Aug 28 '14 at 14:57
I had been using Brent's code from gitinfo to create some hooks that would parse the tree output, but I've not had much time to look at it since he deprecated gitinfo and moved to gitinfo2`. All my stuff is in a very early stage, but I'll get it to a post-able state over the weekend and create a repo. I'll ping you in chat when it's live. :-) –  Paul Gessler Aug 28 '14 at 15:03
@PaulGessler Cool. I can't promise I'll be very active on it in the immediate future, as I've got a few things on my plate, but I'm definitely interested in taking part :) –  Jubobs Aug 28 '14 at 15:31
@Jubobs As you pointed out, GitDAGS does something other than what I am looking for. Nevertheless, this is really cool! –  xvtk Aug 29 '14 at 13:45