Skip to content

Commit 1ae0949

Browse files
committed
Merge branch 'mk/diff-ignore-regex'
"git diff" family of commands learned the "-I<regex>" option to ignore hunks whose changed lines all match the given pattern. * mk/diff-ignore-regex: diff: add -I<regex> that ignores matching changes merge-base, xdiff: zero out xpparam_t structures
2 parents c23cd78 + 296d4a9 commit 1ae0949

File tree

10 files changed

+226
-2
lines changed

10 files changed

+226
-2
lines changed

Documentation/diff-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,11 @@ endif::git-format-patch[]
687687
--ignore-blank-lines::
688688
Ignore changes whose lines are all blank.
689689

690+
-I<regex>::
691+
--ignore-matching-lines=<regex>::
692+
Ignore changes whose all lines match <regex>. This option may
693+
be specified more than once.
694+
690695
--inter-hunk-context=<lines>::
691696
Show the context between diff hunks, up to the specified number
692697
of lines, thereby fusing hunks that are close to each other.

builtin/merge-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static void show_diff(struct merge_list *entry)
109109
xdemitconf_t xecfg;
110110
xdemitcb_t ecb;
111111

112+
memset(&xpp, 0, sizeof(xpp));
112113
xpp.flags = 0;
113114
memset(&xecfg, 0, sizeof(xecfg));
114115
xecfg.ctxlen = 3;

