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
;
28 if (line
[0] != '+' && line
[0] != '-')
32 * NEEDSWORK: we should have a way to terminate the
36 /* Yuck -- line ought to be "const char *"! */
39 data
->hit
= !regexec(data
->regexp
, line
+ 1, 1, ®match
, 0);
43 static int diff_grep(mmfile_t
*one
, mmfile_t
*two
,
44 struct diff_options
*o
,
45 regex_t
*regexp
, kwset_t kws
)
48 struct diffgrep_cb ecbdata
;
53 return !regexec(regexp
, two
->ptr
, 1, ®match
, 0);
55 return !regexec(regexp
, one
->ptr
, 1, ®match
, 0);
58 * We have both sides; need to run textual diff and see if
59 * the pattern appears on added/deleted lines.
61 memset(&xpp
, 0, sizeof(xpp
));
62 memset(&xecfg
, 0, sizeof(xecfg
));
63 ecbdata
.regexp
= regexp
;
65 xecfg
.ctxlen
= o
->context
;
66 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
67 if (xdi_diff_outf(one
, two
, diffgrep_consume
, &ecbdata
, &xpp
, &xecfg
))
72 static unsigned int contains(mmfile_t
*mf
, regex_t
*regexp
, kwset_t kws
)
86 assert(data
[sz
] == '\0');
87 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
89 data
+= regmatch
.rm_eo
;
90 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
95 } else { /* Classic exact string match */
98 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
101 sz
-= offset
+ kwsm
.size
[0];
102 data
+= offset
+ kwsm
.size
[0];
109 static int has_changes(mmfile_t
*one
, mmfile_t
*two
,
110 struct diff_options
*o
,
111 regex_t
*regexp
, kwset_t kws
)
113 unsigned int one_contains
= one
? contains(one
, regexp
, kws
) : 0;
114 unsigned int two_contains
= two
? contains(two
, regexp
, kws
) : 0;
115 return one_contains
!= two_contains
;
118 static int pickaxe_match(struct diff_filepair
*p
, struct diff_options
*o
,
119 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
121 struct userdiff_driver
*textconv_one
= NULL
;
122 struct userdiff_driver
*textconv_two
= NULL
;
129 /* ignore unmerged */
130 if (!DIFF_FILE_VALID(p
->one
) && !DIFF_FILE_VALID(p
->two
))
133 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
134 textconv_one
= get_textconv(p
->one
);
135 textconv_two
= get_textconv(p
->two
);
139 * If we have an unmodified pair, we know that the count will be the
140 * same and don't even have to load the blobs. Unless textconv is in
141 * play, _and_ we are using two different textconv filters (e.g.,
142 * because a pair is an exact rename with different textconv attributes
143 * for each side, which might generate different content).
145 if (textconv_one
== textconv_two
&& diff_unmodified_pair(p
))
148 mf1
.size
= fill_textconv(textconv_one
, p
->one
, &mf1
.ptr
);
149 mf2
.size
= fill_textconv(textconv_two
, p
->two
, &mf2
.ptr
);
151 ret
= fn(DIFF_FILE_VALID(p
->one
) ? &mf1
: NULL
,
152 DIFF_FILE_VALID(p
->two
) ? &mf2
: NULL
,
159 diff_free_filespec_data(p
->one
);
160 diff_free_filespec_data(p
->two
);
165 static void pickaxe(struct diff_queue_struct
*q
, struct diff_options
*o
,
166 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
169 struct diff_queue_struct outq
;
171 DIFF_QUEUE_CLEAR(&outq
);
173 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
174 /* Showing the whole changeset if needle exists */
175 for (i
= 0; i
< q
->nr
; i
++) {
176 struct diff_filepair
*p
= q
->queue
[i
];
177 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
178 return; /* do not munge the queue */
182 * Otherwise we will clear the whole queue by copying
183 * the empty outq at the end of this function, but
184 * first clear the current entries in the queue.
186 for (i
= 0; i
< q
->nr
; i
++)
187 diff_free_filepair(q
->queue
[i
]);
189 /* Showing only the filepairs that has the needle */
190 for (i
= 0; i
< q
->nr
; i
++) {
191 struct diff_filepair
*p
= q
->queue
[i
];
192 if (pickaxe_match(p
, o
, regexp
, kws
, fn
))
195 diff_free_filepair(p
);
203 static void regcomp_or_die(regex_t
*regex
, const char *needle
, int cflags
)
205 int err
= regcomp(regex
, needle
, cflags
);
207 /* The POSIX.2 people are surely sick */
209 regerror(err
, regex
, errbuf
, 1024);
211 die("invalid regex: %s", errbuf
);
215 void diffcore_pickaxe(struct diff_options
*o
)
217 const char *needle
= o
->pickaxe
;
218 int opts
= o
->pickaxe_opts
;
219 regex_t regex
, *regexp
= NULL
;
222 if (opts
& (DIFF_PICKAXE_REGEX
| DIFF_PICKAXE_KIND_G
)) {
223 int cflags
= REG_EXTENDED
| REG_NEWLINE
;
224 if (DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
))
226 regcomp_or_die(®ex
, needle
, cflags
);
228 } else if (DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
) &&
229 has_non_ascii(needle
)) {
230 struct strbuf sb
= STRBUF_INIT
;
231 int cflags
= REG_NEWLINE
| REG_ICASE
;
233 basic_regex_quote_buf(&sb
, needle
);
234 regcomp_or_die(®ex
, sb
.buf
, cflags
);
238 kws
= kwsalloc(DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
)
239 ? tolower_trans_tbl
: NULL
);
240 kwsincr(kws
, needle
, strlen(needle
));
244 /* Might want to warn when both S and G are on; I don't care... */
245 pickaxe(&diff_queued_diff
, o
, regexp
, kws
,
246 (opts
& DIFF_PICKAXE_KIND_G
) ? diff_grep
: has_changes
);