All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] blame: add --fuzzy-lines command line option
@ 2017-10-26  2:19 Isabella Stephens
  2017-10-26  6:15 ` Junio C Hamano
  0 siblings, 1 reply; 27+ messages in thread
From: Isabella Stephens @ 2017-10-26  2:19 UTC (permalink / raw)
  To: git; +Cc: sunshine, peff, gitster, bturner, 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.
This patch adds a --fuzzy-lines command line option to allow this.

Signed-off-by: Isabella Stephens <istephens@atlassian.com>
---
 Documentation/blame-options.txt | 5 +++++
 builtin/blame.c                 | 4 ++++
 t/t8003-blame-corner-cases.sh   | 6 ++++++
 3 files changed, 15 insertions(+)

diff --git a/Documentation/blame-options.txt b/Documentation/blame-options.txt
index dc41957af..664cd8f8b 100644
--- a/Documentation/blame-options.txt
+++ b/Documentation/blame-options.txt
@@ -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).
 
diff --git a/builtin/blame.c b/builtin/blame.c
index 67adaef4d..d25b39d40 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -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",
diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
index 661f9d430..6e7657df2 100755
--- a/t/t8003-blame-corner-cases.sh
+++ b/t/t8003-blame-corner-cases.sh
@@ -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 &&
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH] blame: add --fuzzy-lines command line option
  2017-10-26  2:19 [PATCH] blame: add --fuzzy-lines command line option Isabella Stephens
@ 2017-10-26  6:15 ` Junio C Hamano
  2017-10-26  7:01   ` [PATCH v2] blame: prevent error if range ends past end of file Isabella Stephens
  2017-10-26  7:08   ` [PATCH] blame: add --fuzzy-lines command line option Isabella Stephens
  0 siblings, 2 replies; 27+ messages in thread
From: Junio C Hamano @ 2017-10-26  6:15 UTC (permalink / raw)
  To: Isabella Stephens; +Cc: git, sunshine, peff, bturner

Isabella Stephens <istephens@atlassian.com> writes:

> 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.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v2] blame: prevent error if range ends past end of file
  2017-10-26  6:15 ` Junio C Hamano
@ 2017-10-26  7:01   ` Isabella Stephens
  2017-10-26  8:48     ` Jacob Keller
  2017-10-26 15:31     ` SZEDER Gábor
  2017-10-26  7:08   ` [PATCH] blame: add --fuzzy-lines command line option Isabella Stephens
  1 sibling, 2 replies; 27+ messages in thread
From: Isabella Stephens @ 2017-10-26  7:01 UTC (permalink / raw)
  To: gitster; +Cc: git, sunshine, peff, bturner, 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 <istephens@atlassian.com>
---
 builtin/blame.c               | 4 ++--
 t/t8003-blame-corner-cases.sh | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/builtin/blame.c b/builtin/blame.c
index 67adaef4d..b5b9db147 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -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);
diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
index 661f9d430..32b3788fe 100755
--- a/t/t8003-blame-corner-cases.sh
+++ b/t/t8003-blame-corner-cases.sh
@@ -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


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH] blame: add --fuzzy-lines command line option
  2017-10-26  6:15 ` Junio C Hamano
  2017-10-26  7:01   ` [PATCH v2] blame: prevent error if range ends past end of file Isabella Stephens
@ 2017-10-26  7:08   ` Isabella Stephens
  1 sibling, 0 replies; 27+ messages in thread
From: Isabella Stephens @ 2017-10-26  7:08 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, sunshine, peff, bturner

On 26/10/17 5:15 pm, Junio C Hamano wrote:
> Isabella Stephens <istephens@atlassian.com> writes:
> 
>> 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.
> 

Thanks for following up. I've sent through a version 2 of the patch 
without the command line option.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2] blame: prevent error if range ends past end of file
  2017-10-26  7:01   ` [PATCH v2] blame: prevent error if range ends past end of file Isabella Stephens
@ 2017-10-26  8:48     ` Jacob Keller
  2017-10-26 22:50       ` Isabella Stephens
  2017-10-26 15:31     ` SZEDER Gábor
  1 sibling, 1 reply; 27+ messages in thread
