Coverage Report

Created: 2026-01-09 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/line-range.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "line-range.h"
3
#include "xdiff-interface.h"
4
#include "userdiff.h"
5
6
/*
7
 * Parse one item in the -L option
8
 *
9
 * 'begin' is applicable only to relative range anchors. Absolute anchors
10
 * ignore this value.
11
 *
12
 * When parsing "-L A,B", parse_loc() is called once for A and once for B.
13
 *
14
 * When parsing A, 'begin' must be a negative number, the absolute value of
15
 * which is the line at which relative start-of-range anchors should be
16
 * based. Beginning of file is represented by -1.
17
 *
18
 * When parsing B, 'begin' must be the positive line number immediately
19
 * following the line computed for 'A'.
20
 */
21
static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
22
           void *data, long lines, long begin, long *ret)
23
0
{
24
0
  char *term;
25
0
  const char *line;
26
0
  long num;
27
0
  int reg_error;
28
0
  regex_t regexp;
29
0
  regmatch_t match[1];
30
31
  /* Allow "-L <something>,+20" to mean starting at <something>
32
   * for 20 lines, or "-L <something>,-5" for 5 lines ending at
33
   * <something>.
34
   */
35
0
  if (1 <= begin && (spec[0] == '+' || spec[0] == '-')) {
36
0
    num = strtol(spec + 1, &term, 10);
37
0
    if (term != spec + 1) {
38
0
      if (!ret)
39
0
        return term;
40
0
      if (num == 0)
41
0
        die("-L invalid empty range");
42
0
      if (spec[0] == '-')
43
0
        num = 0 - num;
44
0
      if (0 < num)
45
0
        *ret = begin + num - 2;
46
0
      else if (!num)
47
0
        *ret = begin;
48
0
      else
49
0
        *ret = begin + num > 0 ? begin + num : 1;
50
0
      return term;
51
0
    }
52
0
    return spec;
53
0
  }
54
0
  num = strtol(spec, &term, 10);
55
0
  if (term != spec) {
56
0
    if (ret) {
57
0
      if (num <= 0)
58
0
        die("-L invalid line number: %ld", num);
59
0
      *ret = num;
60
0
    }
61
0
    return term;
62
0
  }
63
64
0
  if (begin < 0) {
65
0
    if (spec[0] != '^')
66
0
      begin = -begin;
67
0
    else {
68
0
      begin = 1;
69
0
      spec++;
70
0
    }
71
0
  }
72
73
0
  if (spec[0] != '/')
74
0
    return spec;
75
76
  /* it could be a regexp of form /.../ */
77
0
  for (term = (char *) spec + 1; *term && *term != '/'; term++) {
78
0
    if (*term == '\\')
79
0
      term++;
80
0
  }
81
0
  if (*term != '/')
82
0
    return spec;
83
84
  /* in the scan-only case we are not interested in the regex */
85
0
  if (!ret)
86
0
    return term+1;
87
88
  /* try [spec+1 .. term-1] as regexp */
89
0
  *term = 0;
90
0
  begin--; /* input is in human terms */
91
0
  line = nth_line(data, begin);
92
93
0
  if (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&
94
0
      !(reg_error = regexec(&regexp, line, 1, match, 0))) {
95
0
    const char *cp = line + match[0].rm_so;
96
0
    const char *nline;
97
98
0
    while (begin++ < lines) {
99
0
      nline = nth_line(data, begin);
100
0
      if (line <= cp && cp < nline)
101
0
        break;
102
0
      line = nline;
103
0
    }
104
0
    *ret = begin;
105
0
    regfree(&regexp);
106
0
    *term++ = '/';
107
0
    return term;
108
0
  }
109
0
  else {
110
0
    char errbuf[1024];
111
0
    regerror(reg_error, &regexp, errbuf, 1024);
112
0
    die("-L parameter '%s' starting at line %ld: %s",
113
0
        spec + 1, begin + 1, errbuf);
114
0
  }
115
0
}
116
117
static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
118
0
{
119
0
  if (xecfg) {
120
0
    char buf[1];
121
0
    return xecfg->find_func(bol, eol - bol, buf, 1,
122
0
          xecfg->find_func_priv) >= 0;
123
0
  }
124
125
0
  if (bol == eol)
126
0
    return 0;
127
0
  if (isalpha(*bol) || *bol == '_' || *bol == '$')
128
0
    return 1;
129
0
  return 0;
130
0
}
131
132
static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
133
             regex_t *regexp)
