git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] add-patch: edit the hunk again
@ 2024-09-15 11:38 Rubén Justo
  2024-09-16 13:33 ` Phillip Wood
  2024-09-18 17:51 ` [PATCH v2] " Rubén Justo
  0 siblings, 2 replies; 17+ messages in thread
From: Rubén Justo @ 2024-09-15 11:38 UTC (permalink / raw)
  To: Git List; +Cc: Johannes Schindelin, Phillip Wood

The "edit" option allows the user to directly modify the hunk to be
applied.

If the modified hunk returned is not an applicable patch, we give the
opportunity to try again.

For this new attempt we provide, again, the original hunk;  the user
has to repeat the modification from scratch.

Instead, let's give them the faulty modified patch back, so they can
identify and fix the problem.

If they really want to start over with a fresh patch they still can
say 'no' to cancel the "edit" and start anew [*].

    * In the old script-based version of "add -p", this "no" meant
      discarding the current hunk and moving on to the next one.

      This changed, presumably unintentionally, during the conversion
      to C in bcdd297b78 (built-in add -p: implement hunk editing,
      2019-12-13).

      Now makes perfect sense not to move to the next hunk when the
      user requests to discard their edits.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---

The message "saying 'no' discards!" comes from ac083c47ea
(git-add--interactive: manual hunk editing mode, 2008-07-03).

I think it was referring to discarding user modifications, not the
current hunk; which is what we were doing then (and now regaining).

However, we stopped behaving that way in 2b8ea7f3c7 (add -p:
calculate offset delta for edited patches, 2018-03-05), perhaps for
some reason I'm missing.

Therefore, this patch also modifies what we did, possibly
unintentionally, in 2b8ea7f3c7.

Thanks.


 add-patch.c                | 26 ++++++++++++++++----------
 t/t3701-add-interactive.sh | 14 ++++++++++++++
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 557903310d..125e79a5ae 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1111,7 +1111,8 @@ static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
 	hunk->colored_end = s->colored.len;
 }
 
-static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
+static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk,
+			      size_t plain_len, size_t colored_len)
 {
 	size_t i;
 
@@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 				      "addp-hunk-edit.diff", NULL) < 0)
 		return -1;
 
