Skip to content

Commit 215e76c

Browse files
sunshinecogitster
authored andcommitted
line-range: teach -L^:RE to search from start of file
The -L:RE option of blame/log searches from the end of the previous -L range, if any. Add new notation -L^:RE to override this behavior and search from start of file. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1ce761a commit 215e76c

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

Documentation/line-range-format.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ 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
2727
funcname line. ``:<regex>'' searches from the end of the previous `-L` range,
2828
if any, otherwise from the start of file.
29+
``^:<regex>'' searches from the start of file.

line-range.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
173173
int reg_error;
174174
regex_t regexp;
175175

176+
if (*arg == '^') {
177+
anchor = 1;
178+
arg++;
179+
}
180+
176181
assert(*arg == ':');
177182
term = arg+1;
178183
while (*term && *term != ':') {
@@ -245,7 +250,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
245250
if (anchor > lines)
246251
anchor = lines + 1;
247252

248-
if (*arg == ':') {
253+
if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':')) {
249254
arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, anchor, begin, end, path);
250255
if (!arg || *arg)
251256
return -1;
@@ -270,7 +275,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
270275

271276
const char *skip_range_arg(const char *arg)
272277
{
273-
if (*arg == ':')
278+
if (*arg == ':' || (*arg == '^' && *(arg + 1) == ':'))
274279
return parse_range_funcname(arg, NULL, NULL, 0, 0, NULL, NULL, NULL);
275280

276281
arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);

t/annotate-tests.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,23 @@ test_expect_success 'blame -L :RE (relative: end-of-file)' '
398398
test_must_fail $PROG -L, -L:main hello.c
399399
'
400400

401+
test_expect_success 'blame -L ^:RE (absolute)' '
402+
check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
403+
'
404+
405+
test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
406+
check_count -f hello.c -L^:ma.. F 4 G 1
407+
'
408+
409+
test_expect_success 'blame -L ^:RE (absolute: not found)' '
410+
test_must_fail $PROG -L4,4 -L^:tambourine hello.c
411+
'
412+
413+
test_expect_success 'blame -L ^:RE (absolute: end-of-file)' '
414+
n=$(printf "%d" $(wc -l <hello.c)) &&
415+
check_count -f hello.c -L$n -L^:ma.. F 4 G 1 H 1
416+
'
417+
401418
test_expect_success 'setup incremental' '
402419
(
403420
GIT_AUTHOR_NAME=I &&

t/t4211-line-log.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ 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
5152
canned_test "-L :main:a.c -L 4,18:a.c simple" multiple-overlapping
5253
canned_test "-L 4:a.c -L 8,12:a.c simple" multiple-superset
5354
canned_test "-L 8,12:a.c -L 4:a.c simple" multiple-superset

0 commit comments

Comments
 (0)