0

I have to use a git command to get the commit hash. I found several approachs but it doesn't seem to work. This is much easier if I use a powershell script where I can import git.exe with $env:Path += ";C:\Program Files (x86)\Git\bin". The fact is that I don't want to use external scripts (such as powershell ones) or importing anything. Is there a way to do that only with msbuild?

I've tried using:

<Exec Command="git describe --long --always" ConsoleToMSBuild="true" />

error MSB3073: The command "git describe --long --always" exited with code 9009.

and

<Exec Command="$env:Path += ';C:\Program Files (x86)\Git\bin'; git describe --long --always" ConsoleToMSBuild="true" />

error MSB3073: The command "$env:Path += ';C:\Program Files (x86)\Git\bin'" exited with code 123

EDIT

I don't know why question is on powershell :s

UPDATE

Now I'm using the following line:

<Exec WorkingDirectory="$(MSBuildProjectDirectory)" Command="&amp; 'C:\Program Files (x86)\Git\bin\git.exe' describe --long --always" ConsoleToMSBuild="true" />

which throws the error: error MSB3073: The command "& '(...)' describe --long --always" exited with code 255..

7
  • Have you tried with the full path to the git exe (Command="C:\Program Files (x86)\Git\bin\git describe --long --always" ? Commented May 24, 2020 at 17:49
  • It throws error MSB3073: The command "C:\Program Files (x86)\Git\bin\git describe --long --always" exited with code 9009 (I also tried with git .exe full path) Commented May 24, 2020 at 17:54
  • 1
    Do the commands work if you run them outside of msbuild? The 9009 errorlevel means that the command/file was not found. 123 indicates that there was an unspecified error - probably parsing or something along those lines. Commented May 24, 2020 at 19:41
  • Yes, I'm pretty sure they work. Actually for test that, I just copied the error (which prints the line I'm trying to execute) then paste it in a powershell and it works. I figured out in google that when you exec a .exe from full-path, you should use '&' at start, then the full line would be: "&amp; 'C:\Program Files (x86)\Git\bin\git.exe' describe --long --always". Now it prints exited with code 255. I don't know what that means but I'm pretty sure it is related with the working directory (if you try to run that code outside a local repo, it throws an error). Commented May 24, 2020 at 20:30
  • That git describe command should always succeed and return something on stdout. Hence the problem is probably elsewhere. I would try and run it separately, but as close to what you have in your csproj as possible. I would also ask myself one very important question: Do I really need to do this? From what you've shown, the output isn't used - so is there any point in putting in the time and effort to get it to work? Commented May 24, 2020 at 20:42

1 Answer 1

0

After a lot of tries, I did it:

<Exec Command="powershell.exe &amp; &quot;C:\Program Files (x86)\Git\bin\git.exe&quot; describe --long --always" ConsoleToMSBuild="true">
    <Output TaskParameter="ConsoleOutput" ItemName="Version" />
</Exec>
Sign up to request clarification or add additional context in comments.

Comments

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.