[PATCH v7 1/2] blame: prevent error if range ends past end of file
From: Isabella Stephens 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 --- builtin/blame.c | 4 ++-- line-range.c | 2 +- t/annotate-tests.sh | 4 ++-- t/t8003-blame-corner-cases.sh | 12 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 9dcb367b9..e1359b192 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -886,13 +886,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); diff --git a/line-range.c b/line-range.c index 323399d16..232c3909e 100644 --- a/line-range.c +++ b/line-range.c @@ -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; diff --git a/t/annotate-tests.sh b/t/annotate-tests.sh index 093832fef..6da48a2e0 100644 --- a/t/annotate-tests.sh +++ b/t/annotate-tests.sh @@ -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 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)' ' diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh index 661f9d430..c92a47b6d 100755 --- a/t/t8003-blame-corner-cases.sh +++ b/t/t8003-blame-corner-cases.sh @@ -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 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' ' -- 2.14.3 (Apple Git-98)
[PATCH v7 2/2] log: prevent error if line range ends past end of file
From: Isabella Stephens 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 --- line-log.c | 4 ++-- t/t4211-line-log.sh | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/line-log.c b/line-log.c index cdc2257db..8ad074e58 100644 --- a/line-log.c +++ b/line-log.c @@ -599,11 +599,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); diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh index d0377fae5..436b13ad2 100755 --- a/t/t4211-line-log.sh +++ b/t/t4211-line-log.sh @@ -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" @@ -86,12 +85,12 @@ test_expect_success '-L ,Y (Y == nlines)' ' test_expect_success '-L ,Y (Y == nlines + 1)' ' n=$(expr $(wc -l
[PATCH v6 2/2] log: prevent error if line range ends past end of file
From: Isabella Stephens 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 --- line-log.c | 8 +--- t/t4211-line-log.sh | 5 ++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/line-log.c b/line-log.c index cdc2257db..4da40dec1 100644 --- a/line-log.c +++ b/line-log.c @@ -599,11 +599,13 @@ 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)) - die("file %s has only %lu lines", name_part, lines); + if ((!lines && (begin || end)) || lines < begin) + die(Q_("file %s has only %lu line", + "file %s has only %lu lines", + 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); diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh index d0377fae5..436b13ad2 100755 --- a/t/t4211-line-log.sh +++ b/t/t4211-line-log.sh @@ -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" @@ -86,12 +85,12 @@ test_expect_success '-L ,Y (Y == nlines)' ' test_expect_success '-L ,Y (Y == nlines + 1)' ' n=$(expr $(wc -l
[PATCH v6 1/2] blame: prevent error if range ends past end of file
From: Isabella Stephens 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 --- builtin/blame.c | 4 ++-- line-range.c | 2 +- t/t8003-blame-corner-cases.sh | 12 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/builtin/blame.c b/builtin/blame.c index 9dcb367b9..e1359b192 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -886,13 +886,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); diff --git a/line-range.c b/line-range.c index 323399d16..232c3909e 100644 --- a/line-range.c +++ b/line-range.c @@ -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; diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh index 661f9d430..c92a47b6d 100755 --- a/t/t8003-blame-corner-cases.sh +++ b/t/t8003-blame-corner-cases.sh @@ -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 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' ' -- 2.14.3 (Apple Git-98)
[PATCH v5 0/2] blame and log: prevent error if range ends past end of file
Okay, I think that sounds reasonable. I've amended the patch so that the line number is clipped to 1 when reading in a line range. And -L,-n is now treated the same as -L1,-n i.e. it will blame (or log) the first line of the file only.
[PATCH] blame: prevent error if range ends past end of file
From: Isabella Stephens 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 and documentation are ammended 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 --- Documentation/git-blame.txt | 10 ++ builtin/blame.c | 4 ++-- line-range.c | 2 +- t/t8003-blame-corner-cases.sh | 12 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Documentation/git-blame.txt b/Documentation/git-blame.txt index 16323eb80..8cb81f57a 100644 --- a/Documentation/git-blame.txt +++ b/Documentation/git-blame.txt @@ -152,6 +152,16 @@ Also you can use a regular expression to specify the line range: which limits the annotation to the body of the `hello` subroutine. +A range that begins or ends outside the bounds of the file will +blame the relevant lines. For example: + + git blame -L 10,-20 foo + git blame -L 10,+20 foo + +will respectively blame the first 10 and last 11 lines of a +20 line file. However, blaming a line range that is entirely +outside the bounds of the file will fail. + When you are not interested in changes older than version v2.6.18, or changes older than 3 weeks, you can use revision range specifiers similar to 'git rev-list': diff --git a/builtin/blame.c b/builtin/blame.c index 9dcb367b9..e1359b192 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -886,13 +886,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); diff --git a/line-range.c b/line-range.c index 323399d16..232c3909e 100644 --- a/line-range.c +++ b/line-range.c @@ -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; diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh index 661f9d430..c92a47b6d 100755 --- a/t/t8003-blame-corner-cases.sh +++ b/t/t8003-blame-corner-cases.sh @@ -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 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' ' -- 2.14.3 (Apple Git-98)
[PATCH] log: prevent error if line range ends past end of file
From: Isabella Stephens 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 --- line-log.c | 8 +--- t/t4211-line-log.sh | 8 +--- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/line-log.c b/line-log.c index cdc2257db..4da40dec1 100644 --- a/line-log.c +++ b/line-log.c @@ -599,11 +599,13 @@ 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)) - die("file %s has only %lu lines", name_part, lines); + if ((!lines && (begin || end)) || lines < begin) + die(Q_("file %s has only %lu line", + "file %s has only %lu lines", + 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); diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh index d0377fae5..c617347c4 100755 --- a/t/t4211-line-log.sh +++ b/t/t4211-line-log.sh @@ -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" @@ -86,12 +85,7 @@ test_expect_success '-L ,Y (Y == nlines)' ' test_expect_success '-L ,Y (Y == nlines + 1)' ' n=$(expr $(wc -l
[PATCH v4 2/2] log: prevent error if line range ends past end of file
From: Isabella Stephens 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 complain that this is an empty range. Signed-off-by: Isabella Stephens --- line-log.c | 10 +++--- t/t4211-line-log.sh | 11 --- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/line-log.c b/line-log.c index cdc2257db..ad3987062 100644 --- a/line-log.c +++ b/line-log.c @@ -599,11 +599,15 @@ 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)) - die("file %s has only %lu lines", name_part, lines); + if (!begin && end < 0) + die("-L invalid empty range"); + if ((!lines && (begin || end)) || lines < begin) + die(Q_("file %s has only %lu line", + "file %s has only %lu lines", + 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); diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh index d0377fae5..0b96496e3 100755 --- a/t/t4211-line-log.sh +++ b/t/t4211-line-log.sh @@ -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" @@ -84,16 +83,6 @@ test_expect_success '-L ,Y (Y == nlines)' ' git log -L ,$n:b.c ' -test_expect_success '-L ,Y (Y == nlines + 1)' ' - n=$(expr $(wc -l
[PATCH v4 1/2] blame: prevent error if range ends past end of file
From: Isabella Stephens 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 and documentation are ammended 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 complain of an empty range, rather than blaming the whole file. Signed-off-by: Isabella Stephens --- Documentation/git-blame.txt | 10 ++ builtin/blame.c | 6 -- line-range.c | 2 +- t/t8003-blame-corner-cases.sh | 17 + 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Documentation/git-blame.txt b/Documentation/git-blame.txt index 16323eb80..8cb81f57a 100644 --- a/Documentation/git-blame.txt +++ b/Documentation/git-blame.txt @@ -152,6 +152,16 @@ Also you can use a regular expression to specify the line range: which limits the annotation to the body of the `hello` subroutine. +A range that begins or ends outside the bounds of the file will +blame the relevant lines. For example: + + git blame -L 10,-20 foo + git blame -L 10,+20 foo + +will respectively blame the first 10 and last 11 lines of a +20 line file. However, blaming a line range that is entirely +outside the bounds of the file will fail. + When you are not interested in changes older than version v2.6.18, or changes older than 3 weeks, you can use revision range specifiers similar to 'git rev-list': diff --git a/builtin/blame.c b/builtin/blame.c index 9dcb367b9..1204ab142 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -886,13 +886,15 @@ 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 (!bottom && top < 0) + die("-L invalid empty range"); + 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); diff --git a/line-range.c b/line-range.c index 323399d16..023aee1f5 100644 --- a/line-range.c +++ b/line-range.c @@ -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 ? begin + num : -1; return term; } return spec; diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh index 661f9d430..4a0c51658 100755 --- a/t/t8003-blame-corner-cases.sh +++ b/t/t8003-blame-corner-cases.sh @@ -216,14 +216,23 @@ 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 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 'blame -L,-n' ' + test_must_fail git blame -L,-1 tres 2>errors && + test_i18ngrep "-L invalid empty range" ' test_expect_success 'indent of line numbers, nine lines' ' -- 2.14.3 (Apple Git-98)
[PATCH v4 0/2] blame and log: prevent error if range ends past end of file
Picking this back up after a little while. This solution means we can still accept -L, for an empty file but any other range will fail. I've made the change for both blame and log (as two separate patches). I've also changed behaviour in a couple of corner cases - before we couldn't distinguish between -Ln,-(n+1) and -Ln, so -Ln,-(n+1) would blame from n to the end of the file rather than the first n lines. Also, we now complain that -L,-n is an empty range where previously this would blame the whole file.