git
/
diffcore-pickaxe.c
291 строка · 7.1 Кб
1/*
2* Copyright (C) 2005 Junio C Hamano
3* Copyright (C) 2010 Google Inc.
4*/
5#include "git-compat-util.h"
6#include "diff.h"
7#include "diffcore.h"
8#include "xdiff-interface.h"
9#include "kwset.h"
10#include "oidset.h"
11#include "pretty.h"
12#include "quote.h"
13
14typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
15struct diff_options *o,
16regex_t *regexp, kwset_t kws);
17
18struct diffgrep_cb {
19regex_t *regexp;
20int hit;
21};
22
23static int diffgrep_consume(void *priv, char *line, unsigned long len)
24{
25struct diffgrep_cb *data = priv;
26regmatch_t regmatch;
27
28if (line[0] != '+' && line[0] != '-')
29return 0;
30if (data->hit)
31BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
32if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
33®match, 0)) {
34data->hit = 1;
35return 1;
36}
37return 0;
38}
39
40static int diff_grep(mmfile_t *one, mmfile_t *two,
41struct diff_options *o,
42regex_t *regexp, kwset_t kws UNUSED)
43{
44struct diffgrep_cb ecbdata;
45xpparam_t xpp;
46xdemitconf_t xecfg;
47int ret;
48
49/*
50* We have both sides; need to run textual diff and see if
51* the pattern appears on added/deleted lines.
52*/
53memset(&xpp, 0, sizeof(xpp));
54memset(&xecfg, 0, sizeof(xecfg));
55ecbdata.regexp = regexp;
56ecbdata.hit = 0;
57xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
58xecfg.ctxlen = o->context;
59xecfg.interhunkctxlen = o->interhunkcontext;
60
61/*
62* An xdiff error might be our "data->hit" from above. See the
63* comment for xdiff_emit_line_fn in xdiff-interface.h
64*/
65ret = xdi_diff_outf(one, two, NULL, diffgrep_consume,
66&ecbdata, &xpp, &xecfg);
67if (ecbdata.hit)
68return 1;
69if (ret)
70return ret;
71return 0;
72}
73
74static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
75unsigned int limit)
76{
77unsigned int cnt = 0;
78unsigned long sz = mf->size;
79const char *data = mf->ptr;
80
81if (regexp) {
82regmatch_t regmatch;
83int flags = 0;
84
85while (sz &&
86!regexec_buf(regexp, data, sz, 1, ®match, flags)) {
87flags |= REG_NOTBOL;
88data += regmatch.rm_eo;
89sz -= regmatch.rm_eo;
90if (sz && regmatch.rm_so == regmatch.rm_eo) {
91data++;
92sz--;
93}
94cnt++;
95
96if (limit && cnt == limit)
97return cnt;
98}
99
100} else { /* Classic exact string match */
101while (sz) {
102struct kwsmatch kwsm;
103size_t offset = kwsexec(kws, data, sz, &kwsm);
104if (offset == -1)
105break;
106sz -= offset + kwsm.size[0];
107data += offset + kwsm.size[0];
108cnt++;
109
110if (limit && cnt == limit)
111return cnt;
112}
113}
114return cnt;
115}
116
117static int has_changes(mmfile_t *one, mmfile_t *two,
118struct diff_options *o UNUSED,
119regex_t *regexp, kwset_t kws)
120{
121unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
122unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
123return c1 != c2;
124}
125
126static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
127regex_t *regexp, kwset_t kws, pickaxe_fn fn)
128{
129struct userdiff_driver *textconv_one = NULL;
130struct userdiff_driver *textconv_two = NULL;
131mmfile_t mf1, mf2;
132int ret;
133
134/* ignore unmerged */
135if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
136return 0;
137
138if (o->objfind) {
139return (DIFF_FILE_VALID(p->one) &&
140oidset_contains(o->objfind, &p->one->oid)) ||
141(DIFF_FILE_VALID(p->two) &&
142oidset_contains(o->objfind, &p->two->oid));
143}
144
145if (o->flags.allow_textconv) {
146textconv_one = get_textconv(o->repo, p->one);
147textconv_two = get_textconv(o->repo, p->two);
148}
149
150/*
151* If we have an unmodified pair, we know that the count will be the
152* same and don't even have to load the blobs. Unless textconv is in
153* play, _and_ we are using two different textconv filters (e.g.,
154* because a pair is an exact rename with different textconv attributes
155* for each side, which might generate different content).
156*/
157if (textconv_one == textconv_two && diff_unmodified_pair(p))
158return 0;
159
160if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
161!o->flags.text &&
162((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
163(!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
164return 0;
165
166mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
167mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
168
169ret = fn(&mf1, &mf2, o, regexp, kws);
170
171if (textconv_one)
172free(mf1.ptr);
173if (textconv_two)
174free(mf2.ptr);
175diff_free_filespec_data(p->one);
176diff_free_filespec_data(p->two);
177
178return ret;
179}
180
181static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
182regex_t *regexp, kwset_t kws, pickaxe_fn fn)
183{
184int i;
185struct diff_queue_struct outq;
186
187DIFF_QUEUE_CLEAR(&outq);
188
189if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
190/* Showing the whole changeset if needle exists */
191for (i = 0; i < q->nr; i++) {
192struct diff_filepair *p = q->queue[i];
193if (pickaxe_match(p, o, regexp, kws, fn))
194return; /* do not munge the queue */
195}
196
197/*
198* Otherwise we will clear the whole queue by copying
199* the empty outq at the end of this function, but
200* first clear the current entries in the queue.
201*/
202for (i = 0; i < q->nr; i++)
203diff_free_filepair(q->queue[i]);
204} else {
205/* Showing only the filepairs that has the needle */
206for (i = 0; i < q->nr; i++) {
207struct diff_filepair *p = q->queue[i];
208if (pickaxe_match(p, o, regexp, kws, fn))
209diff_q(&outq, p);
210else
211diff_free_filepair(p);
212}
213}
214
215free(q->queue);
216*q = outq;
217}
218
219static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
220{
221int err = regcomp(regex, needle, cflags);
222if (err) {
223/* The POSIX.2 people are surely sick */
224char errbuf[1024];
225regerror(err, regex, errbuf, 1024);
226die("invalid regex: %s", errbuf);
227}
228}
229
230void diffcore_pickaxe(struct diff_options *o)
231{
232const char *needle = o->pickaxe;
233int opts = o->pickaxe_opts;
234regex_t regex, *regexp = NULL;
235kwset_t kws = NULL;
236pickaxe_fn fn;
237
238if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
239(!needle || !*needle))
240BUG("should have needle under -G or -S");
241if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
242int cflags = REG_EXTENDED | REG_NEWLINE;
243if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
244cflags |= REG_ICASE;
245regcomp_or_die(®ex, needle, cflags);
246regexp = ®ex;
247
248if (opts & DIFF_PICKAXE_KIND_G)
249fn = diff_grep;
250else if (opts & DIFF_PICKAXE_REGEX)
251fn = has_changes;
252else
253/*
254* We don't need to check the combination of
255* -G and --pickaxe-regex, by the time we get
256* here diff.c has already died if they're
257* combined. See the usage tests in
258* t4209-log-pickaxe.sh.
259*/
260BUG("unreachable");
261} else if (opts & DIFF_PICKAXE_KIND_S) {
262if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
263has_non_ascii(needle)) {
264struct strbuf sb = STRBUF_INIT;
265int cflags = REG_NEWLINE | REG_ICASE;
266
267basic_regex_quote_buf(&sb, needle);
268regcomp_or_die(®ex, sb.buf, cflags);
269strbuf_release(&sb);
270regexp = ®ex;
271} else {
272kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
273? tolower_trans_tbl : NULL);
274kwsincr(kws, needle, strlen(needle));
275kwsprep(kws);
276}
277fn = has_changes;
278} else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
279fn = NULL;
280} else {
281BUG("unknown pickaxe_opts flag");
282}
283
284pickaxe(&diff_queued_diff, o, regexp, kws, fn);
285
286if (regexp)
287regfree(regexp);
288if (kws)
289kwsfree(kws);
290return;
291}
292