git 描述没有返回预期的标签

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

我正在尝试

git describe
,因为我们想在我们的 CI 系统中使用它。 但是,我没有得到预期的结果,git 描述文档和谷歌搜索都没有帮助我理解原因。

存储库

git log --oneline --graph

* 9a45651 (HEAD -> main) m c7
*   b7982a9 merged r3
|\  
| * 8c2adbe (tag: v3.0.0) r3 c1
|/  
* 273a50e m c6
*   6014487 merged r2
|\  
| * aa906b8 (tag: v2.0.0) r2 c2
| * 716c189 r2 c1
|/  
* f0e2c71 m c5
* d42bc37 m c4
*   9060516 merged r1
|\  
| * 619d331 (tag: v1.0.0) r1 c2
| * 75f90de r1 c1
* | 372f1fe m c3
|/  
* a27d5e4 m c2
* a66cb53 m c1

预期结果

当在

main
上运行 git 描述时,我期望

v3.0.0-x-d???????

实际结果

我得到的是

v2.0.0-13-g9a45651

更多信息

git 描述 --debug

# git describe --debug
describe HEAD
No exact match on refs or tags, searching to describe
 annotated         13 v2.0.0
 annotated         13 v1.0.0
 annotated         14 v3.0.0
traversed 15 commits
v2.0.0-13-g9a45651

标签和 HEAD 之间的提交量

$ git log --oneline v2.0.0..HEAD | wc -l
5

$ git log --oneline v3.0.0..HEAD | wc -l
2

git版本

我已经在

2.36.1
上测试了
2.37.0
Ubuntu 20.04.4 LTS
,两者都从
# deb-src http://ppa.launchpad.net/git-core/ppa/ubuntu focal main

安装

重新创建存储库的脚本

Git 邮件列表讨论

https://public-inbox.org/git/CABPp-BH2zuYe87xhjdp5v7M7i+EfEgLHAZgwfzJUAxGk1CFgfA@mail.gmail.com/#t

git git-describe
1个回答
0
投票

该问题根据 Git 邮件列表 讨论。 看起来 git-describe(1) 使用提交时间戳而不是拓扑 用于遍历。

这在这样的脚本中尤其明显; 稍后的线程 邮件列表讨论了提交时间戳如何只有粒度 下降到第二个。 提供的脚本中的所有提交都可以 发生在同一秒内。

我可以使用链接脚本重现[1]该问题(附录(1)):

v2.0.0-13-gf27d2de

这与OP相同:

v2.0.0-13-g

但是如果在每次提交之间

sleep 1.1
,我会得到正确的输出 (附录(2)):[2]

v3.0.0-2-g9c86941

但请注意,脚本最后提交

v3.0.0
。 你可以有一个 最近的可到达标签不是最年轻的提交的拓扑 (毕竟合并了)。 git-describe(1) 在“搜索策略”下说:

并且具有较新日期的标签将始终优先于具有以下日期的标签 较旧的日期

使
v3.0.0
成为最旧的标签,但仍然是拓扑最接近的标签

(见附录(3))

在这里,我们提交并在

v3.0.0
之上标记
m c2

$ git log --oneline v3.0.0
4b5622e (tag: v3.0.0, R3) r3 c1
271bd70 m c2
7318d3c m c1

但是我们在同一点合并它:

git log --oneline --graph main v3.0.0 ^HEAD^{'/m c6'}
* c73c074 (HEAD -> main) m c7
*   116be97 merged r3
|\
| * 4b5622e (tag: v3.0.0, R3) r3 c1
* ad7c002 r3 c1

所以就拓扑而言它仍然是最接近的标签。

全图:

* c73c074 (HEAD -> main) m c7
*   116be97 merged r3
|\
| * 4b5622e (tag: v3.0.0, R3) r3 c1
* | ad7c002 r3 c1
* | b195e0a m c6
* |   c37dd41 merged r2
|\ \
| * | 2b243b3 (tag: v2.0.0) r2 c2
| * | b8821ac r2 c1
|/ /
* | b3b9970 m c5
* | 62efd22 m c4
* |   9530928 merged r1
|\ \
| * | c9c6534 (tag: v1.0.0) r1 c2
| * | 08ddf98 r1 c1
| |/
* / 6934f36 m c3
|/
* 271bd70 m c2
* 7318d3c m c1

