Gitblame命令查看每行的最新标签

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

我正在寻找类似

git blame
的内容,但我不是显示该行上次更改的提交,而是对该行提交的版本感兴趣。

因此,我需要提交时的最新标签,而不是修订 ID。

git tags command-line-interface blame git-blame
2个回答
1
投票

可以使用命令

$ git blame <file> | awk '{ cmd="git describe --always --tags --abbrev=0 $(echo "$1" | sed \"s/\\^//\") 2>/dev/null | tr -d \"\n\""; cmd | getline tag; $1=tag; close(cmd); print }'

此命令将 git-blame 输出中的第一列替换为

git describe --always --tags --abbrev=0 <rev>
的输出。第一列将包含三个可能值之一:

  • 如果提交历史记录中存在标签,第一列将是第一个祖先标签的名称。
  • 如果提交历史记录中不存在标签,第一列将是完整的提交 ID。

如果您想填充/截断第一列以具有固定数量的字符,您可以使用

$ git blame <file> | awk '{ cmd="git describe --always --tags --abbrev=0 $(echo "$1" | sed \"s/\\^//\") 2>/dev/null | tr -d \"\n\" | xargs -0 printf %10s | head -c10"; cmd | getline tag; $1=tag; close(cmd); print }'

添加

| xargs -0 printf %10s | head -c10"
以将第一列编组为恰好有 10 个字符。将两个
10
实例替换为您所需的宽度。

这是使用 CPython README 文件的输出示例

 v3.12.0b1 README.rst (Thomas Wouters 2023-05-22 21:15:32 +0200 1) This is Python version 3.13.0 alpha 0
 v3.11.0b1 README.rst (Pablo Galindo 2022-05-08 03:40:52 +0100 2) =====================================
    v1.6a1 README (Guido van Rossum 2000-04-11 17:11:09 +0000 3)
  v3.9.0a1 README.rst (Steve Dower 2019-12-16 11:15:08 -0800 4) .. image:: https://github.com/python/cpython/workflows/Tests/badge.svg
  v3.9.0a1 README.rst (Steve Dower 2019-12-16 11:15:08 -0800 5) :alt: CPython build status on GitHub Actions
  v3.9.0a1 README.rst (Steve Dower 2019-12-16 11:15:08 -0800 6) :target: https://github.com/python/cpython/actions
  v3.9.0a1 README.rst (Steve Dower 2019-12-16 11:15:08 -0800 7)
 v3.10.0a7 README.rst (Pablo Galindo 2021-05-03 23:36:55 +0100 8) .. image:: https://dev.azure.com/python/cpython/_apis/build/status/Azure%20Pipelines%20CI?branchName=main
  v3.7.0a4 README.rst (Steve Dower 2018-09-24 08:04:33 -0400 9) :alt: CPython build status on Azure DevOps
 v3.10.0a7 README.rst (Pablo Galindo 2021-05-03 23:36:55 +0100 10) :target: https://dev.azure.com/python/cpython/_build/latest?definitionId=4&branchName=main
  v3.7.0a4 README.rst (Steve Dower 2018-08-31 08:11:35 -0700 11)
 v3.10.0a6 README.rst (Erlend Egeberg Aasland 2021-04-02 05:30:40 +0200 12) .. image:: https://img.shields.io/badge/discourse-join_chat-brightgreen.svg
 v3.10.0a6 README.rst (Erlend Egeberg Aasland 2021-04-02 05:30:40 +0200 13) :alt: Python Discourse chat
 v3.10.0a6 README.rst (Erlend Egeberg Aasland 2021-04-02 05:30:40 +0200 14) :target: https://discuss.python.org/
  v3.7.0a4 README.rst (Steve Dower 2018-08-31 08:11:35 -0700 15)
  v3.7.0a4 README.rst (Mariatta 2018-06-29 13:43:45 -0700 16)
 v3.12.0a3 README.rst (Benjamin Peterson 2023-01-08 09:13:25 -0600 17) Copyright © 2001-2023 Python Software Foundation. All rights reserved.
      v2.4 README (Guido van Rossum 2007-08-30 17:16:55 +0000 18)
  v3.6.0b2 README.rst (Zachary Ware 2017-02-13 22:01:03 -0600 19) See the end of this file for further copyright and license information.

0
投票

我认为您可以将

git log
git describe

一起使用
© www.soinside.com 2019 - 2024. All rights reserved.