git 如何在gitlab存储库中下载单个文件夹或文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38047757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to download single folder OR file in gitlab repository
提问by Sujal Patel
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
git 克隆
采纳答案by Anthony Astige
Is there any way to clone a git repository's sub-directory only?
Use a sparse checkout, available since version 1.7.0.
使用sparse checkout,从 1.7.0 版开始可用。
回答by VonC
If only the content of that folder is of interest (not its history), you can, since Git Lab 1.11 (May 2019) download only a folder.
如果仅对该文件夹的内容感兴趣(而不是其历史记录),则可以,因为 Git Lab 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.
根据项目的类型及其大小,下载整个项目的存档可能会很慢或没有帮助——尤其是在大型 monorepos 的情况下。
在 GitLab 11.11 中,您现在可以下载当前目录内容的存档,包括子目录,以便您只下载您需要的文件。
From issue 24704: see documentation.
回答by Izydorr
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 :)
这是一段(远非完美的)sh
代码,用于使用我编写的 GiLab'a API 从 repo 中获取文件或整个目录。如果你觉得它有用,请享受:)
#!/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>
It uses curl and jq(version at least 1.6).
它使用 curl 和jq(版本至少为 1.6)。
Be careful if you may have more then 100 files in a directorysince the above fetchDir
function gets only 100 files. To make it work always you should add some loop there.
如果目录中的文件可能超过 100 个,请小心,因为上述fetchDir
函数仅获取 100 个文件。为了让它始终工作,你应该在那里添加一些循环。