Skip to content

Commit a6ac5f9

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. The new ^/RE/ syntax is valid only as the <start> argument of -L<start>,<end>. The <end> argument, as usual, is relative to <start>. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0bc2cdd commit a6ac5f9

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Documentation/line-range-format.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ absolute line number (lines count from 1).
1111
This form will use the first line matching the given
1212
POSIX regex. If <start> is a regex, it will search from the end of
1313
the previous `-L` range, if any, otherwise from the start of file.
14+
If <start> is ``^/regex/'', it will search from the start of file.
1415
If <end> is a regex, it will search
1516
starting at the line given by <start>.
1617
+

line-range.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
5959
return term;
6060
}
6161

62-
if (begin < 0)
63-
begin = -begin;
62+
if (begin < 0) {
63+
if (spec[0] != '^')
64+
begin = -begin;
65+
else {
66+
begin = 1;
67+
spec++;
68+
}
69+
}
6470

6571
if (spec[0] != '/')
6672
return spec;

t/annotate-tests.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,23 @@ test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
323323
test_must_fail $PROG -L, -L/$/ file
324324
'
325325

326+
test_expect_success 'blame -L ^/RE/ (absolute)' '
327+
check_count -L3,3 -L^/dog/,+2 A 1 B2 1
328+
'
329+
330+
test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
331+
check_count -L^/dog/,+2 A 1 B2 1
332+
'
333+
334+
test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
335+
test_must_fail $PROG -L4,4 -L^/tambourine/ file
336+
'
337+
338+
test_expect_success 'blame -L ^/RE/ (absolute: end-of-file)' '
339+
n=$(expr $(wc -l <file) + 1) &&
340+
check_count -L$n -L^/$/,+2 A 1 C 1 E 1
341+
'
342+
326343
test_expect_success 'setup -L :regex' '
327344
tr Q "\\t" >hello.c <<-\EOF &&
328345
int main(int argc, const char *argv[])
@@ -464,3 +481,7 @@ test_expect_success 'blame -L X,+N (non-numeric N)' '
464481
test_expect_success 'blame -L X,-N (non-numeric N)' '
465482
test_must_fail $PROG -L1,-N file
466483
'
484+
485+
test_expect_success 'blame -L ,^/RE/' '
486+
test_must_fail $PROG -L1,^/99/ file
487+
'

0 commit comments

Comments
 (0)