kiki
kiki

Reputation: 445

MSBuild Exec command always fails

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:

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.

Upvotes: 1

Views: 1407

Answers (2)

Jeroen
Jeroen

Reputation: 63830

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:

Upvotes: 2

Michael Baker
Michael Baker

Reputation: 3434

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

Upvotes: 6

Related Questions