how to download single folder OR file in gitlab repository

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.


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>

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

Use a sparse checkout, available since version 1.7.0.


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:

  • issue 28827 "Download a (sub-)folder from a repository via the Repositories API",
  • resolved with MR 71431 and commit 1b4e0a1:
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.