Skip to content

Commit 96cfa94

Browse files
istephensgitster
authored andcommitted
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>
1 parent fc54c1a commit 96cfa94

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

builtin/blame.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -886,13 +886,13 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
886886
nth_line_cb, &sb, lno, anchor,
887887
&bottom, &top, sb.path))
888888
usage(blame_usage);
889-
if (lno < top || ((lno || bottom) && lno < bottom))
889+
if ((!lno && (top || bottom)) || lno < bottom)
890890
die(Q_("file %s has only %lu line",
891891
"file %s has only %lu lines",
892892
lno), path, lno);
893893
if (bottom < 1)
894894
bottom = 1;
895-
if (top < 1)
895+
if (top < 1 || lno < top)
896896
top = lno;
897897
bottom--;
898898
range_set_append_unsafe(&ranges, bottom, top);

line-range.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
4747
else if (!num)
4848
*ret = begin;
4949
else
50-
*ret = begin + num;
50+
*ret = begin + num > 0 ? begin + num : 1;
5151
return term;
5252
}
5353
return spec;

t/annotate-tests.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ test_expect_success 'blame -L ,Y (Y == nlines)' '
320320

321321
test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
322322
n=$(expr $(wc -l <file) + 2) &&
323-
test_must_fail $PROG -L,$n file
323+
check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
324324
'
325325

326326
test_expect_success 'blame -L ,Y (Y > nlines)' '
327-
test_must_fail $PROG -L,12345 file
327+
check_count -L,12345 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
328328
'
329329

330330
test_expect_success 'blame -L multiple (disjoint)' '

t/t8003-blame-corner-cases.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,18 @@ test_expect_success 'blame -L with invalid start' '
216216
'
217217

218218
test_expect_success 'blame -L with invalid end' '
219-
test_must_fail git blame -L1,5 tres 2>errors &&
220-
test_i18ngrep "has only 2 lines" errors
219+
git blame -L1,5 tres >out &&
220+
test_line_count = 2 out
221221
'
222222

223223
test_expect_success 'blame parses <end> part of -L' '
224224
git blame -L1,1 tres >out &&
225-
cat out &&
226-
test $(wc -l < out) -eq 1
225+
test_line_count = 1 out
226+
'
227+
228+
test_expect_success 'blame -Ln,-(n+1)' '
229+
git blame -L3,-4 nine_lines >out &&
230+
test_line_count = 3 out
227231
'
228232

229233
test_expect_success 'indent of line numbers, nine lines' '

0 commit comments

Comments
 (0)