diff.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3587,6 +3587,8 @@ static void builtin_diff(const char *name_a,
35873587
if (header.len && !o->flags.suppress_diff_headers)
35883588
ecbdata.header = &header;
35893589
xpp.flags = o->xdl_opts;
3590+
xpp.ignore_regex = o->ignore_regex;
3591+
xpp.ignore_regex_nr = o->ignore_regex_nr;
35903592
xpp.anchors = o->anchors;
35913593
xpp.anchors_nr = o->anchors_nr;
35923594
xecfg.ctxlen = o->context;
@@ -3716,6 +3718,8 @@ static void builtin_diffstat(const char *name_a, const char *name_b,
37163718
memset(&xpp, 0, sizeof(xpp));
37173719
memset(&xecfg, 0, sizeof(xecfg));
37183720
xpp.flags = o->xdl_opts;
3721+
xpp.ignore_regex = o->ignore_regex;
3722+
xpp.ignore_regex_nr = o->ignore_regex_nr;
37193723
xpp.anchors = o->anchors;
37203724
xpp.anchors_nr = o->anchors_nr;
37213725
xecfg.ctxlen = o->context;
@@ -5203,6 +5207,22 @@ static int diff_opt_patience(const struct option *opt,
52035207
return 0;
52045208
}
52055209

5210+
static int diff_opt_ignore_regex(const struct option *opt,
5211+
const char *arg, int unset)
5212+
{
5213+
struct diff_options *options = opt->value;
5214+
regex_t *regex;
5215+
5216+
BUG_ON_OPT_NEG(unset);
5217+
regex = xmalloc(sizeof(*regex));
5218+
if (regcomp(regex, arg, REG_EXTENDED | REG_NEWLINE))
5219+
return error(_("invalid regex given to -I: '%s'"), arg);
5220+
ALLOC_GROW(options->ignore_regex, options->ignore_regex_nr + 1,
5221+
options->ignore_regex_alloc);
5222+
options->ignore_regex[options->ignore_regex_nr++] = regex;
5223+
return 0;
5224+
}
5225+
52065226
static int diff_opt_pickaxe_regex(const struct option *opt,
52075227
const char *arg, int unset)
52085228
{
@@ -5491,6 +5511,9 @@ static void prep_parse_options(struct diff_options *options)
54915511
OPT_BIT_F(0, "ignore-blank-lines", &options->xdl_opts,
54925512
N_("ignore changes whose lines are all blank"),
54935513
XDF_IGNORE_BLANK_LINES, PARSE_OPT_NONEG),
5514+
OPT_CALLBACK_F('I', "ignore-matching-lines", options, N_("<regex>"),
5515+
N_("ignore changes whose all lines match <regex>"),
5516+
0, diff_opt_ignore_regex),
54945517
OPT_BIT(0, "indent-heuristic", &options->xdl_opts,
54955518
N_("heuristic to shift diff hunk boundaries for easy reading"),
54965519
XDF_INDENT_HEURISTIC),

diff.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ struct diff_options {
234234
*/
235235
const char *pickaxe;
236236

237+
/* -I<regex> */
238+
regex_t **ignore_regex;
239+
size_t ignore_regex_nr, ignore_regex_alloc;
240+
237241
const char *single_follow;
238242
const char *a_prefix, *b_prefix;
239243
const char *line_prefix;

t/t4013-diff-various.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
test_description='Various diff formatting options'
77

88
. ./test-lib.sh
9+
. "$TEST_DIRECTORY"/diff-lib.sh
910

1011
test_expect_success setup '
1112
@@ -333,6 +334,7 @@ log -SF master --max-count=2
333334
log -GF master
334335
log -GF -p master
335336
log -GF -p --pickaxe-all master
337+
log -IA -IB -I1 -I2 -p master
336338
log --decorate --all
337339
log --decorate=full --all
338340
@@ -473,4 +475,43 @@ test_expect_success 'diff-tree --stdin with log formatting' '
473475
test_cmp expect actual
474476
'
475477

478+
test_expect_success 'diff -I<regex>: setup' '
479+
git checkout master &&
480+
test_seq 50 >file0 &&
481+
git commit -m "Set up -I<regex> test file" file0 &&
482+
test_seq 50 | sed -e "s/13/ten and three/" -e "/7\$/d" >file0 &&
483+
echo >>file0
484+
'
485+
test_expect_success 'diff -I<regex>' '
486+
git diff --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
487+
cat >expect <<-\EOF &&
488+
diff --git a/file0 b/file0
489+
--- a/file0
490+
+++ b/file0
491+
@@ -34,7 +31,6 @@
492+
34
493+
35
494+
36
495+
-37
496+
38
497+
39
498+
40
499+
EOF
500+
compare_diff_patch expect actual
501+
'
502+
503+
test_expect_success 'diff -I<regex> --stat' '
504+
git diff --stat --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
505+
cat >expect <<-\EOF &&
506+
file0 | 1 -
507+
1 file changed, 1 deletion(-)
508+
EOF
509+
test_cmp expect actual
510+
'
511+
512+
test_expect_success 'diff -I<regex>: detect malformed regex' '
513+
test_expect_code 129 git diff --ignore-matching-lines="^[124-9" 2>error &&
514+
test_i18ngrep "invalid regex given to -I: " error
515+
'
516+
476517
test_done
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
$ git log -IA -IB -I1 -I2 -p master
2+
commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
3+
Merge: 9a6d494 c7a2ab9
4+
Author: A U Thor <[email protected]>
5+
Date: Mon Jun 26 00:04:00 2006 +0000
6+
7+
Merge branch 'side'
8+
9+
commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
10+
Author: A U Thor <[email protected]>
11+
Date: Mon Jun 26 00:03:00 2006 +0000
12+
13+
Side
14+
15+
diff --git a/file0 b/file0
16+
index 01e79c3..f4615da 100644
17+
--- a/file0
18+
+++ b/file0
19+
@@ -1,3 +1,6 @@
20+
1
21+
2
22+
3
23+
+A
24+
+B
25+
+C
26+
diff --git a/file3 b/file3
27+
new file mode 100644
28+
index 0000000..7289e35
29+
30+
commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
31+
Author: A U Thor <[email protected]>
32+
Date: Mon Jun 26 00:02:00 2006 +0000
33+
34+
Third
35+
36+
diff --git a/dir/sub b/dir/sub
37+
index 8422d40..cead32e 100644
38+
--- a/dir/sub
39+
+++ b/dir/sub
40+
@@ -2,3 +2,5 @@ A
41+
B
42+
C
43+
D
44+
+E
45+
+F
46+
diff --git a/file1 b/file1
47+
new file mode 100644
48+
index 0000000..b1e6722
49+
--- /dev/null
50+
+++ b/file1
51+
@@ -0,0 +1,3 @@
52+
+A
53+
+B
54+
+C
55+
56+
commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
57+
Author: A U Thor <[email protected]>
58+
Date: Mon Jun 26 00:01:00 2006 +0000
59+
60+
Second
61+
62+
This is the second commit.
63+
64+
diff --git a/dir/sub b/dir/sub
65+
index 35d242b..8422d40 100644
66+
--- a/dir/sub
67+
+++ b/dir/sub
68+
@@ -1,2 +1,4 @@
69+
A
70+
B
71+
+C
72+
+D
73+
diff --git a/file0 b/file0
74+
index 01e79c3..b414108 100644
75+
--- a/file0
76+
+++ b/file0
77+
@@ -1,3 +1,6 @@
78+
1
79+
2
80+
3
81+
+4
82+
+5
83+
+6
84+
diff --git a/file2 b/file2
85+
deleted file mode 100644
86+
index 01e79c3..0000000
87+
--- a/file2
88+
+++ /dev/null
89+
@@ -1,3 +0,0 @@
90+
-1
91+
-2
92+
-3
93+
94+
commit 444ac553ac7612cc88969031b02b3767fb8a353a
95+
Author: A U Thor <[email protected]>
96+
Date: Mon Jun 26 00:00:00 2006 +0000
97+
98+
Initial
99+
$

xdiff/xdiff.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ typedef struct s_mmbuffer {
7979
typedef struct s_xpparam {
8080
unsigned long flags;
8181

82+
/* -I<regex> */
83+
regex_t **ignore_regex;
84+
size_t ignore_regex_nr;
85+
8286
/* See Documentation/diff-options.txt. */
8387
char **anchors;
8488
size_t anchors_nr;

xdiff/xdiffi.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
998998
return 0;
999999
}
10001000

1001-
static void xdl_mark_ignorable(xdchange_t *xscr, xdfenv_t *xe, long flags)
1001+
static void xdl_mark_ignorable_lines(xdchange_t *xscr, xdfenv_t *xe, long flags)
10021002
{
10031003
xdchange_t *xch;
10041004

@@ -1019,6 +1019,46 @@ static void xdl_mark_ignorable(xdchange_t *xscr, xdfenv_t *xe, long flags)
10191019
}
10201020
}
10211021

1022+
static int record_matches_regex(xrecord_t *rec, xpparam_t const *xpp) {
1023+
regmatch_t regmatch;
1024+
int i;
1025+
1026+
for (i = 0; i < xpp->ignore_regex_nr; i++)
1027+
if (!regexec_buf(xpp->ignore_regex[i], rec->ptr, rec->size, 1,
1028+
&regmatch, 0))
1029+
return 1;
1030+
1031+
return 0;
1032+
}
1033+
1034+
static void xdl_mark_ignorable_regex(xdchange_t *xscr, const xdfenv_t *xe,
1035+
xpparam_t const *xpp)
1036+
{
1037+
xdchange_t *xch;
1038+
1039+
for (xch = xscr; xch; xch = xch->next) {
1040+
xrecord_t **rec;
1041+
int ignore = 1;
1042+
long i;
1043+
1044+
/*
1045+
* Do not override --ignore-blank-lines.
1046+
*/
1047+
if (xch->ignore)
1048+
continue;
1049+
1050+
rec = &xe->xdf1.recs[xch->i1];
1051+
for (i = 0; i < xch->chg1 && ignore; i++)
1052+
ignore = record_matches_regex(rec[i], xpp);
1053+
1054+
rec = &xe->xdf2.recs[xch->i2];
1055+
for (i = 0; i < xch->chg2 && ignore; i++)
1056+
ignore = record_matches_regex(rec[i], xpp);
1057+
1058+
xch->ignore = ignore;
1059+
}
1060+
}
1061+
10221062
int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
10231063
xdemitconf_t const *xecfg, xdemitcb_t *ecb) {
10241064
xdchange_t *xscr;
@@ -1038,7 +1078,10 @@ int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp,
10381078
}
10391079
if (xscr) {
10401080
if (xpp->flags & XDF_IGNORE_BLANK_LINES)
1041-
xdl_mark_ignorable(xscr, &xe, xpp->flags);
1081+
xdl_mark_ignorable_lines(xscr, &xe, xpp->flags);
1082+
1083+
if (xpp->ignore_regex)
1084+
xdl_mark_ignorable_regex(xscr, &xe, xpp);
10421085

10431086
if (ef(&xe, xscr, ecb, xecfg) < 0) {
10441087

xdiff/xhistogram.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env,
235235
int line1, int count1, int line2, int count2)
236236
{
237237
xpparam_t xpparam;
238+
239+
memset(&xpparam, 0, sizeof(xpparam));
238240
xpparam.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
239241

240242
return xdl_fall_back_diff(env, &xpparam,

xdiff/xpatience.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ static int fall_back_to_classic_diff(struct hashmap *map,
318318
int line1, int count1, int line2, int count2)
319319
{
320320
xpparam_t xpp;
321+
322+
memset(&xpp, 0, sizeof(xpp));
321323
xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
322324

323325
return xdl_fall_back_diff(map->env, &xpp,

0 commit comments

Comments
 (0)