2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #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
, struct diff_options
*o
,
49 regex_t
*regexp
, kwset_t kws
)
52 struct userdiff_driver
*textconv_one
= NULL
;
53 struct userdiff_driver
*textconv_two
= NULL
;
57 if (diff_unmodified_pair(p
))
60 fill_one(p
->one
, &mf1
, &textconv_one
);
61 fill_one(p
->two
, &mf2
, &textconv_two
);
65 return 0; /* ignore unmerged */
66 /* created "two" -- does it have what we are looking for? */
67 hit
= !regexec(regexp
, p
->two
->data
, 1, ®match
, 0);
68 } else if (!mf2
.ptr
) {
69 /* removed "one" -- did it have what we are looking for? */
70 hit
= !regexec(regexp
, p
->one
->data
, 1, ®match
, 0);
73 * We have both sides; need to run textual diff and see if
74 * the pattern appears on added/deleted lines.
76 struct diffgrep_cb ecbdata
;
80 memset(&xpp
, 0, sizeof(xpp
));
81 memset(&xecfg
, 0, sizeof(xecfg
));
82 ecbdata
.regexp
= regexp
;
84 xecfg
.ctxlen
= o
->context
;
85 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
86 xdi_diff_outf(&mf1
, &mf2
, diffgrep_consume
, &ecbdata
,
97 static void diffcore_pickaxe_grep(struct diff_options
*o
)
99 struct diff_queue_struct
*q
= &diff_queued_diff
;
102 struct diff_queue_struct outq
;
104 outq
.nr
= outq
.alloc
= 0;
106 err
= regcomp(®ex
, o
->pickaxe
, REG_EXTENDED
| REG_NEWLINE
);
109 regerror(err
, ®ex
, errbuf
, 1024);
111 die("invalid log-grep regex: %s", errbuf
);
114 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
115 /* Showing the whole changeset if needle exists */
116 for (i
= 0; i
< q
->nr
; i
++) {
117 struct diff_filepair
*p
= q
->queue
[i
];
118 if (diff_grep(p
, o
, ®ex
, NULL
))
119 goto out
; /* do not munge the queue */
123 * Otherwise we will clear the whole queue by copying
124 * the empty outq at the end of this function, but
125 * first clear the current entries in the queue.
127 for (i
= 0; i
< q
->nr
; i
++)
128 diff_free_filepair(q
->queue
[i
]);
130 /* Showing only the filepairs that has the needle */
131 for (i
= 0; i
< q
->nr
; i
++) {
132 struct diff_filepair
*p
= q
->queue
[i
];
133 if (diff_grep(p
, o
, ®ex
, NULL
))
136 diff_free_filepair(p
);
148 static unsigned int contains(struct diff_filespec
*one
, struct diff_options
*o
,
149 regex_t
*regexp
, kwset_t kws
)
156 if (diff_populate_filespec(one
, 0))
167 assert(data
[sz
] == '\0');
168 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
170 data
+= regmatch
.rm_eo
;
171 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
176 } else { /* Classic exact string match */
178 struct kwsmatch kwsm
;
179 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
184 found
= data
+ offset
;
185 sz
-= found
- data
+ kwsm
.size
[0];
186 data
= found
+ kwsm
.size
[0];
190 diff_free_filespec_data(one
);
194 static int has_changes(struct diff_filepair
*p
, struct diff_options
*o
,
195 regex_t
*regexp
, kwset_t kws
)
197 if (!DIFF_FILE_VALID(p
->one
)) {
198 if (!DIFF_FILE_VALID(p
->two
))
199 return 0; /* ignore unmerged */
201 return contains(p
->two
, o
, regexp
, kws
) != 0;
203 if (!DIFF_FILE_VALID(p
->two
))
204 return contains(p
->one
, o
, regexp
, kws
) != 0;
205 if (!diff_unmodified_pair(p
)) {
206 return contains(p
->one
, o
, regexp
, kws
) !=
207 contains(p
->two
, o
, regexp
, kws
);
212 static void diffcore_pickaxe_count(struct diff_options
*o
)
214 const char *needle
= o
->pickaxe
;
215 int opts
= o
->pickaxe_opts
;
216 struct diff_queue_struct
*q
= &diff_queued_diff
;
217 unsigned long len
= strlen(needle
);
219 regex_t regex
, *regexp
= NULL
;
221 struct diff_queue_struct outq
;
222 DIFF_QUEUE_CLEAR(&outq
);
224 if (opts
& DIFF_PICKAXE_REGEX
) {
226 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
228 /* The POSIX.2 people are surely sick */
230 regerror(err
, ®ex
, errbuf
, 1024);
232 die("invalid pickaxe regex: %s", errbuf
);
236 kws
= kwsalloc(NULL
);
237 kwsincr(kws
, needle
, len
);
241 if (opts
& DIFF_PICKAXE_ALL
) {
242 /* Showing the whole changeset if needle exists */
243 for (i
= 0; i
< q
->nr
; i
++) {
244 struct diff_filepair
*p
= q
->queue
[i
];
245 if (has_changes(p
, o
, regexp
, kws
))
246 goto out
; /* do not munge the queue */
249 /* otherwise we will clear the whole queue
250 * by copying the empty outq at the end of this
251 * function, but first clear the current entries
254 for (i
= 0; i
< q
->nr
; i
++)
255 diff_free_filepair(q
->queue
[i
]);
258 /* Showing only the filepairs that has the needle */
259 for (i
= 0; i
< q
->nr
; i
++) {
260 struct diff_filepair
*p
= q
->queue
[i
];
261 if (has_changes(p
, o
, regexp
, kws
))
264 diff_free_filepair(p
);
271 if (opts
& DIFF_PICKAXE_REGEX
)
278 void diffcore_pickaxe(struct diff_options
*o
)
280 /* Might want to warn when both S and G are on; I don't care... */
281 if (o
->pickaxe_opts
& DIFF_PICKAXE_KIND_G
)
282 diffcore_pickaxe_grep(o
);
284 diffcore_pickaxe_count(o
);