在这里我们再次找到

v2.0.0
,正如预期的那样:

$ git describe
v2.0.0-6-gc73c074

我不知道为什么计数是

6
。 看起来应该是
5
:

$ git log --graph --oneline v2.0.0^..main
* c73c074 (HEAD -> main) m c7
*   116be97 merged r3
|\
| * 4b5622e (tag: v3.0.0, R3) r3 c1
* ad7c002 r3 c1
* b195e0a m c6
* c37dd41 merged r2
* 2b243b3 (tag: v2.0.0) r2 c2

注释

  1. git version 2.47.0
  2. 由 Elijah Newren 在该链接讨论中建议

附录

(1) 原版复制品

来自

#!/bin/bash

set -x
set -e

mkdir sample-git-describe-original
cd sample-git-describe-original
git init

echo 1 >> m.txt && git add m.txt && git commit -m "m c1"
echo 2 >> m.txt && git add m.txt && git commit -m "m c2 "

git checkout -b R1
echo 1 >> r1.txt && git add r1.txt && git commit -m "r1 c1"

git checkout main
echo 3 >> m.txt && git add m.txt && git commit -m "m c3"

git checkout R1
echo 2 >> r1.txt && git add r1.txt && git commit -m "r1 c2"
git tag -a v1.0.0 -m "tagging v1.0.0"
git checkout main
git merge R1 --no-ff --commit -m "merged r1"
git branch -d R1

echo 4 >> m.txt && git add m.txt && git commit -m "m c4"

git checkout tags/v1.0.0 -b support-v1.0.1
echo 3 >> r1.txt && git add r1.txt && git commit -m "support-v1.0.1 c3"
git tag -a v1.0.1 -m "tagging v1.0.1"
git checkout main
git branch -D  support-v1.0.1

echo 5 >> m.txt && git add m.txt && git commit -m "m c5"

git checkout -b R2
echo 1 >> r2.txt && git add r2.txt && git commit -m "r2 c1"
echo 2 >> r2.txt && git add r2.txt && git commit -m "r2 c2"
git tag -a v2.0.0 -m "tagging v2.0.0"
git checkout main
git merge R2 --no-ff --commit -m "merged r2"
git branch -d R2

echo 6 >> m.txt && git add m.txt && git commit -m "m c6"


git checkout -b feature1
echo 1 >> f.txt && git add f.txt && git commit -m "f1 c1"

git checkout main
git checkout -b R3
echo 1 >> r3.txt && git add r3.txt && git commit -m "r3 c1"
git tag -a v3.0.0 -m "tagging v3.0.0"
git checkout main
git merge R3 --no-ff --commit -m "merged r3"

git branch -d R3
echo 7 >> m.txt && git add m.txt && git commit -m "m c7"

# Added
git describe

输出:

v2.0.0-13-gcbe140a

与 OP 相同的标签和计数:

v2.0.0-13-g

(2) 带有
sleep 1.1s

的脚本
mkdir sample-git-describe-sleep
cd sample-git-describe-sleep
git init

echo 1 >> m.txt && git add m.txt && git commit -m "m c1"
sleep 1.1s
echo 2 >> m.txt && git add m.txt && git commit -m "m c2 "
sleep 1.1s

git checkout -b R1
echo 1 >> r1.txt && git add r1.txt && git commit -m "r1 c1"
sleep 1.1s

git checkout main
echo 3 >> m.txt && git add m.txt && git commit -m "m c3"
sleep 1.1s

git checkout R1
echo 2 >> r1.txt && git add r1.txt && git commit -m "r1 c2"
sleep 1.1s
git tag -a v1.0.0 -m "tagging v1.0.0"
git checkout main
git merge R1 --no-ff --commit -m "merged r1"
sleep 1.1s
git branch -d R1

echo 4 >> m.txt && git add m.txt && git commit -m "m c4"
sleep 1.1s

git checkout tags/v1.0.0 -b support-v1.0.1
echo 3 >> r1.txt && git add r1.txt && git commit -m "support-v1.0.1 c3"
sleep 1.1s
git tag -a v1.0.1 -m "tagging v1.0.1"
git checkout main
git branch -D  support-v1.0.1

