Skip to content

Commit

Permalink
Merge branch 'is/parsing-line-range'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitster committed Aug 2, 2018
2 parents af8ac73 + 7f81c00 commit 6566a91
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,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);
Expand Down
4 changes: 2 additions & 2 deletions line-log.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
lines, anchor, &begin, &end,
full_name))
die("malformed -L argument '%s'", range_part);
if (lines < end || ((lines || begin) && lines < begin))
if ((!lines && (begin || end)) || lines < begin)
die("file %s has only %lu lines", name_part, lines);
if (begin < 1)
begin = 1;
if (end < 1)
if (end < 1 || lines < end)
end = lines;
begin--;
line_log_data_insert(&ranges, full_name, begin, end);
Expand Down
2 changes: 1 addition & 1 deletion line-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions t/annotate-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)' '
Expand Down
5 changes: 2 additions & 3 deletions t/t4211-line-log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ test_bad_opts "-L 1:nonexistent" "There is no path"
test_bad_opts "-L 1:simple" "There is no path"
test_bad_opts "-L '/foo:b.c'" "argument not .start,end:file"
test_bad_opts "-L 1000:b.c" "has only.*lines"
test_bad_opts "-L 1,1000:b.c" "has only.*lines"
test_bad_opts "-L :b.c" "argument not .start,end:file"
test_bad_opts "-L :foo:b.c" "no match"

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

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

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

test_expect_success '-L with --first-parent and a merge' '
Expand Down
12 changes: 8 additions & 4 deletions t/t8003-blame-corner-cases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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' '
Expand Down

0 comments on commit 6566a91

Please sign in to comment.