简体   繁体   English

以 depth=1 克隆 repo 时获取最新标签 `git describe --tags'

[英]Get latest tag `git describe --tags' when repo is cloned with depth=1

I have an issue reading the tags of a git repository during the CI automated workflow.在 CI 自动化工作流程期间,我在读取 git 存储库的标签时遇到问题。 I do not want to create a full-clone as this incurs extensive overhead so would prefer to maintain a 'shallow clone' but somehow determine the tag for application versioning.我不想创建完整克隆,因为这会产生大量开销,因此更愿意维护“浅克隆”,但以某种方式确定应用程序版本控制的标签。

Use Case用例

  1. Github Actions CI build checks out the Git-Repository as a 'shallow' clone by setting git clone... --depth=1 . Github Actions CI 构建通过设置git clone... --depth=1将 Git-Repository 检出为“浅”克隆。
  2. The pre-build stage performs git describe --tags to embed version-information into the compiled application预构建阶段执行git describe --tags将版本信息嵌入到已编译的应用程序中

Expected outcome预期结果

With full-clone the tag would be reported as follows:使用完整克隆,标签将报告如下:

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

Actual outcome实际结果

The shallow-clone under CI does not work the same: CI下的浅克隆不一样:

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

Solution Ideas解决方案思路

  • Modify the 'clone' under the CI to include tags somehow?修改 CI 下的“克隆”以以某种方式包含标签?
  • Modify the pre-build 'desribe' step to read the tags from the remote if this were possible?如果可能,修改预构建“desribe”步骤以从远程读取标签?

You cannot do this.你不能做这个。 In order to have git describe work, you have to have the tags and the commits to which they point, and be able to walk backwards through history, finding the nearest tag in history.为了让git describe工作,您必须拥有它们指向的标签和提交,并且能够在历史中倒退,找到历史上最近的标签。 When you've cloned with --depth=1 , you've cloned only a single commit, so traversal is not possible.当您使用--depth=1进行克隆时,您只克隆了一个提交,因此无法进行遍历。

However, you could do a partial clone with --filter=tree:0 and then you'd have only tags and commits until you did a checkout, when the blobs and trees for only that commit would be filled in from the server.但是,您可以使用--filter=tree:0进行部分克隆,然后您将只有标签和提交,直到您进行结帐,此时仅该提交的 blob 和树将从服务器填充。 However, I'm not sure if GitHub Actions supports this natively, so you may have to do it yourself.但是,我不确定 GitHub Actions 是否本机支持此功能,因此您可能必须自己做。

You could also look up on the remote, and get the latest tag from there.您也可以在遥控器上查找,并从那里获取最新标签。 Then when you have it, you can fetch only that one.然后,当您拥有它时,您只能获取那个。

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

The above script should only fetch the latest tag (given that you start them with v and ends with a number.上面的脚本应该只获取最新的标签(假设你以 v 开头并以数字结尾。

Buuut.布特。 Sadly it doesn't fix the git describe problem, since you're missing all the commits between this tag (which you have all data for) and the shallow commit that's your main .可悲的是,它不能解决git describe问题,因为您缺少此标记(您拥有所有数据)和作为您的main .

So I just YOLO'd it with git clone --depth 1024 now, surely we should tag much more often than that.所以我现在只是用git clone --depth 1024对它进行 YOLO 处理,当然我们应该更频繁地标记它。

You can do this fairly easily, getting a sliced-up history like this just requires knowing where to step, so to speak:你可以很容易地做到这一点,获得这样的切片历史只需要知道从哪里开始,可以这么说:

For a hosting server that supports filtering (all the big ones do):对于支持过滤的托管服务器(所有大的都可以):

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

Otherwise, cd in to the upstream repo yourself, and否则,自己 cd 进入上游仓库,然后

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

then copy that file to your destination system, and in your target repo然后将该文件复制到您的目标系统,并在您的目标仓库中

git bundle unbundle just-the-structure.bundle

For me I just did the following:对我来说,我只是做了以下事情:

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

Hope this helps: :)希望这可以帮助: :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM