Skip to content

Commit 6566a91

Browse files
committed
Merge branch 'is/parsing-line-range'
Parsing of -L[<N>][,[<M>]] parameters "git blame" and "git log" take has been tweaked. * is/parsing-line-range: log: prevent error if line range ends past end of file blame: prevent error if range ends past end of file
2 parents af8ac73 + 7f81c00 commit 6566a91

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

builtin/blame.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,13 +1002,13 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
10021002
nth_line_cb, &sb, lno, anchor,
10031003
&bottom, &top, sb.path))
10041004
usage(blame_usage);
1005-
if (lno < top || ((lno || bottom) && lno < bottom))
1005+
if ((!lno && (top || bottom)) || lno < bottom)
10061006
die(Q_("file %s has only %lu line",
10071007
"file %s has only %lu lines",
10081008
lno), path, lno);
10091009
if (bottom < 1)
10101010
bottom = 1;
1011-
if (top < 1)
1011+
if (top < 1 || lno < top)
10121012
top = lno;
10131013
bottom--;
10141014
range_set_append_unsafe(&ranges, bottom, top);

line-log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,11 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
598598
lines, anchor, &begin, &end,
599599
full_name))
600600
die("malformed -L argument '%s'", range_part);
601-
if (lines < end || ((lines || begin) && lines < begin))
601+
if ((!lines && (begin || end)) || lines < begin)
602602
die("file %s has only %lu lines", name_part, lines);
603603
if (begin < 1)
604604
begin = 1;
605-
if (end < 1)
605+
if (end < 1 || lines < end)
606606
end = lines;
607607
begin--;
608608
line_log_data_insert(&ranges, full_name, begin, end);

line-range.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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/t4211-line-log.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ test_bad_opts "-L 1:nonexistent" "There is no path"
6060
test_bad_opts "-L 1:simple" "There is no path"
6161
test_bad_opts "-L '/foo:b.c'" "argument not .start,end:file"
6262
test_bad_opts "-L 1000:b.c" "has only.*lines"
63-
test_bad_opts "-L 1,1000:b.c" "has only.*lines"
6463
test_bad_opts "-L :b.c" "argument not .start,end:file"
6564
test_bad_opts "-L :foo:b.c" "no match"
6665

@@ -86,12 +85,12 @@ test_expect_success '-L ,Y (Y == nlines)' '
8685

8786
test_expect_success '-L ,Y (Y == nlines + 1)' '
8887
n=$(expr $(wc -l <b.c) + 1) &&
89-
test_must_fail git log -L ,$n:b.c
88+
git log -L ,$n:b.c
9089
'
9190

9291
test_expect_success '-L ,Y (Y == nlines + 2)' '
9392
n=$(expr $(wc -l <b.c) + 2) &&
94-
test_must_fail git log -L ,$n:b.c
93+
git log -L ,$n:b.c
9594
'
9695

9796
test_expect_success '-L with --first-parent and a merge' '

t/t8003-blame-corner-cases.sh

Lines changed: 8 additions & 4 deletions
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)