blame: add --fuzzy-lines command line option
PATCH 1/1 blame: add --fuzzy-lines command line option
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. It may instead be desirable to perform a git blame for the line numbers in the intersection of the file and the specified line range. This patch adds a --fuzzy-lines command line option to allow this.
--- Documentation/blame-options.txt | 5 +++++ builtin/blame.c | 4 ++++ t/t8003-blame-corner-cases.sh | 6 ++++++ 3 files changed, 15 insertions(+)
Documentation/blame-options.txt +5 -0
@@ -19,6 +19,11 @@ + include::line-range-format.txt[] +--fuzzy-lines:: + Use fuzzy line ranges. If a range specified with -L starts on a line + within the file but ends past the end of the file, display the blame + for the existing lines rather than failing. + -l:: Show long rev (Default: off).
builtin/blame.c +4 -0
@@ -661,6 +661,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)struct string_list range_list =STRING_LIST_INIT_NODUP ;int output_option =0 , opt =0 ; +int fuzzy_lines =0 ;int show_stats =0 ;const char *revs_file =NULL ;const char *contents_from =NULL ;
@@ -670,6 +671,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)OPT_BOOL (0 ,"root" , &show_root,N_ ("Do not treat root commits as boundaries (Default: off)" )),OPT_BOOL (0 ,"show-stats" , &show_stats,N_ ("Show work cost statistics" )),OPT_BOOL (0 ,"progress" , &show_progress,N_ ("Force progress reporting" )), +OPT_BOOL (0 ,"fuzzy-lines" , &fuzzy_lines,N_ ("Use fuzzy line ranges" )),OPT_BIT (0 ,"score-debug" , &output_option,N_ ("Show output score for blame entries" ),OUTPUT_SHOW_SCORE ),OPT_BIT ('f' ,"show-name" , &output_option,N_ ("Show original filename (Default: auto)" ),OUTPUT_SHOW_NAME ), OPT_BIT('n', "show-number", &output_option, N_("Show original linenumber (Default: off)"), OUTPUT_SHOW_NUMBER),
@@ -878,6 +880,8 @@ 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 (fuzzy_lines && lno < top) + top = lno;if (lno < top || ((lno || bottom) && lno < bottom)) die(Q_("file %s has only %lu line", "file %s has only %lu lines",
t/t8003-blame-corner-cases.sh +6 -0
@@ -220,6 +220,12 @@ test_expect_success 'blame -L with invalid end' ' test_i18ngrep "has only 2 lines" errors ' +test_expect_success 'blame -L with invalid end and fuzzy-lines' ' + git blame -L1,5 tres --fuzzy-lines >out && + cat out && + test $(wc -l < out) -eq 2 +' + test_expect_success 'blame parses <end> part of -L' ' git blame -L1,1 tres >out && cat out &&
Signature · 1 line
2.14.1
Quoted 4 lines · 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. It may instead be desirable to perform a git blame for the line numbers in the intersection of the file and the specified line range.
Even though erroring out upon such input was done as a deliberate design decision, in retrospect, I do not think the design decision made much sense.
The code already takes a nonsense input and tries to make best sense of it, e.g. "-L10,6" is interpreted as "-L6,10" instead of erroring out. So if we were to do this kind of change, I suspect that it may be better to do so unconditionally without introducing a new option.
Thanks.
PATCH v2 1/1 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, at present git will fail with a fatal error. This commit prevents such behaviour - instead the blame is display for any existing lines within the specified range.
--- builtin/blame.c | 4 ++-- t/t8003-blame-corner-cases.sh | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
builtin/blame.c +2 -2
@@ -878,13 +878,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 || 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);
t/t8003-blame-corner-cases.sh +3 -2
@@ -216,8 +216,9 @@ 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 && + cat out && + test $(wc -l < out) -eq 2 ' test_expect_success 'blame parses <end> part of -L' '
Signature · 1 line
2.14.1
Re: [PATCH v2] blame: prevent error if range ends past end of file
Quoted 7 lines · 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, at present git will fail with a fatal error. This commit prevents such behaviour - instead the blame is display for any existing lines within the specified range.
Signed-off-by Isabella Stephens <[email protected]>---
I like this change. We might want to document L to indicate that an L that is outside the range of lines will show all lines that do match.
Maybe we also want it to only succeed if at least some lines are blamed? Could we make it so that it fails if no lines are within the range? (ie: the start point is too far in? or does it already do such?)
Thanks,
Jake
Quoted 43 lines
builtin/blame.c | 4 ++-- t/t8003-blame-corner-cases.sh | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)builtin/blame.c +2
-2@@ -878,13 +878,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 || 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);t/t8003-blame-corner-cases.sh +3
-3@@ -216,8 +216,9 @@ 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 && + cat out && + test $(wc -l < out) -eq 2 ' test_expect_success 'blame parses <end> part of -L' ' --2.14.1
Re: [PATCH v2] blame: prevent error if range ends past end of file
Quoted 13 lines · Jacob Keller
Quoted 7 lines · 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, at present git will fail with a fatal error. This commit prevents such behaviour - instead the blame is display for any existing lines within the specified range.
Signed-off-by Isabella Stephens <[email protected]>---
I like this change. We might want to document L to indicate that an L that is outside the range of lines will show all lines that do match.
Quoted 7 lines
Maybe we also want it to only succeed if at least some lines are blamed? Could we make it so that it fails if no lines are within the range? (ie: the start point is too far in? or does it already do such?)
Thanks,
Jake
Yep, that is exactly how it behaves now - if a range intersects the file at all it will annotate the relevant lines, otherwise it will fail.
I'll add a clarification to the documentation in my next revision. Thanks for reviewing!
Re: [PATCH v2] blame: prevent error if range ends past end of file
In reply to Isabella StephensQuoted 4 lines
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, at present git will fail with a fatal error. This commit prevents such behaviour - instead the blame is display for any existing lines within the specified range.
s/is display/is displayed/ ?
'git log' has a very similar -L option, which errors out, too, if the end of the line range is past the end of the file. IMHO the interpretation of the line range -L<start>,<end> should be kept consistent in the two commands, and 'git log' shouldn't error out, either.
Quoted 31 lines
Signed-off-by Isabella Stephens <[email protected]>--- builtin/blame.c | 4 ++-- t/t8003-blame-corner-cases.sh | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)builtin/blame.c +2
-2@@ -878,13 +878,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 || 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);t/t8003-blame-corner-cases.sh +0
-0
t/t8003-blame-corner-cases.sh test_expect_success 'blame -L with invalid start' ' · line 221
Quoted 8 lines
t/t8003-blame-corner-cases.sh +3
-2@@ -216,8 +216,9 @@ 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 && + cat out && + test $(wc -l < out) -eq 2
Please use the test_line_count helper instead, as it will output a helpful error message on failure, making the 'cat out' above unnecessary.
Quoted 7 lines
t/t8003-blame-corner-cases.sh +0
-0' test_expect_success 'blame parses <end> part of -L' 'Signature · 1 line
2.14.1
Re: [PATCH v2] blame: prevent error if range ends past end of file
Quoted 6 lines · SZEDER Gábor
Quoted 4 lines
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, at present git will fail with a fatal error. This commit prevents such behaviour - instead the blame is display for any existing lines within the specified range.
s/is display/is displayed/ ?
Oops.
Quoted 6 lines
'git log' has a very similar -L option, which errors out, too, if the end of the line range is past the end of the file. IMHO the interpretation of the line range -L<start>,<end> should be kept consistent in the two commands, and 'git log' shouldn't error out, either.
Good suggestion. I'll submit a separate patch for git log.
Quoted 53 lines
Quoted 32 lines
Signed-off-by Isabella Stephens <[email protected]>--- builtin/blame.c | 4 ++-- t/t8003-blame-corner-cases.sh | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)builtin/blame.c +2
-2@@ -878,13 +878,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 || 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);t/t8003-blame-corner-cases.sh +0
-0t/t8003-blame-corner-cases.sh test_expect_success 'blame -L with invalid start' ' · line 221
Quoted 8 lines
t/t8003-blame-corner-cases.sh +3
-2@@ -216,8 +216,9 @@ 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 && + cat out && + test $(wc -l < out) -eq 2Please use the
test_line_counthelper instead, as it will output a helpful error message on failure, making the 'cat out' above unnecessary.
Thanks for pointing that out.Quoted 7 lines
t/t8003-blame-corner-cases.sh +0
-0' test_expect_success 'blame parses <end> part of -L' 'Signature · 1 line
2.14.1
PATCH v3 1/1 blame: prevent error if range ends past end of file
In reply to SZEDER GáborIf 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, at present git will fail with a fatal error. This commit prevents such behavior - instead the blame is displayed for existing lines within the specified range. Blaming a range that is entirely outside the bounds of the file will still fail.
This commit also ammends the relevant test and adds clarification to the documentation.
--- Documentation/git-blame.txt | 10 ++++++++++ builtin/blame.c | 4 ++-- t/t8003-blame-corner-cases.sh | 7 +++---- 3 files changed, 15 insertions(+), 6 deletions(-)
Documentation/git-blame.txt +10 -0
@@ -154,6 +154,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':
builtin/blame.c +2 -2
@@ -878,13 +878,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 || 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);
t/t8003-blame-corner-cases.sh +3 -4
@@ -216,14 +216,13 @@ 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 'indent of line numbers, nine lines' '
Signature · 1 line
2.14.1
Re: [PATCH v3] blame: prevent error if range ends past end of file
Quoted 8 lines · Isabella Stephens
builtin/blame.c +0
-0@@ -878,13 +878,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);
builtin/blame.c int cmd_blame(int argc, const char **argv, const char *prefix) · line 888
Quoted 10 lines
builtin/blame.c +2
-2-if (lno < top || ((lno || bottom) && lno < bottom)) +if ((lno || 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;
This section sanity-checks first and then tweaks the values it allowed to pass the check. Because it wants to later fix up an overly large "top" by capping to "lno" (i.e. total line number), the patch needs to loosen the early sanity-check. And the "fixed up" values are never checked if they are sane.
For example, with an empty file (i.e. lno == 0), you can ask "git blame -L1,-4 ("i.e. "at most four lines, ending at line #1") and the code silently accepts the input without noticing that the request is an utter nonsense; "file X has only 0 lines" error is given a chance to kick in.
There should be an "is the range sensible?" check after all the tweaking to bottom and top are done, I think.
Quoted 24 lines
builtin/blame.c +0
-0bottom--;range_set_append_unsafe (&ranges, bottom, top);t/t8003-blame-corner-cases.sh +3
-4@@ -216,14 +216,13 @@ 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 'indent of line numbers, nine lines' '
Re: [PATCH v3] blame: prevent error if range ends past end of file
Quoted 4 lines · Junio C Hamano
For example, with an empty file (i.e. lno == 0), you can ask "git blame -L1,-4 ("i.e. "at most four lines, ending at line #1") and the code silently accepts the input without noticing that the request is an utter nonsense; "file X has only 0 lines" error is given a chance
s/is given/is not given/; obviously. Sorry for a typo coming from laggy ssh connection.
Quoted 29 lines
to kick in.
There should be an "is the range sensible?" check after all the tweaking to bottom and top are done, I think.
Quoted 24 lines
builtin/blame.c +0
-0bottom--;range_set_append_unsafe (&ranges, bottom, top);t/t8003-blame-corner-cases.sh +3
-4@@ -216,14 +216,13 @@ 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 'indent of line numbers, nine lines' '
Re: [PATCH v3] blame: prevent error if range ends past end of file
In reply to Junio C HamanoQuoted 27 lines · Junio C Hamano
Quoted 8 lines · Isabella Stephens
builtin/blame.c +0
-0@@ -878,13 +878,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);builtin/blame.c int cmd_blame(int argc, const char **argv, const char *prefix) · line 888
Quoted 10 lines
builtin/blame.c +2
-2-if (lno < top || ((lno || bottom) && lno < bottom)) +if ((lno || 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;This section sanity-checks first and then tweaks the values it allowed to pass the check. Because it wants to later fix up an overly large "top" by capping to "lno" (i.e. total line number), the patch needs to loosen the early sanity-check. And the "fixed up" values are never checked if they are sane.
Quoted 8 lines
For example, with an empty file (i.e. lno == 0), you can ask "git blame -L1,-4 ("i.e. "at most four lines, ending at line #1") and the code silently accepts the input without noticing that the request is an utter nonsense; "file X has only 0 lines" error is given a chance to kick in.
There should be an "is the range sensible?" check after all the tweaking to bottom and top are done, I think.
My mistake. I missed that case. I think this section of code is a little hard to read because it avoids treating an empty file as a special case. Why not do something like this:
for (range_i = 0; range_i < range_list.nr; ++range_i) {
long bottom, top;
if (!lno)
die(_("file is empty"));
if (parse_range_arg(range_list.items[range_i].string,
nth_line_cb, &sb, lno, anchor,
&bottom, &top, sb.path))
usage(blame_usage);
if (bottom < 1)
bottom = 1;
if (lno < top)
top = lno;
if (top < 0 || lno < bottom)
die(Q_("file %s has only %lu line",
"file %s has only %lu lines",
lno), path, lno);
bottom--;
range_set_append_unsafe(&ranges, bottom, top);
anchor = top + 1;We'd also need to change parse_range_arg to always make bottom less than top:
- if (*begin && *end && *end < *begin) {
+ if (*end < *begin) {
SWAP(*end, *begin);
}This also fixes the case where the given range is n,-(n+1) (e.g. -L1,-2). At the moment that will blame from n to the end of the file. My suggested change would instead blame the first n lines, which makes a lot more sense IMO.
Happy to leave as is if you aren't happy with this suggestion, however.
Re: [PATCH v3] blame: prevent error if range ends past end of file
Quoted 4 lines · Isabella Stephens
Quoted 2 lines · Junio C Hamano
There should be an "is the range sensible?" check after all the tweaking to bottom and top are done, I think.
Quoted 8 lines
My mistake. I missed that case. I think this section of code is a little hard to read because it avoids treating an empty file as a special case. Why not do something like this:
for (range_i = 0; range_i < range_list.nr; ++range_i) { long bottom, top; if (!lno) die(_("file is empty"));
No need for this conditional to reside within the loop. Hoisting it outside the loop would (IMO) make the intent even clearer:
if (range_list.nr && !lno)
die(_("file is empty; -L has no effect"));
for (...) {
...On the other hand, one could argue that "-L," (where comma is the sole argument to -L), which specifies the entire file, should be allowed even on an empty file without complaining that the file is empty. Although it may not seem sensible for a human to specify "-L," perhaps a tool would do so to override an earlier more restrictive -L range. However, that may be too esoteric a case to worry about.
Quoted 15 lines
if (parse_range_arg(range_list.items[range_i].string,
nth_line_cb, &sb, lno, anchor,
&bottom, &top, sb.path))
usage(blame_usage);
if (bottom < 1)
bottom = 1;
if (lno < top)
top = lno;
if (top < 0 || lno < bottom)
die(Q_("file %s has only %lu line",
"file %s has only %lu lines",
lno), path, lno);
bottom--;
range_set_append_unsafe(&ranges, bottom, top);
anchor = top + 1;v4 cover 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.
PATCH v4 1/2 blame: prevent error if range ends past end of file
From: Isabella Stephens <[email protected]>
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.
--- 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(-)
Documentation/git-blame.txt +10 -0
@@ -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':
builtin/blame.c +4 -2
@@ -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);
line-range.c +1 -1
@@ -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;
t/t8003-blame-corner-cases.sh +13 -4
@@ -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 <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 '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' '
Signature · 1 line
2.14.3 (Apple Git-98)
Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
line-range.c static const char *parse_loc(const char *spec, nth_line_fn_t nth_line, · line 50
Quoted 10 lines · [email protected] writes:
line-range.c +1
-1@@ -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 ;
When parsing "-L<something>,-20" to grab some lines before the line specified by <something>, if that something happens to be line #20, this gives -1 to *ret. If it is line #19, *ret becomes -1, and if it is line #18 or before, *ret becomes -2, -3, ...
Is that what we really want here? It is disturbing that only line #19 and #20 are treated identically in the above example. If it were "if going backwards by -num lines from begin goes beyond the beginning of the file, clip it to the first line", I would understand it, but as written, I am not sure what the code is trying to do.
Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
My intention was to modify existing behaviour as little as possible, but I agree clipping to the first line makes a lot more sense. That raises the question though, do we clip to 1 and treat -L,-n as a valid input, or clip to -1 so that this case be detected?
Quoted 25 lines · Junio C Hamano
line-range.c static const char *parse_loc(const char *spec, nth_line_fn_t nth_line, · line 50
Quoted 10 lines · [email protected] writes:
line-range.c +1
-1@@ -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 ;When parsing "-L<something>,-20" to grab some lines before the line specified by <something>, if that something happens to be line #20, this gives -1 to *ret. If it is line #19, *ret becomes -1, and if it is line #18 or before, *ret becomes -2, -3, ...
Is that what we really want here? It is disturbing that only line #19 and #20 are treated identically in the above example. If it were "if going backwards by -num lines from begin goes beyond the beginning of the file, clip it to the first line", I would understand it, but as written, I am not sure what the code is trying to do.
Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
Quoted 19 lines · Isabella Stephens
Quoted 18 lines · Junio C Hamano
line-range.c static const char *parse_loc(const char *spec, nth_line_fn_t nth_line, · line 50
Quoted 10 lines · [email protected] writes:
line-range.c +1
-1@@ -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 ;When parsing "-L<something>,-20" to grab some lines before the line specified by <something>, if that something happens to be line #20, this gives -1 to *ret. If it is line #19, *ret becomes -1, and if it is line #18 or before, *ret becomes -2, -3, ...
Quoted 7 lines
Quoted 7 lines
Is that what we really want here? It is disturbing that only line #19 and #20 are treated identically in the above example. If it were "if going backwards by -num lines from begin goes beyond the beginning of the file, clip it to the first line", I would understand it, but as written, I am not sure what the code is trying to do.
[administrivia] Do not top-post, but cull the context to leave enough to remind readers what the discussion was about.
Quoted 4 lines
My intention was to modify existing behaviour as little as possible, but I agree clipping to the first line makes a lot more sense. That raises the question though, do we clip to 1 and treat -L,-n as a valid input, or clip to -1 so that this case be detected?
Maybe I misread the previous discussion and/or your cover letter, but I have been assuming that you are trying to avoid failing the command in a useless way (e.g. when the file has only ~800 lines but the user does not know exactly how many, instead of letting -L1,820 to fail with "the file only has 815 lines", pretend that the -L1,815 was given) and instead give a reasonable fall-back behaviour.
And to be consistent with that world view, I would have expected that the meaning of -L<something>,-20 to be updated from "fail if <something> is before line #20, or show 20 lines leading to <something>" to "show lines leading to <something>, up to 20 lines but it is OK if there aren't enough lines in the file to show that many".
So the answer to the question probably depends on what happens when "this case is detected" by returning -1 from here. Do we detect and fail? That would defeat the overall theme of these patches. Do we detct and warn but continue? That may be sensible in theory, but in practice, especially where this "the users may not know how many lines exactly the blob has, so do not force them to count" matters, "blame" and "log" would show a lot of output that is sent to the pager, so the warning message may not be shown in a noticeable way. Compared to that, "pretend as if the first line was specified and go on" looks like we have one fewer thing to worry about ;-)
Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
Quoted 6 lines
Maybe I misread the previous discussion and/or your cover letter, but I have been assuming that you are trying to avoid failing the command in a useless way (e.g. when the file has only ~800 lines but the user does not know exactly how many, instead of letting -L1,820 to fail with "the file only has 815 lines", pretend that the -L1,815 was given) and instead give a reasonable fall-back behaviour.
That's correct. In doing so I picked up on a few extra cases where the behaviour wasn't intuitive, so I've attempted to fix all of those with this patch.
Quoted 6 lines
And to be consistent with that world view, I would have expected that the meaning of -L<something>,-20 to be updated from "fail if <something> is before line #20, or show 20 lines leading to <something>" to "show lines leading to <something>, up to 20 lines but it is OK if there aren't enough lines in the file to show that many".
This is the existing behaviour. -L10,-20 for example will blame the first 10 lines of a file, it will not fail. My patch doesn't change this. The case I am discussing is -L,-20 which at the moment blames the first line of the file. Trying to go backwards from the start of a file should be considered invalid, in my opinion, however I don't feel strongly about it - I don't expect this case is common in practice.
Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
Quoted 7 lines · Isabella Stephens
This is the existing behaviour. -L10,-20 for example will blame the first 10 lines of a file, it will not fail. My patch doesn't change this. The case I am discussing is -L,-20 which at the moment blames the first line of the file. Trying to go backwards from the start of a file should be considered invalid, in my opinion, however I don't feel strongly about it - I don't expect this case is common in practice.
I tend to think that -L,-20 is a sloppy spelling of -L1,-20 (i.e. anything omitted gets reasonable default, and for something that specifies both ends, i.e. "<begin>,<end>", the beginning and the end of the file would be such reasonable default, respectively), and as such I would imagine that the user would expect the same behaviour as -L1,-20. If the longhand version gives only the first line (i.e. show up to 20 lines ending at line #1), I'd say that sounds sensible.
Or does -L1,-20 show nothing and consider the input invalid? If so, then sure, -L,-20 should also be an invalid input.
v5 cover 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 1/1 blame: prevent error if range ends past end of file
From: Isabella Stephens <[email protected]>
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.
--- 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(-)
Documentation/git-blame.txt +10 -0
@@ -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':
builtin/blame.c +2 -2
@@ -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);
line-range.c +1 -1
@@ -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;
t/t8003-blame-corner-cases.sh +8 -4
@@ -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' '
Signature · 1 line
2.14.3 (Apple Git-98)
Re: [PATCH] blame: prevent error if range ends past end of file
Quoted 4 lines · On Tue, May 29, 2018 at 1:30 AM, <[email protected]> wrote:
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.
Makes sense; the new behavior is intuitive and more friendly.
Quoted 1 line
Tests and documentation are ammended accordingly.
s/ammended/amended/
Quoted 10 lines
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 <[email protected]>---
Documentation/git-blame.txt +0
-0@@ -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.
Documentation/git-blame.txt Also you can use a regular expression to specify the line range: · line 162
Quoted 10 lines
Documentation/git-blame.txt +9
-0+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.
This documentation seems misplaced. Rather than inserting it after the discussion of -L/regex/, a more natural place would be just above -L/regex/ where -L<begin>,<end> is discussed.
However, I am not at all convinced that this behavior should be documented to this level of detail. Doing so assigns too much emphasis to what should be intuitive, thus wastes readers' time wondering why it is so heavily emphasized. At _most_, I would think you could say merely:
A range that begins or ends outside the bounds of the file will be clipped to the file's extent.
and drop the example and discussion of the example results altogether.
In fact, because this new behavior is what most users will intuitively expect, it might be perfectly reasonable to not say anything about it at all (that is, don't modify git-blame.txt).
Thanks.
Re: [PATCH] blame: prevent error if range ends past end of file
Quoted 24 lines · Eric Sunshine
Documentation/git-blame.txt Also you can use a regular expression to specify the line range: · line 162
Quoted 13 lines
Documentation/git-blame.txt +9
-0@@ -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.This documentation seems misplaced. Rather than inserting it after the discussion of -L/regex/, a more natural place would be just above -L/regex/ where -L<begin>,<end> is discussed.
However, I am not at all convinced that this behavior should be documented to this level of detail. Doing so assigns too much emphasis to what should be intuitive, thus wastes readers' time wondering why it is so heavily emphasized. At _most_, I would think you could say merely:
Quoted 8 lines
A range that begins or ends outside the bounds of the file will be clipped to the file's extent.
and drop the example and discussion of the example results altogether.
In fact, because this new behavior is what most users will intuitively expect, it might be perfectly reasonable to not say anything about it at all (that is, don't modify git-blame.txt)
Thanks for reviewing Eric. I've submitted a v6 patch in response to your feedback.
I agree that given the behavior is intuitive it's not necessary to document this change, so I've reverted the change to git-blame.txt entirely.
PATCH 1/1 log: prevent error if line range ends past end of file
In reply to [email protected]From: Isabella Stephens <[email protected]>
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.
--- line-log.c | 8 +++++--- t/t4211-line-log.sh | 8 +------- 2 files changed, 6 insertions(+), 10 deletions(-)
line-log.c +5 -3
@@ -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);
t/t4211-line-log.sh +1 -7
@@ -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 <b.c) + 1) && - test_must_fail 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' '
Signature · 1 line
2.14.3 (Apple Git-98)
Re: [PATCH] log: prevent error if line range ends past end of file
Quoted 13 lines · On Tue, May 29, 2018 at 1:30 AM, <[email protected]> wrote:
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 <[email protected]>---
t/t4211-line-log.sh test_expect_success '-L ,Y (Y == nlines)' ' · line 88
Quoted 10 lines
t/t4211-line-log.sh +1
-6@@ -86,12 +85,7 @@ 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 -' - -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 '
Not sure why you removed the 'n+2' test which was added intentionally, along with the 'n' and 'n+1' tests, to probe the boundary handling for correctness. By eliminating 'n+2', coverage is reduced and, even though your change might be correct at this boundary, some future breaking change might go undetected.
PATCH v4 2/2 log: prevent error if line range ends past end of file
In reply to [email protected]From: Isabella Stephens <[email protected]>
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.
--- line-log.c | 10 +++++++--- t/t4211-line-log.sh | 11 ----------- 2 files changed, 7 insertions(+), 14 deletions(-)
line-log.c +7 -3
@@ -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);
t/t4211-line-log.sh +0 -11
@@ -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 <b.c) + 1) && - test_must_fail 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 -' - test_expect_success '-L with --first-parent and a merge' ' git checkout parallel-change && git log --first-parent -L 1,1:b.c
Signature · 1 line
2.14.3 (Apple Git-98)
Quoted 11 lines · Junio C Hamano
Quoted 4 lines · 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. It may instead be desirable to perform a git blame for the line numbers in the intersection of the file and the specified line range.
Even though erroring out upon such input was done as a deliberate design decision, in retrospect, I do not think the design decision made much sense.
Quoted 7 lines
The code already takes a nonsense input and tries to make best sense of it, e.g. "-L10,6" is interpreted as "-L6,10" instead of erroring out. So if we were to do this kind of change, I suspect that it may be better to do so unconditionally without introducing a new option.
Thanks.
Thanks for following up. I've sent through a version 2 of the patch without the command line option.