是否有可能看到谁在git blame报告的提交之前编辑了特定的行,就像一个给定行的提交历史?

例如,我运行以下(在优秀的uncrustify项目上):

$ git blame -L10,+1 src/options.cpp
^fe25b6d (Ben Gardner 2009-10-17 13:13:55 -0500 10) #include "prototypes.h"

我如何才能找到谁在提交fe25b6d之前编辑了这一行?在提交之前是谁编辑的?


当前回答

基于stangls的答案,我把这个脚本放在我的PATH(甚至在Windows上)作为git-bh:

这允许我查找所有涉及到一个词的提交:

git bh path/to/myfile myWord

脚本:

#!/bin/bash
f=$1
shift
csha=""
{ git log --pretty=format:%H -- "$f"; echo; } | {
  while read hash; do
    res=$(git blame -L"/$1/",+1 $hash -- "$f" 2>/dev/null | sed 's/^/  /')
    sha=${res%% (*}
    if [[ "${res}" != "" && "${csha}" != "${sha}" ]]; then
      echo "--- ${hash}"
      echo "${res}"
      csha="${sha}"
    fi
  done
}

其他回答

基于stangls的答案,我把这个脚本放在我的PATH(甚至在Windows上)作为git-bh:

这允许我查找所有涉及到一个词的提交:

git bh path/to/myfile myWord

脚本:

#!/bin/bash
f=$1
shift
csha=""
{ git log --pretty=format:%H -- "$f"; echo; } | {
  while read hash; do
    res=$(git blame -L"/$1/",+1 $hash -- "$f" 2>/dev/null | sed 's/^/  /')
    sha=${res%% (*}
    if [[ "${res}" != "" && "${csha}" != "${sha}" ]]; then
      echo "--- ${hash}"
      echo "${res}"
      csha="${sha}"
    fi
  done
}
git blame -L 10,+1 fe25b6d^ -- src/options.cpp

你可以为git blame指定一个修订,从(而不是默认的HEAD)开始回溯;Fe25b6d ^是Fe25b6d的父元素。


编辑:作为Git 2.23的新版本,我们在Git blame中添加了——ignore-rev选项:

git blame --ignore-rev fe25b6d

虽然这不能回答OP给出提交堆栈的问题(根据其他答案,您将使用git log),但这是该解决方案的一种更好的方式,因为您不会对其他行产生误解。

基于Will Shepard的回答,他的输出将包含没有更改的提交的重复行,因此您可以按照以下方式过滤这些行(使用这个答案)

LINE=1 FILE=a; for commit in $(git rev-list HEAD $FILE); do git blame -n -L$LINE,+1 $commit -- $FILE; done | sed '$!N; /^\(.*\)\n\1$/!P; D'

注意,我删除了REVS参数,这将返回到根提交。这是由于Max Nanasy上面的观察。

对于这个问题,一个非常独特的解决方案是使用git log,正如Andre在这里解释的那样:

git log -p -M——follow——stat——path/to/your/file

在前面的回答的基础上,这个bash一行代码应该可以提供您所要查找的内容。它显示了特定文件的特定行,通过最近5个修订的git责备历史:

LINE=10 FILE=src/options.cpp REVS=5; for commit in $(git rev-list -n $REVS HEAD $FILE); do git blame -n -L$LINE,+1 $commit -- $FILE; done

在这个命令的输出中,对于特定的提交,您可能会看到行内容发生变化,或者显示的行号甚至可能发生变化。

这通常表明这一行是在特定的提交之后第一次添加的。它还可以指示该行从文件的另一部分移动。