From: Jacob Keller @ 2017-10-26  8:48 UTC (permalink / raw)
  To: Isabella Stephens
  Cc: Junio C Hamano, Git mailing list, Eric Sunshine, Jeff King, bturner

On Thu, Oct 26, 2017 at 12:01 AM, Isabella Stephens
<istephens@atlassian.com> 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, 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 <istephens@atlassian.com>
> ---

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

>  builtin/blame.c               | 4 ++--
>  t/t8003-blame-corner-cases.sh | 5 +++--
>  2 files changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/builtin/blame.c b/builtin/blame.c
> index 67adaef4d..b5b9db147 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -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);
> diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
> index 661f9d430..32b3788fe 100755
> --- a/t/t8003-blame-corner-cases.sh
> +++ b/t/t8003-blame-corner-cases.sh
> @@ -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
>

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2] blame: prevent error if range ends past end of file
  2017-10-26  7:01   ` [PATCH v2] blame: prevent error if range ends past end of file Isabella Stephens
  2017-10-26  8:48     ` Jacob Keller
@ 2017-10-26 15:31     ` SZEDER Gábor
  2017-10-26 23:37       ` Isabella Stephens
  2017-10-27  0:56       ` [PATCH v3] " Isabella Stephens
  1 sibling, 2 replies; 27+ messages in thread
From: SZEDER Gábor @ 2017-10-26 15:31 UTC (permalink / raw)
  To: Isabella Stephens
  Cc: SZEDER Gábor, gitster, git, sunshine, peff, bturner

> 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.

> Signed-off-by: Isabella Stephens <istephens@atlassian.com>
> ---
>  builtin/blame.c               | 4 ++--
>  t/t8003-blame-corner-cases.sh | 5 +++--
>  2 files changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/builtin/blame.c b/builtin/blame.c
> index 67adaef4d..b5b9db147 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -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);
> diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
> index 661f9d430..32b3788fe 100755
> --- a/t/t8003-blame-corner-cases.sh
> +++ b/t/t8003-blame-corner-cases.sh
> @@ -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.

>  '
>  
>  test_expect_success 'blame parses <end> part of -L' '
> -- 
> 2.14.1
> 
> 

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2] blame: prevent error if range ends past end of file
  2017-10-26  8:48     ` Jacob Keller
@ 2017-10-26 22:50       ` Isabella Stephens
  0 siblings, 0 replies; 27+ messages in thread
From: Isabella Stephens @ 2017-10-26 22:50 UTC (permalink / raw)
  To: Jacob Keller
  Cc: Junio C Hamano, Git mailing list, Eric Sunshine, Jeff King, bturner

On 26/10/17 7:48 pm, Jacob Keller wrote:
> On Thu, Oct 26, 2017 at 12:01 AM, Isabella Stephens
> <istephens@atlassian.com> 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, 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 <istephens@atlassian.com>
>> ---
> 
> 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

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!

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v2] blame: prevent error if range ends past end of file
  2017-10-26 15:31     ` SZEDER Gábor
@ 2017-10-26 23:37       ` Isabella Stephens
  2017-10-27  0:56       ` [PATCH v3] " Isabella Stephens
  1 sibling, 0 replies; 27+ messages in thread
From: Isabella Stephens @ 2017-10-26 23:37 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: gitster, git, sunshine, peff, bturner

On 27/10/17 2:31 am, SZEDER Gábor 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, 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.

> 
> '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.

> 
>> Signed-off-by: Isabella Stephens <istephens@atlassian.com>
>> ---
>>  builtin/blame.c               | 4 ++--
>>  t/t8003-blame-corner-cases.sh | 5 +++--
>>  2 files changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/builtin/blame.c b/builtin/blame.c
>> index 67adaef4d..b5b9db147 100644
>> --- a/builtin/blame.c
>> +++ b/builtin/blame.c
>> @@ -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);
>> diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
>> index 661f9d430..32b3788fe 100755
>> --- a/t/t8003-blame-corner-cases.sh
>> +++ b/t/t8003-blame-corner-cases.sh
>> @@ -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.
> Thanks for pointing that out.

