2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #include "xdiff-interface.h"
15 static void diffgrep_consume(void *priv
, char *line
, unsigned long len
)
17 struct diffgrep_cb
*data
= priv
;
21 if (line
[0] != '+' && line
[0] != '-')
25 * NEEDSWORK: we should have a way to terminate the
29 /* Yuck -- line ought to be "const char *"! */
32 data
->hit
= !regexec(data
->regexp
, line
+ 1, 1, ®match
, 0);
36 static void fill_one(struct diff_filespec
*one
,
37 mmfile_t
*mf
, struct userdiff_driver
**textconv
)
39 if (DIFF_FILE_VALID(one
)) {
40 *textconv
= get_textconv(one
);
41 mf
->size
= fill_textconv(*textconv
, one
, &mf
->ptr
);
43 memset(mf
, 0, sizeof(*mf
));
47 static int diff_grep(struct diff_filepair
*p
, regex_t
*regexp
, struct diff_options
*o
)
50 struct userdiff_driver
*textconv_one
= NULL
;
51 struct userdiff_driver
*textconv_two
= NULL
;
55 if (diff_unmodified_pair(p
))
58 fill_one(p
->one
, &mf1
, &textconv_one
);
59 fill_one(p
->two
, &mf2
, &textconv_two
);
63 return 0; /* ignore unmerged */
64 /* created "two" -- does it have what we are looking for? */
65 hit
= !regexec(regexp
, p
->two
->data
, 1, ®match
, 0);
66 } else if (!mf2
.ptr
) {
67 /* removed "one" -- did it have what we are looking for? */
68 hit
= !regexec(regexp
, p
->one
->data
, 1, ®match
, 0);
71 * We have both sides; need to run textual diff and see if
72 * the pattern appears on added/deleted lines.
74 struct diffgrep_cb ecbdata
;
78 memset(&xpp
, 0, sizeof(xpp
));
79 memset(&xecfg
, 0, sizeof(xecfg
));
80 ecbdata
.regexp
= regexp
;
82 xecfg
.ctxlen
= o
->context
;
83 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
84 xdi_diff_outf(&mf1
, &mf2
, diffgrep_consume
, &ecbdata
,
95 static void diffcore_pickaxe_grep(struct diff_options
*o
)
97 struct diff_queue_struct
*q
= &diff_queued_diff
;
98 int i
, has_changes
, err
;
100 struct diff_queue_struct outq
;
102 outq
.nr
= outq
.alloc
= 0;
104 err
= regcomp(®ex
, o
->pickaxe
, REG_EXTENDED
| REG_NEWLINE
);
107 regerror(err
, ®ex
, errbuf
, 1024);
109 die("invalid log-grep regex: %s", errbuf
);
112 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
113 /* Showing the whole changeset if needle exists */
114 for (i
= has_changes
= 0; !has_changes
&& i
< q
->nr
; i
++) {
115 struct diff_filepair
*p
= q
->queue
[i
];
116 if (diff_grep(p
, ®ex
, o
))
120 return; /* 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
, ®ex
, o
))
136 diff_free_filepair(p
);
147 static unsigned int contains(struct diff_filespec
*one
,
148 const char *needle
, unsigned long len
,
154 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 const char *found
= memmem(data
, sz
, needle
, len
);
181 sz
-= found
- data
+ len
;
186 diff_free_filespec_data(one
);
190 static void diffcore_pickaxe_count(struct diff_options
*o
)
192 const char *needle
= o
->pickaxe
;
193 int opts
= o
->pickaxe_opts
;
194 struct diff_queue_struct
*q
= &diff_queued_diff
;
195 unsigned long len
= strlen(needle
);
197 regex_t regex
, *regexp
= NULL
;
198 struct diff_queue_struct outq
;
199 DIFF_QUEUE_CLEAR(&outq
);
201 if (opts
& DIFF_PICKAXE_REGEX
) {
203 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
205 /* The POSIX.2 people are surely sick */
207 regerror(err
, ®ex
, errbuf
, 1024);
209 die("invalid pickaxe regex: %s", errbuf
);
214 if (opts
& DIFF_PICKAXE_ALL
) {
215 /* Showing the whole changeset if needle exists */
216 for (i
= has_changes
= 0; !has_changes
&& i
< q
->nr
; i
++) {
217 struct diff_filepair
*p
= q
->queue
[i
];
218 if (!DIFF_FILE_VALID(p
->one
)) {
219 if (!DIFF_FILE_VALID(p
->two
))
220 continue; /* ignore unmerged */
222 if (contains(p
->two
, needle
, len
, regexp
))
225 else if (!DIFF_FILE_VALID(p
->two
)) {
226 if (contains(p
->one
, needle
, len
, regexp
))
229 else if (!diff_unmodified_pair(p
) &&
230 contains(p
->one
, needle
, len
, regexp
) !=
231 contains(p
->two
, needle
, len
, regexp
))
235 return; /* not munge the queue */
237 /* otherwise we will clear the whole queue
238 * by copying the empty outq at the end of this
239 * function, but first clear the current entries
242 for (i
= 0; i
< q
->nr
; i
++)
243 diff_free_filepair(q
->queue
[i
]);
246 /* Showing only the filepairs that has the needle */
247 for (i
= 0; i
< q
->nr
; i
++) {
248 struct diff_filepair
*p
= q
->queue
[i
];
250 if (!DIFF_FILE_VALID(p
->one
)) {
251 if (!DIFF_FILE_VALID(p
->two
))
252 ; /* ignore unmerged */
254 else if (contains(p
->two
, needle
, len
, regexp
))
257 else if (!DIFF_FILE_VALID(p
->two
)) {
258 if (contains(p
->one
, needle
, len
, regexp
))
261 else if (!diff_unmodified_pair(p
) &&
262 contains(p
->one
, needle
, len
, regexp
) !=
263 contains(p
->two
, needle
, len
, regexp
))
269 diff_free_filepair(p
);
272 if (opts
& DIFF_PICKAXE_REGEX
)
280 void diffcore_pickaxe(struct diff_options
*o
)
282 /* Might want to warn when both S and G are on; I don't care... */
283 if (o
->pickaxe_opts
& DIFF_PICKAXE_KIND_G
)
284 diffcore_pickaxe_grep(o
);
286 diffcore_pickaxe_count(o
);