+	/* Drop possible previous edits */
+	strbuf_setlen(&s->plain, plain_len);
+	strbuf_setlen(&s->colored, colored_len);
+
 	/* strip out commented lines */
 	hunk->start = s->plain.len;
 	for (i = 0; i < s->buf.len; ) {
@@ -1257,15 +1262,14 @@ static int edit_hunk_loop(struct add_p_state *s,
 	backup = *hunk;
 
 	for (;;) {
-		int res = edit_hunk_manually(s, hunk);
+		int res = edit_hunk_manually(s, hunk, plain_len, colored_len);
 		if (res == 0) {
 			/* abandoned */
-			*hunk = backup;
-			return -1;
+			break;
 		}
 
 		if (res > 0) {
-			hunk->delta +=
+			hunk->delta = backup.delta +
 				recount_edited_hunk(s, hunk,
 						    backup.header.old_count,
 						    backup.header.new_count);
@@ -1273,10 +1277,6 @@ static int edit_hunk_loop(struct add_p_state *s,
 				return 0;
 		}
 
-		/* Drop edits (they were appended to s->plain) */
-		strbuf_setlen(&s->plain, plain_len);
-		strbuf_setlen(&s->colored, colored_len);
-		*hunk = backup;
 
 		/*
 		 * TRANSLATORS: do not translate [y/n]
@@ -1289,8 +1289,14 @@ static int edit_hunk_loop(struct add_p_state *s,
 					"Edit again (saying \"no\" discards!) "
 					"[y/n]? "));
 		if (res < 1)
-			return -1;
+			break;
 	}
+
+	/* Drop a possible edit */
+	strbuf_setlen(&s->plain, plain_len);
+	strbuf_setlen(&s->colored, colored_len);
+	*hunk = backup;
+	return -1;
 }
 
 static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 718438ffc7..6af5636221 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -165,6 +165,20 @@ test_expect_success 'dummy edit works' '
 	diff_cmp expected diff
 '
 
+test_expect_success 'setup re-edit editor' '
+	write_script "fake_editor.sh" <<-\EOF &&
+	grep been-here "$1" && echo found >output
+	echo been-here > "$1"
+	EOF
+	test_set_editor "$(pwd)/fake_editor.sh"
+'
+
+test_expect_success 'editing again works' '
+	git reset &&
+	test_write_lines e y | GIT_TRACE=1 git add -p &&
+	grep found output
+'
+
 test_expect_success 'setup patch' '
 	cat >patch <<-\EOF
 	@@ -1,1 +1,4 @@
-- 
2.46.1.507.gbcf32d0979

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

* Re: [PATCH] add-patch: edit the hunk again
  2024-09-15 11:38 [PATCH] add-patch: edit the hunk again Rubén Justo
@ 2024-09-16 13:33 ` Phillip Wood
  2024-09-16 17:35   ` Junio C Hamano
  2024-09-16 22:09   ` Rubén Justo
  2024-09-18 17:51 ` [PATCH v2] " Rubén Justo
  1 sibling, 2 replies; 17+ messages in thread
From: Phillip Wood @ 2024-09-16 13:33 UTC (permalink / raw)
  To: Rubén Justo, Git List; +Cc: Johannes Schindelin, Phillip Wood

Hi Rubén

On 15/09/2024 12:38, Rubén Justo wrote:
> The "edit" option allows the user to directly modify the hunk to be
> applied.
> 
> If the modified hunk returned is not an applicable patch, we give the
> opportunity to try again.
> 
> For this new attempt we provide, again, the original hunk;  the user
> has to repeat the modification from scratch.

As you say below it looks like we started doing this by accident with 
2b8ea7f3c7 (add -p: calculate offset delta for edited patches, 
2018-03-05). I think that although the change was accidental it was 
actually a move in the right direction for several reasons.

  - The error message from "git apply" makes it is virtually impossible
    to tell what is wrong with the edited patch. The line numbers in the
    error message refer to the complete patch but the user is editing a
    single hunk so the user has no idea which line of the hunk the error
    message applies to.

  - If the user uses a terminal based editor then they cannot see the
    error messages while they're re-editing the hunk.

  - If the user has deleted a pre-image line then they need to somehow
    magic it back before the hunk will apply.

> Instead, let's give them the faulty modified patch back, so they can
> identify and fix the problem.

The problem is how do they identify the problem? I have some unfinished 
patches [1] that annotate the edited patch with comments explaining 
what's wrong. Because we know what the unedited patch looked like and 
that the pre-image lines should be unchanged it is possible to provide 
much better error messages than we get from trying to apply the whole 
patch with "git apply". It also makes it possible to restore deleted 
pre-image lines.

[1] https://github.com/phillipwood/git/tree/wip/add-p-editing-improvements
     Note that the later patches do not even compile at the moment. I've
     been meaning to split out the first eight patches and clean them up
     as they're mostly functional and just need the commit messages
     cleaning up.

> diff --git a/add-patch.c b/add-patch.c
> index 557903310d..125e79a5ae 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>   				      "addp-hunk-edit.diff", NULL) < 0)
>   		return -1;
>   
> +	/* Drop possible previous edits */
> +	strbuf_setlen(&s->plain, plain_len);
> +	strbuf_setlen(&s->colored, colored_len);
> +

At this point hunk->end points past s->plain.len. If the user has 
deleted all the lines then we return with hunk->end in this invalid 
state. I think that turns out not to matter as we end up restoring 
hunk->end from the backup we make at the beginning of edit_hunk_loop() 
but it is not straight forward to reason about.

> @@ -1273,10 +1277,6 @@ static int edit_hunk_loop(struct add_p_state *s,
>   				return 0;
>   		}
>   
> -		/* Drop edits (they were appended to s->plain) */
> -		strbuf_setlen(&s->plain, plain_len);
> -		strbuf_setlen(&s->colored, colored_len);
> -		*hunk = backup;

In the old version we always restore the hunk from the backup when we 
trim the edited patch which maintains the invariant "hunk->end <= 
s->plain->end"

> diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> index 718438ffc7..6af5636221 100755
> --- a/t/t3701-add-interactive.sh
> +++ b/t/t3701-add-interactive.sh
> @@ -165,6 +165,20 @@ test_expect_success 'dummy edit works' '
>   	diff_cmp expected diff
>   '
>   
> +test_expect_success 'setup re-edit editor' '
> +	write_script "fake_editor.sh" <<-\EOF &&
> +	grep been-here "$1" && echo found >output

'grep been-here "$1" >output' should be sufficient I think

> +	echo been-here > "$1"
> +	EOF
> +	test_set_editor "$(pwd)/fake_editor.sh"
> +'

I don't think we need to write the fake editor in a separate test. Also 
it would be better to call test_set_editor in a subshell so that it does 
not affect later tests.

> +test_expect_success 'editing again works' '
> +	git reset &&
> +	test_write_lines e y | GIT_TRACE=1 git add -p &&

It would be nice to add "n q" to the input to make it complete.

> +	grep found output

Using test_grep makes it easier to debug test failures.


Best Wishes

Phillip

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

* Re: [PATCH] add-patch: edit the hunk again
  2024-09-16 13:33 ` Phillip Wood
@ 2024-09-16 17:35   ` Junio C Hamano
  2024-09-16 22:09   ` Rubén Justo
  1 sibling, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2024-09-16 17:35 UTC (permalink / raw)
  To: Phillip Wood
  Cc: Rubén Justo, Git List, Johannes Schindelin, Phillip Wood

Phillip Wood <phillip.wood123@gmail.com> writes:

> Because we know what the unedited patch
> looked like and that the pre-image lines should be unchanged it is
> possible to provide much better error messages than we get from trying
> to apply the whole patch with "git apply". It also makes it possible
> to restore deleted pre-image lines.
> ...
> [1] https://github.com/phillipwood/git/tree/wip/add-p-editing-improvements
>     Note that the later patches do not even compile at the moment. I've
>     been meaning to split out the first eight patches and clean them up
>     as they're mostly functional and just need the commit messages
>     cleaning up.

I'd love this.  With both patches before and after the edit session,
we should be able to give more accurate line counts than having "git
apply" look at only the post-edit shape of the hunk, which wouldn't
see where any brokenness of the hunk comes from even if it wanted
to.  We should probably be able to stop relying on "--recount" once
we do this right, which would be very nice outcome, too.

Thanks.



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

* Re: [PATCH] add-patch: edit the hunk again
  2024-09-16 13:33 ` Phillip Wood
  2024-09-16 17:35   ` Junio C Hamano
@ 2024-09-16 22:09   ` Rubén Justo
  2024-09-18 10:06     ` phillip.wood123
  1 sibling, 1 reply; 17+ messages in thread
From: Rubén Justo @ 2024-09-16 22:09 UTC (permalink / raw)
  To: phillip.wood, Git List; +Cc: Johannes Schindelin

On Mon, Sep 16, 2024 at 02:33:54PM +0100, Phillip Wood wrote:

> > The "edit" option allows the user to directly modify the hunk to be
> > applied.
> > 
> > If the modified hunk returned is not an applicable patch, we give the
> > opportunity to try again.
> > 
> > For this new attempt we provide, again, the original hunk;  the user
> > has to repeat the modification from scratch.
> 
> As you say below it looks like we started doing this by accident with
> 2b8ea7f3c7 (add -p: calculate offset delta for edited patches, 2018-03-05).
> I think that although the change was accidental it was actually a move in
> the right direction for several reasons.
> 
>  - The error message from "git apply" makes it is virtually impossible
>    to tell what is wrong with the edited patch. The line numbers in the
>    error message refer to the complete patch but the user is editing a
>    single hunk so the user has no idea which line of the hunk the error
>    message applies to.
> 
>  - If the user uses a terminal based editor then they cannot see the
>    error messages while they're re-editing the hunk.
> 
>  - If the user has deleted a pre-image line then they need to somehow
>    magic it back before the hunk will apply.
> 
> > Instead, let's give them the faulty modified patch back, so they can
> > identify and fix the problem.
> 
> The problem is how do they identify the problem? I have some unfinished
> patches [1] that annotate the edited patch with comments explaining what's
> wrong. Because we know what the unedited patch looked like and that the
> pre-image lines should be unchanged it is possible to provide much better
> error messages than we get from trying to apply the whole patch with "git
> apply". It also makes it possible to restore deleted pre-image lines.
> 
> [1] https://github.com/phillipwood/git/tree/wip/add-p-editing-improvements
>     Note that the later patches do not even compile at the moment. I've
>     been meaning to split out the first eight patches and clean them up
>     as they're mostly functional and just need the commit messages
>     cleaning up.

I can imagine that we could give the flawed and annotated patch back to
the user, if they want to fix it and try again.  Am I misunderstanding
your envision?

At any rate, I'm thinking about small fixes and/or avoiding to use a
backup (":w! /tmp/patch" + ":r /tmp/patch") if I have doubts about
making a mistake after spending some time thinking about a hunk, so as
not to lose some work.

> 
> > diff --git a/add-patch.c b/add-patch.c
> > index 557903310d..125e79a5ae 100644
> > --- a/add-patch.c
> > +++ b/add-patch.c
> > @@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
> >   				      "addp-hunk-edit.diff", NULL) < 0)
> >   		return -1;
> > +	/* Drop possible previous edits */
> > +	strbuf_setlen(&s->plain, plain_len);
> > +	strbuf_setlen(&s->colored, colored_len);
> > +
> 
> At this point hunk->end points past s->plain.len. If the user has deleted
> all the lines then we return with hunk->end in this invalid state. I think
> that turns out not to matter as we end up restoring hunk->end from the
> backup we make at the beginning of edit_hunk_loop() but it is not straight
> forward to reason about.

I'm not sure I understand your comment.  We are adjusting "hunk" right
after that, no?

> 
> > @@ -1273,10 +1277,6 @@ static int edit_hunk_loop(struct add_p_state *s,
> >   				return 0;
> >   		}
> > -		/* Drop edits (they were appended to s->plain) */
> > -		strbuf_setlen(&s->plain, plain_len);
> > -		strbuf_setlen(&s->colored, colored_len);
> > -		*hunk = backup;
> 
> In the old version we always restore the hunk from the backup when we trim
> the edited patch which maintains the invariant "hunk->end <= s->plain->end"

Same here.  Are we losing that invariant?

> 
> > diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> > index 718438ffc7..6af5636221 100755
> > --- a/t/t3701-add-interactive.sh
> > +++ b/t/t3701-add-interactive.sh
> > @@ -165,6 +165,20 @@ test_expect_success 'dummy edit works' '
> >   	diff_cmp expected diff
> >   '
> > +test_expect_success 'setup re-edit editor' '
> > +	write_script "fake_editor.sh" <<-\EOF &&
> > +	grep been-here "$1" && echo found >output
> 
> 'grep been-here "$1" >output' should be sufficient I think

As I was writing the test, it was clearer to me using "&& echo found"
here and "grep found" below.

> 
> > +	echo been-here > "$1"
> > +	EOF
> > +	test_set_editor "$(pwd)/fake_editor.sh"
> > +'
> 
> I don't think we need to write the fake editor in a separate test. Also it
> would be better to call test_set_editor in a subshell so that it does not
> affect later tests.

Yes, t3701 deserves an update.  I tried to respect its current style.
I didn't want to start a mix.

> 
> > +test_expect_success 'editing again works' '
> > +	git reset &&
> > +	test_write_lines e y | GIT_TRACE=1 git add -p &&
> 
> It would be nice to add "n q" to the input to make it complete.

I have no objection to that.

> 
> > +	grep found output
> 
> Using test_grep makes it easier to debug test failures.
> 
> 
> Best Wishes
> 
> Phillip

Thanks for your review.

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

* Re: [PATCH] add-patch: edit the hunk again
  2024-09-16 22:09   ` Rubén Justo
@ 2024-09-18 10:06     ` phillip.wood123
  2024-09-18 17:46       ` Rubén Justo
  0 siblings, 1 reply; 17+ messages in thread
From: phillip.wood123 @ 2024-09-18 10:06 UTC (permalink / raw)
  To: Rubén Justo, phillip.wood, Git List; +Cc: Johannes Schindelin

Hi Rubén

On 16/09/2024 23:09, Rubén Justo wrote:
> On Mon, Sep 16, 2024 at 02:33:54PM +0100, Phillip Wood wrote:
> 
> I can imagine that we could give the flawed and annotated patch back to
> the user, if they want to fix it and try again.

Exactly

> At any rate, I'm thinking about small fixes and/or avoiding to use a
> backup (":w! /tmp/patch" + ":r /tmp/patch") if I have doubts about
> making a mistake after spending some time thinking about a hunk, so as
> not to lose some work.

The problem is there is no good solution at the moment. Either we throw 
away the user's work if the edited patch does not apply or we keep the 
broken patch and say "this is broken, please figure out what's wrong 
with it and fix it". As I explained previously fixing a broken patch is 
not necessarily straight forward especially for new users. A few times 
when editing patches that are going to be applied in reverse (from "git 
checkout HEAD -- <path>") I've found it impossible to figure out why a 
particular edit was being rejected. In that case starting again with the 
original patch is my only hope. If you want to be able to re-edit a 
broken hunk perhaps we should add an option for that when we ask the 
user if they want to try again.

>>> diff --git a/add-patch.c b/add-patch.c
>>> index 557903310d..125e79a5ae 100644
>>> --- a/add-patch.c
>>> +++ b/add-patch.c
>>> @@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>>>    				      "addp-hunk-edit.diff", NULL) < 0)
>>>    		return -1;
>>> +	/* Drop possible previous edits */
>>> +	strbuf_setlen(&s->plain, plain_len);
>>> +	strbuf_setlen(&s->colored, colored_len);
>>> +
>>
>> At this point hunk->end points past s->plain.len. If the user has deleted
>> all the lines then we return with hunk->end in this invalid state. I think
>> that turns out not to matter as we end up restoring hunk->end from the
>> backup we make at the beginning of edit_hunk_loop() but it is not straight
>> forward to reason about.
> 
> I'm not sure I understand your comment.  We are adjusting "hunk" right
> after that, no?

