2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #include "xdiff-interface.h"
11 typedef int (*pickaxe_fn
)(struct diff_filepair
*p
, struct diff_options
*o
, regex_t
*regexp
, kwset_t kws
);
13 static void pickaxe(struct diff_queue_struct
*q
, struct diff_options
*o
,
14 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
17 struct diff_queue_struct outq
;
19 DIFF_QUEUE_CLEAR(&outq
);
21 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
22 /* Showing the whole changeset if needle exists */
23 for (i
= 0; i
< q
->nr
; i
++) {
24 struct diff_filepair
*p
= q
->queue
[i
];
25 if (fn(p
, o
, regexp
, kws
))
26 return; /* do not munge the queue */
30 * Otherwise we will clear the whole queue by copying
31 * the empty outq at the end of this function, but
32 * first clear the current entries in the queue.
34 for (i
= 0; i
< q
->nr
; i
++)
35 diff_free_filepair(q
->queue
[i
]);
37 /* Showing only the filepairs that has the needle */
38 for (i
= 0; i
< q
->nr
; i
++) {
39 struct diff_filepair
*p
= q
->queue
[i
];
40 if (fn(p
, o
, regexp
, kws
))
43 diff_free_filepair(p
);
56 static void diffgrep_consume(void *priv
, char *line
, unsigned long len
)
58 struct diffgrep_cb
*data
= priv
;
62 if (line
[0] != '+' && line
[0] != '-')
66 * NEEDSWORK: we should have a way to terminate the
70 /* Yuck -- line ought to be "const char *"! */
73 data
->hit
= !regexec(data
->regexp
, line
+ 1, 1, ®match
, 0);
77 static int diff_grep(struct diff_filepair
*p
, struct diff_options
*o
,
78 regex_t
*regexp
, kwset_t kws
)
81 struct userdiff_driver
*textconv_one
= NULL
;
82 struct userdiff_driver
*textconv_two
= NULL
;
89 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
90 textconv_one
= get_textconv(p
->one
);
91 textconv_two
= get_textconv(p
->two
);
94 if (textconv_one
== textconv_two
&& diff_unmodified_pair(p
))
97 mf1
.size
= fill_textconv(textconv_one
, p
->one
, &mf1
.ptr
);
98 mf2
.size
= fill_textconv(textconv_two
, p
->two
, &mf2
.ptr
);
100 if (!DIFF_FILE_VALID(p
->one
)) {
101 if (!DIFF_FILE_VALID(p
->two
))
102 return 0; /* ignore unmerged */
103 /* created "two" -- does it have what we are looking for? */
104 hit
= !regexec(regexp
, mf2
.ptr
, 1, ®match
, 0);
105 } else if (!DIFF_FILE_VALID(p
->two
)) {
106 /* removed "one" -- did it have what we are looking for? */
107 hit
= !regexec(regexp
, mf1
.ptr
, 1, ®match
, 0);
110 * We have both sides; need to run textual diff and see if
111 * the pattern appears on added/deleted lines.
113 struct diffgrep_cb ecbdata
;
117 memset(&xpp
, 0, sizeof(xpp
));
118 memset(&xecfg
, 0, sizeof(xecfg
));
119 ecbdata
.regexp
= regexp
;
121 xecfg
.ctxlen
= o
->context
;
122 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
123 xdi_diff_outf(&mf1
, &mf2
, diffgrep_consume
, &ecbdata
,
131 diff_free_filespec_data(p
->one
);
132 diff_free_filespec_data(p
->two
);
136 static void diffcore_pickaxe_grep(struct diff_options
*o
)
140 int cflags
= REG_EXTENDED
| REG_NEWLINE
;
142 if (DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
))
145 err
= regcomp(®ex
, o
->pickaxe
, cflags
);
148 regerror(err
, ®ex
, errbuf
, 1024);
150 die("invalid log-grep regex: %s", errbuf
);
153 pickaxe(&diff_queued_diff
, o
, ®ex
, NULL
, diff_grep
);
159 static unsigned int contains(mmfile_t
*mf
, struct diff_options
*o
,
160 regex_t
*regexp
, kwset_t kws
)
174 assert(data
[sz
] == '\0');
175 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
177 data
+= regmatch
.rm_eo
;
178 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
183 } else { /* Classic exact string match */
185 struct kwsmatch kwsm
;
186 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
191 found
= data
+ offset
;
192 sz
-= found
- data
+ kwsm
.size
[0];
193 data
= found
+ kwsm
.size
[0];
200 static int has_changes(struct diff_filepair
*p
, struct diff_options
*o
,
201 regex_t
*regexp
, kwset_t kws
)
203 struct userdiff_driver
*textconv_one
= NULL
;
204 struct userdiff_driver
*textconv_two
= NULL
;
211 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
212 textconv_one
= get_textconv(p
->one
);
213 textconv_two
= get_textconv(p
->two
);
217 * If we have an unmodified pair, we know that the count will be the
218 * same and don't even have to load the blobs. Unless textconv is in
219 * play, _and_ we are using two different textconv filters (e.g.,
220 * because a pair is an exact rename with different textconv attributes
221 * for each side, which might generate different content).
223 if (textconv_one
== textconv_two
&& diff_unmodified_pair(p
))
226 mf1
.size
= fill_textconv(textconv_one
, p
->one
, &mf1
.ptr
);
227 mf2
.size
= fill_textconv(textconv_two
, p
->two
, &mf2
.ptr
);
229 if (!DIFF_FILE_VALID(p
->one
)) {
230 if (!DIFF_FILE_VALID(p
->two
))
231 ret
= 0; /* ignore unmerged */
233 ret
= contains(&mf2
, o
, regexp
, kws
) != 0;
235 else if (!DIFF_FILE_VALID(p
->two
)) /* removed */
236 ret
= contains(&mf1
, o
, regexp
, kws
) != 0;
238 ret
= contains(&mf1
, o
, regexp
, kws
) !=
239 contains(&mf2
, o
, regexp
, kws
);
245 diff_free_filespec_data(p
->one
);
246 diff_free_filespec_data(p
->two
);
251 static void diffcore_pickaxe_count(struct diff_options
*o
)
253 const char *needle
= o
->pickaxe
;
254 int opts
= o
->pickaxe_opts
;
255 unsigned long len
= strlen(needle
);
256 regex_t regex
, *regexp
= NULL
;
259 if (opts
& DIFF_PICKAXE_REGEX
) {
261 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
263 /* The POSIX.2 people are surely sick */
265 regerror(err
, ®ex
, errbuf
, 1024);
267 die("invalid pickaxe regex: %s", errbuf
);
271 kws
= kwsalloc(DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
)
272 ? tolower_trans_tbl
: NULL
);
273 kwsincr(kws
, needle
, len
);
277 pickaxe(&diff_queued_diff
, o
, regexp
, kws
, has_changes
);
279 if (opts
& DIFF_PICKAXE_REGEX
)
286 void diffcore_pickaxe(struct diff_options
*o
)
288 /* Might want to warn when both S and G are on; I don't care... */
289 if (o
->pickaxe_opts
& DIFF_PICKAXE_KIND_G
)
290 diffcore_pickaxe_grep(o
);
292 diffcore_pickaxe_count(o
);