Thread (11 messages) 11 messages, 2 authors, 2025-09-25

Re: [PATCH 2/2] add-patch: update hunk splitability after editing

From: Justin Tobler <hidden>
Date: 2025-02-21 20:32:57

On 25/02/21 02:57PM, Phillip Wood via GitGitGadget wrote:
From: Phillip Wood <redacted>

When the users edits a hunk if they change deletion lines to context
lines or vice versa then the number of hunks that the edited hunk can be
split into may differ from the unedited hunk and so we need to update
hunk->splittable_into. 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.
If I'm understanding this correctly, the issue here is that, when a patch
is editted, the number of hunks in can be split into may change, but is
not reevaluated. This could lead to issue if the editted hunk is
subsequently split.

This issue would also apply to addition lines being changed to context
lines as well correct?
quoted hunk ↗ jump to hunk
Signed-off-by: Phillip Wood <redacted>
---
 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 f44f98275cc..982745373df 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1182,19 +1182,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;
 		}
Ok with this we now recount the potential number of hunks a hunk can be
split into. Whenever there is a change from a context line to a
addition or deletion line, a separate hunk could be formed.

-Justin
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help