Trashman
Trashman

Reputation: 1596

Git command line and batch file have different output

I'm creating a simple, single line batch file to run a git log in my preferred format. When I'm in PowerShell, I type:

git log --pretty=format:"%ai%x09%H%x09%s" > commitlog.csv

And it gives me exactly the output I want. It's the commit author's date and time in ISO format, followed by a tab, followed by the full SHA Hash, followed by a tab, followed by the notes/description. It opens in Excel exactly the way I want.

I tried to make a simple batch file to do the exact same thing so I wouldn't have to remember the format string or search the command history (which occasionally gets cleared, anyway). I noted that I had to use a double percent, %% to replace all the percent signs.

In one version of the batch file I have this line:

git log --pretty=format:"%%ai%%x09%%H%%x09%%s" > commitlog.csv

Which I noted echos the command like this:

git log --pretty=format:"%ai%x09%H%x09%s"  1>commitlog.csv

Which has a "1" interjected into it for some reason, but oddly creates a file with the information, just excluding the tabs, so it all runs together. I read that greater than '>' needs to be preceded by a carat, like so '^>'

So, in the next version of the batch file I changed the line to this:

git log --pretty=format:"%%ai%%x09%%H%%x09%%s" ^> commitlog.csv

Which echos the command like this:

git log --pretty=format:"%ai%x09%H%x09%s" > commitlog.csv

Which I've stared at for a long time, many times, and appears to be EXACTLY what I type in PowerShell, and I would expect to get the same result, but instead I get an error message:

fatal: ambiguous argument '>': unknown revision or path not in the
working tree.

Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

What am I doing wrong?

Upvotes: 4

Views: 2808

Answers (1)

chwarr
chwarr

Reputation: 7192

The git log --pretty=format: command is terminating each line with just a newline (0x0A). Most tools on Windows (like Notepad) expect lines to be terminated with a carriage return (0x0D) and and newline. However, cmd has special handling for just a newline that makes the console output look pretty.

Thus, the batch file should look something like this:

git log --pretty=format:"%%ai%%x09%%H%%x09%%s%%x0D" > commitlog.csv

Notice the extra %%x0D to add the carriage return manually. As you found out already, the %% is needed to escape the single % inside of a batch script.

1> and just > are basically equivalent here. 1 is just the numeric value for stdout (compare to 2 for stderr), but the default is to redirect stdout. Cmd is just normalizing before printing to command that its going to run.

Minor nit: you may want to redirect to commitlog.tsv, as the values are separated by tabs, not commas. :-)

Git on Windows is often plagued by these sort of newline issues, so be on the look of for them elsewhere.

Upvotes: 3

Related Questions