Skip to contentSkip to conversation

add-patch: edit the hunk again

PATCH Superseded Rubén Justo 3 participants 17 messages Sep 15, 2024 – Oct 2, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add-patch: edit the hunk again

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.

---

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(-)
add-patch.c +16 -10
index 557903310d..125e79a5ae 100644
@@ -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,
t/t3701-add-interactive.sh +14 -0
index 718438ffc7..6af5636221 100755
@@ -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 @@
Signature · 1 line
2.46.1.507.gbcf32d0979
#1 lore raw

Hi Rubén

Quoted 8 lines · Rubén Justo

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.

Quoted 2 lines

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.

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1153

Quoted 12 lines
add-patch.c +4 -0
index 557903310d..125e79a5ae 100644
@@ -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.

add-patch.c static int edit_hunk_loop(struct add_p_state *s, · line 1279

Quoted 8 lines
add-patch.c +0 -4
@@ -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"

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 170

Quoted 11 lines
t/t3701-add-interactive.sh +3 -0
index 718438ffc7..6af5636221 100755
@@ -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

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 174

Quoted 4 lines
t/t3701-add-interactive.sh +4 -0
+	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.

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 178

Quoted 3 lines
t/t3701-add-interactive.sh +3 -0
+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.

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 179

Quoted 1 line
t/t3701-add-interactive.sh +1 -0
+	grep found output

Using test_grep makes it easier to debug test failures.

Best Wishes

Phillip

#2 lore raw
Quoted 11 lines · Phillip Wood

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.

#3 lore raw
In reply to Phillip Wood
Quoted 36 lines · Phillip Wood
Quoted 8 lines

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.

Quoted 2 lines

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.

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

Quoted 10 lines
Quoted 10 lines
add-patch.c +2 -0
index 557903310d..125e79a5ae 100644
@@ -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);
Quoted 8 lines

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1153

Quoted 2 lines
add-patch.c +2 -0
+	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?

Quoted 11 lines

add-patch.c static int edit_hunk_loop(struct add_p_state *s, · line 1278

Quoted 8 lines
add-patch.c +0 -4
@@ -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?

Quoted 13 lines

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 169

Quoted 11 lines
t/t3701-add-interactive.sh +3 -0
index 718438ffc7..6af5636221 100755
@@ -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.

Quoted 9 lines

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 174

Quoted 5 lines
t/t3701-add-interactive.sh +4 -0
+	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.

Quoted 6 lines

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 178

Quoted 4 lines
t/t3701-add-interactive.sh +3 -0
+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.

Quoted 9 lines

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 179

Quoted 2 lines
t/t3701-add-interactive.sh +1 -0
+	grep found output

Using test_grep makes it easier to debug test failures.

Best Wishes

Phillip

Thanks for your review.

#4 lore raw

Hi Rubén

Quoted 4 lines · Rubén Justo

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

Quoted 4 lines

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.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
add-patch.c +2 -0
index 557903310d..125e79a5ae 100644
@@ -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);
Quoted 8 lines

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1153

Quoted 2 lines
add-patch.c +2 -0
+	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.

Quoted 2 lines

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

Quoted 11 lines
Quoted 8 lines

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1156

Quoted 4 lines
add-patch.c +4 -0
+	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

#5 lore raw
Quoted 9 lines · On Wed, Sep 18, 2024 at 11:06:34AM +0100, [email protected] wrote:

Hi Rubén

Quoted 4 lines · Rubén Justo

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

Quoted 7 lines
Quoted 5 lines

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.

Quoted 3 lines

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]?

Quoted 2 lines

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.

Quoted 2 lines

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.

Quoted 5 lines

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.

Quoted 2 lines

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.

#6 lore raw

PATCH v2 1/1 add-patch: edit the hunk again

In reply to Rubén Justo

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.

---

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(-)
add-patch.c +19 -12
index 557903310d..75b5129281 100644
@@ -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,
t/t3701-add-interactive.sh +13 -0
index 718438ffc7..f3206a317b 100755
@@ -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' '
Signature · 1 line
2.46.1.507.gdd29a28bc2
#7 lore raw

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.

Quoted 11 lines · Rubén Justo

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.

Quoted 2 lines

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.

add-patch.c static void recolor_hunk(struct add_p_state *s, struct hunk *hunk) · line 1114

Quoted 10 lines
add-patch.c +1 -1
index 557903310d..75b5129281 100644
@@ -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

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1152

Quoted 11 lines
add-patch.c +4 -0
+			      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.

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1167

