line-range: teach -L:RE to search from end of previous -L range
[git] / line-range.c
1 #include "git-compat-util.h"
2 #include "line-range.h"
3 #include "xdiff-interface.h"
4 #include "strbuf.h"
5 #include "userdiff.h"
6
7 /*
8  * Parse one item in the -L option
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'.
21  */
22 static 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          */
36         if (1 <= begin && (spec[0] == '+' || spec[0] == '-')) {
37                 num = strtol(spec + 1, &term, 10);
38                 if (term != spec + 1) {
39                         if (!ret)
40                                 return term;
41                         if (num == 0)
42                                 die("-L invalid empty range");
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) {
57                 if (ret)
58                         *ret = num;
59                 return term;
60         }
61
62         if (begin < 0) {
63                 if (spec[0] != '^')
64                         begin = -begin;
65                 else {
66                         begin = 1;
67                         spec++;
68                 }
69         }
70
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
82         /* in the scan-only case we are not interested in the regex */
83         if (!ret)
84                 return term+1;
85
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);
110                 die("-L parameter '%s' starting at line %ld: %s",
111                     spec + 1, begin + 1, errbuf);
112         }
113 }
114
115 static 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
130 static 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
163 static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
164                                         void *cb_data, long lines, long anchor, 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         anchor--; /* input is in human terms */
191         start = nth_line_cb(cb_data, anchor);
192
193         drv = userdiff_find_by_path(path);
194         if (drv && drv->funcname.pattern) {
195                 const struct userdiff_funcname *pe = &drv->funcname;
196                 xecfg = xcalloc(1, sizeof(*xecfg));
197                 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
198         }
199
200         reg_error = regcomp(&regexp, pattern, REG_NEWLINE);
201         if (reg_error) {
202                 char errbuf[1024];
203                 regerror(reg_error, &regexp, errbuf, 1024);
204                 die("-L parameter '%s': %s", pattern, errbuf);
205         }
206
207         p = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);
208         if (!p)
209                 die("-L parameter '%s' starting at line %ld: no match",
210                     pattern, anchor + 1);
211         *begin = 0;
212         while (p > nth_line_cb(cb_data, *begin))
213                 (*begin)++;
214
215         if (*begin >= lines)
216                 die("-L parameter '%s' matches at EOF", pattern);
217
218         *end = *begin+1;
219         while (*end < lines) {
220                 const char *bol = nth_line_cb(cb_data, *end);
221                 const char *eol = nth_line_cb(cb_data, *end+1);
222                 if (match_funcname(xecfg, bol, eol))
223                         break;
224                 (*end)++;
225         }
226
227         regfree(&regexp);
228         free(xecfg);
229         free(pattern);
230
231         /* compensate for 1-based numbering */
232         (*begin)++;
233
234         return term;
235 }
236
237 int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
238                     void *cb_data, long lines, long anchor,
239                     long *begin, long *end, const char *path)
240 {
241         *begin = *end = 0;
242
243         if (anchor < 1)
244                 anchor = 1;
245         if (anchor > lines)
246                 anchor = lines + 1;
247
248         if (*arg == ':') {
249                 arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, anchor, begin, end, path);
250                 if (!arg || *arg)
251                         return -1;
252                 return 0;
253         }
254
255         arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
256
257         if (*arg == ',')
258                 arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
259
260         if (*arg)
261                 return -1;
262
263         if (*begin && *end && *end < *begin) {
264                 long tmp;
265                 tmp = *end; *end = *begin; *begin = tmp;
266         }
267
268         return 0;
269 }
270
271 const char *skip_range_arg(const char *arg)
272 {
273         if (*arg == ':')
274                 return parse_range_funcname(arg, NULL, NULL, 0, 0, NULL, NULL, NULL);
275
276         arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
277
278         if (*arg == ',')
279                 arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);
280
281         return arg;
282 }