log: prevent error if line range ends past end of file

If the -L option is used to specify a line range in git log, and the end of the range is past the end of the file, git will fail with a fatal error. This commit prevents such behaviour - instead we perform the log for existing lines within the specified range. This commit also fixes a corner case where -L ,-n:file would be treated as a log over the whole file. Now we treat this as -L 1,-n:file and blame the first line of the file instead. 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 7f81c00f3b1bd45c2954b18550b8e351651f72f3
2 files changed +4 -5
line-log.c
+2 -2
@@ -599,11 +599,11 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
599 lines, anchor, &begin, &end,
600 full_name))
601 die("malformed -L argument '%s'", range_part);
602 - if (lines < end || ((lines || begin) && lines < begin))
602 + if ((!lines && (begin || end)) || lines < begin)
603 die("file %s has only %lu lines", name_part, lines);
604 if (begin < 1)
605 begin = 1;
606 - if (end < 1)
606 + if (end < 1 || lines < end)
607 end = lines;
608 begin--;
609 line_log_data_insert(&ranges, full_name, begin, end);
t/t4211-line-log.sh
+2 -3
@@ -60,7 +60,6 @@ test_bad_opts "-L 1:nonexistent" "There is no path"
60 test_bad_opts "-L 1:simple" "There is no path"
61 test_bad_opts "-L '/foo:b.c'" "argument not .start,end:file"
62 test_bad_opts "-L 1000:b.c" "has only.*lines"
63 -test_bad_opts "-L 1,1000:b.c" "has only.*lines"
63 test_bad_opts "-L :b.c" "argument not .start,end:file"
64 test_bad_opts "-L :foo:b.c" "no match"
65
@@ -86,12 +85,12 @@ test_expect_success '-L ,Y (Y == nlines)' '
85
86 test_expect_success '-L ,Y (Y == nlines + 1)' '
87 n=$(expr $(wc -l <b.c) + 1) &&
89 - test_must_fail git log -L ,$n:b.c
88 + git log -L ,$n:b.c
89 '
90
91 test_expect_success '-L ,Y (Y == nlines + 2)' '
92 n=$(expr $(wc -l <b.c) + 2) &&
94 - test_must_fail git log -L ,$n:b.c
93 + git log -L ,$n:b.c
94 '
95
96 test_expect_success '-L with --first-parent and a merge' '