Quoted 10 lines
add-patch.c +3 -0
  	/* 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.

add-patch.c static int edit_hunk_loop(struct add_p_state *s, · line 1291

Quoted 12 lines
add-patch.c +0 -2
  	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.

Quoted 20 lines
add-patch.c +7 -1
  		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,
t/t3701-add-interactive.sh +0 -0
index 718438ffc7..f3206a317b 100755
@@ -165,6 +165,19 @@ test_expect_success 'dummy edit works' '
  	diff_cmp expected diff
  '

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 176

Quoted 10 lines
t/t3701-add-interactive.sh +9 -0
 
+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

Quoted 61 lines
t/t3701-add-interactive.sh +4 -0
+	) &&
+	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' '
#8 lore raw
Quoted 9 lines · [email protected] 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.

#9 lore raw
In reply to [email protected]
Quoted 2 lines · On Mon, Sep 23, 2024 at 10:07:08AM +0100, [email protected] 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.

Quoted 8 lines · Rubén Justo
Quoted 6 lines

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.

Quoted 7 lines
Quoted 5 lines

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.

Quoted 5 lines
Quoted 3 lines

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.

Quoted 2 lines

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.

Quoted 3 lines

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.

Quoted 33 lines

add-patch.c static void recolor_hunk(struct add_p_state *s, struct hunk *hunk) · line 1113

Quoted 10 lines
add-patch.c +1 -1
index 557903310d..75b5129281 100644
@@ -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

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1151

Quoted 9 lines
add-patch.c +4 -0
+			      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.

Quoted 13 lines

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1166

Quoted 9 lines
add-patch.c +3 -0
  	/* 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.

Quoted 7 lines
Quoted 7 lines
add-patch.c +0 -2
  	if (hunk->end == hunk->start)
  		/* The user aborted editing by deleting everything */
  		return 0;
-	recolor_hunk(s, hunk);
-
 
Quoted 8 lines

add-patch.c static int edit_hunk_loop(struct add_p_state *s, · line 1291

Quoted 5 lines
add-patch.c +0 -0
  		/*
  		 * 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.

Quoted 23 lines
Quoted 23 lines
add-patch.c +7 -1
  		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,
t/t3701-add-interactive.sh +3 -0
index 718438ffc7..f3206a317b 100755
@@ -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 &&
Quoted 8 lines

t/t3701-add-interactive.sh test_expect_success 'dummy edit works' ' · line 176

Quoted 6 lines
t/t3701-add-interactive.sh +6 -0
+	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.

#10 lore raw

Hi Rubén

Quoted 4 lines · Rubén Justo

On Mon, Sep 23, 2024 at 10:07:08AM +0100, [email protected] 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

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

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

Quoted 2 lines

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.

Quoted 9 lines
Quoted 7 lines

add-patch.c static int edit_hunk_manually(struct add_p_state *s, struct hunk *hunk) · line 1167

Quoted 3 lines
add-patch.c +3 -0
+
+	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.

Quoted 6 lines
Quoted 1 line

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

#11 lore raw
Quoted 2 lines · Phillip Wood

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.

Quoted 10 lines
Quoted 6 lines
Quoted 1 line

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.

#12 lore raw

PATCH v3 1/1 add-patch: edit the hunk again

In reply to Rubén Justo

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.

---

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(-)
add-patch.c +20 -13
index 557903310d..c847b4a59d 100644
@@ -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,
t/t3701-add-interactive.sh +13 -0
index 718438ffc7..1ceefd96e6 100755
@@ -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
     +'
Signature · 1 line
2.47.0.rc0.1.g1645ecd054
#13 lore raw

Hi Rubén

Quoted 33 lines · Rubén Justo

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.

---

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.

Quoted 4 lines

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/[email protected]

Quoted 157 lines

Thanks.

  add-patch.c                | 33 ++++++++++++++++++++-------------
  t/t3701-add-interactive.sh | 13 +++++++++++++
  2 files changed, 33 insertions(+), 13 deletions(-)
add-patch.c +20 -13
index 557903310d..c847b4a59d 100644
@@ -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,
t/t3701-add-interactive.sh +13 -0
index 718438ffc7..1ceefd96e6 100755
@@ -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
      +'
#14 lore raw
Quoted 6 lines · Phillip Wood

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

Quoted 6 lines

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

#15 lore raw
Quoted 2 lines · Junio C Hamano

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?

#16 lore raw
In reply to Phillip Wood
Quoted 2 lines · Phillip Wood

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.

Quoted 1 line

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:

Quoted 11 lines

add-patch.c static int edit_hunk_loop(struct add_p_state *s, · line 1291

Quoted 7 lines
add-patch.c +1 -1
@@ -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/[email protected]