如何在gitlab存储库中下载单个文件夹或文件
how to download single folder OR file in gitlab repository
我有一个存储库。在此存储库中有多个文件夹可用。
我只需要这个存储库中的一个文件夹。
我已经尝试执行命令,但它不起作用。
git 克隆
Is there any way to clone a git repository's sub-directory only?
Use a sparse checkout, available since version 1.7.0.
如果只对那个文件夹的内容感兴趣(不是它的历史),你可以,因为 GitLab 1.11(2019 年 5 月)只下载一个文件夹。
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.
来自 issue 24704: see documentation.
使用 GitLab 14.4(2021 年 10 月),你有:
- issue 28827 "通过存储库从存储库下载(子)文件夹 API",
- 已通过 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>"
虽然“GitLab 1.44 released”页面中并没有提及它。
这是一段(远非完美的)sh
代码,用于使用我编写的 GiLab'a API 从回购中获取文件或整个目录。觉得有用就点个赞吧:)
#!/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=
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=
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>
它使用 curl 和 jq(至少 1.6 版本)。
如果目录中的文件超过 100 个,请小心,因为上面的 fetchDir
函数只能获取 100 个文件。要使其始终有效,您应该在此处添加一些循环。
感谢Lzydorr。我需要在 Powershell 中使用它来获取 Maven 项目中的版本。此函数下载 pom.xml 并找到“版本”标签。我 post 在这里,以防其他人在 PS.
中需要它
这个函数可以这样调用:
$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
}
本机支持从 Gitlab 14.4 下载文件夹。参见 doc
示例:
https://<GITLAB-URL>/api/v4/projects/<project-id>/repository/archive?path=<subfolder-path>
我有一个存储库。在此存储库中有多个文件夹可用。
我只需要这个存储库中的一个文件夹。
我已经尝试执行命令,但它不起作用。
git 克隆
Is there any way to clone a git repository's sub-directory only?
Use a sparse checkout, available since version 1.7.0.
如果只对那个文件夹的内容感兴趣(不是它的历史),你可以,因为 GitLab 1.11(2019 年 5 月)只下载一个文件夹。
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.
来自 issue 24704: see documentation.
使用 GitLab 14.4(2021 年 10 月),你有:
- issue 28827 "通过存储库从存储库下载(子)文件夹 API",
- 已通过 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>"
虽然“GitLab 1.44 released”页面中并没有提及它。
这是一段(远非完美的)sh
代码,用于使用我编写的 GiLab'a API 从回购中获取文件或整个目录。觉得有用就点个赞吧:)
#!/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=
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=
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>
它使用 curl 和 jq(至少 1.6 版本)。
如果目录中的文件超过 100 个,请小心,因为上面的 fetchDir
函数只能获取 100 个文件。要使其始终有效,您应该在此处添加一些循环。
感谢Lzydorr。我需要在 Powershell 中使用它来获取 Maven 项目中的版本。此函数下载 pom.xml 并找到“版本”标签。我 post 在这里,以防其他人在 PS.
中需要它这个函数可以这样调用:
$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
}
本机支持从 Gitlab 14.4 下载文件夹。参见 doc
示例:
https://<GITLAB-URL>/api/v4/projects/<project-id>/repository/archive?path=<subfolder-path>