1
0
mirror of https://github.com/git/git synced 2024-07-02 15:48:44 +00:00

blame: prevent error if range ends past end of file

If the -L option is used to specify a line range in git blame, and the
end of the range is past the end of the file, git will fail with a fatal
error. This commit prevents such behavior - instead we display the blame
for existing lines within the specified range. Tests are amended
accordingly.

This commit also fixes two corner cases. Blaming -L n,-(n+1) now blames
the first n lines of a file rather than from n to the end of the file.
Blaming -L ,-n will be treated as -L 1,-n and blame the first line of
the file, rather than blaming the whole file.

Signed-off-by: Isabella Stephens <istephens@atlassian.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Isabella Stephens 2018-06-15 16:29:27 +10:00 committed by Junio C Hamano
parent fc54c1af3e
commit 96cfa94e68
4 changed files with 13 additions and 9 deletions

View File

@ -886,13 +886,13 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
nth_line_cb, &sb, lno, anchor,
&bottom, &top, sb.path))
usage(blame_usage);
if (lno < top || ((lno || bottom) && lno < bottom))
if ((!lno && (top || bottom)) || lno < bottom)
die(Q_("file %s has only %lu line",
"file %s has only %lu lines",
lno), path, lno);
if (bottom < 1)
bottom = 1;
if (top < 1)
if (top < 1 || lno < top)
top = lno;
bottom--;
range_set_append_unsafe(&ranges, bottom, top);

View File

@ -47,7 +47,7 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
else if (!num)
*ret = begin;
else
*ret = begin + num;
*ret = begin + num > 0 ? begin + num : 1;
return term;
}
return spec;

View File

@ -320,11 +320,11 @@ test_expect_success 'blame -L ,Y (Y == nlines)' '
test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
n=$(expr $(wc -l <file) + 2) &&
test_must_fail $PROG -L,$n file
check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
'
test_expect_success 'blame -L ,Y (Y > nlines)' '
test_must_fail $PROG -L,12345 file
check_count -L,12345 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
'
test_expect_success 'blame -L multiple (disjoint)' '

View File

@ -216,14 +216,18 @@ test_expect_success 'blame -L with invalid start' '
'
test_expect_success 'blame -L with invalid end' '
test_must_fail git blame -L1,5 tres 2>errors &&
test_i18ngrep "has only 2 lines" errors
git blame -L1,5 tres >out &&
test_line_count = 2 out
'
test_expect_success 'blame parses <end> part of -L' '
git blame -L1,1 tres >out &&
cat out &&
test $(wc -l < out) -eq 1
test_line_count = 1 out
'
test_expect_success 'blame -Ln,-(n+1)' '
git blame -L3,-4 nine_lines >out &&
test_line_count = 3 out
'
test_expect_success 'indent of line numbers, nine lines' '