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