Reputation: 445
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 "--format=%25ci"
-> 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.
Upvotes: 1
Views: 1407
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="%25%25h %25%25ad <%25%25ae>" > $(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:
%25
is the MSBuild Special Characters escape for a %
sign%25%25
then will create two percent signs which is the cmd escape for percent"
is the xml attribute value escape for double quotes>
is the xml attribute value escape for the >
characterUpvotes: 2
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