Sorry I should have said hunk->colored_end and s->colored.len. If we 
return early then we don't call recolor_hunk().

>>> +	echo been-here > "$1"
>>> +	EOF
>>> +	test_set_editor "$(pwd)/fake_editor.sh"
>>> +'
>>
>> I don't think we need to write the fake editor in a separate test. Also it
>> would be better to call test_set_editor in a subshell so that it does not
>> affect later tests.
> 
> Yes, t3701 deserves an update.  I tried to respect its current style.
> I didn't want to start a mix.

I see are four instances of "test_set_editor" in this file, two of which 
setup the editor within the test that uses them and are called from a 
subshell. We should do the same here rather than creating more work for 
whoever decides to clean up this file in the future.

Best Wishes

Phillip

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

* Re: [PATCH] add-patch: edit the hunk again
  2024-09-18 10:06     ` phillip.wood123
@ 2024-09-18 17:46       ` Rubén Justo
  0 siblings, 0 replies; 17+ messages in thread
From: Rubén Justo @ 2024-09-18 17:46 UTC (permalink / raw)
  To: phillip.wood, Git List; +Cc: Johannes Schindelin, Junio C Hamano

On Wed, Sep 18, 2024 at 11:06:34AM +0100, phillip.wood123@gmail.com wrote:
> Hi Rubén
> 
> On 16/09/2024 23:09, Rubén Justo wrote:
> > On Mon, Sep 16, 2024 at 02:33:54PM +0100, Phillip Wood wrote:
> > 
> > I can imagine that we could give the flawed and annotated patch back to
> > the user, if they want to fix it and try again.
> 
> Exactly

So we agree on where we're going ...

> 
> > At any rate, I'm thinking about small fixes and/or avoiding to use a
> > backup (":w! /tmp/patch" + ":r /tmp/patch") if I have doubts about
> > making a mistake after spending some time thinking about a hunk, so as
> > not to lose some work.
> 
> The problem is there is no good solution at the moment.

although not in the length of the stride :)

Maybe in the future we can provide better error descriptions, or even
add annotations to the faulty patch explaining the faults.

But we're not there yet, and honestly, it's not my intention to work
on that.

> Either we keep the broken
> patch and say "this is broken, please figure out what's wrong with it and
> fix it"

Yes, we should keep the users's work if they say "yes" to:

    Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?

> or throw away
> the user's work if the edited patch does not apply.

Only if the user says "no" (as we say in the message).

After that "no", the user has another opportunity to decide about the
hunk:

    Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]? no

    (n/m) Stage this hunk [y,n,q,a,d,e,p,?]

And then, "edit" will allow them to start over and edit the original
hunk, again.

> As I explained previously fixing a broken patch is not necessarily
> straight forward especially for new users.

Very true.  But I don't think that should be a reason to stop the user
from trying.

> A few times when editing patches
> that are going to be applied in reverse (from "git checkout HEAD -- <path>")
> I've found it impossible to figure out why a particular edit was being
> rejected. In that case starting again with the original patch is my only
> hope.

My experience is usually small last-minute adjustments that aren't
worth canceling the interactive session for, and I don't want to have
to remember to make them later.

A small error in a large hunk has been frustrating several times
because I have to go back and review the whole thing.

> If you want to be able to re-edit a broken hunk perhaps we should add
> an option for that when we ask the user if they want to try again.

As we commented in a previous message, this is what we are regaining
with this patch.  The option was introduced in ac083c47ea
(git-add--interactive: manual hunk editing mode, 2008-07-03) and lost
in 2b8ea7f3c7 (add -p: calculate offset delta for edited patches,
2018-03-05).

Thanks.

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

* [PATCH v2] add-patch: edit the hunk again
  2024-09-15 11:38 [PATCH] add-patch: edit the hunk again Rubén Justo
  2024-09-16 13:33 ` Phillip Wood
@ 2024-09-18 17:51 ` Rubén Justo
  2024-09-23  9:07   ` phillip.wood123
  2024-09-28 14:30   ` [PATCH v3] " Rubén Justo
  1 sibling, 2 replies; 17+ messages in thread
From: Rubén Justo @ 2024-09-18 17:51 UTC (permalink / raw)
  To: Git List; +Cc: Phillip Wood, Junio C Hamano, Johannes Schindelin

The "edit" option allows the user to directly modify the hunk to be
applied.

If the modified hunk returned by the user is not an applicable patch,
they will be given the opportunity to try again.

For this new attempt we give them the original hunk;  they have to
repeat the modification from scratch.

Instead, let's give them the modified patch back, so they can identify
and fix the problem.

If they really want to start over with a fresh patch they still can
say "no" to cancel the "edit" and start anew [*].

    * In the old script-based version of "add -p", this "no" meant
      discarding the hunk and moving on to the next one.

      This changed, probably unintentionally, during its conversion to
      C in bcdd297b78 (built-in add -p: implement hunk editing,
      2019-12-13).

      It now makes more sense not to move to the next hunk when the
      user requests to discard their edits.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---

This iteration addresses Phillip's comments:

 - Ensure that `edit_hunk_manually()` exits with sane values in
   `hunk`.

 - Merge the tests into a single one and use a subshell to prevent
   leaking `EDITOR`.

Thanks.

 add-patch.c                | 31 +++++++++++++++++++------------
 t/t3701-add-interactive.sh | 13 +++++++++++++
 2 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 557903310d..75b5129281 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1111,7 +1111,8 @@ static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
 	hunk->colored_end = s->colored.len;
 }
 
-static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
+static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk,
+			      size_t plain_len, size_t colored_len)
 {
 	size_t i;
 
@@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 				      "addp-hunk-edit.diff", NULL) < 0)
 		return -1;
 
+	/* Drop possible previous edits */
+	strbuf_setlen(&s->plain, plain_len);
+	strbuf_setlen(&s->colored, colored_len);
+
 	/* strip out commented lines */
 	hunk->start = s->plain.len;
 	for (i = 0; i < s->buf.len; ) {
@@ -1157,12 +1162,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	}
 
 	hunk->end = s->plain.len;
+
+	recolor_hunk(s, hunk);
+
 	if (hunk->end == hunk->start)
 		/* The user aborted editing by deleting everything */
 		return 0;
 
-	recolor_hunk(s, hunk);
-
 	/*
 	 * If the hunk header is intact, parse it, otherwise simply use the
 	 * hunk header prior to editing (which will adjust `hunk->start` to
@@ -1257,15 +1263,14 @@ static int edit_hunk_loop(struct add_p_state *s,
 	backup = *hunk;
 
 	for (;;) {
-		int res = edit_hunk_manually(s, hunk);
+		int res = edit_hunk_manually(s, hunk, plain_len, colored_len);
 		if (res == 0) {
 			/* abandoned */
