2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #include "xdiff-interface.h"
13 typedef int (*pickaxe_fn
)(mmfile_t
*one
, mmfile_t
*two
,
14 struct diff_options
*o
,
15 regex_t
*regexp
, kwset_t kws
);
22 static void diffgrep_consume(void *priv
, char *line
, unsigned long len
)
24 struct diffgrep_cb
*data
= priv
;
27 if (line
[0] != '+' && line
[0] != '-')
31 * NEEDSWORK: we should have a way to terminate the
35 data
->hit
= !regexec_buf(data
->regexp
, line
+ 1, len
- 1, 1,
39 static int diff_grep(mmfile_t
*one
, mmfile_t
*two
,
40 struct diff_options
*o
,
41 regex_t
*regexp
, kwset_t kws
)
44 struct diffgrep_cb ecbdata
;
49 return !regexec_buf(regexp
, two
->ptr
, two
->size
,
52 return !regexec_buf(regexp
, one
->ptr
, one
->size
,
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
, discard_hunk_line
, diffgrep_consume
,
66 &ecbdata
, &xpp
, &xecfg
))
71 static unsigned int contains(mmfile_t
*mf
, regex_t
*regexp
, kwset_t kws
)
86 !regexec_buf(regexp
, data
, sz
, 1, ®match
, flags
)) {
88 data
+= regmatch
.rm_eo
;
90 if (sz
&& *data
&& regmatch
.rm_so
== regmatch
.rm_eo
) {
97 } else { /* Classic exact string match */
100 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
103 sz
-= offset
+ kwsm
.size
[0];
104 data
+= offset
+ kwsm
.size
[0];
111 static int has_changes(mmfile_t
*one
, mmfile_t
*two
,
112 struct diff_options
*o
,
113 regex_t
*regexp
, kwset_t kws
)
115 unsigned int one_contains
= one
? contains(one
, regexp
, kws
) : 0;
116 unsigned int two_contains
= two
? contains(two
, regexp
, kws
) : 0;
117 return one_contains
!= two_contains
;
120 static int pickaxe_match(struct diff_filepair
*p
, struct diff_options
*o
,
121 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
123 struct userdiff_driver
*textconv_one
= NULL
;
124 struct userdiff_driver
*textconv_two
= NULL
;
128 /* ignore unmerged */
129 if (!DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
))
133 return (DIFF_FILE_VALID(p
->one
) &&
134 oidset_contains(o
->objfind
, &p
->one
->oid
)) ||
135 (DIFF_FILE_VALID(p
->two
) &&
136 oidset_contains(o
->objfind
, &p
->two
->oid
));
142 if (o
->flags
.allow_textconv
) {
143 textconv_one
= get_textconv(o
->repo
->index
, p
->one
);
144 textconv_two
= get_textconv(o
->repo
->index
, p
->two
);
148 * If we have an unmodified pair, we know that the count will be the
149 * same and don't even have to load the blobs. Unless textconv is in
150 * play, _and_ we are using two different textconv filters (e.g.,
151 * because a pair is an exact rename with different textconv attributes
152 * for each side, which might generate different content).
154 if (textconv_one
== textconv_two
&& diff_unmodified_pair(p
))
157 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, p
->one
, &mf1
.ptr
);
158 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, p
->two
, &mf2
.ptr
);
160 ret
= fn(DIFF_FILE_VALID(p
->one
) ? &mf1
: NULL
,
161 DIFF_FILE_VALID(p
->two
) ? &mf2
: NULL
,
168 diff_free_filespec_data(p
->one
);
169 diff_free_filespec_data(p
->two
);
174 static void pickaxe(struct diff_queue_struct
*q
, struct diff_options
*o
,
175 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
178 struct diff_queue_struct outq
;
180 DIFF_QUEUE_CLEAR(&outq
);
182 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
183 /* Showing the whole changeset if needle exists */
184 for (i
= 0; i
< q
->nr
; i
++) {
185 struct diff_filepair
*p
= q
->queue
[i
];
186 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
187 return; /* do not munge the queue */
191 * Otherwise we will clear the whole queue by copying
192 * the empty outq at the end of this function, but
193 * first clear the current entries in the queue.
195 for (i
= 0; i
< q
->nr
; i
++)
196 diff_free_filepair(q
->queue
[i
]);
198 /* Showing only the filepairs that has the needle */
199 for (i
= 0; i
< q
->nr
; i
++) {
200 struct diff_filepair
*p
= q
->queue
[i
];
201 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
204 diff_free_filepair(p
);
212 static void regcomp_or_die(regex_t
*regex
, const char *needle
, int cflags
)
214 int err
= regcomp(regex
, needle
, cflags
);
216 /* The POSIX.2 people are surely sick */
218 regerror(err
, regex
, errbuf
, 1024);
219 die("invalid regex: %s", errbuf
);
223 void diffcore_pickaxe(struct diff_options
*o
)
225 const char *needle
= o
->pickaxe
;
226 int opts
= o
->pickaxe_opts
;
227 regex_t regex
, *regexp
= NULL
;
230 if (opts
& (DIFF_PICKAXE_REGEX
| DIFF_PICKAXE_KIND_G
)) {
231 int cflags
= REG_EXTENDED
| REG_NEWLINE
;
232 if (o
->pickaxe_opts
& DIFF_PICKAXE_IGNORE_CASE
)
234 regcomp_or_die(®ex
, needle
, cflags
);
236 } else if (opts
& DIFF_PICKAXE_KIND_S
) {
237 if (o
->pickaxe_opts
& DIFF_PICKAXE_IGNORE_CASE
&&
238 has_non_ascii(needle
)) {
239 struct strbuf sb
= STRBUF_INIT
;
240 int cflags
= REG_NEWLINE
| REG_ICASE
;
242 basic_regex_quote_buf(&sb
, needle
);
243 regcomp_or_die(®ex
, sb
.buf
, cflags
);
247 kws
= kwsalloc(o
->pickaxe_opts
& DIFF_PICKAXE_IGNORE_CASE
248 ? tolower_trans_tbl
: NULL
);
249 kwsincr(kws
, needle
, strlen(needle
));
254 pickaxe(&diff_queued_diff
, o
, regexp
, kws
,
255 (opts
& DIFF_PICKAXE_KIND_G
) ? diff_grep
: has_changes
);