当以深度 = 1 克隆存储库时获取最新标签 `git describe --tags'

问题描述 投票:0回答:6

我在 CI 自动化工作流程期间读取 git 存储库的标签时遇到问题。我不想创建完整克隆,因为这会产生大量开销,因此更愿意维护“浅克隆”,但以某种方式确定应用程序版本控制的标签。

用例

  1. Github Actions CI 构建通过设置
    git clone ... --depth=1
    将 Git 存储库检查为“浅”克隆。
  2. 预构建阶段执行
    git describe --tags
    将版本信息嵌入到已编译的应用程序中

预期结果

使用完整克隆,标签将报告如下:

> git describe --tags
v0.5.0-95-g7bbc323

实际结果

CI下的浅克隆效果不一样:

> git describe --tags
fatal: No names found, cannot describe anything.

解决方案想法

  • 修改 CI 下的“克隆”以某种方式包含标签?
  • 如果可能的话,修改预构建的“desribe”步骤以从远程读取标签?
git github github-actions git-clone
6个回答
3
投票

结合此线程上的答案,这就是对我有用的结果:
(我在 $BRANCH_NAME 环境变量中有分支的名称)

TAG=$(git ls-remote remote "refs/tags/v*[0-9]" | cut -f 2- | sort -V | tail -1)
# fetch all commits up to but excluding the TAG
git fetch --filter=tree:0 --shallow-exclude $TAG origin $BRANCH_NAME
# extend by one
git fetch --filter=tree:0 --deepen=1 origin $BRANCH_NAME
# get the tag itself
git fetch --filter=tree:0 --depth=1 origin $TAG:$TAG

1
投票

你不能这样做。为了让

git describe
工作,您必须拥有标签和它们指向的提交,并且能够向后遍历历史记录,找到历史记录中最近的标签。当您使用
--depth=1
进行克隆时,您仅克隆了单个提交,因此无法进行遍历。

但是,您可以使用

--filter=tree:0
进行部分克隆,然后您只有标签和提交,直到您进行签出,此时将从服务器填充仅该提交的 blob 和树。但是,我不确定 GitHub Actions 是否原生支持此功能,因此您可能需要自己完成。


1
投票

你可以很容易地做到这一点,获得这样的历史切片只需要知道该走到哪里,可以这么说:

对于支持过滤的托管服务器(所有大型服务器都支持):

git fetch --filter=tree:0 origin +refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*

否则,请自行 cd 到上游存储库,并且

git bundle create just-the-structure.bundle --filter=tree:0 --tags --branches

然后将该文件复制到您的目标系统和目标存储库中

git bundle unbundle just-the-structure.bundle

1
投票

对我来说,我只是做了以下事情:

      - name: Get latest tag
    run: |
      git fetch -a
      latest_tag=$(git describe --tags `git rev-list --tags --max-count=1`)

希望这有帮助! :)


0
投票

您还可以在遥控器上查找,并从那里获取最新的标签。然后当你拥有它时,你只能获取那个。

tag=$(git ls-remote origin "refs/tags/v*[0-9]" | cut -f 2- | sort -V | tail -1)
git fetch --depth 1 origin "$tag":"$tag"

上面的脚本应该只获取最新的标签(假设你以 v 开头并以数字结尾。

布乌特。遗憾的是,它无法解决

git describe
问题,因为您丢失了此标签(您拥有其所有数据)和浅层提交(即您的
main
)之间的所有提交。

所以我现在只是用 YOLO 来标记它,当然我们应该比这更频繁地标记。

    


0
投票

git clone --depth 1024

© www.soinside.com 2019 - 2024. All rights reserved.