git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* builtin add interactive regression
@ 2021-12-09 19:36 SZEDER Gábor
  2021-12-10 14:13 ` Phillip Wood
  0 siblings, 1 reply; 3+ messages in thread
From: SZEDER Gábor @ 2021-12-09 19:36 UTC (permalink / raw)
  To: git

Hi,

This morning 'git add --patch' told me "Sorry, cannot split this hunk"
even though that hunk in question was definitely splittable.  Then I
spent my lunch break trying to reproduce the issue, and it turned out
to be a regression in the builtin add interactive (I have
'add.interactive.useBuiltin=true' in my config).  The following test
demonstrates this issue:

  ---  >8  ---

#!/bin/sh

test_description='test'
. ./test-lib.sh

test_expect_success 'builtin interactive add cannot split hunk?!' '
	printf "%s\n" 1 2 >file &&
	echo a >zzzz &&
	git add file zzzz &&
	git commit -m initial &&
	cat >file <<-\EOF &&
	1
	add a line
	2
	add a line at the end (this is important!)
	EOF
	echo "modify one more file (this, too, is important!)" >zzzz &&

	git diff file &&

	git -c add.interactive.useBuiltin=false add -u -p >expect &&
	git -c add.interactive.useBuiltin=true add -u -p >actual &&
	# Uh-oh, "s" (for splitting the hunk) is missing!
	test_cmp expect actual
'

test_done

  ---  8<  ---

This fails with:

  + git -c add.interactive.useBuiltin=false add -u -p
  + git -c add.interactive.useBuiltin=true add -u -p
  + test_cmp expect actual
  --- expect	2021-12-09 19:21:23.354461170 +0000
  +++ actual	2021-12-09 19:21:23.358461215 +0000
  @@ -7,7 +7,7 @@
   +add a line
    2
   +add a line at the end (this is important!)
  -(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? 
  +(1/1) Stage this hunk [y,n,q,a,d,e,?]? 
   diff --git a/zzzz b/zzzz
   index 7898192..84e1b35 100644
   --- a/zzzz
  error: last command exited with $?=1
  not ok 1 - builtin interactive add cannot split hunk?!

So the builtin add interactive doesn't even offer the 's - split the
current hunk into smaller hunks' option, but my finger memory pressed
's' anyway, and then it told me that "Sorry..." message.  The scripted
version can split the hunk just fine.


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

* Re: builtin add interactive regression
  2021-12-09 19:36 builtin add interactive regression SZEDER Gábor
@ 2021-12-10 14:13 ` Phillip Wood
  2021-12-20 14:51   ` Phillip Wood
  0 siblings, 1 reply; 3+ messages in thread
From: Phillip Wood @ 2021-12-10 14:13 UTC (permalink / raw)
  To: SZEDER Gábor, git

On 09/12/2021 19:36, SZEDER Gábor wrote:
> Hi,
> 
> This morning 'git add --patch' told me "Sorry, cannot split this hunk"
> even though that hunk in question was definitely splittable.  Then I
> spent my lunch break trying to reproduce the issue, and it turned out
> to be a regression in the builtin add interactive (I have
> 'add.interactive.useBuiltin=true' in my config).  The following test
> demonstrates this issue:
> 
>    ---  >8  ---
> 
> #!/bin/sh
> 
> test_description='test'
> . ./test-lib.sh
> 
> test_expect_success 'builtin interactive add cannot split hunk?!' '
> 	printf "%s\n" 1 2 >file &&
> 	echo a >zzzz &&
> 	git add file zzzz &&
> 	git commit -m initial &&
> 	cat >file <<-\EOF &&
> 	1
> 	add a line
> 	2
> 	add a line at the end (this is important!)
> 	EOF
> 	echo "modify one more file (this, too, is important!)" >zzzz &&
> 
> 	git diff file &&
> 
> 	git -c add.interactive.useBuiltin=false add -u -p >expect &&
> 	git -c add.interactive.useBuiltin=true add -u -p >actual &&
> 	# Uh-oh, "s" (for splitting the hunk) is missing!
> 	test_cmp expect actual
> '
> 
> test_done
> 
>    ---  8<  ---
> 
> This fails with:
> 
>    + git -c add.interactive.useBuiltin=false add -u -p
>    + git -c add.interactive.useBuiltin=true add -u -p
>    + test_cmp expect actual
>    --- expect	2021-12-09 19:21:23.354461170 +0000
>    +++ actual	2021-12-09 19:21:23.358461215 +0000
>    @@ -7,7 +7,7 @@
>     +add a line
>      2
>     +add a line at the end (this is important!)
>    -(1/1) Stage this hunk [y,n,q,a,d,s,e,?]?
>    +(1/1) Stage this hunk [y,n,q,a,d,e,?]?
>     diff --git a/zzzz b/zzzz
>     index 7898192..84e1b35 100644
>     --- a/zzzz
>    error: last command exited with $?=1
>    not ok 1 - builtin interactive add cannot split hunk?!
> 
> So the builtin add interactive doesn't even offer the 's - split the
> current hunk into smaller hunks' option, but my finger memory pressed
> 's' anyway, and then it told me that "Sorry..." message.  The scripted
> version can split the hunk just fine.

Thanks for the easy reproduction recipe, the hunk below makes it
pass. We're already checking marker at the very end of the loop
but we need to do it after the end of each diff (I think the perl
version only processes one diff at a time so does not have to
worry about this). If anyone wants to take this forward please
do, if not I'll try and find some time later next week to turn it
into a proper patch with a commit message and test.

Best Wishes

Phillip

---- >8 -----
diff --git a/add-patch.c b/add-patch.c
index 8c41cdfe39..4c2db78460 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -472,6 +472,14 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
                         eol = pend;
  
                 if (starts_with(p, "diff ")) {
+                       if (marker == '-' || marker == '+')
+                               /*
+                                * Last hunk ended in non-context line
+                                * (i.e. it appended lines to the
+                                * file, so there are no trailing
+                                * context lines).
+                                */
+                               hunk->splittable_into++;
                         ALLOC_GROW_BY(s->file_diff, s->file_diff_nr, 1,
                                    file_diff_alloc);
                         file_diff = s->file_diff + s->file_diff_nr - 1;


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

* Re: builtin add interactive regression
  2021-12-10 14:13 ` Phillip Wood
@ 2021-12-20 14:51   ` Phillip Wood
  0 siblings, 0 replies; 3+ messages in thread
From: Phillip Wood @ 2021-12-20 14:51 UTC (permalink / raw)
  To: SZEDER Gábor, git

On 10/12/2021 14:13, Phillip Wood wrote:
> [...]
> If anyone wants to take this forward please
> do, if not I'll try and find some time later next week to turn it
> into a proper patch with a commit message and test.

I've submitted the fix at 
https://lore.kernel.org/git/pull.1100.git.1640010777.gitgitgadget@gmail.com/T/#t

Best Wishes

Phillip

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

end of thread, other threads:[~2021-12-20 15:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-09 19:36 builtin add interactive regression SZEDER Gábor
2021-12-10 14:13 ` Phillip Wood
2021-12-20 14:51   ` Phillip Wood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).