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
, 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
;
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
= 0; i
< q
->nr
; i
++) {
116 struct diff_filepair
*p
= q
->queue
[i
];
117 if (diff_grep(p
, ®ex
, o
))
118 goto out
; /* do not munge the queue */
122 * Otherwise we will clear the whole queue by copying
123 * the empty outq at the end of this function, but
124 * first clear the current entries in the queue.
126 for (i
= 0; i
< q
->nr
; i
++)
127 diff_free_filepair(q
->queue
[i
]);
129 /* Showing only the filepairs that has the needle */
130 for (i
= 0; i
< q
->nr
; i
++) {
131 struct diff_filepair
*p
= q
->queue
[i
];
132 if (diff_grep(p
, ®ex
, o
))
135 diff_free_filepair(p
);
147 static unsigned int contains(struct diff_filespec
*one
, struct diff_options
*o
,
148 regex_t
*regexp
, kwset_t kws
)
155 if (diff_populate_filespec(one
, 0))
166 assert(data
[sz
] == '\0');
167 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
169 data
+= regmatch
.rm_eo
;
170 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
175 } else { /* Classic exact string match */
177 struct kwsmatch kwsm
;
178 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
183 found
= data
+ offset
;
184 sz
-= found
- data
+ kwsm
.size
[0];
185 data
= found
+ kwsm
.size
[0];
189 diff_free_filespec_data(one
);
193 static int has_changes(struct diff_filepair
*p
, struct diff_options
*o
,
194 regex_t
*regexp
, kwset_t kws
)
196 if (!DIFF_FILE_VALID(p
->one
)) {
197 if (!DIFF_FILE_VALID(p
->two
))
198 return 0; /* ignore unmerged */
200 return contains(p
->two
, o
, regexp
, kws
) != 0;
202 if (!DIFF_FILE_VALID(p
->two
))
203 return contains(p
->one
, o
, regexp
, kws
) != 0;
204 if (!diff_unmodified_pair(p
)) {
205 return contains(p
->one
, o
, regexp
, kws
) !=
206 contains(p
->two
, o
, regexp
, kws
);
211 static void diffcore_pickaxe_count(struct diff_options
*o
)
213 const char *needle
= o
->pickaxe
;
214 int opts
= o
->pickaxe_opts
;
215 struct diff_queue_struct
*q
= &diff_queued_diff
;
216 unsigned long len
= strlen(needle
);
218 regex_t regex
, *regexp
= NULL
;
220 struct diff_queue_struct outq
;
221 DIFF_QUEUE_CLEAR(&outq
);
223 if (opts
& DIFF_PICKAXE_REGEX
) {
225 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
227 /* The POSIX.2 people are surely sick */
229 regerror(err
, ®ex
, errbuf
, 1024);
231 die("invalid pickaxe regex: %s", errbuf
);
235 kws
= kwsalloc(NULL
);
236 kwsincr(kws
, needle
, len
);
240 if (opts
& DIFF_PICKAXE_ALL
) {
241 /* Showing the whole changeset if needle exists */
242 for (i
= 0; i
< q
->nr
; i
++) {
243 struct diff_filepair
*p
= q
->queue
[i
];
244 if (has_changes(p
, o
, regexp
, kws
))
245 goto out
; /* do not munge the queue */
248 /* otherwise we will clear the whole queue
249 * by copying the empty outq at the end of this
250 * function, but first clear the current entries
253 for (i
= 0; i
< q
->nr
; i
++)
254 diff_free_filepair(q
->queue
[i
]);
257 /* Showing only the filepairs that has the needle */
258 for (i
= 0; i
< q
->nr
; i
++) {
259 struct diff_filepair
*p
= q
->queue
[i
];
260 if (has_changes(p
, o
, regexp
, kws
))
263 diff_free_filepair(p
);
270 if (opts
& DIFF_PICKAXE_REGEX
)
277 void diffcore_pickaxe(struct diff_options
*o
)
279 /* Might want to warn when both S and G are on; I don't care... */
280 if (o
->pickaxe_opts
& DIFF_PICKAXE_KIND_G
)
281 diffcore_pickaxe_grep(o
);
283 diffcore_pickaxe_count(o
);