2 * Copyright (C) 2005 Junio C Hamano
3 * Copyright (C) 2010 Google Inc.
8 #include "xdiff-interface.h"
11 typedef int (*pickaxe_fn
)(struct diff_filepair
*p
, struct diff_options
*o
, regex_t
*regexp
, kwset_t kws
);
13 static void pickaxe(struct diff_queue_struct
*q
, struct diff_options
*o
,
14 regex_t
*regexp
, kwset_t kws
, pickaxe_fn fn
)
17 struct diff_queue_struct outq
;
19 DIFF_QUEUE_CLEAR(&outq
);
21 if (o
->pickaxe_opts
& DIFF_PICKAXE_ALL
) {
22 /* Showing the whole changeset if needle exists */
23 for (i
= 0; i
< q
->nr
; i
++) {
24 struct diff_filepair
*p
= q
->queue
[i
];
25 if (fn(p
, o
, regexp
, kws
))
26 return; /* do not munge the queue */
30 * Otherwise we will clear the whole queue by copying
31 * the empty outq at the end of this function, but
32 * first clear the current entries in the queue.
34 for (i
= 0; i
< q
->nr
; i
++)
35 diff_free_filepair(q
->queue
[i
]);
37 /* Showing only the filepairs that has the needle */
38 for (i
= 0; i
< q
->nr
; i
++) {
39 struct diff_filepair
*p
= q
->queue
[i
];
40 if (fn(p
, o
, regexp
, kws
))
43 diff_free_filepair(p
);
56 static void diffgrep_consume(void *priv
, char *line
, unsigned long len
)
58 struct diffgrep_cb
*data
= priv
;
62 if (line
[0] != '+' && line
[0] != '-')
66 * NEEDSWORK: we should have a way to terminate the
70 /* Yuck -- line ought to be "const char *"! */
73 data
->hit
= !regexec(data
->regexp
, line
+ 1, 1, ®match
, 0);
77 static void fill_one(struct diff_filespec
*one
,
78 mmfile_t
*mf
, struct userdiff_driver
**textconv
)
80 if (DIFF_FILE_VALID(one
)) {
81 *textconv
= get_textconv(one
);
82 mf
->size
= fill_textconv(*textconv
, one
, &mf
->ptr
);
84 memset(mf
, 0, sizeof(*mf
));
88 static int diff_grep(struct diff_filepair
*p
, struct diff_options
*o
,
89 regex_t
*regexp
, kwset_t kws
)
92 struct userdiff_driver
*textconv_one
= NULL
;
93 struct userdiff_driver
*textconv_two
= NULL
;
97 if (diff_unmodified_pair(p
))
100 fill_one(p
->one
, &mf1
, &textconv_one
);
101 fill_one(p
->two
, &mf2
, &textconv_two
);
105 return 0; /* ignore unmerged */
106 /* created "two" -- does it have what we are looking for? */
107 hit
= !regexec(regexp
, mf2
.ptr
, 1, ®match
, 0);
108 } else if (!mf2
.ptr
) {
109 /* removed "one" -- did it have what we are looking for? */
110 hit
= !regexec(regexp
, mf1
.ptr
, 1, ®match
, 0);
113 * We have both sides; need to run textual diff and see if
114 * the pattern appears on added/deleted lines.
116 struct diffgrep_cb ecbdata
;
120 memset(&xpp
, 0, sizeof(xpp
));
121 memset(&xecfg
, 0, sizeof(xecfg
));
122 ecbdata
.regexp
= regexp
;
124 xecfg
.ctxlen
= o
->context
;
125 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
126 xdi_diff_outf(&mf1
, &mf2
, diffgrep_consume
, &ecbdata
,
137 static void diffcore_pickaxe_grep(struct diff_options
*o
)
141 int cflags
= REG_EXTENDED
| REG_NEWLINE
;
143 if (DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
))
146 err
= regcomp(®ex
, o
->pickaxe
, cflags
);
149 regerror(err
, ®ex
, errbuf
, 1024);
151 die("invalid log-grep regex: %s", errbuf
);
154 pickaxe(&diff_queued_diff
, o
, ®ex
, NULL
, diff_grep
);
160 static unsigned int contains(struct diff_filespec
*one
, struct diff_options
*o
,
161 regex_t
*regexp
, kwset_t kws
)
168 if (diff_populate_filespec(one
, 0))
179 assert(data
[sz
] == '\0');
180 while (*data
&& !regexec(regexp
, data
, 1, ®match
, flags
)) {
182 data
+= regmatch
.rm_eo
;
183 if (*data
&& regmatch
.rm_so
== regmatch
.rm_eo
)
188 } else { /* Classic exact string match */
190 struct kwsmatch kwsm
;
191 size_t offset
= kwsexec(kws
, data
, sz
, &kwsm
);
196 found
= data
+ offset
;
197 sz
-= found
- data
+ kwsm
.size
[0];
198 data
= found
+ kwsm
.size
[0];
202 diff_free_filespec_data(one
);
206 static int has_changes(struct diff_filepair
*p
, struct diff_options
*o
,
207 regex_t
*regexp
, kwset_t kws
)
209 if (!DIFF_FILE_VALID(p
->one
)) {
210 if (!DIFF_FILE_VALID(p
->two
))
211 return 0; /* ignore unmerged */
213 return contains(p
->two
, o
, regexp
, kws
) != 0;
215 if (!DIFF_FILE_VALID(p
->two
))
216 return contains(p
->one
, o
, regexp
, kws
) != 0;
217 if (!diff_unmodified_pair(p
)) {
218 return contains(p
->one
, o
, regexp
, kws
) !=
219 contains(p
->two
, o
, regexp
, kws
);
224 static void diffcore_pickaxe_count(struct diff_options
*o
)
226 const char *needle
= o
->pickaxe
;
227 int opts
= o
->pickaxe_opts
;
228 unsigned long len
= strlen(needle
);
229 regex_t regex
, *regexp
= NULL
;
232 if (opts
& DIFF_PICKAXE_REGEX
) {
234 err
= regcomp(®ex
, needle
, REG_EXTENDED
| REG_NEWLINE
);
236 /* The POSIX.2 people are surely sick */
238 regerror(err
, ®ex
, errbuf
, 1024);
240 die("invalid pickaxe regex: %s", errbuf
);
244 kws
= kwsalloc(DIFF_OPT_TST(o
, PICKAXE_IGNORE_CASE
)
245 ? tolower_trans_tbl
: NULL
);
246 kwsincr(kws
, needle
, len
);
250 pickaxe(&diff_queued_diff
, o
, regexp
, kws
, has_changes
);
252 if (opts
& DIFF_PICKAXE_REGEX
)
259 void diffcore_pickaxe(struct diff_options
*o
)
261 /* Might want to warn when both S and G are on; I don't care... */
262 if (o
->pickaxe_opts
& DIFF_PICKAXE_KIND_G
)
263 diffcore_pickaxe_grep(o
);
265 diffcore_pickaxe_count(o
);