用git blame
可以看到source/original行号但是显示的是last的行号在行中进行修改的提交
我想对文件的特定提交/修订执行相同的操作。
例子,
文件:file.ext
(xyz11 是我们当前正在审查的文件的修订/提交)
内容:
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" 的 "Line 3"。 Git blame 将根据行的提交 (abc13) 提交显示此信息。但是,由于 xyz11 和 abc13 修订包含不同的内容,因此 xyz11 中的实际行号可能不同。
那么如何获取文件特定修订版中的行号呢?
注意:我说“源/原始行号”因为我想获得正确的行号,即使文档是脏的(有未提交的更改)git blame
我的方案是,我将在 API 请求中使用这些行号来添加内嵌评论
所以,假设我修改了 file.ext
Line 1
Line 2
Uncommited Line
Uncommited Line
Line 3
对于 "Line 3" 我应该仍然是 "3" 而不是 "5",否则评论会转到错误的行。正如我所说,它可能与 git blame
但它根据行的提交显示此信息
谢谢
最佳答案
如果我没理解错的话,你有一个包含未提交更改的文件和你的 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> HEAD
. HEAD
是最后一次提交。这将查找来自 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
关于git - 如何在文件的特定修订版中获取源代码行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998810/