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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Aug 6, 2013 at 09:59 UTC a6ac5f9864958f65269d8d58a049324403b039fd
3 files changed +30 -2
Documentation/line-range-format.txt
+1
@@ -11,6 +11,7 @@ absolute line number (lines count from 1).
11 This form will use the first line matching the given
12 POSIX regex. If <start> is a regex, it will search from the end of
13 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.
15 If <end> is a regex, it will search
16 starting at the line given by <start>.
17 +
line-range.c
+8 -2
@@ -59,8 +59,14 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
59 return term;
60 }
61
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 + }
70
71 if (spec[0] != '/')
72 return spec;
t/annotate-tests.sh
+21
@@ -323,6 +323,23 @@ test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
323 test_must_fail $PROG -L, -L/$/ file
324 '
325
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 +
343 test_expect_success 'setup -L :regex' '
344 tr Q "\\t" >hello.c <<-\EOF &&
345 int main(int argc, const char *argv[])
@@ -464,3 +481,7 @@ test_expect_success 'blame -L X,+N (non-numeric N)' '
481 test_expect_success 'blame -L X,-N (non-numeric N)' '
482 test_must_fail $PROG -L1,-N file
483 '
484 +
485 +test_expect_success 'blame -L ,^/RE/' '
486 + test_must_fail $PROG -L1,^/99/ file
487 +'