>>  '
>>  
>>  test_expect_success 'blame parses <end> part of -L' '
>> -- 
>> 2.14.1
>>
>>

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v3] blame: prevent error if range ends past end of file
  2017-10-26 15:31     ` SZEDER Gábor
  2017-10-26 23:37       ` Isabella Stephens
@ 2017-10-27  0:56       ` Isabella Stephens
  2017-10-27  1:58         ` Junio C Hamano
  1 sibling, 1 reply; 27+ messages in thread
From: Isabella Stephens @ 2017-10-27  0:56 UTC (permalink / raw)
  To: szeder.dev
  Cc: git, sunshine, peff, bturner, gitster, jacob.keller, 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 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.

Signed-off-by: Isabella Stephens <istephens@atlassian.com>
---
 Documentation/git-blame.txt   | 10 ++++++++++
 builtin/blame.c               |  4 ++--
 t/t8003-blame-corner-cases.sh |  7 +++----
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-blame.txt b/Documentation/git-blame.txt
index fdc3aea30..8a28b4114 100644
--- a/Documentation/git-blame.txt
+++ b/Documentation/git-blame.txt
@@ -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':
diff --git a/builtin/blame.c b/builtin/blame.c
index 67adaef4d..b5b9db147 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -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);
diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
index 661f9d430..728209fa3 100755
--- a/t/t8003-blame-corner-cases.sh
+++ b/t/t8003-blame-corner-cases.sh
@@ -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' '
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH v3] blame: prevent error if range ends past end of file
  2017-10-27  0:56       ` [PATCH v3] " Isabella Stephens
@ 2017-10-27  1:58         ` Junio C Hamano
  2017-10-27  2:01           ` Junio C Hamano
  2017-10-27  6:18           ` Isabella Stephens
  0 siblings, 2 replies; 27+ messages in thread
From: Junio C Hamano @ 2017-10-27  1:58 UTC (permalink / raw)
  To: Isabella Stephens; +Cc: szeder.dev, git, sunshine, peff, bturner, jacob.keller

Isabella Stephens <istephens@atlassian.com> writes:

> diff --git a/builtin/blame.c b/builtin/blame.c
> index 67adaef4d..b5b9db147 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -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;

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.

>  		bottom--;
>  		range_set_append_unsafe(&ranges, bottom, top);
> diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
> index 661f9d430..728209fa3 100755
> --- a/t/t8003-blame-corner-cases.sh
> +++ b/t/t8003-blame-corner-cases.sh
> @@ -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' '

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3] blame: prevent error if range ends past end of file
  2017-10-27  1:58         ` Junio C Hamano
@ 2017-10-27  2:01           ` Junio C Hamano
  2017-10-27  6:18           ` Isabella Stephens
  1 sibling, 0 replies; 27+ messages in thread
From: Junio C Hamano @ 2017-10-27  2:01 UTC (permalink / raw)
  To: Isabella Stephens; +Cc: szeder.dev, git, sunshine, peff, bturner, jacob.keller

Junio C Hamano <gitster@pobox.com> writes:

> 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.

> to kick in.
>
> There should be an "is the range sensible?" check after all the
> tweaking to bottom and top are done, I think.
>
>>  		bottom--;
>>  		range_set_append_unsafe(&ranges, bottom, top);
>> diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
>> index 661f9d430..728209fa3 100755
>> --- a/t/t8003-blame-corner-cases.sh
>> +++ b/t/t8003-blame-corner-cases.sh
>> @@ -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' '

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3] blame: prevent error if range ends past end of file
  2017-10-27  1:58         ` Junio C Hamano
  2017-10-27  2:01           ` Junio C Hamano
@ 2017-10-27  6:18           ` Isabella Stephens
  2017-10-27  6:56             ` Eric Sunshine
  1 sibling, 1 reply; 27+ messages in thread
From: Isabella Stephens @ 2017-10-27  6:18 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: szeder.dev, git, sunshine, peff, bturner, jacob.keller

On 27/10/17 12:58 pm, Junio C Hamano wrote:
> Isabella Stephens <istephens@atlassian.com> writes:
> 
>> diff --git a/builtin/blame.c b/builtin/blame.c
>> index 67adaef4d..b5b9db147 100644
>> --- a/builtin/blame.c
>> +++ b/builtin/blame.c
>> @@ -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;
> 
> 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.

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.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v3] blame: prevent error if range ends past end of file
  2017-10-27  6:18           ` Isabella Stephens
