1

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.

Thanks.

UPDATE 2:

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

git clone git@bitbucket.org: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
      git@bitbucket.org: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?

Thierry
  • 6,142
  • 13
  • 66
  • 117
  • Is there more to that error? Either above or below this snippet? That's picked up your remote so it must be in your git working directory. Are you perhaps missing the necessary credentials? – Etan Reisner Apr 26 '16 at 01:39
  • From git -h output: `[--git-dir=] [--work-tree=]` so you just need to specify those to run git anywhere. Alternative would be to use Invoke-WebRequest to get a zip with the latest source from bitbucket I guess – stijn Apr 26 '16 at 07:22
  • @EtanReisner - No, unfortunately, that's the full error that got displayed. I changed my ssh not to have any paraphrase and test it and it works as expected, so I don't think it's the credential. Any other ideas? – Thierry Apr 26 '16 at 10:36
  • This isn't just an issue where `pull` is failing because it can't merge or something, right? Does `git fetch` work this way? – Etan Reisner Apr 26 '16 at 13:10
  • @EtanReisner It does when I use it from Git Bash for Windows. I can clone and I can fetch or pull. – Thierry Apr 26 '16 at 13:38
  • @stijn in the command you mentioned, where does it say that it should pull/fetch. Would you mind providing me with a concrete example. Will it be something like: git pull -h output "https://... mywork.git" "c:\work" ? Thanks – Thierry Apr 26 '16 at 13:45
  • The point of stijn's comment was that you can tell git where the `.git` directory and the working tree are via command line arguments (this was based on your "maybe I'm not in the correct directory" comment I believe) but I don't believe that's the issue (or git would complain that it didn't know what `origin` way or something like that). – Etan Reisner Apr 26 '16 at 13:53
  • Do you have a proxy set up in git bash? Do you need one? Does `git fetch` work in `psake` where that `pull` is failing? – Etan Reisner Apr 26 '16 at 13:54
  • @EtanReisner - I haven't got it to work in psake as I need to be in the working folder i.e. c:\work\ for example, where my psake files are located in c:\work\buildapp\. This is part of my question as to how can I get git to pull/fetch from a non-working folder and I think stijn try to provide me with an anser. This is my main problem I think. I don't think there is a problem pulling/fetching. It's the actual call from psake that I need and how to pull/fetch from a non-working folder (sorry. I'm not sure if that's the right term and hope you get what I mean!). – Thierry Apr 26 '16 at 14:15
  • I'm suggesting that you *are* in the right location or git wouldn't know what `origin` was supposed to mean. If you make an empty directory and then run `git pull` in it on your machine (with git bash) what do you get as output? You should get an error indicating that `git` isn't in a repository. Do you? If you do and since you *aren't* getting that in the `psake` call I believe you *are* in the right location and that isn't the issue. – Etan Reisner Apr 26 '16 at 14:26
  • @EtanReisner - I've updated my question with additional info. As for origin, I have to go an read about what it does. I just took this from a sample as I'm not familiar with Git. If you know what the right command line argument should be, please let me know. Thanks. – Thierry Apr 26 '16 at 14:54
  • `origin` is the remote you are pulling from/pushing to. It is part of the configuration in the `.git` directory of a working tree. Without that configuration `git pull` doesn't know where to pull from. (Like I said try `git pull` from a new empty directory and see what you get.) – Etan Reisner Apr 26 '16 at 14:55
  • @EtanReisner - Sorry busy with other projects. Anyway, I understand what you're saying but then my question would be is there a way to specify a configuration when calling git so it will know where to pull the latest files to? When calling git-clone, irrelevant my current location, I can call git clone git@bitbucket.org:tfierens/work.git "c:/Work/" - I would like to be able to do the same but with git pull/fetch. Is that not possible? – Thierry Apr 28 '16 at 13:12
  • Yes, it is. that's what stijn was showing you. That's what the `--git-dir` and `--work-tree` arguments are for. But I don't think you need those right now. I don't think that's the problem. I think that is *already* working for you or you would be getting a **different** error from `git` when you ran it. (This is why I keep suggesting that you run `git pull` from an empty directory to see what you get from that.) – Etan Reisner Apr 28 '16 at 13:34
  • @EtanReisner From git bash, I went to a new empty folder i.e. c:\work1 and ran "git pull git@bitbucket.org:myrepo/work.git" and I got the following error: "fatal: Not a git repository (or any of the parent directories): .git" – Thierry Apr 28 '16 at 13:44
  • Right. Which is the error I would expect. But you *aren't* getting that in `psake`. There you are seeing `git` try to connect to the correct remote server and *fail* to do that for some reason. So you **aren't** having an "incorrect git directory" or "incorrect work-tree" problem as far as I can tell. Is the `buildapp` directory checked in as part of the `c:\work\mycompany` repository? – Etan Reisner Apr 28 '16 at 14:21
  • @etanreisner it was, but it's no longer the case. I'm running this from c:\temp\buildapp. Strangely I was going to send a message saying everything is ok now since using stijn had suggested as I got it to work, but now can't anymore. Will try to figure out why later on today. – Thierry Apr 29 '16 at 07:54

1 Answers1

0

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
   git@bitbucket.org: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!

Community
  • 1
  • 1
Thierry
  • 6,142
  • 13
  • 66
  • 117