Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 267 lines (235 sloc) 6.276 kB
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
1 /*
2 * Copyright (C) 2005 Junio C Hamano
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
3 * Copyright (C) 2010 Google Inc.
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
4 */
5 #include "cache.h"
6 #include "diff.h"
7 #include "diffcore.h"
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
8 #include "xdiff-interface.h"
b95c5ad @frekui Use kwset in pickaxe
frekui authored
9 #include "kwset.h"
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
10
8a94151 pickaxe: factor out pickaxe
René Scharfe authored
11 typedef int (*pickaxe_fn)(struct diff_filepair *p, struct diff_options *o, regex_t *regexp, kwset_t kws);
12
13 static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
14 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
15 {
16 int i;
17 struct diff_queue_struct outq;
18
19 DIFF_QUEUE_CLEAR(&outq);
20
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 */
27 }
28
29 /*
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.
33 */
34 for (i = 0; i < q->nr; i++)
35 diff_free_filepair(q->queue[i]);
36 } else {
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))
41 diff_q(&outq, p);
42 else
43 diff_free_filepair(p);
44 }
45 }
46
47 free(q->queue);
48 *q = outq;
49 }
50
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
51 struct diffgrep_cb {
52 regex_t *regexp;
53 int hit;
54 };
55
56 static void diffgrep_consume(void *priv, char *line, unsigned long len)
57 {
58 struct diffgrep_cb *data = priv;
59 regmatch_t regmatch;
60 int hold;
61
62 if (line[0] != '+' && line[0] != '-')
63 return;
64 if (data->hit)
65 /*
66 * NEEDSWORK: we should have a way to terminate the
67 * caller early.
68 */
69 return;
70 /* Yuck -- line ought to be "const char *"! */
71 hold = line[len];
72 line[len] = '\0';
73 data->hit = !regexec(data->regexp, line + 1, 1, &regmatch, 0);
74 line[len] = hold;
75 }
76
77 static void fill_one(struct diff_filespec *one,
78 mmfile_t *mf, struct userdiff_driver **textconv)
79 {
80 if (DIFF_FILE_VALID(one)) {
81 *textconv = get_textconv(one);
82 mf->size = fill_textconv(*textconv, one, &mf->ptr);
83 } else {
84 memset(mf, 0, sizeof(*mf));
85 }
86 }
87
db99cb7 pickaxe: give diff_grep the same signature as has_changes
René Scharfe authored
88 static int diff_grep(struct diff_filepair *p, struct diff_options *o,
89 regex_t *regexp, kwset_t kws)
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
90 {
91 regmatch_t regmatch;
92 struct userdiff_driver *textconv_one = NULL;
93 struct userdiff_driver *textconv_two = NULL;
94 mmfile_t mf1, mf2;
95 int hit;
96
97 if (diff_unmodified_pair(p))
98 return 0;
99
100 fill_one(p->one, &mf1, &textconv_one);
101 fill_one(p->two, &mf2, &textconv_two);
102
103 if (!mf1.ptr) {
104 if (!mf2.ptr)
105 return 0; /* ignore unmerged */
106 /* created "two" -- does it have what we are looking for? */
107 hit = !regexec(regexp, p->two->data, 1, &regmatch, 0);
108 } else if (!mf2.ptr) {
109 /* removed "one" -- did it have what we are looking for? */
110 hit = !regexec(regexp, p->one->data, 1, &regmatch, 0);
111 } else {
112 /*
113 * We have both sides; need to run textual diff and see if
114 * the pattern appears on added/deleted lines.
115 */
116 struct diffgrep_cb ecbdata;
117 xpparam_t xpp;
118 xdemitconf_t xecfg;
119
120 memset(&xpp, 0, sizeof(xpp));
121 memset(&xecfg, 0, sizeof(xecfg));
122 ecbdata.regexp = regexp;
123 ecbdata.hit = 0;
124 xecfg.ctxlen = o->context;
125 xecfg.interhunkctxlen = o->interhunkcontext;
126 xdi_diff_outf(&mf1, &mf2, diffgrep_consume, &ecbdata,
127 &xpp, &xecfg);
128 hit = ecbdata.hit;
129 }
130 if (textconv_one)
131 free(mf1.ptr);
132 if (textconv_two)
133 free(mf2.ptr);
134 return hit;
135 }
136
137 static void diffcore_pickaxe_grep(struct diff_options *o)
138 {
8a94151 pickaxe: factor out pickaxe
René Scharfe authored
139 int err;
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
140 regex_t regex;
accccde @gitster pickaxe: allow -i to search in patch case-insensitively
authored
141 int cflags = REG_EXTENDED | REG_NEWLINE;
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
142
accccde @gitster pickaxe: allow -i to search in patch case-insensitively
authored
143 if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
144 cflags |= REG_ICASE;
145
146 err = regcomp(&regex, o->pickaxe, cflags);
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
147 if (err) {
148 char errbuf[1024];
149 regerror(err, &regex, errbuf, 1024);
150 regfree(&regex);
151 die("invalid log-grep regex: %s", errbuf);
152 }
153
8a94151 pickaxe: factor out pickaxe
René Scharfe authored
154 pickaxe(&diff_queued_diff, o, &regex, NULL, diff_grep);
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
155
2b5f07f pickaxe: plug regex leak
René Scharfe authored
156 regfree(&regex);
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
157 return;
158 }
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
159
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
160 static unsigned int contains(struct diff_filespec *one, struct diff_options *o,
b95c5ad @frekui Use kwset in pickaxe
frekui authored
161 regex_t *regexp, kwset_t kws)
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
162 {
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
163 unsigned int cnt;
ce163c7 diffcore-pickaxe: use memmem()
René Scharfe authored
164 unsigned long sz;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
165 const char *data;
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
166 if (!o->pickaxe[0])
e1b1611 @peff diffcore-pickaxe: fix infinite loop on zero-length needle
peff authored
167 return 0;
05ac978 pickaxe: plug diff filespec leak with empty needle
René Scharfe authored
168 if (diff_populate_filespec(one, 0))
169 return 0;
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
170
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
171 sz = one->size;
172 data = one->data;
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
173 cnt = 0;
174
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
175 if (regexp) {
176 regmatch_t regmatch;
177 int flags = 0;
178
50fd699 pickaxe: count regex matches only once
René Scharfe authored
179 assert(data[sz] == '\0');
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
180 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
181 flags |= REG_NOTBOL;
50fd699 pickaxe: count regex matches only once
René Scharfe authored
182 data += regmatch.rm_eo;
183 if (*data && regmatch.rm_so == regmatch.rm_eo)
184 data++;
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
185 cnt++;
186 }
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
187
188 } else { /* Classic exact string match */
ce163c7 diffcore-pickaxe: use memmem()
René Scharfe authored
189 while (sz) {
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
190 struct kwsmatch kwsm;
191 size_t offset = kwsexec(kws, data, sz, &kwsm);
b95c5ad @frekui Use kwset in pickaxe
frekui authored
192 const char *found;
193 if (offset == -1)
ce163c7 diffcore-pickaxe: use memmem()
René Scharfe authored
194 break;
b95c5ad @frekui Use kwset in pickaxe
frekui authored
195 else
196 found = data + offset;
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
197 sz -= found - data + kwsm.size[0];
198 data = found + kwsm.size[0];
ce163c7 diffcore-pickaxe: use memmem()
René Scharfe authored
199 cnt++;
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
200 }
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
201 }
a0cb940 diff -S: release the image after looking for needle in it
Junio C Hamano authored
202 diff_free_filespec_data(one);
2002eed [PATCH] diffcore-pickaxe: switch to "counting" behaviour.
Junio C Hamano authored
203 return cnt;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
204 }
205
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
206 static int has_changes(struct diff_filepair *p, struct diff_options *o,
207 regex_t *regexp, kwset_t kws)
15dafaf pickaxe: factor out has_changes
René Scharfe authored
208 {
209 if (!DIFF_FILE_VALID(p->one)) {
210 if (!DIFF_FILE_VALID(p->two))
211 return 0; /* ignore unmerged */
212 /* created */
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
213 return contains(p->two, o, regexp, kws) != 0;
15dafaf pickaxe: factor out has_changes
René Scharfe authored
214 }
215 if (!DIFF_FILE_VALID(p->two))
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
216 return contains(p->one, o, regexp, kws) != 0;
15dafaf pickaxe: factor out has_changes
René Scharfe authored
217 if (!diff_unmodified_pair(p)) {
5d176fb pickaxe: pass diff_options to contains and has_changes
René Scharfe authored
218 return contains(p->one, o, regexp, kws) !=
219 contains(p->two, o, regexp, kws);
15dafaf pickaxe: factor out has_changes
René Scharfe authored
220 }
221 return 0;
222 }
223
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
224 static void diffcore_pickaxe_count(struct diff_options *o)
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
225 {
382f013 @gitster diff: pass the entire diff-options to diffcore_pickaxe()
authored
226 const char *needle = o->pickaxe;
227 int opts = o->pickaxe_opts;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
228 unsigned long len = strlen(needle);
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
229 regex_t regex, *regexp = NULL;
b95c5ad @frekui Use kwset in pickaxe
frekui authored
230 kwset_t kws = NULL;
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
231
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
232 if (opts & DIFF_PICKAXE_REGEX) {
233 int err;
234 err = regcomp(&regex, needle, REG_EXTENDED | REG_NEWLINE);
235 if (err) {
236 /* The POSIX.2 people are surely sick */
237 char errbuf[1024];
238 regerror(err, &regex, errbuf, 1024);
239 regfree(&regex);
240 die("invalid pickaxe regex: %s", errbuf);
241 }
242 regexp = &regex;
b95c5ad @frekui Use kwset in pickaxe
frekui authored
243 } else {
accccde @gitster pickaxe: allow -i to search in patch case-insensitively
authored
244 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
245 ? tolower_trans_tbl : NULL);
b95c5ad @frekui Use kwset in pickaxe
frekui authored
246 kwsincr(kws, needle, len);
247 kwsprep(kws);
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
248 }
249
8a94151 pickaxe: factor out pickaxe
René Scharfe authored
250 pickaxe(&diff_queued_diff, o, regexp, kws, has_changes);
8e854b0 pickaxe: plug regex/kws leak
René Scharfe authored
251
95ae69b @drafnel diffcore-pickaxe.c: remove unnecessary curly braces
drafnel authored
252 if (opts & DIFF_PICKAXE_REGEX)
d01d8c6 Support for pickaxe matching regular expressions
Petr Baudis authored
253 regfree(&regex);
b95c5ad @frekui Use kwset in pickaxe
frekui authored
254 else
255 kwsfree(kws);
52e9578 [PATCH] Introducing software archaeologist's tool "pickaxe".
Junio C Hamano authored
256 return;
257 }
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
258
259 void diffcore_pickaxe(struct diff_options *o)
260 {
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)
8520913 @drafnel diffcore-pickaxe.c: a void function shouldn't try to return something
drafnel authored
263 diffcore_pickaxe_grep(o);
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
264 else
8520913 @drafnel diffcore-pickaxe.c: a void function shouldn't try to return something
drafnel authored
265 diffcore_pickaxe_count(o);
f506b8e @gitster git log/diff: add -G<regexp> that greps in the patch text
authored
266 }
Something went wrong with that request. Please try again.