使用grep命令筛选出所有文件中特定行的git blame

11

我知道如何在文件中运行 gblame。

我知道如何在目录中所有文件中搜索内容。

我想看到包含某个内容的特定行周围的 gblame。例如:

$ blame -R "content" ./

我看到一个文件列表。我想要对它们进行gblame,了解是谁修改过这些行的代码。

3个回答

23

你可以使用Perl来完成它:

git grep -n 'content' | perl -F':' -anpe '$_=`git blame -L$F[1],+1 $F[0]`'

如果您想创建自定义命令,可以将类似以下内容添加到gitconfig的别名部分:

gb = "!f() { git grep -n $1 | perl -F':' -anpe '$_=`git blame -L$F[1],+1 $F[0]`'; }; f"

对我来说,第一个命令没有显示文件名。如何扩展该命令以显示文件名? - Arun
2
@Arun,你可以使用 -f / --show-name 选项来查看 blame 的文件名,所以命令应该是: git blame -fL$F[1],+1 $F[0] - Cash Lo
最终我使用了 git blame -lf $F[1],+1 $F[0] | | git name-rev --stdin --tags --always 命令来查看最接近的发布标签。 - Dan Berindei

0

查找有针头的文件,对每个文件进行指责,并对每个指责输出搜索针头

for file in `grep -lr needle *`; do  git blame $file |grep needle ; done

你可以使用-C参数添加上下文

for file in `grep -lr needle *`; do  git blame $file |grep -C5 needle ; done

在这种情况下,git grep 更可取。 - Edward Anderson

0
我写了这个小脚本来实现 git grep + blame:
#!/bin/bash

if [ "$1" = "" ] ; then
    echo "usage: $0 <term>" 1>&2
    exit 1
fi

for file in $(git grep $1 | cut -d ':' -f 1 | uniq) ; do
    echo $file ::
    git blame $file | grep $1
done 

我进行了修改,去掉了 #!/bin/bash,然后将其变成 .bash_profile 中可添加的形式:function ggb { 你的内容 }现在一切都如此完美。非常感谢你的分享。 - esaruoho

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接