代码之家  ›  专栏  ›  技术社区  ›  tangoal

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

git
  •  9
  • tangoal  · 技术社区  · 7 年前
    • 我掌握哪些信息: 上的文件名(myfile.txt) 主分支。上次使用提交“3”修改了该文件 生成版本2。

    • 我要检索的内容: 标签名“0.0.2”,包括 提交最后一个标签“0.0.1”之后的“3”和“4”。

    enter image description here

    • 我所知道的:

    (a) 如何在两个标记之间获取更改的文件(请参见 here ):

    git diff --name-only 0.0.1 0.0.2
    

    这将打印“myfile.txt”等。

    (b) 正常情况下,这应该正好符合我的需要(见 here ):

    git describe --always `git log --pretty=format:%H -n 1 myfile.txt`
    

    但是我没有得到标记名“0.0.2”或与此标记相关的提交。相反,我得到commit 3的commit sha-1,其中包括最新的 myfile.txt的更改。

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

    git for-each-ref --format='%(refname) %(objecttype)' refs/tags
    

    打印内容:

    refs/tags/0.0.1 tag
    refs/tags/0.0.2 tag
    refs/tags/0.0.3 tag
    refs/tags/0.0.4 tag
    

    问题

    所以我的问题是:这条路是否适合我的目的?如果是,如何更改它以获得所需的标记名?或者有没有其他方法来获得我需要的东西?

    1 回复  |  直到 7 年前
        1
  •  6
  •   chuckx    7 年前

    DR

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

    文档

    来自 git-describe documentation :

    --always
    Show uniquely abbreviated commit object as fallback.
    

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

    --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.
    

    这就更接近了,因为它允许使用所有标记,不管是否加了注释。但是,它仍然受制于查找标记的默认行为 之前 最后一次提交。

    --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.
    

    答对了。

    示例用法

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

    $ 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
    

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

    只运行 git 命令生成此输出:

    $ git describe --contains `git log --pretty=format:%H -n 1 test.txt`
    v0.0.3~1
    

    这个 ~1 指示对文件的最后一次更改是在提供的标记后面提交1次。管道连接至 sed 为了完整性起见,单独获取标记。

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