Powershell – Get file from bitbucket (git) from powershell

gitpowershell

I need to grab a single file from bitbucket (git) using powershell. Complicating matters I need to do so over http/https as the system I'm running powershell from does not have direct internet access (therefore ssh isn't an option). How can I do so?

I thought that perhaps I could grab it using Invoke-WebRequest and accessing the raw URL. For example: https://bitbucket.org/company/project/raw/HEAD/some/special/file.txt

But it appears to access that you have to authenticate. I've tried setting up authentication but I can't get it working.

I thought next I could just pull it from git using git archive:
git archive –remote=git://bitbucket.org/Company/project.git HEAD:some/path/file.txt but that also doesn't seem to work.

So how can I get a single file. I don't want to just do git clone because then I have to pull down (and store) 500MB worth of other files I don't care about in the repo which is an issue for two reasons

1) I have to have space to store all that data (at least temporarily)
2) Unless I want to tie up that storage permanently I have to re-download it each time I need the file. 

The file I need is 10KB so to tie up 500MB for a 10kb file seems absurd.

What's the answer? There's got to be a way to get Invoke-WebRequest to authenticate properly or just download a single file? Can any git gurus – particularly with powershell/windows backgrounds help?

Thanks!
Brad

Best Answer

BitBucket appears to have a pretty standard REST API you can use with Invoke-WebRequest or Invoke-RestMethod.

Here are some links to their documentation: Authentication and GET raw content. The gist of those doc pages is that they support basic authentication and GET requests for raw file contents. So from your example, you'd need to do something like this:

# create a credential variable for your account
$cred = Get-Credential

# modify the base URL to hit the API endpoint instead of the standard web endpoint, pass the credentials with the request, and send the output to a file
Invoke-RestMethod -Credential $cred -Uri https://api.bitbucket.org/1.0/repositories/company/project/raw/HEAD/some/special/file.txt -OutFile .\file.txt