Mateus Viccari
Mateus Viccari

Reputation: 7709

How to execute git log on windows cmd?

I have the following command:

git log --pretty=tformat:'<li>%h %ci %d %s</li>' > changelog.html

That saves the git log to the changelog.html file. When i execute it through git bash, it works fine, but when i put this code in a .bat file and run it, i get the following error:

The system could not find the specified file

I think it's caused by the quotes on the format parameter, but i don't know how to solve the problem... Is there a way to escape the quotes maybe?

Upvotes: 4

Views: 1883

Answers (1)

mklement0
mklement0

Reputation: 440501

Inside a Windows batch file, you have to:

  • double % chars. for them to be taken as literals
  • use double quotes to protect embedded whitespace or other special chars. such as >:
git log --pretty=tformat:"<li>%%h %%ci %%d %%s</li>" > changelog.html
  • Generally, you can also use ^ to escape individual characters in an unquoted string (e.g., ^% to represent literal %, or ^| to represent literal |), but that wouldn't work here, because of the following caveat:
    You cannot use ^ to escape a space character in order to form a single argument with embedded whitespace; for instance, foo^ bar is still split into 2 arguments, foo and bar.

Some background for readers from the Unix world:

  • There is no equivalent to a POSIX-like shell's single-quoted strings (strings to be taken literally, with no expansions taking place) in the Windows batch language.
    • Single quotes generally have no special meaning in the Windows batch language (except in certain special contexts, such as a for loop) - they canNOT be used to delimit a string with embedded whitespace.
  • Thus, analogous to how you have to escape $in double-quoted bash strings as \$ to prevent expansion, you have to escape % as %% in double-quoted strings to prevent expansions in batch files, given that an identifier enclosed in % chars. (e.g., %PATH%) signifies a variable reference to expand.
    • The curious thing is that an interactive cmd.exe prompt doesn't require escaping of %, whereas batch files do. More specifically, % chars. in an interactive command line are treated as literals, unless they are part of a variable reference to an existing variable (e.g., %windir%), in which case expansion does take place. This oddity is the reason that a for loop must be written as for %i in ... (one %) on the command line, and as for %%i in ... (two %) in a batch file.
  • A caveat re passing double-quoted strings to batch files: When passing an argument enclosed in double quotes, the double quotes are included in the argument value; the ~ parameter modifier can be used to strip them; e.g., %~1 returns the 1st argument without double quotes (if any).
  • Finally, the counterpart of \-escaping individual characters in an unquoted string in a POSIX shell is ^-escaping, but, as stated, ^ cannot be used to escape a space in order to form a single argument with embedded whitespace.

Upvotes: 13

Related Questions