Forum Discussion

grsm001's avatar
grsm001
Copper Contributor
Jul 02, 2020
Solved

Trying to down just specific file in Azure repo to Windows and Linux.

I am new to Azure and created a text file to test with in my Azure repo. I have the command "git" on Windows to download from the Windows server and "wget" or "curl" on Linux. Could somebody give me the syntax to download my test flat file? I am having no luck.  On Windows, I can clone my my repo via the following command:

 

>git clone https://GregSmith0896@dev.azure.com/GregSmith0896/WebSphere/_git/WebSphere

 

On Linux, again I can download the repo with :

 

>wget clone https://GregSmith0896@dev.azure.com/GregSmith0896/WebSphere/_git/WebSphere

 

I would like to just download a specific file in the repo which exists in the branch "scripts" and the file name is "test.txt" 

 

Please let me know. 

 

Thank you. 

 

  • I was able to find a solution per this thread: https://stackoverflow.com/questions/2466735/how-to-sparsely-checkout-only-one-single-file-from-a-git-repository/19501643#19501643 - using the -n parameter clone the branch without downloading anything then using the "checkout"  command with "HEAD", you can download a single file.

     

    git clone -n -b <branch> <path to master> --depth 1

    cd <repo>

    git checkout HEAD <file in branch>

     

    For example: download "test.txt"  file from the "scripts" branch of the repo "WebSphere"

     

    >git clone -n -b scripts https://<*.com>/_git/WebSphere --depth 1

     

    >cd WebSphere

     

    >git checkout HEAD test.txt

     

3 Replies

  • Scott_Gray's avatar
    Scott_Gray
    Copper Contributor

    Azure Repos are based off https://git-scm.com/, and there are many suggestions out there for downloading individual files, for example, you could create an archive:

    git archive --format=zip HEAD test.txt -o test.zip

    or you could use https://git-scm.com/docs/git-sparse-checkout which clones the repo, but "hides" certain files you are not interested in, this is an experimental feature, so be careful.

    Alternatively, the https://docs.microsoft.com/en-us/rest/api/azure/devops/git/blobs/get%20blob?view=azure-devops-rest-5.1 Api may be of use, which appears to allow you to download specified files directly from the repo: 

    GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/blobs/{sha1}?download={download}&fileName={fileName}&$format={$format}&resolveLfs={resolveLfs}&api-version=5.1 

    Depending on your use case, you might also consider adding a task to your Azure DevOps Release pipeline to copy the required files to a specified locaton (e.g. AzureBlob File Copy).

    steps:
    - task: AzureFileCopy@4
      displayName: 'AzureBlob File Copy'
      inputs:
        SourcePath: '$(System.DefaultWorkingDirectory)\Build\site\$(Build.BuildId)\test.txt'
        azureSubscription: 'Visual Studio Enterprise (bc180cfa-8d6b-46bc-97d6-aeaab72b18c4)'
        Destination: AzureBlob
        storage: sitestaging
        ContainerName: '$web'

     

    • grsm001's avatar
      grsm001
      Copper Contributor

      Scott_Gray Thank you for the information.

       

      I am getting the following when I try to download and create a container .. such as a zip file:

       

      C:\Users\grsm001\git>git archive --format=zip HEAD test.txt -o test.zip
      fatal: not a valid object name: HEAD

       

      Doing a Git status shows:

       

      C:\Users\grsm001\git>git status
      On branch scripts

      No commits yet

      Untracked files:
      (use "git add <file>..." to include in what will be committed)
      test.zip

      nothing added to commit but untracked files present (use "git add" to track)

       

      Can you help me with the syntax?

       

      Also, I am not versed in using GET. Is there a command I can use this with? 

      • grsm001's avatar
        grsm001
        Copper Contributor

        I was able to find a solution per this thread: https://stackoverflow.com/questions/2466735/how-to-sparsely-checkout-only-one-single-file-from-a-git-repository/19501643#19501643 - using the -n parameter clone the branch without downloading anything then using the "checkout"  command with "HEAD", you can download a single file.

         

        git clone -n -b <branch> <path to master> --depth 1

        cd <repo>

        git checkout HEAD <file in branch>

         

        For example: download "test.txt"  file from the "scripts" branch of the repo "WebSphere"

         

        >git clone -n -b scripts https://<*.com>/_git/WebSphere --depth 1

         

        >cd WebSphere

         

        >git checkout HEAD test.txt

         

Resources