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

[PATCH v2 0/2] add-patch: handle splitting hunks with diff.suppressBlankEmpty

From
PGPhillip Wood via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 20, 2024, 16:01 UTC
Message-ID
<pull.1763.v2.git.1721491320.gitgitgadget@gmail.com>
In-Reply-To
<pull.1763.git.1721312619822.gitgitgadget@gmail.com>

This is an alternative to jk/add-patch-with-suppress-blank-empty which was recently discarded from next. I hope that normalizing the context marker will simplify any future changes to the code.

Changes since V1
 * Updated merge_hunks() to use normalize_marker() as spotted by Junio
 * Updated the test so it checks merge_hunks() as well.
Phillip Wood (2):
  add-patch: handle splitting hunks with diff.suppressBlankEmpty
  add-patch: use normalize_marker() when recounting edited hunk
 add-patch.c                | 23 +++++++++++++++--------
 t/t3701-add-interactive.sh | 19 +++++++++++++++++++
 2 files changed, 34 insertions(+), 8 deletions(-)
base-commit: 790a17fb19d6eadd16c52e5d284a5c6921744766
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1763%2Fphillipwood%2Fadd-p-suppress-blank-empty-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1763/phillipwood/add-p-suppress-blank-empty-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1763
Range-diff vs v1:
 1:  fd09e66727a ! 1:  34d8fd44a97 add-patch: handle splitting hunks with diff.suppressBlankEmpty
     @@ add-patch.c: static void complete_file(char marker, struct hunk *hunk)
       }
       
      +/* Empty context lines may omit the leading ' ' */
     -+static int normalize_marker(char *p)
     ++static int normalize_marker(const char *p)
      +{
      +	return p[0] == '\n' || (p[0] == '\r' && p[1] == '\n') ? ' ' : p[0];
      +}
     @@ add-patch.c: static int parse_diff(struct add_p_state *s, const struct pathspec
       
       		p = eol == pend ? pend : eol + 1;
       		hunk->end = p - plain->buf;
     +@@ add-patch.c: static int merge_hunks(struct add_p_state *s, struct file_diff *file_diff,
     + 					    (int)(hunk->end - hunk->start),
     + 					    plain + hunk->start);
     + 
     +-				if (plain[overlap_end] != ' ')
     ++				if (normalize_marker(&plain[overlap_end]) != ' ')
     + 					return error(_("expected context line "
     + 						       "#%d in\n%.*s"),
     + 						     (int)(j + 1),
      @@ add-patch.c: static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
       	context_line_count = 0;
       
       	while (splittable_into > 1) {
      -		ch = s->plain.buf[current];
     -+		ch = normalize_marker(s->plain.buf + current);
     ++		ch = normalize_marker(&s->plain.buf[current]);
       
       		if (!ch)
       			BUG("buffer overrun while splitting hunks");
     @@ t/t3701-add-interactive.sh: test_expect_success 'reset -p with unmerged files' '
       
      +test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
      +	test_config diff.suppressBlankEmpty true &&
     -+	test_write_lines a b c "" d e f >file &&
     ++	write_script fake-editor.sh <<-\EOF &&
     ++	tr F G <"$1" >"$1.tmp" &&
     ++	mv "$1.tmp" "$1"
     ++	EOF
     ++
     ++	test_write_lines a b "" c d  "" e f "" >file &&
      +	git add file &&
     -+	test_write_lines p q r "" s t u >file &&
     -+	test_write_lines s n y q | git add -p &&
     ++	test_write_lines A b "" c D  "" e F "" >file &&
     ++	(
     ++		test_set_editor "$(pwd)/fake-editor.sh" &&
     ++		test_write_lines s n y e q | git add -p file
     ++	) &&
      +	git cat-file blob :file >actual &&
     -+	test_write_lines a b c "" s t u >expect &&
     ++	test_write_lines a b "" c D "" e G "" >expect &&
      +	test_cmp expect actual
      +'
      +
 -:  ----------- > 2:  7bdcd2df012 add-patch: use normalize_marker() when recounting edited hunk
-- 
gitgitgadget
Previous: Phillip WoodNext: Phillip Wood via GitGitGadget
Message 4 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.