diff options
author | Karl Berry <karl@freefriends.org> | 2019-02-08 23:00:40 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2019-02-08 23:00:40 +0000 |
commit | f22da8de751b570b9a23401289409b32368e867e (patch) | |
tree | 13d7223d8ac412aa4a9a30b83e324ac151825e91 /Master/texmf-dist/tex/latex/gitver | |
parent | 7604d048a7cb34edaea8ddd3a8600d9f11fbda95 (diff) |
gitver (7feb19)
git-svn-id: svn://tug.org/texlive/trunk@49980 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/latex/gitver')
-rw-r--r-- | Master/texmf-dist/tex/latex/gitver/gitver.sty | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/latex/gitver/gitver.sty b/Master/texmf-dist/tex/latex/gitver/gitver.sty new file mode 100644 index 00000000000..38165fe8098 --- /dev/null +++ b/Master/texmf-dist/tex/latex/gitver/gitver.sty @@ -0,0 +1,143 @@ +% gitVer +% +% (c) Charles Baynham 2018 +% +% License: LaTeX Project Public License 1.3c +% +% This package will get a description of the current git version of this +% document and store it in a command "\gitVer". If memoir is installed, it +% will also add this to the document headers unless the option "noheader" is +% passed. +% +% It also defines a command "\versionBox{}" which outputs a box containing the +% version and date of compilation +% +% For this to work, you must have git installed and available on the command +% line, and latex must be running in "shell escape" mode. +% + +\NeedsTeXFormat{LaTeX2e} +\def\@gitVerPkgName{gitver} +\ProvidesPackage{\@gitVerPkgName} + [2019/02/06 v1.0 Access current git version and optionally add it to document headers] + +\RequirePackage{hyperref} +\RequirePackage{catchfile} +\RequirePackage{pdftexcmds} +\RequirePackage{datetime} + +%%% Package options %%% + +%% 'noheader' option +\newif\if@gitver@noheader +\@gitver@noheaderfalse +\DeclareOption{noheader}{ + \@gitver@noheadertrue + \PackageInfo{\@gitVerPkgName}{Header info disabled}% +} +% +%% 'nopdfinfo' option +\newif\if@gitver@pdfsubect +\@gitver@pdfsubecttrue +\DeclareOption{nopdfinfo}{ + \@gitver@pdfsubectfalse + \PackageInfo{\@gitVerPkgName}{Metadata info disabled}% +} +% +\ProcessOptions\relax + +%%% End package options %%% + +% Define some ifs +\newif\if@gitver@ready +\newif\if@gitver@success + +% https://tex.stackexchange.com/questions/88614/how-do-you-detect-restricted-write18-support + +\ifcase\pdf@shellescape + \@gitver@readyfalse% + \or + \@gitver@readytrue% + \or + \@gitver@readyfalse% + \fi + +\if@gitver@ready + \PackageInfo{\@gitVerPkgName}{Shell escape enabled}% +% + % Get current git version + % Note: this command requires (a) git installed and (b) shell escape in latex enabled + \immediate\write18{ git describe --match nopenopenope --always --long --dirty --abbrev=6 > \jobname_desc.aux } +% + % Extract this into a new variable, gitVer + \@gitver@successfalse + \IfFileExists{\jobname_desc.aux}{ + % Read the file into \gitVer + \CatchFileDef{\gitVer}{\jobname_desc.aux}{} + + % Check if the macro \gitVer is "\par ": this is its definition if the file had no contents + \def\@gitver@emptyfile{\par } + + \ifx\gitVer\@gitver@emptyfile% + \PackageError{\@gitVerPkgName}{Git output is empty: is this folder a git repo?}{Make sure that this directory has had "git init" called in it, and has at least one commit.} + \else + \@gitver@successtrue + \fi + } + + \if@gitver@success + \else + \PackageWarning{\@gitVerPkgName}{Git command failed: writing as "unknown"}% + \def\gitVer{unknown} + \fi +% + % Make a box containing the date and version + % \newcommand{\versionBox}{\fbox{Manuscript version: \textit{\#\gitVer} - \today{} \currenttime{} }} + \newcommand{\versionBox}{\fbox{Manuscript version: \textit{\#\gitVer} - \today{} \currenttime{} }} +% + % If we're using memoir, put the tag into into the header +% + \@ifclassloaded{memoir}% + {% + \PackageInfo{\@gitVerPkgName}{Memoir detected}% + \if@gitver@noheader% + \else% + % Add the git version and the date/time to the header / footer + \PackageInfo{\@gitVerPkgName}{Adding git info to header}% + \makeoddfoot{Ruled}{\versionBox}{}{\thepage} + \makeevenfoot{Ruled}{\thepage}{}{\versionBox} + \makeevenhead {Ruled}{\scshape\leftmark}{}{\textit{\#\gitVer}} + \makeoddhead {Ruled}{\textit{\#\gitVer}}{}{\rightmark} + \fi% + }{% + % If we're not using memoir, but we are using fancyhdr, also add to the header + \@ifpackageloaded{fancyhdr} + {% + \if@gitver@noheader% + \else% + \fancyfoot[L]{\versionBox} + \fancyfoot[C]{} + \fancyfoot[R]{\thepage} + \fi + }{% + % No memoir or fancyhdr + \if@gitver@noheader% + \else% + \PackageWarning{\@gitVerPkgName}{Not changing the document headers because you're not using "memoir" or "fancyhdr" package. Pass "noheader" to suppress this warning, or use one of these classes / packages.} + \fi + } + }% + + \if@gitver@pdfsubect% + \PackageInfo{\@gitVerPkgName}{Setting pdf subject metadata}% + % Put it into the metadata too + \hypersetup{ + pdfsubject={Version \#\gitVer{}} + }% + \fi% +\else + \PackageError{\@gitVerPkgName}{Shell escape not enabled}{Latex failed to write a file. This is probably because shell-escape mode is disabled: you must turn this on!} + + \def\versionBox{???} + \def\gitVer{unknown} +\fi |