Coverage Report

Created: 2024-09-08 06:23

/src/git/diffcore-pickaxe.c
Line
Count
Source (jump to first uncovered line)
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
14
typedef int (*pickaxe_fn)(mmfile_t *one, mmfile_t *two,
15
        struct diff_options *o,
16
        regex_t *regexp, kwset_t kws);
17
18
struct diffgrep_cb {
19
  regex_t *regexp;
20
  int hit;
21
};
22
23
static int diffgrep_consume(void *priv, char *line, unsigned long len)
24
0
{
25
0
  struct diffgrep_cb *data = priv;
26
0
  regmatch_t regmatch;
27
28
0
  if (line[0] != '+' && line[0] != '-')
29
0
    return 0;
30
0
  if (data->hit)
31
0
    BUG("Already matched in diffgrep_consume! Broken xdiff_emit_line_fn?");
32
0
  if (!regexec_buf(data->regexp, line + 1, len - 1, 1,
33
0
       &regmatch, 0)) {
34
0
    data->hit = 1;
35
0
    return 1;
36
0
  }
37
0
  return 0;
38
0
}
39
40
static int diff_grep(mmfile_t *one, mmfile_t *two,
41
         struct diff_options *o,
42
         regex_t *regexp, kwset_t kws UNUSED)
43
0
{
44
0
  struct diffgrep_cb ecbdata;
45
0
  xpparam_t xpp;
46
0
  xdemitconf_t xecfg;
47
0
  int 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
   */
53
0
  memset(&xpp, 0, sizeof(xpp));
54
0
  memset(&xecfg, 0, sizeof(xecfg));
55
0
  ecbdata.regexp = regexp;
56
0
  ecbdata.hit = 0;
57
0
  xecfg.flags = XDL_EMIT_NO_HUNK_HDR;
58
0
  xecfg.ctxlen = o->context;
59
0
  xecfg.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
   */
65
0
  ret = xdi_diff_outf(one, two, NULL, diffgrep_consume,
66
0
          &ecbdata, &xpp, &xecfg);
67
0
  if (ecbdata.hit)
68
0
    return 1;
69
0
  if (ret)
70
0
    return ret;
71
0
  return 0;
72
0
}
73
74
static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws,
75
           unsigned int limit)
76
0
{
77
0
  unsigned int cnt = 0;
78
0
  unsigned long sz = mf->size;
79
0
  const char *data = mf->ptr;
80
81
0
  if (regexp) {
82
0
    regmatch_t regmatch;
83
0
    int flags = 0;
84
85
0
    while (sz &&
86
0
           !regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
87
0
      flags |= REG_NOTBOL;
88
0
      data += regmatch.rm_eo;
89
0
      sz -= regmatch.rm_eo;
90
0
      if (sz && regmatch.rm_so == regmatch.rm_eo) {
91
0
        data++;
92
0
        sz--;
93
0
      }
94
0
      cnt++;
95
96
0
      if (limit && cnt == limit)
97
0
        return cnt;
98
0
    }
99
100
0
  } else { /* Classic exact string match */
101
0
    while (sz) {
102
0
      struct kwsmatch kwsm;
103
0
      size_t offset = kwsexec(kws, data, sz, &kwsm);
104
0
      if (offset == -1)
105
0
        break;
106
0
      sz -= offset + kwsm.size[0];
107
0
      data += offset + kwsm.size[0];
108
0
      cnt++;
109
110
0
      if (limit && cnt == limit)
111
0
        return cnt;
112
0
    }
113
0
  }
114
0
  return cnt;
115
0
}
116
117
static int has_changes(mmfile_t *one, mmfile_t *two,
118
           struct diff_options *o UNUSED,
119
           regex_t *regexp, kwset_t kws)
120
0
{
121
0
  unsigned int c1 = one ? contains(one, regexp, kws, 0) : 0;
122
0
  unsigned int c2 = two ? contains(two, regexp, kws, c1 + 1) : 0;
123
0
  return c1 != c2;
124
0
}
125
126
static int pickaxe_match(struct diff_filepair *p, struct diff_options *o,
127
       regex_t *regexp, kwset_t kws, pickaxe_fn fn)
