%%
%% This is file `latexgit.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% latexgit.dtx  (with options: `package')
%% latexgit
%% Author: Camil Staps <info@camilstaps.nl>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{latexgit}[2016/08/24]

\RequirePackage{pgfkeys}
\RequirePackage{datetime}
\newcommand{\latexgit}[0]{\LaTeX{}git}
\newcommand{\git@result}[0]{\git@rawresult\unskip}
\newif\ifgit@opt@FormatDate
\newif\ifgit@opt@FormatTime
\newif\ifgit@opt@ShortHash
\newif\ifgit@opt@ShowTimeZone
\pgfkeys{
  /git/.is family, /git,
  default/.style={
    directory=.,
    formatDate=false,
    formatInterDateTime=\space{}at\space{},
    formatTime=false,
    revision=HEAD,
    showTimeZone=false,
    shortHash=true,
  },
  directory/.estore in=\git@opt@Directory,
  formatDate/.is if=git@opt@FormatDate,
  formatInterDateTime/.estore in=\git@opt@FormatInterDateTime,
  formatTime/.is if=git@opt@FormatTime,
  revision/.estore in=\git@opt@Revision,
  showTimeZone/.is if=git@opt@ShowTimeZone,
  shortHash/.is if=git@opt@ShortHash,
}
\newread\git@stream%
\newcommand{\git@command}[1]{%
  \def\git@rawresult{}%
  \openin\git@stream|"cd \git@opt@Directory; #1"
  \ifeof\git@stream%
    \PackageError{latexgit}%
      {invoke LaTeX with the -shell-escape flag}%
      {invoke LaTeX with the -shell-escape flag}%
  \else%
    \begingroup%
    \catcode`\^^M9%
    \endlinechar=-1%
    \readline\git@stream to \git@streamoutput%
    \global\let\git@rawresult\git@streamoutput%
    \endgroup%
  \fi%
}
\newcommand{\gitcommithash}[1][]{%
  \git@commithash[#1]\git@result}
\newcommand{\git@commithash}[1][]{%
  \pgfkeys{/git,default,#1}%
  \ifgit@opt@ShortHash%
    \git@command{git log -n 1 --oneline \git@opt@Revision
      | cut -d' ' -f1}%
  \else%
    \git@command{git log -n 1 \git@opt@Revision
      | head -1 | cut -d' ' -f2}%
  \fi%
}
\newcommand{\gitcommitmsg}[1][]{%
  \git@commitmsg[#1]\git@result}
\newcommand{\git@commitmsg}[1][]{%
  \pgfkeys{/git,default,#1}%
  \git@command{git log -n 1 --oneline \git@opt@Revision
    | cut -d' ' -f2-}%
}
\def\git@formatCommitDate#1-#2-#3 #4:#5:#6 +#7\relax{%
  \formatdate{#3}{#2}{#1}%
}
\def\git@formatCommitTime#1-#2-#3 #4:#5:#6 +#7\relax{%
  \formattime{#4}{#5}{#6}%
  \ifgit@opt@ShowTimeZone%
    \space(+#7\unskip)%
  \fi%
}
\newcommand{\gitcommitdate}[1][]{%
  \git@commitdate[#1]%
  \ifgit@opt@FormatDate%
    \expandafter\git@formatCommitDate\git@rawresult\relax%
    \ifgit@opt@FormatTime%
      \git@opt@FormatInterDateTime%
      \expandafter\git@formatCommitTime\git@rawresult\relax%
    \fi
  \else\ifgit@opt@FormatTime%
    \expandafter\git@formatCommitTime\git@rawresult\relax%
  \else
    \git@result%
  \fi\fi%
}
\newcommand{\git@commitdate}[1][]{%
  \pgfkeys{/git,default,#1}%
  \git@command{git log -n 1 --date=iso \git@opt@Revision
    | grep Date | head -1 | cut -d' ' -f2-}%
}
\newcommand{\gitcommitauthor}[1][]{%
  \git@commitauthor[#1]\git@result}
\newcommand{\git@commitauthor}[1][]{%
  \pgfkeys{/git,default,#1}%
  \git@command{git log -n 1 \git@opt@Revision
    | grep Author | head -1 | cut -d' ' -f2-}%
}
\newcommand{\gitcommitauthorname}[1][]{%
  \git@commitauthorname[#1]\git@result}
\newcommand{\git@commitauthorname}[1][]{%
  \pgfkeys{/git,default,#1}%
  \git@command{git log -n 1 \git@opt@Revision
    | grep Author | head -1 | cut -d' ' -f2- | cut -d'<' -f1}%
}
\newcommand{\gitcommitauthoremail}[1][]{%
  \git@commitauthoremail[#1]\git@result}
\newcommand{\git@commitauthoremail}[1][]{%
  \pgfkeys{/git,default,#1}%
  \git@command{git log -n 1 \git@opt@Revision
    | grep Author | head -1 | cut -d' ' -f2-
    | cut -d'<' -f2 | cut -d'>' -f1}%
}
\newcommand{\git@commitparent}[1][]{%
  \pgfkeys{/git,default,#1}%
  \git@command{git log -n 1 --pretty=raw \git@opt@Revision
    | grep parent | head -1 | cut -d' ' -f2}%
}
\newcommand{\gitchanges}[1][]{%
  \git@changes[#1]{HEAD}
}
\newcommand{\git@changes}[2][]{%
  \edef\git@@revision{#2}%
  \git@commithash[revision=\git@@revision]%
  \edef\git@@thishash{\git@rawresult}%
  \git@command{git log -n 1 --date=iso \git@@revision
    | grep Date | head -1 | cut -d' ' -f4}%
  \edef\git@@thisdate{\git@rawresult}%
  \git@commitmsg[revision=\git@@revision]%
  \edef\git@@thismsg{\git@rawresult}%
  \changes{\git@@thisdate\unskip: \git@@thishash}\git@@thisdate\git@@thismsg%
  \git@commitparent[revision=\git@@revision]%
  \let\git@@parent\git@rawresult%
  \setbox0=\hbox{\git@@parent\unskip}\ifdim\wd0=0pt
  \else%
    \git@changes{\git@@parent}%
  \fi%
}
%% Copyright (c) 2016 Camil Staps <info@camilstaps.nl>
%% Licensed under GPL v3.
%%
%% End of file `latexgit.sty'.
