2

I have a project in Azure DevOps. I want to copy a particular file from the project's Git Repository to my unix box. I want to achieve this using command line.

I tried the below command

  wget -O /unix/path/to/save/file.txt "https://tfs-glo-org.visualstudio.com/team/_git/projectName?path=/fileTobeCopied&version=GBbranch"

It successfully copies a file to the unix box but instead of just the file that I want, it saves a whole html page.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Rosily
  • 87
  • 1
  • 2
  • 7

2 Answers2

4

This is no easy feat. The raw download url is a bit more complex:

 GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}
     /items?path={path}&scopePath={scopePath}&recursionLevel={recursionLevel}
     &includeContentMetadata={includeContentMetadata}&latestProcessedChange={latestProcessedChange}
     &download={download}&$format={$format}&versionDescriptor.versionOptions={versionDescriptor.versionOptions}
     &versionDescriptor.version={versionDescriptor.version}&versionDescriptor.versionType={versionDescriptor.versionType}
     &includeContent={includeContent}&resolveLfs={resolveLfs}&api-version=5.1

The most important thing for you here is to set &download=true. Easiest way ti get these values is to download a file from the UI and inspect the download url:

enter image description here

You can get the full url from the Downloads page in your browser:

enter image description here

The full docs for the request format can be found here.

In order for the download to succeed you'll also need to pass in an authentication token or Personal access token. The security parts are explained here.

The easiest option would be to use a Personal Access Token:

 wget --user . --password {PAT} https://dev.azure.com/....

If the credentials aren't passed in, Azure DevOps will send the error page contents instead.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • It may just be our admin settings but I get an html doc downloaded instead of a .sh that I am targeting. Is this something you ever encountered? – Virtual Penman May 22 '20 at 19:25
  • When you get an HTML it is most likely an access denied or file not found. My guess would be the token doesn't have the right permissions, is expired or not for the right org. – jessehouwing May 23 '20 at 06:56
1

Files are stored in Git Source control for a reason. Not to be downloaded the way that you are trying to do.

Using the command line git clone the repository and find the file that you want to pick.

https://www.atlassian.com/git/tutorials/setting-up-a-repository/git-clone

You can always target a specific tag or a commit.

  • Why clone when you want only a single file? If I just want to use a script on the linux server, I will prefer to download the single file. – Joseph Bolade Caxton-Idowu Dec 26 '21 at 21:09
  • Correction: Why clone when you want only a single file is the question. If you just want to use a file like a script on the server, It is tempting to want to download the single file. But you may end up picking the wrong version of the file. Hence clone is better – Joseph Bolade Caxton-Idowu Dec 26 '21 at 21:17