blob: 185f86b2840d3337eac9fb2b17b260ca0c53fbab [file] [log] [blame]
Junio C Hamano52e95782005-05-21 02:40:01 -07001/*
2 * Copyright (C) 2005 Junio C Hamano
Junio C Hamanof506b8e2010-08-23 10:17:03 -07003 * Copyright (C) 2010 Google Inc.
Junio C Hamano52e95782005-05-21 02:40:01 -07004 */
5#include "cache.h"
6#include "diff.h"
7#include "diffcore.h"
Junio C Hamanof506b8e2010-08-23 10:17:03 -07008#include "xdiff-interface.h"
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +02009#include "kwset.h"
Junio C Hamanof506b8e2010-08-23 10:17:03 -070010
Jeff King61690bf2013-04-05 01:28:10 -040011typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
12 struct diff_options *o,
13 regex_t *regexp, kwset_t kws);
14
Junio C Hamanof506b8e2010-08-23 10:17:03 -070015struct diffgrep_cb {
16 regex_t *regexp;
17 int hit;
18};
19
20static void diffgrep_consume(void *priv, char *line, unsigned long len)
21{
22 struct diffgrep_cb *data = priv;
23 regmatch_t regmatch;
24 int hold;
25
26 if (line[0] != '+' && line[0] != '-')
27 return;
28 if (data->hit)
29 /*
30 * NEEDSWORK: we should have a way to terminate the
31 * caller early.
32 */
33 return;
34 /* Yuck -- line ought to be "const char *"! */
35 hold = line[len];
36 line[len] = '\0';
37 data->hit = !regexec(data->regexp, line + 1, 1, &regmatch, 0);
38 line[len] = hold;
39}
40
Jeff King61690bf2013-04-05 01:28:10 -040041static int diff_grep(mmfile_t *one, mmfile_t *two,
42 struct diff_options *o,
René Scharfedb99cb72011-10-06 18:50:18 +020043 regex_t *regexp, kwset_t kws)
Junio C Hamanof506b8e2010-08-23 10:17:03 -070044{
45 regmatch_t regmatch;
Jeff King61690bf2013-04-05 01:28:10 -040046 struct diffgrep_cb ecbdata;
47 xpparam_t xpp;
48 xdemitconf_t xecfg;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070049
Jeff King61690bf2013-04-05 01:28:10 -040050 if (!one)
51 return !regexec(regexp, two->ptr, 1, &regmatch, 0);
52 if (!two)
53 return !regexec(regexp, one->ptr, 1, &regmatch, 0);
Junio C Hamanof506b8e2010-08-23 10:17:03 -070054
Jeff King61690bf2013-04-05 01:28:10 -040055 /*
56 * We have both sides; need to run textual diff and see if
57 * the pattern appears on added/deleted lines.
58 */
59 memset(&xpp, 0, sizeof(xpp));
60 memset(&xecfg, 0, sizeof(xecfg));
61 ecbdata.regexp = regexp;
62 ecbdata.hit = 0;
63 xecfg.ctxlen = o->context;
64 xecfg.interhunkctxlen = o->interhunkcontext;
65 xdi_diff_outf(one, two, diffgrep_consume, &ecbdata,
66 &xpp, &xecfg);
67 return ecbdata.hit;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070068}
69
René Scharfe3bdb5b92013-07-06 15:53:27 +020070static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
Junio C Hamano52e95782005-05-21 02:40:01 -070071{
Junio C Hamano2002eed2005-07-23 16:35:25 -070072 unsigned int cnt;
René Scharfece163c72009-03-03 00:00:55 +010073 unsigned long sz;
Junio C Hamano52e95782005-05-21 02:40:01 -070074 const char *data;
Junio C Hamano2002eed2005-07-23 16:35:25 -070075
Jeff Kingef90ab62012-10-28 08:27:12 -040076 sz = mf->size;
77 data = mf->ptr;
Junio C Hamano2002eed2005-07-23 16:35:25 -070078 cnt = 0;
79
Petr Baudisd01d8c62006-03-29 02:16:33 +020080 if (regexp) {
81 regmatch_t regmatch;
82 int flags = 0;
83
René Scharfe50fd6992009-03-16 19:38:42 +010084 assert(data[sz] == '\0');
Petr Baudisd01d8c62006-03-29 02:16:33 +020085 while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
86 flags |= REG_NOTBOL;
René Scharfe50fd6992009-03-16 19:38:42 +010087 data += regmatch.rm_eo;
88 if (*data && regmatch.rm_so == regmatch.rm_eo)
89 data++;
Junio C Hamano2002eed2005-07-23 16:35:25 -070090 cnt++;
91 }
Petr Baudisd01d8c62006-03-29 02:16:33 +020092
93 } else { /* Classic exact string match */
René Scharfece163c72009-03-03 00:00:55 +010094 while (sz) {
René Scharfe5d176fb2011-10-06 18:50:06 +020095 struct kwsmatch kwsm;
96 size_t offset = kwsexec(kws, data, sz, &kwsm);
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +020097 if (offset == -1)
René Scharfece163c72009-03-03 00:00:55 +010098 break;
René Scharfee4aab502014-03-22 18:16:00 +010099 sz -= offset + kwsm.size[0];
100 data += offset + kwsm.size[0];
René Scharfece163c72009-03-03 00:00:55 +0100101 cnt++;
Petr Baudisd01d8c62006-03-29 02:16:33 +0200102 }
Junio C Hamano2002eed2005-07-23 16:35:25 -0700103 }
104 return cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -0700105}
106
Jeff King61690bf2013-04-05 01:28:10 -0400107static int has_changes(mmfile_t *one, mmfile_t *two,
108 struct diff_options *o,
René Scharfe5d176fb2011-10-06 18:50:06 +0200109 regex_t *regexp, kwset_t kws)
René Scharfe15dafaf2011-10-06 18:26:24 +0200110{
René Scharfe3bdb5b92013-07-06 15:53:27 +0200111 unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
112 unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
113 return one_contains != two_contains;
Jeff King61690bf2013-04-05 01:28:10 -0400114}
115
116static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
117 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
118{
Simon Ruderichbc615892013-04-04 22:20:29 +0200119 struct userdiff_driver *textconv_one = NULL;
120 struct userdiff_driver *textconv_two = NULL;
Jeff Kingef90ab62012-10-28 08:27:12 -0400121 mmfile_t mf1, mf2;
122 int ret;
123
Jeff King8fa4b092012-10-28 08:34:06 -0400124 if (!o->pickaxe[0])
125 return 0;
126
Jeff King61690bf2013-04-05 01:28:10 -0400127 /* ignore unmerged */
128 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
129 return 0;
130
Simon Rudericha8f61092013-04-05 15:16:30 +0200131 if (DIFF_OPT_TST(o, ALLOW_TEXTCONV)) {
132 textconv_one = get_textconv(p->one);
133 textconv_two = get_textconv(p->two);
134 }
Simon Ruderichbc615892013-04-04 22:20:29 +0200135
Jeff Kingef90ab62012-10-28 08:27:12 -0400136 /*
137 * If we have an unmodified pair, we know that the count will be the
138 * same and don't even have to load the blobs. Unless textconv is in
139 * play, _and_ we are using two different textconv filters (e.g.,
140 * because a pair is an exact rename with different textconv attributes
141 * for each side, which might generate different content).
142 */
143 if (textconv_one == textconv_two && diff_unmodified_pair(p))
144 return 0;
145
Jeff King7cdb9b42013-04-04 20:08:47 -0400146 mf1.size = fill_textconv(textconv_one, p->one, &mf1.ptr);
147 mf2.size = fill_textconv(textconv_two, p->two, &mf2.ptr);
Jeff Kingef90ab62012-10-28 08:27:12 -0400148
Jeff King61690bf2013-04-05 01:28:10 -0400149 ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
150 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
151 o, regexp, kws);
Jeff Kingef90ab62012-10-28 08:27:12 -0400152
153 if (textconv_one)
154 free(mf1.ptr);
155 if (textconv_two)
156 free(mf2.ptr);
157 diff_free_filespec_data(p->one);
158 diff_free_filespec_data(p->two);
159
160 return ret;
René Scharfe15dafaf2011-10-06 18:26:24 +0200161}
162
René Scharfe3753bd12014-03-22 18:15:58 +0100163static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
164 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
165{
166 int i;
167 struct diff_queue_struct outq;
168
169 DIFF_QUEUE_CLEAR(&outq);
170
171 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
172 /* Showing the whole changeset if needle exists */
173 for (i = 0; i < q->nr; i++) {
174 struct diff_filepair *p = q->queue[i];
175 if (pickaxe_match(p, o, regexp, kws, fn))
176 return; /* do not munge the queue */
177 }
178
179 /*
180 * Otherwise we will clear the whole queue by copying
181 * the empty outq at the end of this function, but
182 * first clear the current entries in the queue.
183 */
184 for (i = 0; i < q->nr; i++)
185 diff_free_filepair(q->queue[i]);
186 } else {
187 /* Showing only the filepairs that has the needle */
188 for (i = 0; i < q->nr; i++) {
189 struct diff_filepair *p = q->queue[i];
190 if (pickaxe_match(p, o, regexp, kws, fn))
191 diff_q(&outq, p);
192 else
193 diff_free_filepair(p);
194 }
195 }
196
197 free(q->queue);
198 *q = outq;
199}
200
René Scharfe63b52af2014-03-22 18:15:57 +0100201void diffcore_pickaxe(struct diff_options *o)
Junio C Hamano52e95782005-05-21 02:40:01 -0700202{
Junio C Hamano382f0132010-08-31 13:44:39 -0700203 const char *needle = o->pickaxe;
204 int opts = o->pickaxe_opts;
Petr Baudisd01d8c62006-03-29 02:16:33 +0200205 regex_t regex, *regexp = NULL;
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200206 kwset_t kws = NULL;
Junio C Hamano52e95782005-05-21 02:40:01 -0700207
René Scharfe63b52af2014-03-22 18:15:57 +0100208 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +0200209 int err;
René Scharfe218c45a2014-03-22 18:15:56 +0100210 int cflags = REG_EXTENDED | REG_NEWLINE;
211 if (DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE))
212 cflags |= REG_ICASE;
213 err = regcomp(&regex, needle, cflags);
Petr Baudisd01d8c62006-03-29 02:16:33 +0200214 if (err) {
215 /* The POSIX.2 people are surely sick */
216 char errbuf[1024];
217 regerror(err, &regex, errbuf, 1024);
218 regfree(&regex);
Ramkumar Ramachandra276b22d2013-05-31 17:42:14 +0530219 die("invalid regex: %s", errbuf);
Petr Baudisd01d8c62006-03-29 02:16:33 +0200220 }
221 regexp = &regex;
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200222 } else {
Junio C Hamanoaccccde2012-02-21 01:02:46 -0800223 kws = kwsalloc(DIFF_OPT_TST(o, PICKAXE_IGNORE_CASE)
224 ? tolower_trans_tbl : NULL);
René Scharfe542b2aa2014-03-22 18:15:59 +0100225 kwsincr(kws, needle, strlen(needle));
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200226 kwsprep(kws);
Petr Baudisd01d8c62006-03-29 02:16:33 +0200227 }
228
René Scharfe63b52af2014-03-22 18:15:57 +0100229 /* Might want to warn when both S and G are on; I don't care... */
230 pickaxe(&diff_queued_diff, o, regexp, kws,
231 (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
Junio C Hamano367cec12005-05-27 15:55:28 -0700232
René Scharfe63b52af2014-03-22 18:15:57 +0100233 if (regexp)
234 regfree(regexp);
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200235 else
236 kwsfree(kws);
Junio C Hamano52e95782005-05-21 02:40:01 -0700237 return;
238}