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>
Isabella Stephens committed
Jun 15, 2018 at 16:29 UTC
96cfa94e68cfbe9942e230ae18b35eaf1ca30f99
4 files changed
+13
-9
builtin/blame.c
+2
-2
@@ -886,13 +886,13 @@ parse_done:
886
nth_line_cb, &sb, lno, anchor,
887
&bottom, &top, sb.path))
888
usage(blame_usage);
889
- if (lno < top || ((lno || bottom) && lno < bottom))
889
+ if ((!lno && (top || bottom)) || lno < bottom)
890
die(Q_("file %s has only %lu line",
891
"file %s has only %lu lines",
892
lno), path, lno);
893
if (bottom < 1)
894
bottom = 1;
895
- if (top < 1)
895
+ if (top < 1 || lno < top)
896
top = lno;
897
bottom--;
898
range_set_append_unsafe(&ranges, bottom, top);
line-range.c
+1
-1
@@ -47,7 +47,7 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
47
else if (!num)
48
*ret = begin;
49
else
50
- *ret = begin + num;
50
+ *ret = begin + num > 0 ? begin + num : 1;
51
return term;
52
}
53
return spec;
t/annotate-tests.sh
+2
-2
@@ -320,11 +320,11 @@ test_expect_success 'blame -L ,Y (Y == nlines)' '
320
321
test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
322
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
324
'
325
326
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
328
'
329
330
test_expect_success 'blame -L multiple (disjoint)' '
t/t8003-blame-corner-cases.sh
+8
-4
@@ -216,14 +216,18 @@ test_expect_success 'blame -L with invalid start' '
216
'
217
218
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
221
'
222
223
test_expect_success 'blame parses <end> part of -L' '
224
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
231
'
232
233
test_expect_success 'indent of line numbers, nine lines' '