15

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

2

5 Answers 5

8

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://about.gitlab.com/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.

2
7

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

Use a sparse checkout, available since version 1.7.0.

1
  • 2
    Beware that it downloads all repository history content also. May 31, 2018 at 7:07
7

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>
0
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.

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.