2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #include "xdiff-interface.h"
11 typedef int (*pickaxe_fn
)(mmfile_t
*one
, mmfile_t
*two
,
12 struct diff_options
*o
,
13 regex_t
*regexp
, kwset_t kws
);
20 static void diffgrep_consume(void *priv
, char *line
, unsigned long len
)
22 struct diffgrep_cb
*data
= priv
;
26 if (line
[0] != '+' && line
[0] != '-')
30 * NEEDSWORK: we should have a way to terminate the
34 /* Yuck -- line ought to be "const char *"! */
37 data
->hit
= !regexec(data
->regexp
, line
+ 1, 1, ®match
, 0);
41 static int diff_grep(mmfile_t
*one
, mmfile_t
*two
,
42 struct diff_options
*o
,
43 regex_t
*regexp
, kwset_t kws
)
46 struct diffgrep_cb ecbdata
;
51 return !regexec(regexp
, two
->ptr
, 1, ®match
, 0);
53 return !regexec(regexp
, one
->ptr
, 1, ®match
, 0);
56 * We have both sides; need to run textual diff and see if
57 * the pattern appears on added/deleted lines.
59 memset(&xpp
, 0, sizeof(xpp
));
60 memset(&xecfg
, 0, sizeof(xecfg
));
61 ecbdata
.regexp
= regexp
;
63 xecfg
.ctxlen
= o
->context
;
64 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
65 if (xdi_diff_outf(one
, two
, diffgrep_consume
, &ecbdata
, &xpp
, &xecfg
))
70 static unsigned int contains(mmfile_t
*mf
, regex_t
*regexp
, kwset_t kws
)
84 assert(data
[sz
] == '\0');
85 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
87 data
+= regmatch
.rm_eo
;
88 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
93 } else { /* Classic exact string match */
96 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
99 sz
-= offset
+ kwsm
.size
[0];
100 data
+= offset
+ kwsm
.size
[0];
107 static int has_changes(mmfile_t
*one
, mmfile_t
*two
,
108 struct diff_options
*o
,
109 regex_t
*regexp
, kwset_t kws
)
111 unsigned int one_contains
= one
? contains(one
, regexp
, kws
) : 0;
112 unsigned int two_contains
= two
? contains(two
, regexp
, kws
) : 0;
113 return one_contains
!= two_contains
;
116 static int pickaxe_match(struct diff_filepair
*p
, struct diff_options
*o
,
117 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
119 struct userdiff_driver
*textconv_one
= NULL
;
120 struct userdiff_driver
*textconv_two
= NULL
;
127 /* ignore unmerged */
128 if (!DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
))
131 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
132 textconv_one
= get_textconv(p
->one
);
133 textconv_two
= get_textconv(p
->two
);
137 * If we have an unmodified pair, we know that the count will be the
138 * same and don't even have to load the blobs. Unless textconv is in
139 * play, _and_ we are using two different textconv filters (e.g.,
140 * because a pair is an exact rename with different textconv attributes
141 * for each side, which might generate different content).
143 if (textconv_one
== textconv_two
&& diff_unmodified_pair(p
))
146 mf1
.size
= fill_textconv(textconv_one
, p
->one
, &mf1
.ptr
);
147 mf2
.size
= fill_textconv(textconv_two
, p
->two
, &mf2
.ptr
);
149 ret
= fn(DIFF_FILE_VALID(p
->one
) ? &mf1
: NULL
,
150 DIFF_FILE_VALID(p
->two
) ? &mf2
: NULL
,
157 diff_free_filespec_data(p
->one
);
158 diff_free_filespec_data(p
->two
);
163 static void pickaxe(struct diff_queue_struct
*q
, struct diff_options
*o
,
164 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
167 struct diff_queue_struct outq
;
169 DIFF_QUEUE_CLEAR(&outq
);
171 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
172 /* Showing the whole changeset if needle exists */
173 for (i
= 0; i
< q
->nr
; i
++) {
174 struct diff_filepair
*p
= q
->queue
[i
];
175 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
176 return; /* do not munge the queue */
180 * Otherwise we will clear the whole queue by copying
181 * the empty outq at the end of this function, but
182 * first clear the current entries in the queue.
184 for (i
= 0; i
< q
->nr
; i
++)
185 diff_free_filepair(q
->queue
[i
]);
187 /* Showing only the filepairs that has the needle */
188 for (i
= 0; i
< q
->nr
; i
++) {
189 struct diff_filepair
*p
= q
->queue
[i
];
190 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
193 diff_free_filepair(p
);
201 void diffcore_pickaxe(struct diff_options
*o
)
203 const char *needle
= o
->pickaxe
;
204 int opts
= o
->pickaxe_opts
;
205 regex_t regex
, *regexp
= NULL
;
208 if (opts
& (DIFF_PICKAXE_REGEX
| DIFF_PICKAXE_KIND_G
)) {
210 int cflags
= REG_EXTENDED
| REG_NEWLINE
;
211 if (DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
))
213 err
= regcomp(®ex
, needle
, cflags
);
215 /* The POSIX.2 people are surely sick */
217 regerror(err
, ®ex
, errbuf
, 1024);
219 die("invalid regex: %s", errbuf
);
223 kws
= kwsalloc(DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
)
224 ? tolower_trans_tbl
: NULL
);
225 kwsincr(kws
, needle
, strlen(needle
));
229 /* Might want to warn when both S and G are on; I don't care... */
230 pickaxe(&diff_queued_diff
, o
, regexp
, kws
,
231 (opts
& DIFF_PICKAXE_KIND_G
) ? diff_grep
: has_changes
);