How to grep the git diff?

你。 提交于 2019-12-03 04:25:49

问题


Is there a way to show the git-diff filtered by a given pattern.

Something like

git grepdiff pattern

changed file
+++ some sentence with pattern
changed file 2
--- some other pattern

Unfortunately the simplest solution is not good enough

git diff | grep pattern 

+++ some sentence with pattern
--- some other pattern
# not an option as doesn't put the filename close to the match

I came with a workaround using awk

git diff | awk "/\+\+\+/{f = \$2}; /PATTERN/ {print f \$0} "

But would love to find out that there is a command for this.


回答1:


Not sure but isn't git diff -G <regex> flag OK?

-G < regex>

Look for differences whose added or removed line matches the given <regex>.



回答2:


Another possibility would be to view the whole diff and search the output using the normal less commands (type / and then the pattern).

When you have less configured to show some lines before the match using --jump-target=N, this is pretty useful. Try it like this:

PAGER="/usr/bin/less --jump-target=10" git diff

This means that the match should be shown on line 10 (shows 9 lines of context above), which may be enough to also see the file name.

You can also use e.g. --jump-target=.5 to make it position the match in the middle of the screen.




回答3:


Have you tried git diff -S<string> or git diff -G".*string.*"? Note that they are not equivalent, see the documentation about pickaxe for what -S does.




回答4:


I use git log -p, which opens less (configurable, though), which in turn can be searched for with /. There's also git log -S <searchword>.




回答5:


I think your approach to "grep" diff output is the best workaround.

You may improve your awk script by using sed:

colored="(^[\[[0-9;]*[a-zA-Z])"
marker="^$colored+diff"
pattern="^$colored+.*(\+|\-).*PATTERN"
git diff --color | sed -rn -e "/$marker/! H; /$marker/ ba; $ ba; b; :a; x; /$pattern/ p"
  • colored: regex to match terminal colored lines
  • marker: marker to match division from differents diff hunks, lines starting with colored "diff"
  • pattern: pattern to search for, lines starting with colored "+" or "-" and containing "PATTERN"

This will print full diff hunks, with added or removed PATTERN, also maintaining useful colored output.

Note that ^[ in colored should be actual, literal ^[. You can type them in bash by pressing Ctrl + V, Ctrl + [




回答6:


Here is a custom diff tool that allows grepping inside changes (but not the context):

Usage

GIT_EXTERNAL_DIFF="mydiff --grep foo" git diff

This will output those lines in your changes that contain foo (including lines where foo disappeared because of your changes). Any grep pattern can be used instead of foo.

Each output line starts with the following prefix:

filename: oldlinenum: newlinenum|

The script can also be used without the --grep option, in which case it simply formats the full diff (i.e. providing full context) as described above.

mydiff

#!/bin/bash

my_diff()
{
    diff --old-line-format="$1"':%6dn:      |-%L'     \
         --new-line-format="$1"':      :%6dn|+%L'     \
         --unchanged-line-format="$1"':%6dn:%6dn| %L' \
         $2 $3
}

if [[ $1 == '--grep' ]]
then
    pattern="$2"
    shift 2
    my_diff "$1" "$2" "$5"|grep --color=never '^[^|]\+|[-+].\+'"$pattern"'.*'
else
    my_diff "$1" "$2" "$5"
fi

exit 0



回答7:


On Windows, a simple solution is:

git diff -U0 | findstr string

If you want grouping by filename, use this

FOR /F "usebackq delims==" %i IN (`git diff --name-only`) do git diff -U0 %~fi | findstr string



回答8:


This did the job for me, I hope it will help someone:

git diff | grep  -P '^\+|^\-'


来源:https://stackoverflow.com/questions/12462538/how-to-grep-the-git-diff

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!