Skip to content

Commit 1ce761a

Browse files
sunshinecogitster
authored andcommitted
line-range: teach -L:RE to search from end of previous -L range
For consistency with -L/RE/, teach -L:RE to search relative to the end of the previous -L range, if any. The new behavior invalidates one test in t4211 which assumes that -L:RE begins searching at start of file. This test will be resurrected in a follow-up patch which teaches -L:RE how to override the default relative search behavior. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a6ac5f9 commit 1ce761a

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

Documentation/line-range-format.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ of lines before or after the line given by <start>.
2424
+
2525
If ``:<regex>'' is given in place of <start> and <end>, it denotes the range
2626
from the first funcname line that matches <regex>, up to the next
27-
funcname line.
27+
funcname line. ``:<regex>'' searches from the end of the previous `-L` range,
28+
if any, otherwise from the start of file.

line-range.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char
161161
}
162162

163163
static 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,
164+
void *cb_data, long lines, long anchor, long *begin, long *end,
165165
const char *path)
166166
{
167167
char *pattern;
@@ -187,7 +187,8 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
187187

188188
pattern = xstrndup(arg+1, term-(arg+1));
189189

190-
start = nth_line_cb(cb_data, 0);
190+
anchor--; /* input is in human terms */
191+
start = nth_line_cb(cb_data, anchor);
191192

192193
drv = userdiff_find_by_path(path);
193194
if (drv && drv->funcname.pattern) {
@@ -205,7 +206,8 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
205206

206207
p = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);
207208
if (!p)
208-
die("-L parameter '%s': no match", pattern);
209+
die("-L parameter '%s' starting at line %ld: no match",
210+
pattern, anchor + 1);
209211
*begin = 0;
210212
while (p > nth_line_cb(cb_data, *begin))
211213
(*begin)++;
@@ -244,7 +246,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
244246
anchor = lines + 1;
245247

246248
if (*arg == ':') {
247-
arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
249+
arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, anchor, begin, end, path);
248250
if (!arg || *arg)
249251
return -1;
250252
return 0;
@@ -269,7 +271,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
269271
const char *skip_range_arg(const char *arg)
270272
{
271273
if (*arg == ':')
272-
return parse_range_funcname(arg, NULL, NULL, 0, NULL, NULL, NULL);
274+
return parse_range_funcname(arg, NULL, NULL, 0, 0, NULL, NULL, NULL);
273275

274276
arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
275277

t/annotate-tests.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,22 @@ test_expect_success 'blame -L :nomatch' '
382382
test_must_fail $PROG -L:nomatch hello.c
383383
'
384384

385+
test_expect_success 'blame -L :RE (relative)' '
386+
check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
387+
'
388+
389+
test_expect_success 'blame -L :RE (relative: no preceding range)' '
390+
check_count -f hello.c -L:ma.. F 4 G 1
391+
'
392+
393+
test_expect_success 'blame -L :RE (relative: not found)' '
394+
test_must_fail $PROG -L3,3 -L:tambourine hello.c
395+
'
396+
397+
test_expect_success 'blame -L :RE (relative: end-of-file)' '
398+
test_must_fail $PROG -L, -L:main hello.c
399+
'
400+
385401
test_expect_success 'setup incremental' '
386402
(
387403
GIT_AUTHOR_NAME=I &&

t/t4211-line-log.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ canned_test "-M -L '/long f/,/^}/:b.c' move-support" move-support-f
4848
canned_test "-M -L ':f:b.c' parallel-change" parallel-change-f-to-main
4949

5050
canned_test "-L 4,12:a.c -L :main:a.c simple" multiple
51-
canned_test "-L 4,18:a.c -L :main:a.c simple" multiple-overlapping
5251
canned_test "-L :main:a.c -L 4,18:a.c simple" multiple-overlapping
5352
canned_test "-L 4:a.c -L 8,12:a.c simple" multiple-superset
5453
canned_test "-L 8,12:a.c -L 4:a.c simple" multiple-superset

0 commit comments

Comments
 (0)