-			*hunk = backup;
-			return -1;
+			break;
 		}
 
 		if (res > 0) {
-			hunk->delta +=
+			hunk->delta = backup.delta +
 				recount_edited_hunk(s, hunk,
 						    backup.header.old_count,
 						    backup.header.new_count);
@@ -1273,10 +1278,6 @@ static int edit_hunk_loop(struct add_p_state *s,
 				return 0;
 		}
 
-		/* Drop edits (they were appended to s->plain) */
-		strbuf_setlen(&s->plain, plain_len);
-		strbuf_setlen(&s->colored, colored_len);
-		*hunk = backup;
 
 		/*
 		 * TRANSLATORS: do not translate [y/n]
@@ -1289,8 +1290,14 @@ static int edit_hunk_loop(struct add_p_state *s,
 					"Edit again (saying \"no\" discards!) "
 					"[y/n]? "));
 		if (res < 1)
-			return -1;
+			break;
 	}
+
+	/* Drop a possible edit */
+	strbuf_setlen(&s->plain, plain_len);
+	strbuf_setlen(&s->colored, colored_len);
+	*hunk = backup;
+	return -1;
 }
 
 static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 718438ffc7..f3206a317b 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -165,6 +165,19 @@ test_expect_success 'dummy edit works' '
 	diff_cmp expected diff
 '
 
+test_expect_success 'editing again works' '
+	git reset &&
+	write_script "fake_editor.sh" <<-\EOF &&
+	grep been-here "$1" >output
+	echo been-here >"$1"
+	EOF
+	(
+		test_set_editor "$(pwd)/fake_editor.sh" &&
+		test_write_lines e y | GIT_TRACE=1 git add -p
+	) &&
+	test_grep been-here output
+'
+
 test_expect_success 'setup patch' '
 	cat >patch <<-\EOF
 	@@ -1,1 +1,4 @@

