how to download single folder OR file in gitlab repository
Asked Answered
S

5

16

i have one repository. In this repository multiple folders are available.

i have required only one folder in this repository.

i am already try to following command but it's not working.

git clone

Stieglitz answered 27/6, 2016 at 7:4 Comment(2)
Possible duplicate of Is there any way to clone a git repository's sub-directory only?Sergio
Forum topic: forum.gitlab.com/t/… Seems like there isn't a way to do it from the Gitlab GUI - seems to be a bit more support in Github #4605163Interurban
B
7

Is there any way to clone a git repository's sub-directory only?

Use a sparse checkout, available since version 1.7.0.

Bottommost answered 27/6, 2016 at 7:5 Comment(1)
Beware that it downloads all repository history content also.Aesthesia
B
9

If only the content of that folder is of interest (not its history), you can, since GitLab 1.11 (May 2019) download only a folder.

Download archives of directories within a repository

https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfhWmf0cC2nZ7-sWV9QWRft/images/11_11/repo_download-archive.png

Depending on the type of project and its size, downloading an archive of the entire project may be slow or unhelpful – particularly in the case of large monorepos.

In GitLab 11.11, you can now download an archive of the contents of the current directory, including subdirectories, so that you download only the files you need.

From issue 24704: see documentation.


With GitLab 14.4 (Oct. 2021), you have:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
"https://gitlab.com/api/v4/projects/<project_id>/repository/archive?sha=<commit_sha>&path=<path>"

It was not somehow mentioned in the "GitLab 1.44 released" page though.

Blast answered 26/5, 2019 at 22:16 Comment(2)
Can we download using curl or wget?Collection
@Collection Not yet: that is followed by gitlab.com/gitlab-org/gitlab/issues/28827Blast
B
8

From Gitlab 14.4 download folder is supported natively. See the doc

Example:

https://<GITLAB-URL>/api/v4/projects/<project-id>/repository/archive?path=<subfolder-path>
Boyett answered 30/11, 2021 at 15:37 Comment(0)
B
7

Is there any way to clone a git repository's sub-directory only?

Use a sparse checkout, available since version 1.7.0.

Bottommost answered 27/6, 2016 at 7:5 Comment(1)
Beware that it downloads all repository history content also.Aesthesia
G
2

Here's a (far from perfect) piece of sh code to fetch a file or whole directory from repo using GiLab'a API I wrote. Enjoy if you find it useful :)

#!/bin/sh

GITLAB_API_URL=https://gitlab.com/api/v4
GITLAB_TOKEN=<YOUR TOKEN>
PROJECT=path/to/gitlab/project

PROJECT_ENC=$(echo -n ${PROJECT} | jq -sRr @uri)

function fetchFile() {
  FILE=$1
  FILE_ENC=$(echo -n ${FILE} | jq -sRr @uri)

  curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "${GITLAB_API_URL}/projects/${PROJECT_ENC}/repository/files/${FILE_ENC}?ref=master" -o /tmp/file.info
  if [ "$(dirname $FILE)" != "." ]; then
    mkdir -p $(dirname $FILE)
  fi
  cat /tmp/file.info | jq -r '.content' | tr -d "\n" | jq -sRr '@base64d' > $FILE
  rm /tmp/file.info
}

function fetchDir() {
  DIR=$1
  FILES=$(curl -s --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "${GITLAB_API_URL}/projects/${PROJECT_ENC}/repository/tree?ref=master&per_page=100&recursive=true&path=${DIR}" | jq -r '.[] | select(.type == "blob") | .path')
  for FILE in $FILES; do
    fetchFile $FILE
  done
}

fetchDir <REPO_DIR_TO_FETCH>

It uses curl and jq (version at least 1.6).

Be careful if you may have more then 100 files in a directory since the above fetchDir function gets only 100 files. To make it work always you should add some loop there.

Gerta answered 6/3, 2020 at 16:43 Comment(0)
P
1

Thanks to Lzydorr. I needed this in Powershell to get version in maven project. This function download pom.xml and find "version" tag. I post it here, in case any other needs this in PS.

This function can for example be called like this:

$version = gitlabFetchVersion "integration/$appname" "master"
function gitlabFetchVersion {
  Add-Type -AssemblyName System.Web
  #$project = "integration/ssys-integration"
  $project = $($args[0])
  $project_enc = [System.Web.HTTPUtility]::UrlEncode($project)
  $file="pom.xml"
  $branch=$($args[1])
  $GITLAB_API_URL="https://git.infosynergi.no/api/v4"
  $GITLAB_TOKEN="XXXXXXX"
  $file_enc=[System.Web.HTTPUtility]::UrlEncode($file)
  $headers = @{
    Authorization = $GITLAB_TOKEN
    Accept        = "application/json"
  }

  $url = $GITLAB_API_URL + "/projects/" + $project_enc + "/repository/files/" + $file_enc + "?ref="+$branch
  $pom64 = Invoke-RestMethod -Method Get -Headers @{ 'PRIVATE-TOKEN'='qPxLx5Hk5cB4HLgbVDsQ' } -Uri $url
  $xml = [xml]([System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($pom64.content)))
  $pversion = $xml.project.version
  Write-Host "Version: " $pversion   
  return $pversion
}
Playbill answered 18/11, 2020 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.