1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
3 * Copyright (C) 2005 Junio C Hamano
4 * Copyright (C) 2010 Google Inc.
9 #include "xdiff-interface.h"
16 static void diffgrep_consume(void *priv
, char *line
, unsigned long len
)
18 struct diffgrep_cb
*data
= priv
;
22 if (line
[0] != '+' && line
[0] != '-')
26 * NEEDSWORK: we should have a way to terminate the
30 /* Yuck -- line ought to be "const char *"! */
33 data
->hit
= !regexec(data
->regexp
, line
+ 1, 1, ®match
, 0);
37 static void fill_one(struct diff_filespec
*one
,
38 mmfile_t
*mf
, struct userdiff_driver
**textconv
)
40 if (DIFF_FILE_VALID(one
)) {
41 *textconv
= get_textconv(one
);
42 mf
->size
= fill_textconv(*textconv
, one
, &mf
->ptr
);
44 memset(mf
, 0, sizeof(*mf
));
48 static int diff_grep(struct diff_filepair
*p
, regex_t
*regexp
, struct diff_options
*o
)
51 struct userdiff_driver
*textconv_one
= NULL
;
52 struct userdiff_driver
*textconv_two
= NULL
;
56 if (diff_unmodified_pair(p
))
59 fill_one(p
->one
, &mf1
, &textconv_one
);
60 fill_one(p
->two
, &mf2
, &textconv_two
);
64 return 0; /* ignore unmerged */
65 /* created "two" -- does it have what we are looking for? */
66 hit
= !regexec(regexp
, p
->two
->data
, 1, ®match
, 0);
67 } else if (!mf2
.ptr
) {
68 /* removed "one" -- did it have what we are looking for? */
69 hit
= !regexec(regexp
, p
->one
->data
, 1, ®match
, 0);
72 * We have both sides; need to run textual diff and see if
73 * the pattern appears on added/deleted lines.
75 struct diffgrep_cb ecbdata
;
79 memset(&xpp
, 0, sizeof(xpp
));
80 memset(&xecfg
, 0, sizeof(xecfg
));
81 ecbdata
.regexp
= regexp
;
83 xecfg
.ctxlen
= o
->context
;
84 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
85 xdi_diff_outf(&mf1
, &mf2
, diffgrep_consume
, &ecbdata
,
96 static void diffcore_pickaxe_grep(struct diff_options
*o
)
98 struct diff_queue_struct
*q
= &diff_queued_diff
;
99 int i
, has_changes
, err
;
101 struct diff_queue_struct outq
;
103 outq
.nr
= outq
.alloc
= 0;
105 err
= regcomp(®ex
, o
->pickaxe
, REG_EXTENDED
| REG_NEWLINE
);
108 regerror(err
, ®ex
, errbuf
, 1024);
110 die("invalid log-grep regex: %s", errbuf
);
113 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
114 /* Showing the whole changeset if needle exists */
115 for (i
= has_changes
= 0; !has_changes
&& i
< q
->nr
; i
++) {
116 struct diff_filepair
*p
= q
->queue
[i
];
117 if (diff_grep(p
, ®ex
, o
))
121 return; /* do not munge the queue */
124 * Otherwise we will clear the whole queue by copying
125 * the empty outq at the end of this function, but
126 * first clear the current entries in the queue.
128 for (i
= 0; i
< q
->nr
; i
++)
129 diff_free_filepair(q
->queue
[i
]);
131 /* Showing only the filepairs that has the needle */
132 for (i
= 0; i
< q
->nr
; i
++) {
133 struct diff_filepair
*p
= q
->queue
[i
];
134 if (diff_grep(p
, ®ex
, o
))
137 diff_free_filepair(p
);
153 unsigned char text
[];
156 static void bmh_init(struct needle
*needle
)
159 for (i
= 0; i
< 256; i
++)
160 needle
->skip
[i
] = needle
->len
;
162 // Last character in needle can not be a mismatch in last position */
163 for (i
= 0; i
< needle
->len
- 1; i
++)
164 needle
->skip
[needle
->text
[i
]] = needle
->len
- i
- 1;
168 static unsigned bmh(const unsigned char *haystack
, unsigned long hlen
,
169 const struct needle
*needle
)
173 if (hlen
< needle
->len
)
176 const unsigned char *end
= haystack
+ hlen
- needle
->len
;
178 while (haystack
<= end
) {
179 while (haystack
[needle
->len
- 1] != needle
->text
[needle
->len
- 1]) {
180 haystack
+= needle
->skip
[haystack
[needle
->len
- 1]];
184 for (i
= 0; i
< needle
->len
- 1; i
++)
185 if (haystack
[i
] != needle
->text
[i
])
188 haystack
+= needle
->len
;
191 haystack
+= needle
->skip
[haystack
[needle
->len
- 1]];
198 static unsigned int contains(struct diff_filespec
*one
,
199 struct needle
*needle
)
204 if (diff_populate_filespec(one
, 0))
213 if (needle
->use_regex
) {
217 assert(data
[sz
] == '\0');
218 while (*data
&& !regexec(&needle
->regex
, data
, 1, ®match
, flags
)) {
220 data
+= regmatch
.rm_eo
;
221 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
227 cnt
= bmh(data
, sz
, needle
);
228 diff_free_filespec_data(one
);
232 static void diffcore_pickaxe_count(struct diff_options
*o
)
234 const char *needle_txt
= o
->pickaxe
;
235 int opts
= o
->pickaxe_opts
;
236 struct diff_queue_struct
*q
= &diff_queued_diff
;
237 unsigned long len
= strlen(needle_txt
);
239 struct diff_queue_struct outq
;
240 DIFF_QUEUE_CLEAR(&outq
);
241 struct needle
*needle
= NULL
;
243 if (opts
& DIFF_PICKAXE_REGEX
) {
244 needle
= xcalloc(1, sizeof(*needle
));
245 needle
->use_regex
= 1;
248 err
= regcomp(&needle
->regex
, needle_txt
, REG_EXTENDED
| REG_NEWLINE
);
250 /* The POSIX.2 people are surely sick */
252 regerror(err
, &needle
->regex
, errbuf
, 1024);
253 regfree(&needle
->regex
);
254 die("invalid pickaxe regex: %s", errbuf
);
258 needle
= xcalloc(1, sizeof(*needle
) + len
);
259 memcpy(needle
->text
, needle_txt
, len
);
264 if (opts
& DIFF_PICKAXE_ALL
) {
265 /* Showing the whole changeset if needle exists */
266 for (i
= has_changes
= 0; !has_changes
&& i
< q
->nr
; i
++) {
267 struct diff_filepair
*p
= q
->queue
[i
];
268 if (!DIFF_FILE_VALID(p
->one
)) {
269 if (!DIFF_FILE_VALID(p
->two
))
270 continue; /* ignore unmerged */
272 if (contains(p
->two
, needle
))
275 else if (!DIFF_FILE_VALID(p
->two
)) {
276 if (contains(p
->one
, needle
))
279 else if (!diff_unmodified_pair(p
) &&
280 contains(p
->one
, needle
) !=
281 contains(p
->two
, needle
))
285 return; /* not munge the queue */
287 /* otherwise we will clear the whole queue
288 * by copying the empty outq at the end of this
289 * function, but first clear the current entries
292 for (i
= 0; i
< q
->nr
; i
++)
293 diff_free_filepair(q
->queue
[i
]);
296 /* Showing only the filepairs that has the needle */
297 for (i
= 0; i
< q
->nr
; i
++) {
298 struct diff_filepair
*p
= q
->queue
[i
];
300 if (!DIFF_FILE_VALID(p
->one
)) {
301 if (!DIFF_FILE_VALID(p
->two
))
302 ; /* ignore unmerged */
304 else if (contains(p
->two
, needle
))
307 else if (!DIFF_FILE_VALID(p
->two
)) {
308 if (contains(p
->one
, needle
))
311 else if (!diff_unmodified_pair(p
) &&
312 contains(p
->one
, needle
) !=
313 contains(p
->two
, needle
))
319 diff_free_filepair(p
);
322 if (opts
& DIFF_PICKAXE_REGEX
)
330 void diffcore_pickaxe(struct diff_options
*o
)
332 /* Might want to warn when both S and G are on; I don't care... */
333 if (o
->pickaxe_opts
& DIFF_PICKAXE_KIND_G
)
334 diffcore_pickaxe_grep(o
);
336 diffcore_pickaxe_count(o
);