Coverage Report

Created: 2026-02-14 06:27

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