@ 2017-10-27  6:56             ` Eric Sunshine
  2018-04-26  7:45               ` [PATCH v4 0/2] blame and log: " istephens
  0 siblings, 1 reply; 27+ messages in thread
From: Eric Sunshine @ 2017-10-27  6:56 UTC (permalink / raw)
  To: Isabella Stephens
  Cc: Junio C Hamano, szeder.dev, Git List, Jeff King, bturner, Jacob Keller

On Fri, Oct 27, 2017 at 2:18 AM, Isabella Stephens
<istephens@atlassian.com> wrote:
> On 27/10/17 12:58 pm, Junio C Hamano wrote:
>> 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"));

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.

>           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;

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v4 0/2] blame and log: prevent error if range ends past end of file
  2017-10-27  6:56             ` Eric Sunshine
@ 2018-04-26  7:45               ` istephens
  2018-04-26  7:45                 ` [PATCH v4 1/2] blame: " istephens
  2018-04-26  7:45                 ` [PATCH v4 2/2] " istephens
  0 siblings, 2 replies; 27+ messages in thread
From: istephens @ 2018-04-26  7:45 UTC (permalink / raw)
  To: sunshine; +Cc: szeder.dev, gitster, git, peff, bturner, jacob.keller

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.


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v4 1/2] blame: prevent error if range ends past end of file
  2018-04-26  7:45               ` [PATCH v4 0/2] blame and log: " istephens
@ 2018-04-26  7:45                 ` istephens
  2018-04-27  0:50                   ` Junio C Hamano
  2018-04-26  7:45                 ` [PATCH v4 2/2] " istephens
  1 sibling, 1 reply; 27+ messages in thread
From: istephens @ 2018-04-26  7:45 UTC (permalink / raw)
  To: sunshine
  Cc: szeder.dev, gitster, git, peff, bturner, jacob.keller, Isabella Stephens

From: Isabella Stephens <istephens@atlassian.com>

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 <istephens@atlassian.com>
---
 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 <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' '
-- 
2.14.3 (Apple Git-98)


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH v4 2/2] log: prevent error if line range ends past end of file
  2018-04-26  7:45               ` [PATCH v4 0/2] blame and log: " istephens
  2018-04-26  7:45                 ` [PATCH v4 1/2] blame: " istephens
@ 2018-04-26  7:45                 ` istephens
  1 sibling, 0 replies; 27+ messages in thread
From: istephens @ 2018-04-26  7:45 UTC (permalink / raw)
  To: sunshine
  Cc: szeder.dev, gitster, git, peff, bturner, jacob.keller, Isabella Stephens

From: Isabella Stephens <istephens@atlassian.com>

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 <istephens@atlassian.com>
---
 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 <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
-- 
2.14.3 (Apple Git-98)


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
  2018-04-26  7:45                 ` [PATCH v4 1/2] blame: " istephens
@ 2018-04-27  0:50                   ` Junio C Hamano
  2018-04-27  1:42                     ` Isabella Stephens
  0 siblings, 1 reply; 27+ messages in thread
From: Junio C Hamano @ 2018-04-27  0:50 UTC (permalink / raw)
  To: istephens; +Cc: sunshine, szeder.dev, git, peff, bturner, jacob.keller

istephens@atlassian.com writes:

> 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;

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.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
  2018-04-27  0:50                   ` Junio C Hamano
@ 2018-04-27  1:42                     ` Isabella Stephens
  2018-04-27  2:09                       ` Junio C Hamano
  0 siblings, 1 reply; 27+ messages in thread
From: Isabella Stephens @ 2018-04-27  1:42 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: sunshine, szeder.dev, git, peff, bturner, jacob.keller

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?

On 27/4/18 10:50 am, Junio C Hamano wrote:
> istephens@atlassian.com writes:
> 
>> 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;
> 
> 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.
> 

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
  2018-04-27  1:42                     ` Isabella Stephens