Range-diff:
1:  bcf32d0979 ! 1:  2b55a759d5 add-patch: edit the hunk again
    @@ add-patch.c: static int edit_hunk_manually(struct add_p_state *s, struct hunk *h
      	/* strip out commented lines */
      	hunk->start = s->plain.len;
      	for (i = 0; i < s->buf.len; ) {
    +@@ add-patch.c: static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
    + 	}
    + 
    + 	hunk->end = s->plain.len;
    ++
    ++	recolor_hunk(s, hunk);
    ++
    + 	if (hunk->end == hunk->start)
    + 		/* The user aborted editing by deleting everything */
    + 		return 0;
    + 
    +-	recolor_hunk(s, hunk);
    +-
    + 	/*
    + 	 * If the hunk header is intact, parse it, otherwise simply use the
    + 	 * hunk header prior to editing (which will adjust `hunk->start` to
     @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
      	backup = *hunk;
      
    @@ t/t3701-add-interactive.sh: test_expect_success 'dummy edit works' '
      	diff_cmp expected diff
      '
      
    -+test_expect_success 'setup re-edit editor' '
    -+	write_script "fake_editor.sh" <<-\EOF &&
    -+	grep been-here "$1" && echo found >output
    -+	echo been-here > "$1"
    -+	EOF
    -+	test_set_editor "$(pwd)/fake_editor.sh"
    -+'
    -+
     +test_expect_success 'editing again works' '
     +	git reset &&
    -+	test_write_lines e y | GIT_TRACE=1 git add -p &&
    -+	grep found output
    ++	write_script "fake_editor.sh" <<-\EOF &&
    ++	grep been-here "$1" >output
    ++	echo been-here >"$1"
    ++	EOF
    ++	(
    ++		test_set_editor "$(pwd)/fake_editor.sh" &&
    ++		test_write_lines e y | GIT_TRACE=1 git add -p
    ++	) &&
    ++	test_grep been-here output
     +'
     +
      test_expect_success 'setup patch' '
-- 
2.46.1.507.gdd29a28bc2

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

* Re: [PATCH v2] add-patch: edit the hunk again
  2024-09-18 17:51 ` [PATCH v2] " Rubén Justo
@ 2024-09-23  9:07   ` phillip.wood123
  2024-09-23 16:02     ` Junio C Hamano
  2024-09-24 22:54     ` Rubén Justo
  2024-09-28 14:30   ` [PATCH v3] " Rubén Justo
  1 sibling, 2 replies; 17+ messages in thread
From: phillip.wood123 @ 2024-09-23  9:07 UTC (permalink / raw)
  To: Rubén Justo, Git List
  Cc: Phillip Wood, Junio C Hamano, Johannes Schindelin

Hi Rubén

Thanks for the re-roll. I'm still not convinced that changing this 
without keeping an easy way to get the current behavior is a good idea.

On 18/09/2024 18:51, Rubén Justo wrote:
> The "edit" option allows the user to directly modify the hunk to be
> applied.
> 
> If the modified hunk returned by the user is not an applicable patch,
> they will be given the opportunity to try again.
> 
> For this new attempt we give them the original hunk;  they have to
> repeat the modification from scratch.
> 
> Instead, let's give them the modified patch back, so they can identify
> and fix the problem.

It's still not clear how an inexperienced user is meant to do that.

> If they really want to start over with a fresh patch they still can
> say "no" to cancel the "edit" and start anew [*].

This is not very obvious to the user, it would be much better to give 
them the choice when we prompt them about editing the hunk again. We've 
been giving the user the original hunk for the last six and a half years 
so I think it's a bit late to unilaterally change that now.

> diff --git a/add-patch.c b/add-patch.c
> index 557903310d..75b5129281 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -1111,7 +1111,8 @@ static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
>   	hunk->colored_end = s->colored.len;
>   }
>   
> -static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
> +static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk,

I would add
				const struct hunk *backup,

here

> +			      size_t plain_len, size_t colored_len)
>   {
>   	size_t i;
>   
> @@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>   				      "addp-hunk-edit.diff", NULL) < 0)
>   		return -1;
>   
> +	/* Drop possible previous edits */
> +	strbuf_setlen(&s->plain, plain_len);
> +	strbuf_setlen(&s->colored, colored_len);

then we can restore the back up here with

	*hunk = *backup;

That would make it clear that we're resetting the hunk and would 
continue to work if we change struct hunk in the future.

>   	/* strip out commented lines */
>   	hunk->start = s->plain.len;
>   	for (i = 0; i < s->buf.len; ) {
> @@ -1157,12 +1162,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>   	}
>   
>   	hunk->end = s->plain.len;
> +
> +	recolor_hunk(s, hunk);
> +

This means we're now forking an external process when there is no hunk 
to color. It would be better to avoid that by leaving this code where it 
was and restoring the backup hunk above.

>   	if (hunk->end == hunk->start)
>   		/* The user aborted editing by deleting everything */
>   		return 0;
>   
> -	recolor_hunk(s, hunk);
> -
 >
>   		/*
>   		 * TRANSLATORS: do not translate [y/n]
> @@ -1289,8 +1290,14 @@ static int edit_hunk_loop(struct add_p_state *s,
>   					"Edit again (saying \"no\" discards!) "
>   					"[y/n]? "));

I think we should make this a three-way choice so the user can choose to 
keep their changes or start from a valid hunk.

>   		if (res < 1)
> -			return -1;
> +			break;
>   	}
> +
> +	/* Drop a possible edit */
> +	strbuf_setlen(&s->plain, plain_len);
> +	strbuf_setlen(&s->colored, colored_len);
> +	*hunk = backup;
> +	return -1;
>   }
>   
>   static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
> diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> index 718438ffc7..f3206a317b 100755
> --- a/t/t3701-add-interactive.sh
> +++ b/t/t3701-add-interactive.sh
> @@ -165,6 +165,19 @@ test_expect_success 'dummy edit works' '
>   	diff_cmp expected diff
>   '
>   
> +test_expect_success 'editing again works' '
> +	git reset &&
> +	write_script "fake_editor.sh" <<-\EOF &&
> +	grep been-here "$1" >output
> +	echo been-here >"$1"
> +	EOF
> +	(
> +		test_set_editor "$(pwd)/fake_editor.sh" &&
> +		test_write_lines e y | GIT_TRACE=1 git add -p

This is still missing "n q". Apart from that the test is looking good.

Best Wishes

Phillip

> +	) &&
> +	test_grep been-here output
> +'
> +
>   test_expect_success 'setup patch' '
>   	cat >patch <<-\EOF
>   	@@ -1,1 +1,4 @@
> 
> Range-diff:
> 1:  bcf32d0979 ! 1:  2b55a759d5 add-patch: edit the hunk again
>      @@ add-patch.c: static int edit_hunk_manually(struct add_p_state *s, struct hunk *h
>        	/* strip out commented lines */
>        	hunk->start = s->plain.len;
>        	for (i = 0; i < s->buf.len; ) {
>      +@@ add-patch.c: static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>      + 	}
>      +
>      + 	hunk->end = s->plain.len;
>      ++
>      ++	recolor_hunk(s, hunk);
>      ++
>      + 	if (hunk->end == hunk->start)
>      + 		/* The user aborted editing by deleting everything */
>      + 		return 0;
>      +
>      +-	recolor_hunk(s, hunk);
>      +-
>      + 	/*
>      + 	 * If the hunk header is intact, parse it, otherwise simply use the
>      + 	 * hunk header prior to editing (which will adjust `hunk->start` to
>       @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
>        	backup = *hunk;
>        
>      @@ t/t3701-add-interactive.sh: test_expect_success 'dummy edit works' '
>        	diff_cmp expected diff
>        '
>        
>      -+test_expect_success 'setup re-edit editor' '
>      -+	write_script "fake_editor.sh" <<-\EOF &&
>      -+	grep been-here "$1" && echo found >output
>      -+	echo been-here > "$1"
>      -+	EOF
>      -+	test_set_editor "$(pwd)/fake_editor.sh"
>      -+'
>      -+
>       +test_expect_success 'editing again works' '
>       +	git reset &&
>      -+	test_write_lines e y | GIT_TRACE=1 git add -p &&
>      -+	grep found output
>      ++	write_script "fake_editor.sh" <<-\EOF &&
>      ++	grep been-here "$1" >output
>      ++	echo been-here >"$1"
>      ++	EOF
>      ++	(
>      ++		test_set_editor "$(pwd)/fake_editor.sh" &&
>      ++		test_write_lines e y | GIT_TRACE=1 git add -p
>      ++	) &&
>      ++	test_grep been-here output
>       +'
>       +
>        test_expect_success 'setup patch' '

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

* Re: [PATCH v2] add-patch: edit the hunk again
  2024-09-23  9:07   ` phillip.wood123
@ 2024-09-23 16:02     ` Junio C Hamano
  2024-09-24 22:54     ` Rubén Justo
  1 sibling, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2024-09-23 16:02 UTC (permalink / raw)
  To: phillip.wood123
  Cc: Rubén Justo, Git List, Phillip Wood, Johannes Schindelin

phillip.wood123@gmail.com writes:

> Thanks for the re-roll. I'm still not convinced that changing this
> without keeping an easy way to get the current behavior is a good
> idea.
>
> This is not very obvious to the user, it would be much better to give
> them the choice when we prompt them about editing the hunk
> again. We've been giving the user the original hunk for the last six
> and a half years so I think it's a bit late to unilaterally change
> that now.

I almost never use the (e)dit in "add -p", but after trying it and
deliberately screwing up the edit, I tend to agree with you.  It is
very easy to lose what the original change was, what you wanted it
to say after the edit in the end state, and how the patch for the
current state should look like, and being able to easily start over
(and more importantly, knowing that I'd get the version that has
none of my screw-ups) was the only thing that convinced me that I
might in the future try to use the (e)dit mode again when I find an
applicable situation.

Thanks for review.

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

* Re: [PATCH v2] add-patch: edit the hunk again
  2024-09-23  9:07   ` phillip.wood123
  2024-09-23 16:02     ` Junio C Hamano
@ 2024-09-24 22:54     ` Rubén Justo
  2024-10-01 10:02       ` Phillip Wood
  1 sibling, 1 reply; 17+ messages in thread
From: Rubén Justo @ 2024-09-24 22:54 UTC (permalink / raw)
  To: phillip.wood, Git List; +Cc: Junio C Hamano, Johannes Schindelin

On Mon, Sep 23, 2024 at 10:07:08AM +0100, phillip.wood123@gmail.com wrote:

> Thanks for the re-roll. I'm still not convinced that changing this without
> keeping an easy way to get the current behavior is a good idea.

Thank you for maintaining an open attitude and helping make the change
reasonable.

> 
> On 18/09/2024 18:51, Rubén Justo wrote:
> > The "edit" option allows the user to directly modify the hunk to be
> > applied.
> > 
> > If the modified hunk returned by the user is not an applicable patch,
> > they will be given the opportunity to try again.
> > 
> > For this new attempt we give them the original hunk;  they have to
> > repeat the modification from scratch.
> > 
> > Instead, let's give them the modified patch back, so they can identify
> > and fix the problem.
> 
> It's still not clear how an inexperienced user is meant to do that.

If inexperience refers to the user not being familiar with the patch
format, I don't think it's something we should worry about.  It is
very likely that few users will attempt to (e)dit, and those who do,
inexperienced users experimenting, the normal (and sensible) path they
will follow at the slightest difficulty will probably be to abandon
patch editing and try a more accessible option, such as: cancel the
`add -p` session, edit normally, and restart the session.

If inexperience refers to users unfamiliar with the (e)dit option but
knowledgeable about the patch format, I believe they will have enough
experience to know that reconstructing a patch is a tricky task.
Again, the most likely path they will follow when encountering
difficulties will be to restart the edit (Junio's message in the
thread is an example of this).

At any rate, although I understand the concern, this series doesn't
aim to improve the mechanisms that help identify the problems in a
faulty patch.  The goal of this series is to offer (actually to
recover) the possibility for the user to make corrections.

Something I haven't mentioned before in this thread is that currently,
if the user makes a mistake while editing a patch, we don't give them
the opportunity to review their error.  We leave them in doubt.  This
happened recently to me editing a patch with context lines containing
whitespace errors and "whitespace=fix".  But that's another story that
I still need to work on [1].

So, to me, it seems sensible to let the user review the faulty patch,
even if it's only to discard it.

> 
> > If they really want to start over with a fresh patch they still can
> > say "no" to cancel the "edit" and start anew [*].
> 
> This is not very obvious to the user,

It has been so for a decade...

Keep in mind that this message will probably only be shown very _very_
rarely to users who are most likely very familiar with (e)dit.

> it would be much better to give them
> the choice when we prompt them about editing the hunk again.

That's an option I explored but abandoned.

I didn't come up with any message that I liked that wasn't literally a
long paragraph.

In the end, I gave up in favor of what I believe is a better option,
which is recovering the original intention of "no".

I'm not against the option you propose, I'm just not convinced that
what we already have, since ac083c47ea (git-add--interactive: manual
hunk editing mode, 2008-07-03), isn't intuitive enough for the
users of (e)dit.

> We've been
> giving the user the original hunk for the last six and a half years so I
> think it's a bit late to unilaterally change that now.

For me, this isn't a reason not to make the change.

> 
> > diff --git a/add-patch.c b/add-patch.c
> > index 557903310d..75b5129281 100644
> > --- a/add-patch.c
> > +++ b/add-patch.c
> > @@ -1111,7 +1111,8 @@ static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
> >   	hunk->colored_end = s->colored.len;
> >   }
> > -static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
> > +static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk,
> 
> I would add
> 				const struct hunk *backup,
> 
> here
> 
> > +			      size_t plain_len, size_t colored_len)
> >   {
> >   	size_t i;
> > @@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
> >   				      "addp-hunk-edit.diff", NULL) < 0)
> >   		return -1;
> > +	/* Drop possible previous edits */
> > +	strbuf_setlen(&s->plain, plain_len);
> > +	strbuf_setlen(&s->colored, colored_len);
> 
> then we can restore the back up here with
> 
> 	*hunk = *backup;
> 
> That would make it clear that we're resetting the hunk and would continue to
> work if we change struct hunk in the future.
> 
> >   	/* strip out commented lines */
> >   	hunk->start = s->plain.len;
> >   	for (i = 0; i < s->buf.len; ) {
> > @@ -1157,12 +1162,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
> >   	}
> >   	hunk->end = s->plain.len;
> > +
> > +	recolor_hunk(s, hunk);
> > +
> 
> This means we're now forking an external process when there is no hunk to
> color. It would be better to avoid that by leaving this code where it was
> and restoring the backup hunk above.

I don't see that external process. ¿?

After reviewing the code in the previous iteration, based on your
comments, I concluded that `recolor_hunk()` makes more sense before
the "return 0".  Even if we end up discarding this series.  I think.

> 
> >   	if (hunk->end == hunk->start)
> >   		/* The user aborted editing by deleting everything */
> >   		return 0;
> > -	recolor_hunk(s, hunk);
> > -
> >
> >   		/*
> >   		 * TRANSLATORS: do not translate [y/n]
> > @@ -1289,8 +1290,14 @@ static int edit_hunk_loop(struct add_p_state *s,
> >   					"Edit again (saying \"no\" discards!) "
> >   					"[y/n]? "));
> 
> I think we should make this a three-way choice so the user can choose to
> keep their changes or start from a valid hunk.

As I said above, I don't object to this, but I'm not convinced it's
necessary.

> 
> >   		if (res < 1)
> > -			return -1;
> > +			break;
> >   	}
> > +
> > +	/* Drop a possible edit */
> > +	strbuf_setlen(&s->plain, plain_len);
> > +	strbuf_setlen(&s->colored, colored_len);
> > +	*hunk = backup;
> > +	return -1;
> >   }
> >   static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
> > diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> > index 718438ffc7..f3206a317b 100755
> > --- a/t/t3701-add-interactive.sh
> > +++ b/t/t3701-add-interactive.sh
> > @@ -165,6 +165,19 @@ test_expect_success 'dummy edit works' '
> >   	diff_cmp expected diff
> >   '
> > +test_expect_success 'editing again works' '
> > +	git reset &&
> > +	write_script "fake_editor.sh" <<-\EOF &&
> > +	grep been-here "$1" >output
> > +	echo been-here >"$1"
> > +	EOF
> > +	(
> > +		test_set_editor "$(pwd)/fake_editor.sh" &&
> > +		test_write_lines e y | GIT_TRACE=1 git add -p
> 
> This is still missing "n q". Apart from that the test is looking good.

I've been resisting the idea of "completeness", because I think "e y"
should also be fine.  But I'm not going to resist anymore here :-),
since I don't think the test has much more value without "n q".  So
I'll add it.

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

* [PATCH v3] add-patch: edit the hunk again
  2024-09-18 17:51 ` [PATCH v2] " Rubén Justo
  2024-09-23  9:07   ` phillip.wood123
@ 2024-09-28 14:30   ` Rubén Justo
  2024-10-01 10:03     ` Phillip Wood
  1 sibling, 1 reply; 17+ messages in thread
From: Rubén Justo @ 2024-09-28 14:30 UTC (permalink / raw)
  To: Git List; +Cc: Phillip Wood, Junio C Hamano, Johannes Schindelin

The "edit" option allows the user to directly modify the hunk to be
applied.

If the modified hunk returned by the user is not an applicable patch,
they will be given the opportunity to try again.

For this new attempt we give them the original hunk;  they have to
repeat the modification from scratch.

Instead, let's give them the modified patch back, so they can identify
and fix the problem.

If they really want to start over with a fresh patch they still can
say 'no', to cancel the "edit" and start anew [*].

    * In the old script-based version of "add -p", this "no" meant
      discarding the hunk and moving on to the next one.

      This changed, probably unintentionally, during its conversion to
      C in bcdd297b78 (built-in add -p: implement hunk editing,
      2019-12-13).

      It now makes more sense not to move to the next block when the
      user requests to discard their edits.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---

In this iteration, I'm modifying the message 'saying no discards' to
make its meaning more explicit, perhaps also gaining some clarity
along the way for the user who wants to restart editing from the
original patch.

In the test, I'm adding "n q" to the script as suggested by Phillip.

And, just a bit of mental peace by restoring the hunk from the backup
before trimming the two strbuf.

Thanks.

 add-patch.c                | 33 ++++++++++++++++++++-------------
 t/t3701-add-interactive.sh | 13 +++++++++++++
 2 files changed, 33 insertions(+), 13 deletions(-)

diff --git a/add-patch.c b/add-patch.c
index 557903310d..c847b4a59d 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1111,7 +1111,8 @@ static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
 	hunk->colored_end = s->colored.len;
 }
 
