【问题标题】:How to get source line numbers in a specific revision of a file?如何在文件的特定版本中获取源代码行号?
【发布时间】:2019-05-28 15:45:39
【问题描述】:

可以看到 source/original 行号 git blame 但它根据 最后一次提交在这条线

我想对文件的特定提交/修订执行相同的操作。

例子,

文件:file.extxyz11 是我们当前正在审查的文件的修订/提交)

内容:

Line 1 (**abc11** is the last commit changed this line)
Line 2 (**abc12** is the last commit changed this line)
Line 3 (**abc13** is the last commit changed this line)

我想为 “第 3 行” 获取 “3”。 Git blame 将根据 行的提交 (abc13) 提交显示此信息。但是,由于 xyz11abc13 版本包含不同的内容,xyz11 中的实际行号可能不同。

那么如何获取文件特定版本的行号?

注意:我说的是“源/原始行号”因为即使文档很脏,我也想获得正确的行号(有未提交的更改)git blame是可能的

我的方案是,我将在 API 请求中使用这些行号来添加内联 cmets

所以,假设我修改了file.ext

Line 1
Line 2
Uncommited Line 
Uncommited Line
Line 3

对于“Line 3”,我应该仍然得到“3”,而不是“5”,否则评论会转到错误的行。正如我所说,git blame 是可能的,但它会根据行的提交显示此信息

谢谢

【问题讨论】:

    标签: git git-blame


    【解决方案1】:

    如果我理解正确,您有一个未提交更改的文件,您的 git blame 看起来像这样。

    $ git blame foo
    
    ^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
    ^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
    00000000 (Not Committed Yet  2019-01-01 12:58:04 -0800 3) Uncommitted Line
    00000000 (Not Committed Yet  2019-01-01 12:58:04 -0800 4) Uncommitted Line
    ^592c0a1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3
    

    使用-n 显示它在原始提交中的哪一行。

    $ git blame -n foo
    
    ^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
    ^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
    00000000 3 (Not Committed Yet  2019-01-01 12:58:47 -0800 3) Uncommitted Line
    00000000 4 (Not Committed Yet  2019-01-01 12:58:47 -0800 4) Uncommitted Line
    ^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3
    

    要忽略所有未提交和未暂存的更改,请使用git blame <file> HEADHEAD 是最后一次提交。这将从HEAD 向后查找文件的所有更改。因为介入的提交也会丢弃行号,所以您仍然希望-n 获得该提交中的行号。例如。

    $ git blame -n foo
    
    ^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
    ^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
    00000000 3 (Not Committed Yet  2019-01-01 13:03:06 -0800 3) Uncommitted line
    4a87d48f 3 (Michael G. Schwern 2019-01-01 13:02:32 -0800 4) Line 2.5
    ^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 5) Line 3
    
    $ git blame -n foo HEAD
    
    ^592c0a1 1 (Michael G. Schwern 2019-01-01 12:56:35 -0800 1) Line 1
    ^592c0a1 2 (Michael G. Schwern 2019-01-01 12:56:35 -0800 2) Line 2
    4a87d48f 3 (Michael G. Schwern 2019-01-01 13:02:32 -0800 3) Line 2.5
    ^592c0a1 3 (Michael G. Schwern 2019-01-01 12:56:35 -0800 4) Line 3
    

    【讨论】:

    • 这个(-n 参数)和@Josué Cortina 的答案似乎是我想要的。谢谢
    【解决方案2】:

    git blame 有一个选项 rev,因此,您可以指定要责备的提交/修订:

    git blame <rev> file
    

    示例

    git blame xyz11 file.txt
    

    更多信息在docs

    【讨论】:

    • 这似乎是我正在寻找的。谢谢
    • @user3790180,如果这对您有用,请将其标记为已接受(绿色复选标记✓)并投票!提前谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2011-01-07
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多