@ 2018-04-27  2:09                       ` Junio C Hamano
  2018-04-27  4:15                         ` Isabella Stephens
  0 siblings, 1 reply; 27+ messages in thread
From: Junio C Hamano @ 2018-04-27  2:09 UTC (permalink / raw)
  To: Isabella Stephens; +Cc: sunshine, szeder.dev, git, peff, bturner, jacob.keller

Isabella Stephens <istephens@atlassian.com> writes:

> On 27/4/18 10:50 am, Junio C Hamano wrote:
>> istephens@atlassian.com writes:
>> 
>>> 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;
>> 
>> 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.
>> 

[administrivia] Do not top-post, but cull the context to leave
enough to remind readers what the discussion was about.

> 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 ;-)



^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
  2018-04-27  2:09                       ` Junio C Hamano
@ 2018-04-27  4:15                         ` Isabella Stephens
  2018-05-02  2:47                           ` Junio C Hamano
  0 siblings, 1 reply; 27+ messages in thread
From: Isabella Stephens @ 2018-04-27  4:15 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: sunshine, szeder.dev, git, peff, bturner, jacob.keller

> 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. 

> 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.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH v4 1/2] blame: prevent error if range ends past end of file
  2018-04-27  4:15                         ` Isabella Stephens
@ 2018-05-02  2:47                           ` Junio C Hamano
  2018-05-29  5:30                             ` [PATCH v5 0/2] blame and log: " istephens
  0 siblings, 1 reply; 27+ messages in thread
From: Junio C Hamano @ 2018-05-02  2:47 UTC (permalink / raw)
  To: Isabella Stephens; +Cc: sunshine, szeder.dev, git, peff, bturner, jacob.keller

Isabella Stephens <istephens@atlassian.com> writes:

> 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.


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH v5 0/2] blame and log: prevent error if range ends past end of file
  2018-05-02  2:47                           ` Junio C Hamano
@ 2018-05-29  5:30                             ` istephens
  2018-05-29  5:30                               ` [PATCH] blame: " istephens
  2018-05-29  5:30                               ` [PATCH] log: prevent error if line " istephens
  0 siblings, 2 replies; 27+ messages in thread
From: istephens @ 2018-05-29  5:30 UTC (permalink / raw)
  To: gitster; +Cc: szeder.dev, git, peff, bturner, jacob.keller

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.


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [PATCH] blame: prevent error if range ends past end of file
  2018-05-29  5:30                             ` [PATCH v5 0/2] blame and log: " istephens
@ 2018-05-29  5:30                               ` istephens
  2018-05-30  8:45                                 ` Eric Sunshine
  2018-05-29  5:30                               ` [PATCH] log: prevent error if line " istephens
  1 sibling, 1 reply; 27+ messages in thread
From: istephens @ 2018-05-29  5:30 UTC (permalink / raw)
  To: gitster; +Cc: szeder.dev, git, peff, bturner, jacob.keller, Isabella Stephens

From: Isabella Stephens <istephens@atlassian.com>

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 <istephens@atlassian.com>
---
 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 <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' '
-- 
2.14.3 (Apple Git-98)


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* [PATCH] log: prevent error if line range ends past end of file
  2018-05-29  5:30                             ` [PATCH v5 0/2] blame and log: " istephens
  2018-05-29  5:30                               ` [PATCH] blame: " istephens
@ 2018-05-29  5:30                               ` istephens
  2018-05-30  8:59                                 ` Eric Sunshine
  1 sibling, 1 reply; 27+ messages in thread
From: istephens @ 2018-05-29  5:30 UTC (permalink / raw)
  To: gitster; +Cc: szeder.dev, git, peff, bturner, jacob.keller, Isabella Stephens

From: Isabella Stephens <istephens@atlassian.com>

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>
---
 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 <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' '