-static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
+static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk,
+			      size_t plain_len, size_t colored_len)
 {
 	size_t i;
 
@@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 				      "addp-hunk-edit.diff", NULL) < 0)
 		return -1; 
 
+	/* Drop possible previous edits */
+	strbuf_setlen(&s->plain, plain_len);
+	strbuf_setlen(&s->colored, colored_len);
+
 	/* strip out commented lines */
 	hunk->start = s->plain.len;
 	for (i = 0; i < s->buf.len; ) {
@@ -1157,12 +1162,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
 	}
 
 	hunk->end = s->plain.len;
+
+	recolor_hunk(s, hunk);
+
 	if (hunk->end == hunk->start)
 		/* The user aborted editing by deleting everything */
 		return 0;
 
-	recolor_hunk(s, hunk);
-
 	/*
 	 * If the hunk header is intact, parse it, otherwise simply use the
 	 * hunk header prior to editing (which will adjust `hunk->start` to
@@ -1257,15 +1263,14 @@ static int edit_hunk_loop(struct add_p_state *s,
 	backup = *hunk;
 
 	for (;;) {
-		int res = edit_hunk_manually(s, hunk);
+		int res = edit_hunk_manually(s, hunk, plain_len, colored_len);
 		if (res == 0) {
 			/* abandoned */
-			*hunk = backup;
-			return -1;
+			break;
 		}
 
 		if (res > 0) {
-			hunk->delta +=
+			hunk->delta = backup.delta +
 				recount_edited_hunk(s, hunk,
 						    backup.header.old_count,
 						    backup.header.new_count);
@@ -1273,10 +1278,6 @@ static int edit_hunk_loop(struct add_p_state *s,
 				return 0;
 		}
 
-		/* Drop edits (they were appended to s->plain) */
-		strbuf_setlen(&s->plain, plain_len);
-		strbuf_setlen(&s->colored, colored_len);
-		*hunk = backup;
 
 		/*
 		 * TRANSLATORS: do not translate [y/n]
@@ -1286,11 +1287,17 @@ static int edit_hunk_loop(struct add_p_state *s,
 		 * of the word "no" does not start with n.
 		 */
 		res = prompt_yesno(s, _("Your edited hunk does not apply. "
-					"Edit again (saying \"no\" discards!) "
+					"Edit again (saying \"no\" discards your edits!) "
 					"[y/n]? "));
 		if (res < 1)
-			return -1;
+			break;
 	}
+
+	/* Drop a possible edit */
+	*hunk = backup;
+	strbuf_setlen(&s->plain, plain_len);
+	strbuf_setlen(&s->colored, colored_len);
+	return -1;
 }
 
 static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 718438ffc7..1ceefd96e6 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -165,6 +165,19 @@ test_expect_success 'dummy edit works' '
 	diff_cmp expected diff
 '
 
+test_expect_success 'editing again works' '
+	git reset &&
+	write_script "fake_editor.sh" <<-\EOF &&
+	grep been-here "$1" >output
+	echo been-here >"$1"
+	EOF
+	(
+		test_set_editor "$(pwd)/fake_editor.sh" &&
+		test_write_lines e y n q | GIT_TRACE=1 git add -p
+	) &&
+	test_grep been-here output
+'
+
 test_expect_success 'setup patch' '
 	cat >patch <<-\EOF
 	@@ -1,1 +1,4 @@

Range-diff against v2:
1:  2b55a759d5 ! 1:  7e76606751 add-patch: edit the hunk again
    @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
      		/*
      		 * TRANSLATORS: do not translate [y/n]
     @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
    - 					"Edit again (saying \"no\" discards!) "
    + 		 * of the word "no" does not start with n.
    + 		 */
    + 		res = prompt_yesno(s, _("Your edited hunk does not apply. "
    +-					"Edit again (saying \"no\" discards!) "
    ++					"Edit again (saying \"no\" discards your edits!) "
      					"[y/n]? "));
      		if (res < 1)
     -			return -1;
    @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
      	}
     +
     +	/* Drop a possible edit */
    ++	*hunk = backup;
     +	strbuf_setlen(&s->plain, plain_len);
     +	strbuf_setlen(&s->colored, colored_len);
    -+	*hunk = backup;
     +	return -1;
      }
      
    @@ t/t3701-add-interactive.sh: test_expect_success 'dummy edit works' '
     +	EOF
     +	(
     +		test_set_editor "$(pwd)/fake_editor.sh" &&
    -+		test_write_lines e y | GIT_TRACE=1 git add -p
    ++		test_write_lines e y n q | GIT_TRACE=1 git add -p
     +	) &&
     +	test_grep been-here output
     +'
