how to download single folder OR file in gitlab repository
Solution 1
Is there any way to clone a git repository's sub-directory only?
Use a sparse checkout, available since version 1.7.0.
Solution 2
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
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.
Solution 3
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.
Solution 4
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>
Solution 5
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
}
Sujal Patel
Updated on December 01, 2021Comments
-
Sujal Patel over 2 years
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
-
Fredrick Gauss almost 6 yearsBeware that it downloads all repository history content also.
-
Ashwani over 4 yearsCan we download using curl or wget?
-
VonC over 4 years@Ashwani Not yet: that is followed by gitlab.com/gitlab-org/gitlab/issues/28827
-
VonC over 2 yearsGood catch. Upvoted. I have referenced this feature in my own answer.