获取与特定分支上文件的上次更改相关的标记名称

tan*_*oal 9 git

  • 我有哪些信息:主分支上的文件名(myfile.txt).上次使用提交"3"修改了文件,导致版本2.

  • 我想要检索的内容:标签名称"0.0.2",其中包括自上一个标签"0.0.1"以来的提交"3"和"4".

在此输入图像描述

  • 我知道的:

(A)如何在2个标签之间获取更改的文件(参见此处):

git diff --name-only 0.0.1 0.0.2
Run Code Online (Sandbox Code Playgroud)

这会打印'myfile.txt'等.

(B)通常应该完全符合我的需要(见这里):

git describe --always `git log --pretty=format:%H -n 1 myfile.txt`
Run Code Online (Sandbox Code Playgroud)

但是我没有得到标签名称'0.0.2'或与此标签相关的提交.相反,我得到了提交3的提交SHA-1,其中包含myfile.txt的最新更改.

(C)如果以下命令打印相应的标记名称,则注释标记:

git for-each-ref --format='%(refname) %(objecttype)' refs/tags
Run Code Online (Sandbox Code Playgroud)

打印:

refs/tags/0.0.1 tag
refs/tags/0.0.2 tag
refs/tags/0.0.3 tag
refs/tags/0.0.4 tag
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:(B)对我来说是正确的方式吗?如果是,我如何更改它确实获得所需的标签名称?还是有另一种方式而不是(B)得到我需要的东西?

chu*_*ckx 6

TL;博士

git describe --contains `git log --pretty=format:%H -n 1 myfile.txt` | sed 's/\(.*\)[~^].*/\1/'
Run Code Online (Sandbox Code Playgroud)

文档

git-describe文档中的相关标志描述:

--always
Show uniquely abbreviated commit object as fallback.
Run Code Online (Sandbox Code Playgroud)

这不是你想要的.这允许打印提交哈希而不是标记.

--tags
Instead of using only the annotated tags, use any tag found in refs/tags
namespace. This option enables matching a lightweight (non-annotated) tag.
Run Code Online (Sandbox Code Playgroud)

这更接近,因为它允许使用所有标签,注释与否.但是,它仍然受到在上次提交之前查找标记的默认行为的影响.

--contains
Instead of finding the tag that predates the commit, find the tag that comes
after the commit, and thus contains it. Automatically implies --tags.
Run Code Online (Sandbox Code Playgroud)

答对了.

示例用法

给定具有以下提交历史记录的存储库:

$ git log --decorate=short -p | grep -v Author
commit d79ae00046a3ce456316fb431af5c4473a9868c8 (HEAD -> master, tag: v0.0.3)
Date:   Mon May 28 22:54:33 2018 -0700

    Commit #5

diff --git a/foo.txt b/foo.txt
index 257cc56..3bd1f0e 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,2 @@
 foo
+bar

commit 7921bbcd4bb0712e4b819231829bed5a857f99a5
Date:   Mon May 28 22:54:11 2018 -0700

    Commit #4

diff --git a/test.txt b/test.txt
index 7698346..fadbf1d 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 test1
 test2
 test3
+test4

commit fbe5a73bc2b5edcd3cb7afa26b80f8ecb12f982d (tag: v0.0.2)
Date:   Mon May 28 22:53:28 2018 -0700

    Commit #3

diff --git a/test.txt b/test.txt
index bae42c5..7698346 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 test1
 test2
+test3

commit 794519596d9e2de93ec71686a1708e5f81fbba21
Date:   Mon May 28 22:52:51 2018 -0700

    Commit #2

diff --git a/test.txt b/test.txt
index a5bce3f..bae42c5 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 test1
+test2

commit 10f854c9c09ac6c4de10311ffb5809f09a1edd1a (tag: v0.0.1)
Date:   Mon May 28 22:52:00 2018 -0700

    Commit #1

diff --git a/foo.txt b/foo.txt
new file mode 100644
index 0000000..257cc56
--- /dev/null
+++ b/foo.txt
@@ -0,0 +1 @@
+foo
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..a5bce3f
--- /dev/null
+++ b/test.txt
@@ -0,0 +1 @@
+test1
Run Code Online (Sandbox Code Playgroud)

请注意,文件test.txt在提交#1-4中编辑,但不在#5中编辑.提交#5被标记为v0.0.3,这是我们想要的输出.

只运行git命令会产生以下输出:

$ git describe --contains `git log --pretty=format:%H -n 1 test.txt`
v0.0.3~1
Run Code Online (Sandbox Code Playgroud)

~1表明在文件的最后一个变化是1承诺提供的标签后面.sed为了完整起见,使用管道来自行获取标签.

$ git describe --contains `git log --pretty=format:%H -n 1 test.txt` | sed 's/\(.*\)[~^].*/\1/'
v0.0.3
Run Code Online (Sandbox Code Playgroud)