-- 
2.47.0.rc0.1.g1645ecd054

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

* Re: [PATCH v2] add-patch: edit the hunk again
  2024-09-24 22:54     ` Rubén Justo
@ 2024-10-01 10:02       ` Phillip Wood
  2024-10-02 16:36         ` Rubén Justo
  0 siblings, 1 reply; 17+ messages in thread
From: Phillip Wood @ 2024-10-01 10:02 UTC (permalink / raw)
  To: Rubén Justo, phillip.wood, Git List
  Cc: Junio C Hamano, Johannes Schindelin

Hi Rubén

On 24/09/2024 23:54, Rubén Justo wrote:
> On Mon, Sep 23, 2024 at 10:07:08AM +0100, phillip.wood123@gmail.com wrote:
 >
> So, to me, it seems sensible to let the user review the faulty patch,
> even if it's only to discard it.

I agree we could add that option but not as the default

>>> If they really want to start over with a fresh patch they still can
>>> say "no" to cancel the "edit" and start anew [*].
>>
>> This is not very obvious to the user,
> 
> It has been so for a decade...

That does not make it obvious though

> Keep in mind that this message will probably only be shown very _very_
> rarely to users who are most likely very familiar with (e)dit.

I'd argue that users who are not familiar with (e)dit are more likely to 
make mistakes when editing hunks and are less likely to be able to fix them.

>>> +
>>> +	recolor_hunk(s, hunk);
>>> +
>>
>> This means we're now forking an external process when there is no hunk to
>> color. It would be better to avoid that by leaving this code where it was
>> and restoring the backup hunk above.
> 
> I don't see that external process. ¿?

Oh sorry, I thought we ran interactive.diffFilter on the edited hunk, 
but we don't. I'll try and find time to fix that.

>> This is still missing "n q". Apart from that the test is looking good.
> 
> I've been resisting the idea of "completeness", because I think "e y"
> should also be fine.  But I'm not going to resist anymore here :-),
> since I don't think the test has much more value without "n q".  So
> I'll add it.

The reason I think we should have it is that the tests ought to be 
testing realistic user input and not rely on getting EOF which is 
unlikely to happen in real life.

Best Wishes

Phillip


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

* Re: [PATCH v3] add-patch: edit the hunk again
  2024-09-28 14:30   ` [PATCH v3] " Rubén Justo
@ 2024-10-01 10:03     ` Phillip Wood
  2024-10-01 17:58       ` Junio C Hamano
  2024-10-02 17:27       ` Rubén Justo
  0 siblings, 2 replies; 17+ messages in thread
From: Phillip Wood @ 2024-10-01 10:03 UTC (permalink / raw)
  To: Rubén Justo, Git List
  Cc: Phillip Wood, Junio C Hamano, Johannes Schindelin

Hi Rubén

On 28/09/2024 15:30, Rubén Justo wrote:
> The "edit" option allows the user to directly modify the hunk to be
> applied.
> 
> If the modified hunk returned by the user is not an applicable patch,
> they will be given the opportunity to try again.
> 
> For this new attempt we give them the original hunk;  they have to
> repeat the modification from scratch.
> 
> Instead, let's give them the modified patch back, so they can identify
> and fix the problem.
> 
> If they really want to start over with a fresh patch they still can
> say 'no', to cancel the "edit" and start anew [*].
> 
>      * In the old script-based version of "add -p", this "no" meant
>        discarding the hunk and moving on to the next one.
> 
>        This changed, probably unintentionally, during its conversion to
>        C in bcdd297b78 (built-in add -p: implement hunk editing,
>        2019-12-13).
> 
>        It now makes more sense not to move to the next block when the
>        user requests to discard their edits.
> 
> Signed-off-by: Rubén Justo <rjusto@gmail.com>
> ---
> 
> In this iteration, I'm modifying the message 'saying no discards' to
> make its meaning more explicit, perhaps also gaining some clarity
> along the way for the user who wants to restart editing from the
> original patch.
> 
> In the test, I'm adding "n q" to the script as suggested by Phillip.
> 
> And, just a bit of mental peace by restoring the hunk from the backup
> before trimming the two strbuf.

I hoped that change would be in edit_hunk_manually() but it isn't.

I'm afraid I still don't think that changing the default is a good idea 
as it is often very difficult to correct a badly edited hunk. Can we 
offer the user a choice of

     (e) edit the original hunk again
     (f) fix the edited hunk
     (d) discard the edit

In [1] you say you discarded that idea because the wording was too 
verbose but something along like the above should be succinct enough.

Best Wishes

Phillip

[1] 
https://lore.kernel.org/git/6f392446-10b4-4074-a993-97ac444275f8@gmail.com

> Thanks.
> 
>   add-patch.c                | 33 ++++++++++++++++++++-------------
>   t/t3701-add-interactive.sh | 13 +++++++++++++
>   2 files changed, 33 insertions(+), 13 deletions(-)
> 
> diff --git a/add-patch.c b/add-patch.c
> index 557903310d..c847b4a59d 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -1111,7 +1111,8 @@ static void recolor_hunk(struct add_p_state *s, struct hunk *hunk)
>   	hunk->colored_end = s->colored.len;
>   }
>   
> -static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
> +static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk,
> +			      size_t plain_len, size_t colored_len)
>   {
>   	size_t i;
>   
> @@ -1146,6 +1147,10 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>   				      "addp-hunk-edit.diff", NULL) < 0)
>   		return -1;
>   
> +	/* Drop possible previous edits */
> +	strbuf_setlen(&s->plain, plain_len);
> +	strbuf_setlen(&s->colored, colored_len);
> +
>   	/* strip out commented lines */
>   	hunk->start = s->plain.len;
>   	for (i = 0; i < s->buf.len; ) {
> @@ -1157,12 +1162,13 @@ static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk)
>   	}
>   
>   	hunk->end = s->plain.len;
> +
> +	recolor_hunk(s, hunk);
> +
>   	if (hunk->end == hunk->start)
>   		/* The user aborted editing by deleting everything */
>   		return 0;
>   
> -	recolor_hunk(s, hunk);
> -
>   	/*
>   	 * If the hunk header is intact, parse it, otherwise simply use the
>   	 * hunk header prior to editing (which will adjust `hunk->start` to
> @@ -1257,15 +1263,14 @@ static int edit_hunk_loop(struct add_p_state *s,
>   	backup = *hunk;
>   
>   	for (;;) {
> -		int res = edit_hunk_manually(s, hunk);
> +		int res = edit_hunk_manually(s, hunk, plain_len, colored_len);
>   		if (res == 0) {
>   			/* abandoned */
> -			*hunk = backup;
> -			return -1;
> +			break;
>   		}
>   
>   		if (res > 0) {
> -			hunk->delta +=
> +			hunk->delta = backup.delta +
>   				recount_edited_hunk(s, hunk,
>   						    backup.header.old_count,
>   						    backup.header.new_count);
> @@ -1273,10 +1278,6 @@ static int edit_hunk_loop(struct add_p_state *s,
>   				return 0;
>   		}
>   
> -		/* Drop edits (they were appended to s->plain) */
> -		strbuf_setlen(&s->plain, plain_len);
> -		strbuf_setlen(&s->colored, colored_len);
> -		*hunk = backup;
>   
>   		/*
>   		 * TRANSLATORS: do not translate [y/n]
> @@ -1286,11 +1287,17 @@ static int edit_hunk_loop(struct add_p_state *s,
>   		 * of the word "no" does not start with n.
>   		 */
>   		res = prompt_yesno(s, _("Your edited hunk does not apply. "
> -					"Edit again (saying \"no\" discards!) "
> +					"Edit again (saying \"no\" discards your edits!) "
>   					"[y/n]? "));
>   		if (res < 1)
> -			return -1;
> +			break;
>   	}
> +
> +	/* Drop a possible edit */
> +	*hunk = backup;
> +	strbuf_setlen(&s->plain, plain_len);
> +	strbuf_setlen(&s->colored, colored_len);
> +	return -1;
>   }
>   
>   static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
> diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> index 718438ffc7..1ceefd96e6 100755
> --- a/t/t3701-add-interactive.sh
> +++ b/t/t3701-add-interactive.sh
> @@ -165,6 +165,19 @@ test_expect_success 'dummy edit works' '
>   	diff_cmp expected diff
>   '
>   
> +test_expect_success 'editing again works' '
> +	git reset &&
> +	write_script "fake_editor.sh" <<-\EOF &&
> +	grep been-here "$1" >output
> +	echo been-here >"$1"
> +	EOF
> +	(
> +		test_set_editor "$(pwd)/fake_editor.sh" &&
> +		test_write_lines e y n q | GIT_TRACE=1 git add -p
> +	) &&
> +	test_grep been-here output
> +'
> +
>   test_expect_success 'setup patch' '
>   	cat >patch <<-\EOF
>   	@@ -1,1 +1,4 @@
> 
> Range-diff against v2:
> 1:  2b55a759d5 ! 1:  7e76606751 add-patch: edit the hunk again
>      @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
>        		/*
>        		 * TRANSLATORS: do not translate [y/n]
>       @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
>      - 					"Edit again (saying \"no\" discards!) "
>      + 		 * of the word "no" does not start with n.
>      + 		 */
>      + 		res = prompt_yesno(s, _("Your edited hunk does not apply. "
>      +-					"Edit again (saying \"no\" discards!) "
>      ++					"Edit again (saying \"no\" discards your edits!) "
>        					"[y/n]? "));
>        		if (res < 1)
>       -			return -1;
>      @@ add-patch.c: static int edit_hunk_loop(struct add_p_state *s,
>        	}
>       +
>       +	/* Drop a possible edit */
>      ++	*hunk = backup;
>       +	strbuf_setlen(&s->plain, plain_len);
>       +	strbuf_setlen(&s->colored, colored_len);
>      -+	*hunk = backup;
>       +	return -1;
>        }
>        
>      @@ t/t3701-add-interactive.sh: test_expect_success 'dummy edit works' '
>       +	EOF
>       +	(
>       +		test_set_editor "$(pwd)/fake_editor.sh" &&
>      -+		test_write_lines e y | GIT_TRACE=1 git add -p
>      ++		test_write_lines e y n q | GIT_TRACE=1 git add -p
>       +	) &&
>       +	test_grep been-here output
>       +'


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

* Re: [PATCH v3] add-patch: edit the hunk again
  2024-10-01 10:03     ` Phillip Wood
