Peculiar interaction between System.cmd/2 and "git log"

I just ran into some peculiar behavior when using System.cmd/2 to invoke git log. Can someone tell me what is going on?

When I run git log from the command line, the string “format:” does not appear in the result:

$ git log -1 --pretty="format:%ci" mix.exs
2020-02-03 10:37:31 -0800

$ git log -1 --pretty="%ci" mix.exs
2020-02-03 10:37:31 -0800

However, when I run the command via System.cmd/2, the string “format:” does appear:

$ iex
...
iex(1)> arg_list = ~w(log -1 --pretty="%ci" mix.exs)
["log", "-1", "--pretty=\"%ci\"", "mix.exs"]

iex(2)> System.cmd("git", arg_list)
{"\"2020-02-03 10:37:31 -0800\"\n", 0}

iex(3)> arg_list = ~w(log -1 --pretty="format:%ci" mix.exs)
["log", "-1", "--pretty=\"format:%ci\"", "mix.exs"]

iex(4)> System.cmd("git", arg_list)                        
{"\"format:2020-02-03 10:37:31 -0800\"\n", 0}

Wazzup?

-r

Assuming you are using a POSIX compliant shell, your second System.cmd/2 call is equivalent to the following on your shell:

git log -1 --pretty='"format:%ci"' mix.exs

You need to omit the doublequotes in elixir (you do not even need them on your shell).

4 Likes