blob: a9c6d60df22862e47ccb7fc1c7de9ed7cc4b1236 [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
22static void diffgrep_consume(void *priv, char *line, unsigned long len)
23{
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] != '-')
28 return;
29 if (data->hit)
30 /*
31 * NEEDSWORK: we should have a way to terminate the
32 * caller early.
33 */
34 return;
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +020035 data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
36 &regmatch, 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,
René Scharfedb99cb72011-10-06 18:50:18 +020041 regex_t *regexp, kwset_t kws)
Junio C Hamanof506b8e2010-08-23 10:17:03 -070042{
43 regmatch_t regmatch;
Jeff King61690bf2013-04-05 01:28:10 -040044 struct diffgrep_cb ecbdata;
45 xpparam_t xpp;
46 xdemitconf_t xecfg;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070047
Jeff King61690bf2013-04-05 01:28:10 -040048 if (!one)
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +020049 return !regexec_buf(regexp, two->ptr, two->size,
50 1, &regmatch, 0);
Jeff King61690bf2013-04-05 01:28:10 -040051 if (!two)
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +020052 return !regexec_buf(regexp, one->ptr, one->size,
53 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;
Jeff King3b40a092018-11-02 02:36:06 -040065 if (xdi_diff_outf(one, two, discard_hunk_line, diffgrep_consume,
66 &ecbdata, &xpp, &xecfg))
Jeff King3efb9882015-09-24 19:12:23 -040067 return 0;
Jeff King61690bf2013-04-05 01:28:10 -040068 return ecbdata.hit;
Junio C Hamanof506b8e2010-08-23 10:17:03 -070069}
70
René Scharfe3bdb5b92013-07-06 15:53:27 +020071static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
Junio C Hamano52e95782005-05-21 02:40:01 -070072{
Junio C Hamano2002eed2005-07-23 16:35:25 -070073 unsigned int cnt;
René Scharfece163c72009-03-03 00:00:55 +010074 unsigned long sz;
Junio C Hamano52e95782005-05-21 02:40:01 -070075 const char *data;
Junio C Hamano2002eed2005-07-23 16:35:25 -070076
Jeff Kingef90ab62012-10-28 08:27:12 -040077 sz = mf->size;
78 data = mf->ptr;
Junio C Hamano2002eed2005-07-23 16:35:25 -070079 cnt = 0;
80
Petr Baudisd01d8c62006-03-29 02:16:33 +020081 if (regexp) {
82 regmatch_t regmatch;
83 int flags = 0;
84
SZEDER Gáborf53c5de2017-03-18 19:24:08 +010085 while (sz && *data &&
Johannes Schindelinb7d36ff2016-09-21 20:24:14 +020086 !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
Petr Baudisd01d8c62006-03-29 02:16:33 +020087 flags |= REG_NOTBOL;
René Scharfe50fd6992009-03-16 19:38:42 +010088 data += regmatch.rm_eo;
SZEDER Gáborf53c5de2017-03-18 19:24:08 +010089 sz -= regmatch.rm_eo;
90 if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
René Scharfe50fd6992009-03-16 19:38:42 +010091 data++;
SZEDER Gáborf53c5de2017-03-18 19:24:08 +010092 sz--;
93 }
Junio C Hamano2002eed2005-07-23 16:35:25 -070094 cnt++;
95 }
Petr Baudisd01d8c62006-03-29 02:16:33 +020096
97 } else { /* Classic exact string match */
René Scharfece163c72009-03-03 00:00:55 +010098 while (sz) {
René Scharfe5d176fb2011-10-06 18:50:06 +020099 struct kwsmatch kwsm;
100 size_t offset = kwsexec(kws, data, sz, &kwsm);
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200101 if (offset == -1)
René Scharfece163c72009-03-03 00:00:55 +0100102 break;
René Scharfee4aab502014-03-22 18:16:00 +0100103 sz -= offset + kwsm.size[0];
104 data += offset + kwsm.size[0];
René Scharfece163c72009-03-03 00:00:55 +0100105 cnt++;
Petr Baudisd01d8c62006-03-29 02:16:33 +0200106 }
Junio C Hamano2002eed2005-07-23 16:35:25 -0700107 }
108 return cnt;
Junio C Hamano52e95782005-05-21 02:40:01 -0700109}
110
Jeff King61690bf2013-04-05 01:28:10 -0400111static int has_changes(mmfile_t *one, mmfile_t *two,
112 struct diff_options *o,
René Scharfe5d176fb2011-10-06 18:50:06 +0200113 regex_t *regexp, kwset_t kws)
René Scharfe15dafaf2011-10-06 18:26:24 +0200114{
René Scharfe3bdb5b92013-07-06 15:53:27 +0200115 unsigned int one_contains = one ? contains(one, regexp, kws) : 0;
116 unsigned int two_contains = two ? contains(two, regexp, kws) : 0;
117 return one_contains != two_contains;
Jeff King61690bf2013-04-05 01:28:10 -0400118}
119
120static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
121 regex_t *regexp, kwset_t kws, pickaxe_fn fn)
122{
Simon Ruderichbc615892013-04-04 22:20:29 +0200123 struct userdiff_driver *textconv_one = NULL;
124 struct userdiff_driver *textconv_two = NULL;
Jeff Kingef90ab62012-10-28 08:27:12 -0400125 mmfile_t mf1, mf2;
126 int ret;
127
Jeff King61690bf2013-04-05 01:28:10 -0400128 /* ignore unmerged */
129 if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
130 return 0;
131
Stefan Beller15af58c2018-01-04 14:50:42 -0800132 if (o->objfind) {
133 return (DIFF_FILE_VALID(p->one) &&
134 oidset_contains(o->objfind, &p->one->oid)) ||
135 (DIFF_FILE_VALID(p->two) &&
136 oidset_contains(o->objfind, &p->two->oid));
137 }
138
139 if (!o->pickaxe[0])
140 return 0;
141
Brandon Williams0d1e0e72017-10-31 11:19:11 -0700142 if (o->flags.allow_textconv) {
Nguyễn Thái Ngọc Duybd7ad452018-11-10 06:49:06 +0100143 textconv_one = get_textconv(o->repo, p->one);
144 textconv_two = get_textconv(o->repo, p->two);
Simon Rudericha8f61092013-04-05 15:16:30 +0200145 }
Simon Ruderichbc615892013-04-04 22:20:29 +0200146
Jeff Kingef90ab62012-10-28 08:27:12 -0400147 /*
148 * If we have an unmodified pair, we know that the count will be the
149 * same and don't even have to load the blobs. Unless textconv is in
150 * play, _and_ we are using two different textconv filters (e.g.,
151 * because a pair is an exact rename with different textconv attributes
152 * for each side, which might generate different content).
153 */
154 if (textconv_one == textconv_two && diff_unmodified_pair(p))
155 return 0;
156
Thomas Braune0e7cb82018-12-14 19:49:12 +0100157 if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
158 !o->flags.text &&
159 ((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
160 (!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
161 return 0;
162
Nguyễn Thái Ngọc Duy6afaf802018-09-21 17:57:22 +0200163 mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
164 mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
Jeff Kingef90ab62012-10-28 08:27:12 -0400165
Jeff King61690bf2013-04-05 01:28:10 -0400166 ret = fn(DIFF_FILE_VALID(p->one) ? &mf1 : NULL,
167 DIFF_FILE_VALID(p->two) ? &mf2 : NULL,
168 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;
Junio C Hamano52e95782005-05-21 02:40:01 -0700235
René Scharfe63b52af2014-03-22 18:15:57 +0100236 if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
René Scharfe218c45a2014-03-22 18:15:56 +0100237 int cflags = REG_EXTENDED | REG_NEWLINE;
Stefan Bellerc1ddc462018-01-04 14:50:40 -0800238 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
René Scharfe218c45a2014-03-22 18:15:56 +0100239 cflags |= REG_ICASE;
Nguyễn Thái Ngọc Duy3d5b23a2016-06-25 07:22:36 +0200240 regcomp_or_die(&regex, needle, cflags);
Petr Baudisd01d8c62006-03-29 02:16:33 +0200241 regexp = &regex;
Stefan Beller15af58c2018-01-04 14:50:42 -0800242 } else if (opts & DIFF_PICKAXE_KIND_S) {
243 if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
244 has_non_ascii(needle)) {
245 struct strbuf sb = STRBUF_INIT;
246 int cflags = REG_NEWLINE | REG_ICASE;
Nguyễn Thái Ngọc Duyb51a9c12016-06-25 07:22:37 +0200247
Stefan Beller15af58c2018-01-04 14:50:42 -0800248 basic_regex_quote_buf(&sb, needle);
249 regcomp_or_die(&regex, sb.buf, cflags);
250 strbuf_release(&sb);
251 regexp = &regex;
252 } else {
253 kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
254 ? tolower_trans_tbl : NULL);
255 kwsincr(kws, needle, strlen(needle));
256 kwsprep(kws);
257 }
Petr Baudisd01d8c62006-03-29 02:16:33 +0200258 }
259
René Scharfe63b52af2014-03-22 18:15:57 +0100260 pickaxe(&diff_queued_diff, o, regexp, kws,
261 (opts & DIFF_PICKAXE_KIND_G) ? diff_grep : has_changes);
Junio C Hamano367cec12005-05-27 15:55:28 -0700262
René Scharfe63b52af2014-03-22 18:15:57 +0100263 if (regexp)
264 regfree(regexp);
Stefan Beller15af58c2018-01-04 14:50:42 -0800265 if (kws)
Fredrik Kuivinenb95c5ad2011-08-21 00:41:57 +0200266 kwsfree(kws);
Junio C Hamano52e95782005-05-21 02:40:01 -0700267 return;
268}