@ 2024-10-01 17:58       ` Junio C Hamano
  2024-10-02 17:34         ` Rubén Justo
  2024-10-02 17:27       ` Rubén Justo
  1 sibling, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2024-10-01 17:58 UTC (permalink / raw)
  To: Phillip Wood
  Cc: Rubén Justo, Git List, Phillip Wood, Johannes Schindelin

Phillip Wood <phillip.wood123@gmail.com> writes:

> I hoped that change would be in edit_hunk_manually() but it isn't.
>
> I'm afraid I still don't think that changing the default is a good
> idea as it is often very difficult to correct a badly edited hunk. Can
> we offer the user a choice of
>
>     (e) edit the original hunk again
>     (f) fix the edited hunk
>     (d) discard the edit
>
> In [1] you say you discarded that idea because the wording was too
> verbose but something along like the above should be succinct enough.

I too disagree with the change of the default, but I would not
complain if we offered the feature to re-edit as long as it is
clearly marked as a new optional choice.

Phillip, thanks for being firm yet still constructive.


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

* Re: [PATCH v2] add-patch: edit the hunk again
  2024-10-01 10:02       ` Phillip Wood
@ 2024-10-02 16:36         ` Rubén Justo
  0 siblings, 0 replies; 17+ messages in thread
From: Rubén Justo @ 2024-10-02 16:36 UTC (permalink / raw)
  To: phillip.wood, Git List; +Cc: Junio C Hamano, Johannes Schindelin

On Tue, Oct 01, 2024 at 11:02:53AM +0100, Phillip Wood wrote:

> I'd argue that users who are not familiar with (e)dit are more likely to
> make mistakes when editing hunks and are less likely to be able to fix them.

This series is not about making errors more descriptive or making
(e)dit more (or less) accessible.

Editing the original hunk is already quite challenging and prone to
errors.

This series is about regaining the possibility for the user to see
and correct their mistakes.

> > > This is still missing "n q". Apart from that the test is looking good.
> > 
> > I've been resisting the idea of "completeness", because I think "e y"
> > should also be fine.  But I'm not going to resist anymore here :-),
> > since I don't think the test has much more value without "n q".  So
> > I'll add it.
> 
> The reason I think we should have it is that the tests ought to be testing
> realistic user input and not rely on getting EOF which is unlikely to happen
> in real life.

Sometimes, I use ctrl+d instead of 'q'.  So, some of my interactive
"add -p" sessions can be described better with "y" than with "y q" or
"y n... q".  And I'm real ;-)

Of course, non-interactively: "e y" or "y" work as expected as the
test in this series and others in t3701 demonstrate.

Since we already have other tests, I didn't mind adding "n q" in an
attempt to move the series forward.

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

* Re: [PATCH v3] add-patch: edit the hunk again
  2024-10-01 10:03     ` Phillip Wood
  2024-10-01 17:58       ` Junio C Hamano
@ 2024-10-02 17:27       ` Rubén Justo
  1 sibling, 0 replies; 17+ messages in thread
From: Rubén Justo @ 2024-10-02 17:27 UTC (permalink / raw)
  To: phillip.wood, Git List; +Cc: Junio C Hamano, Johannes Schindelin

On Tue, Oct 01, 2024 at 11:03:10AM +0100, Phillip Wood wrote:

> I'm afraid I still don't think that changing the default is a good idea as
> it is often very difficult to correct a badly edited hunk.

This series isn't about how hard it is to fix a badly edited hunk.

> In [1] you say you discarded that idea because the wording was too verbose

Not really.  I still believe that regaining the original intention of
"no" is a better option than adding new options to the interface.

I am not opposed to that change, I just think it's an unnecessary
complication.

A user who experiences problems with a badly edited hunk, edited by
themselves, will probably encounter similar issues as they would when
editing the original hunk.  I think.

I don't think reconstructing a patch is a realistic (sensible)
scenario that we should be concerned about.

The small change in the message, in this iteration, adds a bit of
clarity for them, I think:

> > @@ -1286,11 +1287,17 @@ static int edit_hunk_loop(struct add_p_state *s,
> >   		 * of the word "no" does not start with n.
> >   		 */
> >   		res = prompt_yesno(s, _("Your edited hunk does not apply. "
> > -					"Edit again (saying \"no\" discards!) "
> > +					"Edit again (saying \"no\" discards your edits!) "
> >   					"[y/n]? "));

> 
> [1]
> https://lore.kernel.org/git/6f392446-10b4-4074-a993-97ac444275f8@gmail.com

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

* Re: [PATCH v3] add-patch: edit the hunk again
  2024-10-01 17:58       ` Junio C Hamano
@ 2024-10-02 17:34         ` Rubén Justo
  0 siblings, 0 replies; 17+ messages in thread
From: Rubén Justo @ 2024-10-02 17:34 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git List, Phillip Wood, Phillip Wood, Johannes Schindelin

On Tue, Oct 01, 2024 at 10:58:15AM -0700, Junio C Hamano wrote:

> I would not
> complain if we offered the feature to re-edit

I'm using this:

   $ GIT_EDITOR='bash -c "vim \"$1\" && cp -f $1 /tmp/backup"' git add -p

That way, I have a copy of the edited hunk in case I want to try again
or review it.  In that case, I can simply try again and reload from
"/tmp/backup".

I'm sure there's a more clever way, but I haven't come up with
anything more elegant.

Maybe interactive.editor, apply.editor, add.editor?

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

end of thread, other threads:[~2024-10-02 17:34 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-15 11:38 [PATCH] add-patch: edit the hunk again Rubén Justo
2024-09-16 13:33 ` Phillip Wood
2024-09-16 17:35   ` Junio C Hamano
2024-09-16 22:09   ` Rubén Justo
2024-09-18 10:06     ` phillip.wood123
2024-09-18 17:46       ` Rubén Justo
2024-09-18 17:51 ` [PATCH v2] " Rubén Justo
2024-09-23  9:07   ` phillip.wood123
2024-09-23 16:02     ` Junio C Hamano
2024-09-24 22:54     ` Rubén Justo
2024-10-01 10:02       ` Phillip Wood
2024-10-02 16:36         ` Rubén Justo
2024-09-28 14:30   ` [PATCH v3] " Rubén Justo
2024-10-01 10:03     ` Phillip Wood
2024-10-01 17:58       ` Junio C Hamano
2024-10-02 17:34         ` Rubén Justo
2024-10-02 17:27       ` Rubén Justo

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