134
0
{
135
0
  int reg_error;
136
0
  regmatch_t match[1];
137
0
  while (*start) {
138
0
    const char *bol, *eol;
139
0
    reg_error = regexec(regexp, start, 1, match, 0);
140
0
    if (reg_error == REG_NOMATCH)
141
0
      return NULL;
142
0
    else if (reg_error) {
143
0
      char errbuf[1024];
144
0
      regerror(reg_error, regexp, errbuf, 1024);
145
0
      die("-L parameter: regexec() failed: %s", errbuf);
146
0
    }
147
    /* determine extent of line matched */
148
0
    bol = start+match[0].rm_so;
149
0
    eol = start+match[0].rm_eo;
150
0
    while (bol > start && *--bol != '\n')
151
0
      ; /* nothing */
152
0
    if (*bol == '\n')
153
0
      bol++;
154
0
    while (*eol && *eol != '\n')
155
0
      eol++;
156
0
    if (*eol == '\n')
157
0
      eol++;
158
    /* is it a funcname line? */
159
0
    if (match_funcname(xecfg, (char*) bol, (char*) eol))
160
0
      return bol;
161
0
    start = eol;
162
0
  }
163
0
  return NULL;
164
0
}
165
166
static const char *parse_range_funcname(
167
  const char *arg, nth_line_fn_t nth_line_cb,
168
  void *cb_data, long lines, long anchor, long *begin, long *end,
169
  const char *path, struct index_state *istate)
170
0
{
171
0
  char *pattern;
172
0
  const char *term;
173
0
  struct userdiff_driver *drv;
174
0
  xdemitconf_t *xecfg = NULL;
175
0
  const char *start;
176
0
  const char *p;
177
0
  int reg_error;
178
0
  regex_t regexp;
179
180
0
  if (*arg == '^') {
181
0
    anchor = 1;
182
0
    arg++;
183
0
  }
184
185
0
  assert(*arg == ':');
186
0
  term = arg+1;
187
0
  while (*term && *term != ':') {
188
0
    if (*term == '\\' && *(term+1))
189
0
      term++;
190
0
    term++;
191
0
  }
192
0
  if (term == arg+1)
193
0
    return NULL;
194
0
  if (!begin) /* skip_range_arg case */
195
0
    return term;
196
197
0
  pattern = xstrndup(arg+1, term-(arg+1));
198
199
0
  anchor--; /* input is in human terms */
200
0
  start = nth_line_cb(cb_data, anchor);
201
202
0
  drv = userdiff_find_by_path(istate, path);
203
0
  if (drv && drv->funcname.pattern) {
204
0
    const struct userdiff_funcname *pe = &drv->funcname;
205
0
    CALLOC_ARRAY(xecfg, 1);
206
0
    xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
207
0
  }
208
209
0
  reg_error = regcomp(&regexp, pattern, REG_NEWLINE);
210
0
  if (reg_error) {
211
0
    char errbuf[1024];
212
0
    regerror(reg_error, &regexp, errbuf, 1024);
213
0
    die("-L parameter '%s': %s", pattern, errbuf);
214
0
  }
215
216
0
  p = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);
217
0
  if (!p)
218
0
    die("-L parameter '%s' starting at line %ld: no match",
219
0
        pattern, anchor + 1);
220
0
  *begin = 0;
221
0
  while (p > nth_line_cb(cb_data, *begin))
222
0
    (*begin)++;
223
224
0
  if (*begin >= lines)
225
0
    die("-L parameter '%s' matches at EOF", pattern);
226
227
0
  *end = *begin+1;
228
0
  while (*end < lines) {
229
0
    const char *bol = nth_line_cb(cb_data, *end);
230
0
    const char *eol = nth_line_cb(cb_data, *end+1);
231
0
    if (match_funcname(xecfg, bol, eol))
232
0
      break;
233
0
    (*end)++;
234
0
  }
235
236
0
  regfree(&regexp);
237
0
  if (xecfg)
238
0
    xdiff_clear_find_func(xecfg);
239
0
  free(xecfg);
240
0
  free(pattern);
241
242
  /* compensate for 1-based numbering */
243
0
  (*begin)++;
244
245
0
  return term;
246
0
}
247
248
int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
249
        void *cb_data, long lines, long anchor,
250
        long *begin, long *end,
251
        const char *path, struct index_state *istate)
252
0
{
253
0
  *begin = *end = 0;
254
255
0
  if (anchor < 1)
256
0
    anchor = 1;
257
0
  if (anchor > lines)
258
0
    anchor = lines + 1;
259
260
0
  if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':')) {
261
0
    arg = parse_range_funcname(arg, nth_line_cb, cb_data,
262
0
             lines, anchor, begin, end,
263
0
             path, istate);
264
0
    if (!arg || *arg)
265
0
      return -1;
266
0
    return 0;
267
0
  }
268
269
0
  arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
270
271
0
  if (*arg == ',')
272
0
    arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
273
274
0
  if (*arg)
275
0
    return -1;
276
277
0
  if (*begin && *end && *end < *begin) {
278
0
    SWAP(*end, *begin);
279
0
  }
280
281
0
  return 0;
282
0
}
283
284
const char *skip_range_arg(const char *arg, struct index_state *istate)
285
0
{
286
0
  if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':'))
287
0
    return parse_range_funcname(arg, NULL, NULL,
288
0
              0, 0, NULL, NULL,
289
0
              NULL, istate);
290
291
0
  arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
292
293
0
  if (*arg == ',')
294
0
    arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);
295
296
0
  return arg;
297
0
}