128
0
{
129
0
  struct userdiff_driver *textconv_one = NULL;
130
0
  struct userdiff_driver *textconv_two = NULL;
131
0
  mmfile_t mf1, mf2;
132
0
  int ret;
133
134
  /* ignore unmerged */
135
0
  if (!DIFF_FILE_VALID(p->one) && !DIFF_FILE_VALID(p->two))
136
0
    return 0;
137
138
0
  if (o->objfind) {
139
0
    return  (DIFF_FILE_VALID(p->one) &&
140
0
       oidset_contains(o->objfind, &p->one->oid)) ||
141
0
      (DIFF_FILE_VALID(p->two) &&
142
0
       oidset_contains(o->objfind, &p->two->oid));
143
0
  }
144
145
0
  if (o->flags.allow_textconv) {
146
0
    textconv_one = get_textconv(o->repo, p->one);
147
0
    textconv_two = get_textconv(o->repo, p->two);
148
0
  }
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
   */
157
0
  if (textconv_one == textconv_two && diff_unmodified_pair(p))
158
0
    return 0;
159
160
0
  if ((o->pickaxe_opts & DIFF_PICKAXE_KIND_G) &&
161
0
      !o->flags.text &&
162
0
      ((!textconv_one && diff_filespec_is_binary(o->repo, p->one)) ||
163
0
       (!textconv_two && diff_filespec_is_binary(o->repo, p->two))))
164
0
    return 0;
165
166
0
  mf1.size = fill_textconv(o->repo, textconv_one, p->one, &mf1.ptr);
167
0
  mf2.size = fill_textconv(o->repo, textconv_two, p->two, &mf2.ptr);
168
169
0
  ret = fn(&mf1, &mf2, o, regexp, kws);
170
171
0
  if (textconv_one)
172
0
    free(mf1.ptr);
173
0
  if (textconv_two)
174
0
    free(mf2.ptr);
175
0
  diff_free_filespec_data(p->one);
176
0
  diff_free_filespec_data(p->two);
177
178
0
  return ret;
179
0
}
180
181
static void pickaxe(struct diff_queue_struct *q, struct diff_options *o,
182
        regex_t *regexp, kwset_t kws, pickaxe_fn fn)
183
0
{
184
0
  int i;
185
0
  struct diff_queue_struct outq;
186
187
0
  DIFF_QUEUE_CLEAR(&outq);
188
189
0
  if (o->pickaxe_opts & DIFF_PICKAXE_ALL) {
190
    /* Showing the whole changeset if needle exists */
191
0
    for (i = 0; i < q->nr; i++) {
192
0
      struct diff_filepair *p = q->queue[i];
193
0
      if (pickaxe_match(p, o, regexp, kws, fn))
194
0
        return; /* do not munge the queue */
195
0
    }
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
     */
202
0
    for (i = 0; i < q->nr; i++)
203
0
      diff_free_filepair(q->queue[i]);
204
0
  } else {
205
    /* Showing only the filepairs that has the needle */
206
0
    for (i = 0; i < q->nr; i++) {
207
0
      struct diff_filepair *p = q->queue[i];
208
0
      if (pickaxe_match(p, o, regexp, kws, fn))
209
0
        diff_q(&outq, p);
210
0
      else
211
0
        diff_free_filepair(p);
212
0
    }
213
0
  }
214
215
0
  free(q->queue);
216
0
  *q = outq;
217
0
}
218
219
static void regcomp_or_die(regex_t *regex, const char *needle, int cflags)
220
0
{
221
0
  int err = regcomp(regex, needle, cflags);
222
0
  if (err) {
223
    /* The POSIX.2 people are surely sick */
224
0
    char errbuf[1024];
225
0
    regerror(err, regex, errbuf, 1024);
226
0
    die("invalid regex: %s", errbuf);
227
0
  }
228
0
}
229
230
void diffcore_pickaxe(struct diff_options *o)
231
0
{
232
0
  const char *needle = o->pickaxe;
233
0
  int opts = o->pickaxe_opts;
234
0
  regex_t regex, *regexp = NULL;
235
0
  kwset_t kws = NULL;
236
0
  pickaxe_fn fn;
237
238
0
  if (opts & ~DIFF_PICKAXE_KIND_OBJFIND &&
239
0
      (!needle || !*needle))
240
0
    BUG("should have needle under -G or -S");
241
0
  if (opts & (DIFF_PICKAXE_REGEX | DIFF_PICKAXE_KIND_G)) {
242
0
    int cflags = REG_EXTENDED | REG_NEWLINE;
243
0
    if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE)
244
0
      cflags |= REG_ICASE;
245
0
    regcomp_or_die(&regex, needle, cflags);
246
0
    regexp = &regex;
247
248
0
    if (opts & DIFF_PICKAXE_KIND_G)
249
0
      fn = diff_grep;
250
0
    else if (opts & DIFF_PICKAXE_REGEX)
251
0
      fn = has_changes;
252
0
    else
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
       */
260
0
      BUG("unreachable");
261
0
  } else if (opts & DIFF_PICKAXE_KIND_S) {
262
0
    if (o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE &&
263
0
        has_non_ascii(needle)) {
264
0
      struct strbuf sb = STRBUF_INIT;
265
0
      int cflags = REG_NEWLINE | REG_ICASE;
266
267
0
      basic_regex_quote_buf(&sb, needle);
268
0
      regcomp_or_die(&regex, sb.buf, cflags);
269
0
      strbuf_release(&sb);
270
0
      regexp = &regex;
271
0
    } else {
272
0
      kws = kwsalloc(o->pickaxe_opts & DIFF_PICKAXE_IGNORE_CASE
273
0
               ? tolower_trans_tbl : NULL);
274
0
      kwsincr(kws, needle, strlen(needle));
275
0
      kwsprep(kws);
276
0
    }
277
0
    fn = has_changes;
278
0
  } else if (opts & DIFF_PICKAXE_KIND_OBJFIND) {
279
0
    fn = NULL;
280
0
  } else {
281
0
    BUG("unknown pickaxe_opts flag");
282
0
  }
283
284
0
  pickaxe(&diff_queued_diff, o, regexp, kws, fn);
285
286
0
  if (regexp)
287
0
    regfree(regexp);
288
0
  if (kws)
289
0
    kwsfree(kws);
290
0
  return;
291
0
}