-- 
2.14.3 (Apple Git-98)


^ permalink raw reply related	[flat|nested] 27+ messages in thread

* Re: [PATCH] blame: prevent error if range ends past end of file
  2018-05-29  5:30                               ` [PATCH] blame: " istephens
@ 2018-05-30  8:45                                 ` Eric Sunshine
  2018-05-31  5:07                                   ` Isabella Stephens
  0 siblings, 1 reply; 27+ messages in thread
From: Eric Sunshine @ 2018-05-30  8:45 UTC (permalink / raw)
  To: Isabella Stephens
  Cc: Junio C Hamano, SZEDER Gábor, Git List, Jeff King,
	Bryan Turner, Jacob Keller

On Tue, May 29, 2018 at 1:30 AM,  <istephens@atlassian.com> 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.

> Tests and documentation are ammended accordingly.

s/ammended/amended/

> 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 <istephens@atlassian.com>
> ---
> diff --git 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.

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.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH] log: prevent error if line range ends past end of file
  2018-05-29  5:30                               ` [PATCH] log: prevent error if line " istephens
@ 2018-05-30  8:59                                 ` Eric Sunshine
  0 siblings, 0 replies; 27+ messages in thread
From: Eric Sunshine @ 2018-05-30  8:59 UTC (permalink / raw)
  To: Isabella Stephens
  Cc: Junio C Hamano, SZEDER Gábor, Git List, Jeff King,
	Bryan Turner, Jacob Keller

On Tue, May 29, 2018 at 1:30 AM,  <istephens@atlassian.com> 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 <istephens@atlassian.com>
> ---
> diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh
> @@ -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.

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [PATCH] blame: prevent error if range ends past end of file
  2018-05-30  8:45                                 ` Eric Sunshine
@ 2018-05-31  5:07                                   ` Isabella Stephens
  0 siblings, 0 replies; 27+ messages in thread
From: Isabella Stephens @ 2018-05-31  5:07 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Junio C Hamano, SZEDER Gábor, Git List, Jeff King,
	Bryan Turner, Jacob Keller

On 30/5/18 6:45 pm, Eric Sunshine wrote:
>> diff --git 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.
> 
> 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 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.

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2018-05-31  5:08 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-26  2:19 [PATCH] blame: add --fuzzy-lines command line option Isabella Stephens
2017-10-26  6:15 ` Junio C Hamano
2017-10-26  7:01   ` [PATCH v2] blame: prevent error if range ends past end of file Isabella Stephens
2017-10-26  8:48     ` Jacob Keller
2017-10-26 22:50       ` Isabella Stephens
2017-10-26 15:31     ` SZEDER Gábor
2017-10-26 23:37       ` Isabella Stephens
2017-10-27  0:56       ` [PATCH v3] " Isabella Stephens
2017-10-27  1:58         ` Junio C Hamano
2017-10-27  2:01           ` Junio C Hamano
2017-10-27  6:18           ` Isabella Stephens
2017-10-27  6:56             ` Eric Sunshine
2018-04-26  7:45               ` [PATCH v4 0/2] blame and log: " istephens
2018-04-26  7:45                 ` [PATCH v4 1/2] blame: " istephens
2018-04-27  0:50                   ` Junio C Hamano
2018-04-27  1:42                     ` Isabella Stephens
2018-04-27  2:09                       ` Junio C Hamano
2018-04-27  4:15                         ` Isabella Stephens
2018-05-02  2:47                           ` Junio C Hamano
2018-05-29  5:30                             ` [PATCH v5 0/2] blame and log: " istephens
2018-05-29  5:30                               ` [PATCH] blame: " istephens
2018-05-30  8:45                                 ` Eric Sunshine
2018-05-31  5:07                                   ` Isabella Stephens
2018-05-29  5:30                               ` [PATCH] log: prevent error if line " istephens
2018-05-30  8:59                                 ` Eric Sunshine
2018-04-26  7:45                 ` [PATCH v4 2/2] " istephens
2017-10-26  7:08   ` [PATCH] blame: add --fuzzy-lines command line option Isabella Stephens

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.