1

I am trying to execute this simple command from MSBuild (VS2015, from a .target file) to generate the date of the current git commit:

git show -s --format=%cd --date=format:%d.%m.%Y

So in MSBuild I have tried:

<Exec Command="git show -s --format=%25cd --date=format:%25d.%25m.%25Y"  ConsoleToMSBuild="true">
  <Output TaskParameter="ConsoleOutput" PropertyName="BuildDate" />
</Exec>

But it yields only an error:

1>------ Build started: Project: example, Configuration: Release Dll Win32 ------
1>  fatal: invalid --pretty format: d.Y
1>D:\example\gitversion.targets(26,5): error MSB3073: The command "git show -s --format=%cd --date=format:%d.%m.%Y" exited with code 128.

If I post the command within the quotation marks to the console, it works like a charm and prints 19.12.2016.

I have tried the following things:

  • Escape also the = sign, :, ... still does not work

  • Use only Command="git show -s --format=%25ci" -> yields also an error fatal: invalid --pretty format: ci but works fine in console.

  • surround with quotes &quot;--format=%25ci&quot; -> same error

  • Call with Command="git --version", this works as expected and returns the git version (same as on console)

I suspect that it somehow does not accept the = to specify the argument, but git won't let me pass it as separate arguments, e.g. separated by a space.

kiki
  • 325
  • 6
  • 20

2 Answers2

5

You want %25 to escape %, so your command becomes

<Exec Command="git show -s --format=%25%25cd --date=format:%25%25d.%25%25m.%25%25Y" ConsoleToMSBuild="true">
  <Output TaskParameter="ConsoleOutput" PropertyName="BuildDate" />
</Exec>

See here for MSBuild escape characters

Michael Baker
  • 3,338
  • 26
  • 29
  • It still does not really make sense to me, why I need to escape it twice, but it works like a charm. – kiki Jan 13 '17 at 10:59
2

The other answer helped me, but I had a slightly more complex scenario. Here's my own solution, with some added explanation:

<Exec Command="git log -1 --oneline --pretty=&quot;%25%25h %25%25ad &lt;%25%25ae&gt;&quot; &gt; $(OutputPath)/git-info.txt" />

This will execute this command:

git log -1 --oneline --pretty='%h %ad <%ae>' > bin/debug/git-info.txt

Here's what I understood of why each escape is needed:

Jeroen
  • 60,696
  • 40
  • 206
  • 339