diff: add -I<regex> that ignores matching changes

Add a new diff option that enables ignoring changes whose all lines
(changed, removed, and added) match a given regular expression.  This is
similar to the -I/--ignore-matching-lines option in standalone diff
utilities and can be used e.g. to ignore changes which only affect code
comments or to look for unrelated changes in commits containing a large
number of automatically applied modifications (e.g. a tree-wide string
replacement).  The difference between -G/-S and the new -I option is
that the latter filters output on a per-change basis.

Use the 'ignore' field of xdchange_t for marking a change as ignored or
not.  Since the same field is used by --ignore-blank-lines, identical
hunk emitting rules apply for --ignore-blank-lines and -I.  These two
options can also be used together in the same git invocation (they are
complementary to each other).

Rename xdl_mark_ignorable() to xdl_mark_ignorable_lines(), to indicate
that it is logically a "sibling" of xdl_mark_ignorable_regex() rather
than its "parent".

Signed-off-by: Michał Kępień <michal@isc.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michał Kępień
2020-10-20 08:48:09 +02:00
committed by Junio C Hamano
parent ec7967cfaf
commit 296d4a94e7
7 changed files with 221 additions and 2 deletions

View File

@ -6,6 +6,7 @@
test_description='Various diff formatting options'
. ./test-lib.sh
. "$TEST_DIRECTORY"/diff-lib.sh
test_expect_success setup '
@ -309,6 +310,7 @@ log -SF master --max-count=2
log -GF master
log -GF -p master
log -GF -p --pickaxe-all master
log -IA -IB -I1 -I2 -p master
log --decorate --all
log --decorate=full --all
@ -449,4 +451,43 @@ test_expect_success 'diff-tree --stdin with log formatting' '
test_cmp expect actual
'
test_expect_success 'diff -I<regex>: setup' '
git checkout master &&
test_seq 50 >file0 &&
git commit -m "Set up -I<regex> test file" file0 &&
test_seq 50 | sed -e "s/13/ten and three/" -e "/7\$/d" >file0 &&
echo >>file0
'
test_expect_success 'diff -I<regex>' '
git diff --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
cat >expect <<-\EOF &&
diff --git a/file0 b/file0
--- a/file0
+++ b/file0
@@ -34,7 +31,6 @@
34
35
36
-37
38
39
40
EOF
compare_diff_patch expect actual
'
test_expect_success 'diff -I<regex> --stat' '
git diff --stat --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
cat >expect <<-\EOF &&
file0 | 1 -
1 file changed, 1 deletion(-)
EOF
test_cmp expect actual
'
test_expect_success 'diff -I<regex>: detect malformed regex' '
test_expect_code 129 git diff --ignore-matching-lines="^[124-9" 2>error &&
test_i18ngrep "invalid regex given to -I: " error
'
test_done