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
);
15 static int pickaxe_match(struct diff_filepair
*p
, struct diff_options
*o
,
16 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
);
18 static void pickaxe(struct diff_queue_struct
*q
, struct diff_options
*o
,
19 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
22 struct diff_queue_struct outq
;
24 DIFF_QUEUE_CLEAR(&outq
);
26 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
27 /* Showing the whole changeset if needle exists */
28 for (i
= 0; i
< q
->nr
; i
++) {
29 struct diff_filepair
*p
= q
->queue
[i
];
30 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
31 return; /* do not munge the queue */
35 * Otherwise we will clear the whole queue by copying
36 * the empty outq at the end of this function, but
37 * first clear the current entries in the queue.
39 for (i
= 0; i
< q
->nr
; i
++)
40 diff_free_filepair(q
->queue
[i
]);
42 /* Showing only the filepairs that has the needle */
43 for (i
= 0; i
< q
->nr
; i
++) {
44 struct diff_filepair
*p
= q
->queue
[i
];
45 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
48 diff_free_filepair(p
);
61 static void diffgrep_consume(void *priv
, char *line
, unsigned long len
)
63 struct diffgrep_cb
*data
= priv
;
67 if (line
[0] != '+' && line
[0] != '-')
71 * NEEDSWORK: we should have a way to terminate the
75 /* Yuck -- line ought to be "const char *"! */
78 data
->hit
= !regexec(data
->regexp
, line
+ 1, 1, ®match
, 0);
82 static int diff_grep(mmfile_t
*one
, mmfile_t
*two
,
83 struct diff_options
*o
,
84 regex_t
*regexp
, kwset_t kws
)
87 struct diffgrep_cb ecbdata
;
92 return !regexec(regexp
, two
->ptr
, 1, ®match
, 0);
94 return !regexec(regexp
, one
->ptr
, 1, ®match
, 0);
97 * We have both sides; need to run textual diff and see if
98 * the pattern appears on added/deleted lines.
100 memset(&xpp
, 0, sizeof(xpp
));
101 memset(&xecfg
, 0, sizeof(xecfg
));
102 ecbdata
.regexp
= regexp
;
104 xecfg
.ctxlen
= o
->context
;
105 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
106 xdi_diff_outf(one
, two
, diffgrep_consume
, &ecbdata
,
111 static void diffcore_pickaxe_grep(struct diff_options
*o
)
115 int cflags
= REG_EXTENDED
| REG_NEWLINE
;
117 if (DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
))
120 err
= regcomp(®ex
, o
->pickaxe
, cflags
);
123 regerror(err
, ®ex
, errbuf
, 1024);
125 die("invalid regex: %s", errbuf
);
128 pickaxe(&diff_queued_diff
, o
, ®ex
, NULL
, diff_grep
);
134 static unsigned int contains(mmfile_t
*mf
, regex_t
*regexp
, kwset_t kws
)
148 assert(data
[sz
] == '\0');
149 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
151 data
+= regmatch
.rm_eo
;
152 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
157 } else { /* Classic exact string match */
159 struct kwsmatch kwsm
;
160 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
165 found
= data
+ offset
;
166 sz
-= found
- data
+ kwsm
.size
[0];
167 data
= found
+ kwsm
.size
[0];
174 static int has_changes(mmfile_t
*one
, mmfile_t
*two
,
175 struct diff_options
*o
,
176 regex_t
*regexp
, kwset_t kws
)
178 unsigned int one_contains
= one
? contains(one
, regexp
, kws
) : 0;
179 unsigned int two_contains
= two
? contains(two
, regexp
, kws
) : 0;
180 return one_contains
!= two_contains
;
183 static int pickaxe_match(struct diff_filepair
*p
, struct diff_options
*o
,
184 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
186 struct userdiff_driver
*textconv_one
= NULL
;
187 struct userdiff_driver
*textconv_two
= NULL
;
194 /* ignore unmerged */
195 if (!DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
))
198 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
199 textconv_one
= get_textconv(p
->one
);
200 textconv_two
= get_textconv(p
->two
);
204 * If we have an unmodified pair, we know that the count will be the
205 * same and don't even have to load the blobs. Unless textconv is in
206 * play, _and_ we are using two different textconv filters (e.g.,
207 * because a pair is an exact rename with different textconv attributes
208 * for each side, which might generate different content).
210 if (textconv_one
== textconv_two
&& diff_unmodified_pair(p
))
213 mf1
.size
= fill_textconv(textconv_one
, p
->one
, &mf1
.ptr
);
214 mf2
.size
= fill_textconv(textconv_two
, p
->two
, &mf2
.ptr
);
216 ret
= fn(DIFF_FILE_VALID(p
->one
) ? &mf1
: NULL
,
217 DIFF_FILE_VALID(p
->two
) ? &mf2
: NULL
,
224 diff_free_filespec_data(p
->one
);
225 diff_free_filespec_data(p
->two
);
230 static void diffcore_pickaxe_count(struct diff_options
*o
)
232 const char *needle
= o
->pickaxe
;
233 int opts
= o
->pickaxe_opts
;
234 unsigned long len
= strlen(needle
);
235 regex_t regex
, *regexp
= NULL
;
238 if (opts
& DIFF_PICKAXE_REGEX
) {
240 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
242 /* The POSIX.2 people are surely sick */
244 regerror(err
, ®ex
, errbuf
, 1024);
246 die("invalid regex: %s", errbuf
);
250 kws
= kwsalloc(DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
)
251 ? tolower_trans_tbl
: NULL
);
252 kwsincr(kws
, needle
, len
);
256 pickaxe(&diff_queued_diff
, o
, regexp
, kws
, has_changes
);
258 if (opts
& DIFF_PICKAXE_REGEX
)
265 void diffcore_pickaxe(struct diff_options
*o
)
267 /* Might want to warn when both S and G are on; I don't care... */
268 if (o
->pickaxe_opts
& DIFF_PICKAXE_KIND_G
)
269 diffcore_pickaxe_grep(o
);
271 diffcore_pickaxe_count(o
);