From: "Phillip Wood via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Justin Tobler <jltobler@gmail.com>,
Junio C Hamano <gitster@pobox.com>,
Phillip Wood <phillip.wood123@gmail.com>,
Phillip Wood <phillip.wood@dunelm.org.uk>,
Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH v3 2/2] add-patch: update hunk splitability after editing
Date: Thu, 25 Sep 2025 15:10:38 +0000 [thread overview]
Message-ID: <390686dbb3665b86d4651dfbc9dae0c3067e8745.1758813038.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1863.v3.git.1758813038.gitgitgadget@gmail.com>
From: Phillip Wood <phillip.wood@dunelm.org.uk>
If, when the user edits a hunk, they change deletion lines into
context lines or vice versa, then the number of hunks that the edited
hunk can be split into may differ from the unedited hunk. This means
that so we should recalculate `hunk->splittable_into` after the hunk
has been edited. In practice users are unlikely to hit this bug as it
is doubtful that a user who has edited a hunk will split it afterwards.
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
add-patch.c | 12 +++++++++++-
t/t3701-add-interactive.sh | 21 +++++++++++++++++++++
2 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/add-patch.c b/add-patch.c
index 61f42de9ea..bcc2d7666f 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1185,19 +1185,29 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
{
struct hunk_header *header = &hunk->header;
size_t i;
+ char ch, marker = ' ';
+ hunk->splittable_into = 0;
header->old_count = header->new_count = 0;
for (i = hunk->start; i < hunk->end; ) {
- switch(normalize_marker(&s->plain.buf[i])) {
+ ch = normalize_marker(&s->plain.buf[i]);
+ switch (ch) {
case '-':
header->old_count++;
+ if (marker == ' ')
+ hunk->splittable_into++;
+ marker = ch;
break;
case '+':
header->new_count++;
+ if (marker == ' ')
+ hunk->splittable_into++;
+ marker = ch;
break;
case ' ':
header->old_count++;
header->new_count++;
+ marker = ch;
break;
}
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index a6829fd085..13739a4582 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -1311,4 +1311,25 @@ test_expect_success 'splitting previous hunk marks split hunks as undecided' '
test_cmp expect actual
'
+test_expect_success 'splitting edited hunk' '
+ # Before the first hunk is edited it can be split into two
+ # hunks, after editing it can be split into three hunks.
+
+ write_script fake-editor.sh <<-\EOF &&
+ sed "s/^ c/-c/" "$1" >"$1.tmp" &&
+ mv "$1.tmp" "$1"
+ EOF
+
+ test_write_lines a b c d e f g h i j k l m n >file &&
+ git add file &&
+ test_write_lines A b c d E f g h i j k l M n >file &&
+ (
+ test_set_editor "$(pwd)/fake-editor.sh" &&
+ test_write_lines e K s j y n y q | git add -p file
+ ) &&
+ git cat-file blob :file >actual &&
+ test_write_lines a b d e f g h i j k l M n >expect &&
+ test_cmp expect actual
+'
+
test_done
--
gitgitgadget
prev parent reply other threads:[~2025-09-25 15:10 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-21 14:57 [PATCH 0/2] add -p: a couple of hunk splitting fixes Phillip Wood via GitGitGadget
2025-02-21 14:57 ` [PATCH 1/2] add -p: mark split hunks as undecided Phillip Wood via GitGitGadget
2025-02-21 19:52 ` Justin Tobler
2025-02-21 21:31 ` Junio C Hamano
2025-02-26 14:40 ` phillip.wood123
2025-02-26 16:49 ` Junio C Hamano
2025-02-27 16:22 ` phillip.wood123
2025-02-27 18:36 ` Junio C Hamano
2025-02-28 16:19 ` Phillip Wood
2025-02-28 17:06 ` Junio C Hamano
2025-03-04 10:25 ` Phillip Wood
2025-02-21 14:57 ` [PATCH 2/2] add-patch: update hunk splitability after editing Phillip Wood via GitGitGadget
2025-02-21 20:29 ` Justin Tobler
2025-02-21 21:42 ` Junio C Hamano
2025-09-15 15:29 ` [PATCH v2 0/2] add -p: a couple of hunk splitting fixes Phillip Wood via GitGitGadget
2025-09-15 15:29 ` [PATCH v2 1/2] add -p: mark split hunks as undecided Phillip Wood via GitGitGadget
2025-09-15 17:44 ` Junio C Hamano
2025-09-16 9:36 ` Phillip Wood
2025-09-16 16:03 ` Junio C Hamano
2025-09-15 15:29 ` [PATCH v2 2/2] add-patch: update hunk splitability after editing Phillip Wood via GitGitGadget
2025-09-25 15:10 ` [PATCH v3 0/2] add -p: a couple of hunk splitting fixes Phillip Wood via GitGitGadget
2025-09-25 15:10 ` [PATCH v3 1/2] add -p: mark split hunks as undecided Phillip Wood via GitGitGadget
2025-09-25 18:21 ` Junio C Hamano
2025-09-26 10:12 ` Phillip Wood
2025-09-26 17:37 ` Junio C Hamano
2025-10-08 13:51 ` Phillip Wood
2025-09-25 15:10 ` Phillip Wood via GitGitGadget [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=390686dbb3665b86d4651dfbc9dae0c3067e8745.1758813038.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jltobler@gmail.com \
--cc=phillip.wood123@gmail.com \
--cc=phillip.wood@dunelm.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).