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
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:
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.
Is there any way to clone a git repository's sub-directory only?
Use a sparse checkout, available since version 1.7.0.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With