git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] add-patch: use normalize_marker() when recounting edited hunk

From
PGPhillip Wood via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 20, 2024, 16:02 UTC
Message-ID
<7bdcd2df01246932e452417815368f6c56d83e8d.1721491320.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1763.v2.git.1721491320.gitgitgadget@gmail.com>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

After the user has edited a hunk the number of lines in the pre- and post- image lines is recounted the hunk header can be updated before passing the hunk to "git apply". The recounting code correctly handles empty context lines where the leading ' ' is omitted by treating '\n' and '\r' as context lines.

Update this code to use normalize_marker() so that the handling of empty context lines is consistent with the rest of the hunk parsing code. There is a small change in behavior as normalize_marker() only treats "\r\n" as an empty context line rather than any line starting with '\r'. This should not matter in practice as Macs have used Unix line endings since MacOs 10 was released in 2001 and if it transpires that someone is still using an earlier version of MacOs where lines end with '\r' then we will need to change the handling of '\r' in normalize_marker() anyway.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 add-patch.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/add-patch.c b/add-patch.c
index 8feb719483f..2a72ad63d14 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1178,14 +1178,14 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
 
 	header->old_count = header->new_count = 0;
 	for (i = hunk->start; i < hunk->end; ) {
-		switch (s->plain.buf[i]) {
+		switch(normalize_marker(&s->plain.buf[i])) {
 		case '-':
 			header->old_count++;
 			break;
 		case '+':
 			header->new_count++;
 			break;
-		case ' ': case '\r': case '\n':
+		case ' ':
 			header->old_count++;
 			header->new_count++;
 			break;
-- 
gitgitgadget
Previous: Phillip Wood via GitGitGadget
Message 6 of 6 in “add-patch: handle splitting hunks with diff.suppressBlankEmpty”
  1. add-patch: handle splitting hunks with diff.suppressBlankEmptyPhillip Wood via GitGitGadget, Jul 18, 2024
  2. Junio C HamanoJul 18, 2024
  3. Phillip WoodJul 19, 2024
  4. 0/2 add-patch: handle splitting hunks with diff.suppressBlankEmptyPhillip Wood via GitGitGadget, Jul 20, 2024
  5. 1/2 add-patch: handle splitting hunks with diff.suppressBlankEmptyPhillip Wood via GitGitGadget, Jul 20, 2024
  6. 2/2 add-patch: use normalize_marker() when recounting edited hunkPhillip Wood via GitGitGadget, Jul 20, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.