4

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

My Windows programs did not have a problem with the newlines, everything was on the line it was supposed to be. I attempted your suggestion anyway, but adding the %%x0D did not make a difference in my output. For some odd reason, it's actually not adding the TABS (%%x09) when I run the batch file, the data in each each line just runs together.
And .tsv is not a filetype recognized by Excel, so I don't use it. It can parse csv files regardless of what actual delimiter is being used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.