我有一个浅Git存储库,创建如下:
mkdir repo
cd repo
git init
git remote add origin $URL
git fetch --depth 1 origin $SHA
作为构建过程的一部分,我希望使用git describe --tags
来描述相对于其最近的祖先标记的修订。因为我只是获取了我需要的特定版本,所以它无法做到这一点,因为它不知道我提交的任何祖先。
因此,我想编写一个简单的bash
脚本,以便根据需要加深历史:
GIT_HEAD=$(git rev-parse HEAD)
until git describe --tags
do
git fetch --deepen 100 origin $GIT_HEAD
done
这不起作用,因为,正如git-fetch
的文档所述:
-深度=从每个远程分支历史记录的尖端获取指定数量的提交。如果取到由git克隆创建的浅存储库(请参见git(1)),则深化或缩短历史到指定数量的提交。未获取深化提交的标记。
然后,我尝试使用git fetch --tags
来获取标记列表,这是可行的,但它也获取每个标记的提交数据。我正在使用的存储库具有大量的历史记录和大量标记,这会导致大量磁盘/网络/时间的使用(这就是为什么我首先使用一个浅克隆!)。
是否有一种方法可以使Git只为标记获取SHAs,以便在试图加深历史时将它们与存储库的修订列表相匹配?或者,我是否可以在获取与该深度相关的标记的同时,对存储库的历史进行浅浅提取?
发布于 2019-06-06 05:50:44
我能够通过使用一个稍微复杂一些的bash
脚本来完成这项工作。这个想法是,从一个浅的存储库开始,我迭代地加深历史,每次提交一小块,在每个块中查找我可以从远程获取的标记(使用git ls-remote --tags
获取标记参考列表,谢谢@ElpieKay的建议)。我重复这个过程,直到找到一些祖先标记,然后获取它们。
# Save the SHA that we're looking backward from.
GIT_HEAD=$(git rev-parse HEAD)
# Number of commits to grab at a time when deepening our commit history.
GIT_FETCH_CHUNK=250
# Loop until we have found some ancestor tags.
ANCESTOR_TAGS=""
while [ -z "$ANCESTOR_TAGS" ]; do
# Deepen the Git history by a chunk of commits to see if we can find a tag ancestor.
git fetch --deepen $GIT_FETCH_CHUNK origin $GIT_HEAD
# Get a list of remote tags and iterate over them.
while read line; do
# Tokenize the output, with the SHA in the first column and the tag name in the second.
TOKENS=($line)
# Check to see if our repository contains the specified SHA.
if git branch --contains ${TOKENS[0]} >/dev/null 2>&1; then
ANCESTOR_TAGS="$ANCESTOR_TAGS ${TOKENS[1]}:${TOKENS[1]}"
fi
done <<< "$(git ls-remote --tags)"
done
# Fetch the ancestor tags that we found.
git fetch origin --no-tags $ANCESTOR_TAGS
# Now, we can describe the current revision.
git describe --tags
https://stackoverflow.com/questions/56477321
复制相似问题