echo 5 >> m.txt && git add m.txt && git commit -m "m c5"
sleep 1.1s

git checkout -b R2
echo 1 >> r2.txt && git add r2.txt && git commit -m "r2 c1"
sleep 1.1s
echo 2 >> r2.txt && git add r2.txt && git commit -m "r2 c2"
sleep 1.1s
git tag -a v2.0.0 -m "tagging v2.0.0"
git checkout main
git merge R2 --no-ff --commit -m "merged r2"
sleep 1.1s
git branch -d R2

echo 6 >> m.txt && git add m.txt && git commit -m "m c6"
sleep 1.1s


git checkout -b feature1
echo 1 >> f.txt && git add f.txt && git commit -m "f1 c1"
sleep 1.1s

git checkout main
git checkout -b R3
echo 1 >> r3.txt && git add r3.txt && git commit -m "r3 c1"
sleep 1.1s
git tag -a v3.0.0 -m "tagging v3.0.0"
git checkout main
git merge R3 --no-ff --commit -m "merged r3"
sleep 1.1s

git branch -d R3
echo 7 >> m.txt && git add m.txt && git commit -m "m c7"

git describe
v3.0.0-2-g9c86941
  • 标签
    v3.0.0
  • 2

预计。

(3) 脚本,其中
v3.0.0
作为最旧的标记提交

#!/usr/bin/env bash

mkdir sample-git-describe-old-tag
cd sample-git-describe-old-tag
git init

echo 1 >> m.txt && git add m.txt && git commit -m "m c1"
sleep 1.1s
echo 2 >> m.txt && git add m.txt && git commit -m "m c2 "
sleep 1.1s

# Moved: make this the oldest tag
git checkout main
git checkout -b R3
echo 1 >> r3.txt && git add r3.txt && git commit -m "r3 c1"
sleep 1.1s
git tag -a v3.0.0 -m "tagging v3.0.0"
# Wait until later to merge

git checkout main
git checkout -b R1
echo 1 >> r1.txt && git add r1.txt && git commit -m "r1 c1"
sleep 1.1s

git checkout main
echo 3 >> m.txt && git add m.txt && git commit -m "m c3"
sleep 1.1s

git checkout R1
echo 2 >> r1.txt && git add r1.txt && git commit -m "r1 c2"
sleep 1.1s
git tag -a v1.0.0 -m "tagging v1.0.0"
git checkout main
git merge R1 --no-ff --commit -m "merged r1"
sleep 1.1s
git branch -d R1

echo 4 >> m.txt && git add m.txt && git commit -m "m c4"
sleep 1.1s

git checkout tags/v1.0.0 -b support-v1.0.1
echo 3 >> r1.txt && git add r1.txt && git commit -m "support-v1.0.1 c3"
sleep 1.1s
git tag -a v1.0.1 -m "tagging v1.0.1"
git checkout main
git branch -D  support-v1.0.1

echo 5 >> m.txt && git add m.txt && git commit -m "m c5"
sleep 1.1s

git checkout -b R2
echo 1 >> r2.txt && git add r2.txt && git commit -m "r2 c1"
sleep 1.1s
echo 2 >> r2.txt && git add r2.txt && git commit -m "r2 c2"
sleep 1.1s
git tag -a v2.0.0 -m "tagging v2.0.0"
git checkout main
git merge R2 --no-ff --commit -m "merged r2"
sleep 1.1s
git branch -d R2

echo 6 >> m.txt && git add m.txt && git commit -m "m c6"
sleep 1.1s


git checkout -b feature1
echo 1 >> f.txt && git add f.txt && git commit -m "f1 c1"
sleep 1.1s

git checkout main
git checkout -b R3
echo 1 >> r3.txt && git add r3.txt && git commit -m "r3 c1"
sleep 1.1s
git tag -a v3.0.0 -m "tagging v3.0.0"
git checkout main
git merge R3 --no-ff --commit -m "merged r3"
sleep 1.1s

# Finally merge in the old tag: still the same distance from HEAD as in
# the previous scripts
git checkout main
git merge R3 --no-ff --commit -m "merged r3"
sleep 1.1s
echo 7 >> m.txt && git add m.txt && git commit -m "m c7"
git describe
© www.soinside.com 2019 - 2024. All rights reserved.