5

我想使用搜索特定字符串,git grep但我想通过使用来过滤结果,git blame以知道我正在寻找的字符串已被特定人更改。

我只是不知道如何将它们结合起来以获得我想要的结果。

感谢您的帮助。

4

2 回答 2

3

您可以编写一个小 shell 脚本来完成此操作:

git rev-list --author=Doe HEAD |
while read rev; do
    if git show -p $rev | grep "PATTERN" >/dev/null; then
        echo $rev
    fi
done

这将输出 HEAD 可访问的 SHA,其作者为“Doe”并且在提交内容中具有“PATTERN”。

于 2015-12-03T01:11:35.607 回答
0

这应该做你想要的。

author="Some User"
searchstring=string
searchfiles=(file1 file2 file3) # Leave empty for all files.

while IFS= read -rd '' file; read -rd '' nr; read -r line; do
    if git annotate -p -L "$nr,$nr" -- "$file" | grep -q "$author"; then
        printf '%s:%s:%s\n' "$file" "$nr" "$line"
    fi
done < <(git grep -nz "$searchstring" -- "${searchfiles[@]}")

这是否比 Jonathan.Brink 的答案更好/更快取决于该行的匹配量、历史记录的大小、作者的提交在历史记录中的位置、更改是最近还是最近作者的提交等

用于git grep -z对任意文件名安全并read -d ''读取那些NUL- 分隔的字段。

用于git annotate -L限制需要注释的行。

输出是原始git grep -n输出,但仅适用于作者匹配行。

于 2015-12-03T17:43:50.640 回答