Skip to content

Commit 815834e

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bd9b79 commit 815834e

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

builtin/blame.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2479,7 +2479,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
24792479
for (range_i = 0; range_i < range_list.nr; ++range_i) {
24802480
long bottom, top;
24812481
if (parse_range_arg(range_list.items[range_i].string,
2482-
nth_line_cb, &sb, lno,
2482+
nth_line_cb, &sb, lno, 1,
24832483
&bottom, &top, sb.path))
24842484
usage(blame_usage);
24852485
if (lno < top || ((lno || bottom) && lno < bottom))

line-log.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
591591
cb_data.line_ends = ends;
592592

593593
if (parse_range_arg(range_part, nth_line, &cb_data,
594-
lines, &begin, &end,
594+
lines, 1, &begin, &end,
595595
full_name))
596596
die("malformed -L argument '%s'", range_part);
597597
if (lines < end || ((lines || begin) && lines < begin))

line-range.c

+26-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
/*
88
* 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'.
921
*/
1022
static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
1123
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,
4658
*ret = num;
4759
return term;
4860
}
61+
62+
if (begin < 0)
63+
begin = -begin;
64+
4965
if (spec[0] != '/')
5066
return spec;
5167

@@ -85,7 +101,8 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
85101
else {
86102
char errbuf[1024];
87103
regerror(reg_error, &regexp, 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);
89106
}
90107
}
91108

@@ -210,19 +227,24 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
210227
}
211228

212229
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)
215232
{
216233
*begin = *end = 0;
217234

235+
if (anchor < 1)
236+
anchor = 1;
237+
if (anchor > lines)
238+
anchor = lines + 1;
239+
218240
if (*arg == ':') {
219241
arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
220242
if (!arg || *arg)
221243
return -1;
222244
return 0;
223245
}
224246

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);
226248

227249
if (*arg == ',')
228250
arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);

line-range.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
* line 'lno' inside the 'cb_data'. The caller is expected to already
1010
* have a suitable map at hand to make this a constant-time lookup.
1111
*
12+
* 'anchor' is the 1-based line at which relative range specifications
13+
* should be anchored. Absolute ranges are unaffected by this value.
14+
*
1215
* Returns 0 in case of success and -1 if there was an error. The
1316
* actual range is stored in *begin and *end. The counting starts
1417
* 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);
1821

1922
extern int parse_range_arg(const char *arg,
2023
nth_line_fn_t nth_line_cb,
21-
void *cb_data, long lines,
24+
void *cb_data, long lines, long anchor,
2225
long *begin, long *end,
2326
const char *path);
2427

0 commit comments

Comments
 (0)