有没有办法告诉 git 给我在文件中移动一行的提交?

Is there a way to tell git to give me the commit which moved a line in a file?

我有一个显示堆栈跟踪的日志。堆栈跟踪中的每一行都显示调用的函数、文件名和行号。

事实是,这些与预期的构建不一致。所以,我需要知道函数是什么时候从当前位置移动到过去的位置的。

例如

System.NullReferenceException: Object reference not set to an instance of an object.
at ns1.ns2.cn.fn2(t1 p1, t2 p2) in c:\path\file2.cs:line 456
at ns1.ns2.cn.fn1(t1 p1, t2 p2) in c:\path\file1.cs:line 123
...

在这种情况下,过去调用了 ns1.ns2.cn.fn1(t1 p1, t2 p2),它调用了文件 c:\path\file1.cs 中第 123 行的另一个函数 ns1.ns2.cn.fn2(t1 p1, t2 p2)

但是,目前,该行在行 223 上,比行低 100 行。

有没有办法让 git 告诉我什么时候在 223 之前添加的行将其向下移动了 100 行?

我想出的最快方法是制作一个脚本来查看文件的所有不同版本:

$  export file="path_from_repo_root/file1.cs";\
git log --pretty=short -u -L 1,99999999:"$file"|perl -e '
use warnings;
use strict;
my ($linesAdded, $linesDeleted) = (0, 0);
while (<>) {
 if (/^commit ([a-f0-9]+)/ and !eof()) {
   my $linesDiff = ($linesAdded - $linesDeleted);
   print;  # prints commit hash
   if ($linesDiff) {
     system("git show :$ENV{file} | grep -n --color=always fn2");
   }
   ($linesAdded, $linesDeleted) = (0, 0);
 } elsif (/^-/ ) {
   ++$linesDeleted;
 } elsif (/^\+/) {
   ++$linesAdded;
 }
}' | less -R

如果文件中的行数发生变化,它将吐出所有提交到文件的哈希值和函数在文件中的位置。

请注意,这不是一个完美的解决方案,但足以找到我需要的东西。此外,99999999 可以更改为函数当前所在的行(我认为),但我不确定,所以我基本上只是说要查看整个文件。