blob: 03fcbcb40ba6ab42037d1c429658ed48ee40982f [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"
Nguyễn Thái Ngọc Duyb51a9c12016-06-25 07:22:37 +020010#include "commit.h"
11#include "quote.h"
Junio C Hamanof506b8e2010-08-23 10:17:03 -070012
Jeff King61690bf2013-04-05 01:28:10 -040013typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
14 struct diff_options *o,
15 regex_t *regexp, kwset_t kws);
16
Junio C Hamanof506b8e2010-08-23 10:17:03 -070017struct diffgrep_cb {
18 regex_t *regexp;
19 int hit;
20};
21
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +020022static int diffgrep_consume(void *priv, char *line, unsigned long len)
Junio C Hamanof506b8e2010-08-23 10:17:03 -070023{
24 struct diffgrep_cb *data = priv;
25 regmatch_t regmatch;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070026
27 if (line[0] != '+' && line[0] != '-')
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +020028 return 0;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070029 if (data->hit)
Ævar Arnfjörð Bjarmasonfa59e7b2021-04-12 19:15:26 +020030 BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
31 if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
32 &regmatch, 0)) {
33 data->hit = 1;
34 return 1;
35 }
Ævar Arnfjörð Bjarmasona8d5eb62021-04-12 19:15:24 +020036 return 0;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070037}
38
Jeff King61690bf2013-04-05 01:28:10 -040039static int diff_grep(mmfile_t *one, mmfile_t *two,
40 struct diff_options *o,
Jeff King0ada4b92022-10-17 21:09:07 -040041 regex_t *regexp, kwset_t kws UNUSED)
Junio C Hamanof506b8e2010-08-23 10:17:03 -070042{
Jeff King61690bf2013-04-05 01:28:10 -040043 struct diffgrep_cb ecbdata;
44 xpparam_t xpp;
45 xdemitconf_t xecfg;
Ævar Arnfjörð Bjarmasonfa59e7b2021-04-12 19:15:26 +020046 int ret;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070047
Jeff King61690bf2013-04-05 01:28:10 -040048 /*
49 * We have both sides; need to run textual diff and see if
50 * the pattern appears on added/deleted lines.
51 */
52 memset(&xpp, 0, sizeof(xpp));
53 memset(&xecfg, 0, sizeof(xecfg));
54 ecbdata.regexp = regexp;
55 ecbdata.hit = 0;
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +020056 xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
Jeff King61690bf2013-04-05 01:28:10 -040057 xecfg.ctxlen = o->context;
58 xecfg.interhunkctxlen = o->interhunkcontext;
Ævar Arnfjörð Bjarmasonfa59e7b2021-04-12 19:15:26 +020059
60 /*
61 * An xdiff error might be our "data->hit" from above. See the
62 * comment for xdiff_emit_line_fn in xdiff-interface.h
63 */
Ævar Arnfjörð Bjarmason5d934602021-04-12 19:15:29 +020064 ret = xdi_diff_outf(one, two, NULL, diffgrep_consume,
Ævar Arnfjörð Bjarmasonfa59e7b2021-04-12 19:15:26 +020065 &ecbdata, &xpp, &xecfg);
66 if (ecbdata.hit)
67 return 1;
68 if (ret)
69 return ret;
70 return 0;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070071}
72
Ævar Arnfjörð Bjarmason5b0672a2021-04-12 19:15:23 +020073static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
74 unsigned int limit)
Junio C Hamano52e95782005-05-21 02:40:01 -070075{
Ævar Arnfjörð Bjarmason102fdd22021-04-12 19:15:17 +020076 unsigned int cnt = 0;
77 unsigned long sz = mf->size;
78 const char *data = mf->ptr;
Junio C Hamano2002eed2005-07-23 16:35:25 -070079
Petr Baudisd01d8c62006-03-29 02:16:33 +020080 if (regexp) {
81 regmatch_t regmatch;
82 int flags = 0;
83
Ævar Arnfjörð Bjarmason52e011c2021-04-12 19:15:21 +020084 while (sz &&
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +020085 !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +020086 flags |= REG_NOTBOL;
René Scharfe50fd6992009-03-16 19:38:42 +010087 data += regmatch.rm_eo;
SZEDER Gáborf53c5de2017-03-18 19:24:08 +010088 sz -= regmatch.rm_eo;
Ævar Arnfjörð Bjarmason52e011c2021-04-12 19:15:21 +020089 if (sz && regmatch.rm_so == regmatch.rm_eo) {
René Scharfe50fd6992009-03-16 19:38:42 +010090 data++;
SZEDER Gáborf53c5de2017-03-18 19:24:08 +010091 sz--;
92 }
Junio C Hamano2002eed2005-07-23 16:35:25 -070093 cnt++;
Ævar Arnfjörð Bjarmason5b0672a2021-04-12 19:15:23 +020094
95 if (limit && cnt == limit)
96 return cnt;
Junio C Hamano2002eed2005-07-23 16:35:25 -070097 }
Petr Baudisd01d8c62006-03-29 02:16:33 +020098
99 } else { /* Classic exact string match */
René Scharfece163c72009-03-03 00:00:55 +0100100 while (sz) {
René Scharfe5d176fb2011-10-06 18:50:06 +0200101 struct kwsmatch kwsm;
102 size_t offset = kwsexec(kws, data, sz, &kwsm);
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200103 if (offset == -1)
René Scharfece163c72009-03-03 00:00:55 +0100104 break;
René Scharfee4aab502014-03-22 18:16:00 +0100105 sz -= offset + kwsm.size[0];
106 data += offset + kwsm.size[0];
René Scharfece163c72009-03-03 00:00:55 +0100107 cnt++;
Ævar Arnfjörð Bjarmason5b0672a2021-04-12 19:15:23 +0200108
109 if (limit && cnt == limit)
110 return cnt;
Petr Baudisd01d8c62006-03-29 02:16:33 +0200111 }
Junio C Hamano2002eed2005-07-23 16:35:25 -0700112 }
113 return cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -0700114}
115
Jeff King61690bf2013-04-05 01:28:10 -0400116static int has_changes(mmfile_t *one, mmfile_t *two,
Jeff King0ada4b92022-10-17 21:09:07 -0400117 struct diff_options *o UNUSED,
René Scharfe5d176fb2011-10-06 18:50:06 +0200118 regex_t *regexp, kwset_t kws)
René Scharfe15dafaf2011-10-06 18:26:24 +0200119{
Ævar Arnfjörð Bjarmason5b0672a2021-04-12 19:15:23 +0200120 unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
121 unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
Ævar Arnfjörð Bjarmason5d35a952021-04-12 19:15:22 +0200122 return c1 != c2;
Jeff King61690bf2013-04-05 01:28:10 -0400123}
124
125static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
126 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
127{
Simon Ruderichbc615892013-04-04 22:20:29 +0200128 struct userdiff_driver *textconv_one = NULL;
129 struct userdiff_driver *textconv_two = NULL;
Jeff Kingef90ab62012-10-28 08:27:12 -0400130 mmfile_t mf1, mf2;
131 int ret;
132
Jeff King61690bf2013-04-05 01:28:10 -0400133 /* ignore unmerged */
134 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
135 return 0;
136
Stefan Beller15af58c2018-01-04 14:50:42 -0800137 if (o->objfind) {
138 return (DIFF_FILE_VALID(p->one) &&
139 oidset_contains(o->objfind, &p->one->oid)) ||
140 (DIFF_FILE_VALID(p->two) &&
141 oidset_contains(o->objfind, &p->two->oid));
142 }
143
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700144 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100145 textconv_one = get_textconv(o->repo, p->one);
146 textconv_two = get_textconv(o->repo, p->two);
Simon Rudericha8f61092013-04-05 15:16:30 +0200147 }
Simon Ruderichbc615892013-04-04 22:20:29 +0200148
Jeff Kingef90ab62012-10-28 08:27:12 -0400149 /*
150 * If we have an unmodified pair, we know that the count will be the
151 * same and don't even have to load the blobs. Unless textconv is in
152 * play, _and_ we are using two different textconv filters (e.g.,
153 * because a pair is an exact rename with different textconv attributes
154 * for each side, which might generate different content).
155 */
156 if (textconv_one == textconv_two && diff_unmodified_pair(p))
157 return 0;
158
Thomas Braune0e7cb82018-12-14 19:49:12 +0100159 if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
160 !o->flags.text &&
161 ((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
162 (!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
163 return 0;
164
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200165 mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
166 mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
Jeff Kingef90ab62012-10-28 08:27:12 -0400167
Ævar Arnfjörð Bjarmasonf97fe352021-04-12 19:15:27 +0200168 ret = fn(&mf1, &mf2, o, regexp, kws);
Jeff Kingef90ab62012-10-28 08:27:12 -0400169
170 if (textconv_one)
171 free(mf1.ptr);
172 if (textconv_two)
173 free(mf2.ptr);
174 diff_free_filespec_data(p->one);
175 diff_free_filespec_data(p->two);
176
177 return ret;
René Scharfe15dafaf2011-10-06 18:26:24 +0200178}
179
René Scharfe3753bd12014-03-22 18:15:58 +0100180static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
181 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
182{
183 int i;
184 struct diff_queue_struct outq;
185
186 DIFF_QUEUE_CLEAR(&outq);
187
188 if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
189 /* Showing the whole changeset if needle exists */
190 for (i = 0; i < q->nr; i++) {
191 struct diff_filepair *p = q->queue[i];
192 if (pickaxe_match(p, o, regexp, kws, fn))
193 return; /* do not munge the queue */
194 }
195
196 /*
197 * Otherwise we will clear the whole queue by copying
198 * the empty outq at the end of this function, but
199 * first clear the current entries in the queue.
200 */
201 for (i = 0; i < q->nr; i++)
202 diff_free_filepair(q->queue[i]);
203 } else {
204 /* Showing only the filepairs that has the needle */
205 for (i = 0; i < q->nr; i++) {
206 struct diff_filepair *p = q->queue[i];
207 if (pickaxe_match(p, o, regexp, kws, fn))
208 diff_q(&outq, p);
209 else
210 diff_free_filepair(p);
211 }
212 }
213
214 free(q->queue);
215 *q = outq;
216}
217
Nguyễn Thái Ngọc Duy3d5b23a2016-06-25 07:22:36 +0200218static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
219{
220 int err = regcomp(regex, needle, cflags);
221 if (err) {
222 /* The POSIX.2 people are surely sick */
223 char errbuf[1024];
224 regerror(err, regex, errbuf, 1024);
Nguyễn Thái Ngọc Duy3d5b23a2016-06-25 07:22:36 +0200225 die("invalid regex: %s", errbuf);
226 }
227}
228
René Scharfe63b52af2014-03-22 18:15:57 +0100229void diffcore_pickaxe(struct diff_options *o)
Junio C Hamano52e95782005-05-21 02:40:01 -0700230{
Junio C Hamano382f0132010-08-31 13:44:39 -0700231 const char *needle = o->pickaxe;
232 int opts = o->pickaxe_opts;
Petr Baudisd01d8c62006-03-29 02:16:33 +0200233 regex_t regex, *regexp = NULL;
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200234 kwset_t kws = NULL;
Ævar Arnfjörð Bjarmason03c1f142021-04-12 19:15:19 +0200235 pickaxe_fn fn;
Junio C Hamano52e95782005-05-21 02:40:01 -0700236
Ævar Arnfjörð Bjarmason2e197a72021-04-12 19:15:20 +0200237 if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
238 (!needle || !*needle))
239 BUG("should have needle under -G or -S");
René Scharfe63b52af2014-03-22 18:15:57 +0100240 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
René Scharfe218c45a2014-03-22 18:15:56 +0100241 int cflags = REG_EXTENDED | REG_NEWLINE;
Stefan Bellerc1ddc462018-01-04 14:50:40 -0800242 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
René Scharfe218c45a2014-03-22 18:15:56 +0100243 cflags |= REG_ICASE;
Nguyễn Thái Ngọc Duy3d5b23a2016-06-25 07:22:36 +0200244 regcomp_or_die(&regex, needle, cflags);
Petr Baudisd01d8c62006-03-29 02:16:33 +0200245 regexp = &regex;
Ævar Arnfjörð Bjarmason03c1f142021-04-12 19:15:19 +0200246
247 if (opts & DIFF_PICKAXE_KIND_G)
248 fn = diff_grep;
249 else if (opts & DIFF_PICKAXE_REGEX)
250 fn = has_changes;
251 else
252 /*
253 * We don't need to check the combination of
254 * -G and --pickaxe-regex, by the time we get
255 * here diff.c has already died if they're
256 * combined. See the usage tests in
257 * t4209-log-pickaxe.sh.
258 */
259 BUG("unreachable");
Stefan Beller15af58c2018-01-04 14:50:42 -0800260 } else if (opts & DIFF_PICKAXE_KIND_S) {
261 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
262 has_non_ascii(needle)) {
263 struct strbuf sb = STRBUF_INIT;
264 int cflags = REG_NEWLINE | REG_ICASE;
Nguyễn Thái Ngọc Duyb51a9c12016-06-25 07:22:37 +0200265
Stefan Beller15af58c2018-01-04 14:50:42 -0800266 basic_regex_quote_buf(&sb, needle);
267 regcomp_or_die(&regex, sb.buf, cflags);
268 strbuf_release(&sb);
269 regexp = &regex;
270 } else {
271 kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
272 ? tolower_trans_tbl : NULL);
273 kwsincr(kws, needle, strlen(needle));
274 kwsprep(kws);
275 }
Ævar Arnfjörð Bjarmason03c1f142021-04-12 19:15:19 +0200276 fn = has_changes;
277 } else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
278 fn = NULL;
279 } else {
280 BUG("unknown pickaxe_opts flag");
Petr Baudisd01d8c62006-03-29 02:16:33 +0200281 }
282
Ævar Arnfjörð Bjarmason03c1f142021-04-12 19:15:19 +0200283 pickaxe(&diff_queued_diff, o, regexp, kws, fn);
Junio C Hamano367cec12005-05-27 15:55:28 -0700284
René Scharfe63b52af2014-03-22 18:15:57 +0100285 if (regexp)
286 regfree(regexp);
Stefan Beller15af58c2018-01-04 14:50:42 -0800287 if (kws)
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200288 kwsfree(kws);
Junio C Hamano52e95782005-05-21 02:40:01 -0700289 return;
290}