line-range: teach -L/RE/ to search relative to anchor point
Range specification -L/RE/ for blame/log unconditionally begins searching at line one. Mailing list discussion [1] suggests that, in the presence of multiple -L options, -L/RE/ should search relative to the endpoint of the previous -L range, if any. Teach the parsing machinery underlying blame's and log's -L options to accept a start point for -L/RE/ searches. Follow-up patches will upgrade blame and log to take advantage of this ability. [1]: http://thread.gmane.org/gmane.comp.version-control.git/229755/focus=229966 Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Eric Sunshine committed
Aug 6, 2013 at 09:59 UTC
815834e9aa6148b7815b9aea7db5d44640a4383a
4 files changed
+32
-7
builtin/blame.c
+1
-1
@@ -2479,7 +2479,7 @@ parse_done:
2479
for (range_i = 0; range_i < range_list.nr; ++range_i) {
2480
long bottom, top;
2481
if (parse_range_arg(range_list.items[range_i].string,
2482
- nth_line_cb, &sb, lno,
2482
+ nth_line_cb, &sb, lno, 1,
2483
&bottom, &top, sb.path))
2484
usage(blame_usage);
2485
if (lno < top || ((lno || bottom) && lno < bottom))
line-log.c
+1
-1
@@ -591,7 +591,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
591
cb_data.line_ends = ends;
592
593
if (parse_range_arg(range_part, nth_line, &cb_data,
594
- lines, &begin, &end,
594
+ lines, 1, &begin, &end,
595
full_name))
596
die("malformed -L argument '%s'", range_part);
597
if (lines < end || ((lines || begin) && lines < begin))
line-range.c
+26
-4
@@ -6,6 +6,18 @@
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)
@@ -46,6 +58,10 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
58
*ret = num;
59
return term;
60
}
61
+
62
+ if (begin < 0)
63
+ begin = -begin;
64
+
65
if (spec[0] != '/')
66
return spec;
67
@@ -85,7 +101,8 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
101
else {
102
char errbuf[1024];
103
regerror(reg_error, ®exp, errbuf, 1024);
88
- die("-L parameter '%s': %s", spec + 1, errbuf);
104
+ die("-L parameter '%s' starting at line %ld: %s",
105
+ spec + 1, begin + 1, errbuf);
106
}
107
}
108
@@ -210,11 +227,16 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
227
}
228
229
int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
213
- void *cb_data, long lines, long *begin, long *end,
214
- const char *path)
230
+ void *cb_data, long lines, long anchor,
231
+ long *begin, long *end, const char *path)
232
{
233
*begin = *end = 0;
234
235
+ if (anchor < 1)
236
+ anchor = 1;
237
+ if (anchor > lines)
238
+ anchor = lines + 1;
239
+
240
if (*arg == ':') {
241
arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
242
if (!arg || *arg)
@@ -222,7 +244,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
244
return 0;
245
}
246
225
- arg = parse_loc(arg, nth_line_cb, cb_data, lines, 1, begin);
247
+ arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
248
249
if (*arg == ',')
250
arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
line-range.h
+4
-1
@@ -9,6 +9,9 @@
9
* line 'lno' inside the 'cb_data'. The caller is expected to already
10
* have a suitable map at hand to make this a constant-time lookup.
11
*
12
+ * 'anchor' is the 1-based line at which relative range specifications
13
+ * should be anchored. Absolute ranges are unaffected by this value.
14
+ *
15
* Returns 0 in case of success and -1 if there was an error. The
16
* actual range is stored in *begin and *end. The counting starts
17
* at 1! In case of error, the caller should show usage message.
@@ -18,7 +21,7 @@ typedef const char *(*nth_line_fn_t)(void *data, long lno);
21
22
extern int parse_range_arg(const char *arg,
23
nth_line_fn_t nth_line_cb,
21
- void *cb_data, long lines,
24
+ void *cb_data, long lines, long anchor,
25
long *begin, long *end,
26
const char *path);
27