Another way to achieve your goal would be to use an external diff driver, and the grepdiff
utility from the patchutils
package. (I answered a similar question for Linux here).
Caveat: I don't use Windows, and it seems patchutils
is not available in the usual Windows package managers. So a prerequisite to this answer would be to compile grepdiff
yourself (which might be too much :P). If you can do that, and find a way to install it inside Git Bash, then this approach might work.
First create a script under C:\Program Files\Git\usr\bin
and name it pickaxe-diff
(Note: again, I don't use Windows, but I'm reading on SO that this is an easy way to add stuff to your Git Bash PATH
) :
#!/bin/bash
# pickaxe-diff : external diff driver for Git.
# To be used with the pickaxe options (git [log|show|diff[.*] [-S|-G])
# to only show hunks containing the searched string/regex.
path=$1
old_file=$2
old_hex=$3
old_mode=$4
new_file=$5
new_hex=$6
new_mode=$7
filtered_diff=$(diff -u -p $old_file $new_file | \
grepdiff "$GREPDIFF_REGEX" --output-matching=hunk | \
grep -v -e '+++ ' -e '--- ')
a_path="a/$path"
b_path="b/$path"
echo "diff --git $a_path $b_path"
echo "index $old_hex..$new_hex $old_mode"
echo "--- $a_path"
echo "+++ $b_path"
echo "$filtered_diff"
Call git log -G
and tell Git to use the pickaxe-diff
script as an external diff driver:
export GREPDIFF_REGEX=<string>;
GIT_EXTERNAL_DIFF=pickaxe-diff git log -p --ext-diff -G $GREPDIFF_REGEX
This will use the pickaxe-diff script just to generate the diffs, so the rest of the git log
output (commit hash, message, etc) will be untouched.
Caveat
The way that the Git pickaxe work is that it limits the output to the files whose hunks change the given string/regex. This means that if another hunk in these files also contain the search string/regex, but does not change it, it will still be displayed with the above script. This is a limitation of grepdiff
(up to version 0.3.4). A recently merged pull request at the patchutils project added an --only-match
flag to grepdiff
, which should provide the needed functionality to correctly filter out these hunks (you would need patchutils 0.4.0 or higher).
I did a write-up of my solution in this gist.