On Linux/Debian/Sid, inside any git repository, the following pipeline gives some git commit id:

git log --format=oneline -1 --abbrev=16 --abbrev-commit -q | cut -d' ' -f1

for example, in one directory, it outputs 03aa650c1bbd20c3. I want to show that string in my LuaLaTeX document.

However, with the following lx.tex file:

% file lx.tex
  pip=io.popen("git log --format=oneline -1 --abbrev=16 --abbrev-commit -q | cut -d' ' -f1")

The gitid is \mygitid

that I process with lualatex --shell-escape --file-line-error --halt-on-error lx I am getting an error

% lualatex --shell-escape --file-line-error --halt-on-error lx
This is LuaTeX, Version 1.07.0 (TeX Live 2019/dev/Debian) 
 system commands enabled.
LaTeX2e <2018-04-01> patch level 5

luaotfload | main : initialization completed in 0.594 seconds (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(./lx.aux)[\directlua]:2: bad argument #1 to 'read' (invalid option)
stack traceback:
    [C]: in function 'read'
    [\directlua]:2: in main chunk.
\luacode@dbg@exec ...code@maybe@printdbg {#1} #1 }

l.8 \end{luacode*}

 350 words of node memory still in use:
   1 hlist, 1 rule, 1 dir, 3 kern, 1 glyph, 3 attribute, 45 glue_spec, 3 attrib
ute_list, 1 write nodes
   avail lists: 2:8,3:1,4:1,5:4,7:2,8:1,9:3
./lx.tex:8:  ==> Fatal error occurred, no output PDF file produced!
Transcript written on lx.log.

But if I run lua5.3 in a terminal with the following code

 % lua5.3            
Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
>   pip=io.popen("git log --format=oneline -1 --abbrev=16 --abbrev-commit -q | cut -d' ' -f1")
>   gitid=pip:read("l")
>   pip:close()
true    exit    0
> gitid
> type(gitid)

it works as I expect.

What am I doing wrong?


LuaTeX version 1.07.0 uses Lua 5.2 by default and in Lua 5.2 you have to use read'*l' instead of read'l'. (For compatibility, read'*l' still works with Lua 5.3)

So you can either add the * to the read argument or switch to luatex53 (LuaTex with Lua 5.3) if it is installed.

For :read, the argument l or *l is the default, so another option is to not pass any argument: (Additionally I added local to avoid the global variable pip and --no-color because e.g. on my system git log outputs color escape sequences otherwise)

% file lx.tex
  local pip=io.popen("git log --no-color --format=oneline -1 --abbrev=16 --abbrev-commit -q | cut -d' ' -f1")

The gitid is \mygitid
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.