log: teach -L/RE/ to search from end of previous -L range
[alt-git.git] / line-range.c
blobbbf3c0f4481e9dd762d6a55e531cb983bbcbaff1
1 #include "git-compat-util.h"
2 #include "line-range.h"
3 #include "xdiff-interface.h"
4 #include "strbuf.h"
5 #include "userdiff.h"
7 /*
8 * Parse one item in the -L option
10 * 'begin' is applicable only to relative range anchors. Absolute anchors
11 * ignore this value.
13 * When parsing "-L A,B", parse_loc() is called once for A and once for B.
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.
19 * When parsing B, 'begin' must be the positive line number immediately
20 * following the line computed for 'A'.
22 static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
23 void *data, long lines, long begin, long *ret)
25 char *term;
26 const char *line;
27 long num;
28 int reg_error;
29 regex_t regexp;
30 regmatch_t match[1];
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>.
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;
53 return spec;
55 num = strtol(spec, &term, 10);
56 if (term != spec) {
57 if (ret)
58 *ret = num;
59 return term;
62 if (begin < 0)
63 begin = -begin;
65 if (spec[0] != '/')
66 return spec;
68 /* it could be a regexp of form /.../ */
69 for (term = (char *) spec + 1; *term && *term != '/'; term++) {
70 if (*term == '\\')
71 term++;
73 if (*term != '/')
74 return spec;
76 /* in the scan-only case we are not interested in the regex */
77 if (!ret)
78 return term+1;
80 /* try [spec+1 .. term-1] as regexp */
81 *term = 0;
82 begin--; /* input is in human terms */
83 line = nth_line(data, begin);
85 if (!(reg_error = regcomp(&regexp, spec + 1, REG_NEWLINE)) &&
86 !(reg_error = regexec(&regexp, line, 1, match, 0))) {
87 const char *cp = line + match[0].rm_so;
88 const char *nline;
90 while (begin++ < lines) {
91 nline = nth_line(data, begin);
92 if (line <= cp && cp < nline)
93 break;
94 line = nline;
96 *ret = begin;
97 regfree(&regexp);
98 *term++ = '/';
99 return term;
101 else {
102 char errbuf[1024];
103 regerror(reg_error, &regexp, errbuf, 1024);
104 die("-L parameter '%s' starting at line %ld: %s",
105 spec + 1, begin + 1, errbuf);
109 static int match_funcname(xdemitconf_t *xecfg, const char *bol, const char *eol)
111 if (xecfg) {
112 char buf[1];
113 return xecfg->find_func(bol, eol - bol, buf, 1,
114 xecfg->find_func_priv) >= 0;
117 if (bol == eol)
118 return 0;
119 if (isalpha(*bol) || *bol == '_' || *bol == '$')
120 return 1;
121 return 0;
124 static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char *start,
125 regex_t *regexp)
127 int reg_error;
128 regmatch_t match[1];
129 while (1) {
130 const char *bol, *eol;
131 reg_error = regexec(regexp, start, 1, match, 0);
132 if (reg_error == REG_NOMATCH)
133 return NULL;
134 else if (reg_error) {
135 char errbuf[1024];
136 regerror(reg_error, regexp, errbuf, 1024);
137 die("-L parameter: regexec() failed: %s", errbuf);
139 /* determine extent of line matched */
140 bol = start+match[0].rm_so;
141 eol = start+match[0].rm_eo;
142 while (bol > start && *bol != '\n')
143 bol--;
144 if (*bol == '\n')
145 bol++;
146 while (*eol && *eol != '\n')
147 eol++;
148 if (*eol == '\n')
149 eol++;
150 /* is it a funcname line? */
151 if (match_funcname(xecfg, (char*) bol, (char*) eol))
152 return bol;
153 start = eol;
157 static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
158 void *cb_data, long lines, long *begin, long *end,
159 const char *path)
161 char *pattern;
162 const char *term;
163 struct userdiff_driver *drv;
164 xdemitconf_t *xecfg = NULL;
165 const char *start;
166 const char *p;
167 int reg_error;
168 regex_t regexp;
170 assert(*arg == ':');
171 term = arg+1;
172 while (*term && *term != ':') {
173 if (*term == '\\' && *(term+1))
174 term++;
175 term++;
177 if (term == arg+1)
178 return NULL;
179 if (!begin) /* skip_range_arg case */
180 return term;
182 pattern = xstrndup(arg+1, term-(arg+1));
184 start = nth_line_cb(cb_data, 0);
186 drv = userdiff_find_by_path(path);
187 if (drv && drv->funcname.pattern) {
188 const struct userdiff_funcname *pe = &drv->funcname;
189 xecfg = xcalloc(1, sizeof(*xecfg));
190 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
193 reg_error = regcomp(&regexp, pattern, REG_NEWLINE);
194 if (reg_error) {
195 char errbuf[1024];
196 regerror(reg_error, &regexp, errbuf, 1024);
197 die("-L parameter '%s': %s", pattern, errbuf);
200 p = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);
201 if (!p)
202 die("-L parameter '%s': no match", pattern);
203 *begin = 0;
204 while (p > nth_line_cb(cb_data, *begin))
205 (*begin)++;
207 if (*begin >= lines)
208 die("-L parameter '%s' matches at EOF", pattern);
210 *end = *begin+1;
211 while (*end < lines) {
212 const char *bol = nth_line_cb(cb_data, *end);
213 const char *eol = nth_line_cb(cb_data, *end+1);
214 if (match_funcname(xecfg, bol, eol))
215 break;
216 (*end)++;
219 regfree(&regexp);
220 free(xecfg);
221 free(pattern);
223 /* compensate for 1-based numbering */
224 (*begin)++;
226 return term;
229 int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
230 void *cb_data, long lines, long anchor,
231 long *begin, long *end, const char *path)
233 *begin = *end = 0;
235 if (anchor < 1)
236 anchor = 1;
237 if (anchor > lines)
238 anchor = lines + 1;
240 if (*arg == ':') {
241 arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
242 if (!arg || *arg)
243 return -1;
244 return 0;
247 arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
249 if (*arg == ',')
250 arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
252 if (*arg)
253 return -1;
255 if (*begin && *end && *end < *begin) {
256 long tmp;
257 tmp = *end; *end = *begin; *begin = tmp;
260 return 0;
263 const char *skip_range_arg(const char *arg)
265 if (*arg == ':')
266 return parse_range_funcname(arg, NULL, NULL, 0, NULL, NULL, NULL);
268 arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
270 if (*arg == ',')
271 arg = parse_loc(arg+1, NULL, NULL, 0, 0, NULL);
273 return arg;