0

I am discussing code which is in version control and would like to specify which version of the code to include.

I did some research and found packages like gitlatex and gitinfo, but though useful, they only work for the document itself and of course listings itself.

I would prefer not to have copies of the file(s) in the document directory, but keep them in the git controlled directory.

927589452
  • 118
  • 1
    Welcome to TeX.SX! You could use the \write18 approach to execute a git command that stores the specific file version in a temp file, and include it normally via \lstinputlisting afterwards. – siracusa Jul 18 '19 at 15:21

1 Answers1

1

A file can be included by using PDFLaTeXs | / shellescape feature (thanks @siracusa for pointing me to \write18 ).

Reading command output is done by using {|"script"} for the file argument src

and these " can be replaced by using \string" to force compability with packages like ngermansrc.

A file having a specified commit can be checkedout (of a remote git repository) by using git archive --remote=ssh://git@server/repo.git VERSION path/to/file (here VERSION can be a TAG or a commithash or just master)src.

To clean the output from the ssh connection we pipe the stderr to the trash by appending a 2>/dev/null.

As git archive generates a tar archive, we have to remove the tar headers by using tar to read from stdin - and --extract it --to-stdout: | tar --extract --file - --to-stdout src.

TL:DR

for a remote file

\lstinputlisting{|\string"git archive --remote=ssh://git@server/repo.git VERSION path/to/file 2>/dev/null | tar --extract --file - --to-stdout\string"}

for a lokal file

\lstinputlisting{|\string"git archive --remote=/path/to/.git VERSION path/to/file 2>/dev/null | tar --extract --file - --to-stdout\string"}

Useful macro for this has been worked out here

927589452
  • 118