Home » Git » git and psake from Powershell

git and psake from Powershell

Posted by: admin November 22, 2021 Leave a comment


I’m trying to automate pulling the latest version of our code from bitbucket using git.exe via psake in Powershell but I’m struggling with a number of issues.

I’ve got the following code in a Task:

    Exec {
       &('c:\Program Files\Git\bin\git.exe') pull --progress --no-rebase -v origin

but I get the following error:

Error: 25/04/2016 22:51:02:
At C:\work\mycompany\buildapp\psakefile.ps1:117 char:5 +     
&('c:\Program Files\Git\bin\git.exe') pull --progress --no-rebase -v origin
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
[<<==>>] Exception: From bitbucket.org:mybitbucketusername/work

I assume that’s because I’m not calling git.exe from my clone folder but how do I do this? Is there a way to a) call git from my current location and apply it to a specific clone folder? or b) if a) can’t be done, how do I change folder in psake?

Assuming i can fix the first problem, how can I find out the exact error returned from git. Just getting Exception: From bitbucket.org is too vague.

Is there a better way to pull my code from bitbucket? Should I call git bash (for windows) using psake from Powershell? Is that better? If so, how do I call it with the specific command line.

If you want me to provide more information, please let me know.

Update 1:

This is what I get when I call git pull

Error: 26/04/2016 15:46:30: 
At C:\work\mycompany\buildapp\psakefile.ps1:120 char:5 +     
&('c:\Program Files\Git\bin\git.exe') pull +
[<<==>>] Exception: There is no tracking information for the current branch.

This is to be expected as c:\work\mycompany\buildapp is not the cloned folder. It is a subfolder of the cloned folder.

I need to be able to call git while in c:\work\mycompany\buildapp (as this is where my script is running from) but I need git to pull data from c:\work\mycompany\.

I hope the above clarifies my question.



The following git command lines work perfectly running a command line being called from any folder:

git clone [email protected]:myusername/work.git "c:/Work/"

git --git-dir=c:\work\.git --work-tree=c:\work pull origin master

The following works with PSake:

 Exec {
   &('c:\Program Files\Git\bin\git.exe') clone
      [email protected]:myusername/work.git 'C:/Work/'

But pull still doesn’t work when called this way:

 exec {
   &('c:\program files\git\bin\git.exe') --git-dir=c:\work\.git 
      --work-tree=c:\work pull origin master

I still get an error:

Error: 29/04/2016 10:06:36: 
At C:\temp\buildapp\psakefile.ps1:124 char:8 +        
&('c:\program files\git\bin\git.exe') --git-dir=c:\work\.git - ... +  
[<<==>>] Exception: From bitbucket.org:myusername/work

Unfortunately the exception doesn’t tell us much.

I’ve tried various combinations with quotes/double quote with the arguments but to no avail.

Any ideas?


Going to post my own answer but many thanks to @etanreisner & @spijn for their help and suggestions.

Huge amount of time has been wasted on this issue and it was all down to one thing!! Running my scripts directly in ISE. This was kindly highlighted to me by Dave Wyatt after I posted a bug report on psake. Here’s is what he said:

Is this happening from the powershell.exe console, from the ISE, or both? The ISE treats any console application that writes to the stderr stream as an exception, but powershell.exe does not. (This is because powershell.exe is a win32 console application, and can just let that subsystem handle interaction with other console apps. The ISE has its own code to pretend to be a console when it’s really a WinForms app, and the behavior is different. I believe Microsoft intends to fix this soon.)

Anyhow, git.exe writes to stderr all over the place, which is why I always keep a separate console open when I want to run git commands (so I don’t have to stare at the red text all the time.)

Here is the actual issue I created on github: Psake always throws an exception when calling git.exe and trying to clone #174

After trying the following in Powershell rather than ISE:

Git Clone:

Exec {
&('c:\program files\git\bin\git.exe') clone
   [email protected]:myusername/work.git "C:\Work"

Git Pull:

exec {
&('c:\program files\git\bin\git.exe') --git-dir="c:/work/.git" 
   --work-tree="c:/work/" pull origin master

Hope this helps other and won’t end up wasting as much time as I did on something so trivial yet not obvious unless you are aware of it!