Volume XXII, number 280Wednesday, October 7, 2026Latest message 59 minutes ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

patchadd -p: introduce 'w' command to view hunk with --word-diff

9 messages between May 6, 2026 and May 12, 2026, from Javier Bassi, Pablo, Phillip Wood, D. Ben Knoble, Junio C Hamano.

Plain Markdown or JSON for tools and agents. Diffs are folded; open one to read it.

Javier BassiMay 6, 2026, 23:54 UTC on lore

When using `git add --patch`, reviewing changes in long lines can be difficult with the default line-based diff. This is particularly noticeable in formats such as JSONP, CSV, LaTeX, Markdown, or other plain text where small inline edits can be hard to spot.

Added `w - print the current hunk with word-diff` during hunk selection to re-display the current hunk using `--word-diff`. This provides a clearer inline view of changes without modifying the hunk or affecting how patches are applied or staged.

Signed-off-by: Javier Bassi <javierbassi@gmail.com>
---
 Documentation/git-add.adoc |   1 +
 add-patch.c                | 105 ++++++++++++++++++++++++++++++++++++-
 t/t3701-add-interactive.sh |  59 +++++++++++++--------
 3 files changed, 142 insertions(+), 23 deletions(-)
Show changes to 3 files +142 −23

Documentation/git-add.adoc, add-patch.c, t/t3701-add-interactive.sh

diff --git a/Documentation/git-add.adoc b/Documentation/git-add.adoc
index 941135dc63..d2ee1cf9a9 100644
--- a/Documentation/git-add.adoc
+++ b/Documentation/git-add.adoc
@@ -351,6 +351,7 @@ patch::
        K - go to the previous hunk, roll over at the top
        s - split the current hunk into smaller hunks
        e - manually edit the current hunk
+       w - print the current hunk with word-diff
        p - print the current hunk
        P - print the current hunk using the pager
        ? - print help
diff --git a/add-patch.c b/add-patch.c
index f27edcbe8d..0364f4bc97 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -7,6 +7,7 @@
 #include "commit.h"
 #include "config.h"
 #include "diff.h"
+#include "diffcore.h"
 #include "editor.h"
 #include "environment.h"
 #include "gettext.h"
@@ -1508,6 +1509,105 @@ static void summarize_hunk(struct add_p_state *s, struct hunk *hunk,
 	strbuf_complete_line(out);
 }
 
+static void trim_trailing_lf(struct strbuf *buf)
+{
+	if (buf->len && buf->buf[buf->len - 1] == '\n')
+		strbuf_setlen(buf, buf->len - 1);
+}
+
+static void add_word_diff_line(struct strbuf *old, struct strbuf *new,
+			       const char *line, size_t len, char marker)
+{
+	if (marker == '-' || marker == '+' || *line == ' ') {
+		line++;
+		len--;
+	}
+
+	if (marker != '+')
+		strbuf_add(old, line, len);
+	if (marker != '-')
+		strbuf_add(new, line, len);
+}
+
+static void build_word_diff_files(struct add_p_state *s, struct hunk *hunk,
+				  struct strbuf *old, struct strbuf *new)
+{
+	size_t i;
+	char last_marker = '\0';
+
+	for (i = hunk->start; i < hunk->end; i = find_next_line(&s->plain, i)) {
+		size_t next = find_next_line(&s->plain, i);
+		char marker = normalize_marker(s->plain.buf + i);
+
+		if (marker == '\\') {
+			if (last_marker != '+')
+				trim_trailing_lf(old);
+			if (last_marker != '-')
+				trim_trailing_lf(new);
+			continue;
+		}
+
+		if (marker != ' ' && marker != '-' && marker != '+')
+			BUG("unhandled diff marker: '%c'", marker);
+
+		add_word_diff_line(old, new, s->plain.buf + i, next - i,
+				   marker);
+		last_marker = marker;
+	}
+}
+
+static struct diff_filespec *word_diff_filespec(struct repository *r,
+						const char *name,
+						struct strbuf *buf)
+{
+	struct diff_filespec *spec = alloc_filespec(name);
+	size_t size;
+
+	fill_filespec(spec, null_oid(r->hash_algo), 0, 0100644);
+	spec->data = strbuf_detach(buf, &size);
+	spec->size = size;
+	spec->should_free = 1;
+	spec->is_stdin = 1;
+
+	return spec;
+}
+
+static void show_hunk_word_diff(struct add_p_state *s, struct hunk *hunk,
+				int colored)
+{
+	struct hunk_header *header = &hunk->header;
+	struct strbuf old = STRBUF_INIT, new = STRBUF_INIT;
+	struct diff_options opts;
+	struct diff_queue_struct queue;
+
+	if (!header->old_offset && !header->new_offset) {
+		strbuf_reset(&s->buf);
+		render_hunk(s, hunk, 0, colored, &s->buf);
+		fputs(s->buf.buf, stdout);
+		return;
+	}
+
+	build_word_diff_files(s, hunk, &old, &new);
+
+	repo_diff_setup(s->r, &opts);
+	opts.output_format = DIFF_FORMAT_PATCH;
+	opts.use_color = colored ? s->cfg.use_color_diff : GIT_COLOR_NEVER;
+	opts.word_diff = DIFF_WORDS_PLAIN;
+	opts.context = header->old_count > header->new_count ?
+		header->old_count : header->new_count;
+	opts.flags.suppress_diff_headers = 1;
+	diff_setup_done(&opts);
+
+	memcpy(&queue, &diff_queued_diff, sizeof(diff_queued_diff));
+	diff_queue_init(&diff_queued_diff);
+	diff_queue(&diff_queued_diff,
+		   word_diff_filespec(s->r, "a", &old),
+		   word_diff_filespec(s->r, "b", &new));
+	diffcore_std(&opts);
+	diff_flush(&opts);
+	memcpy(&diff_queued_diff, &queue, sizeof(diff_queued_diff));
+}
+
 #define DISPLAY_HUNKS_LINES 20
 static size_t display_hunks(struct add_p_state *s,
 			    struct file_diff *file_diff, size_t start_index)
@@ -1540,6 +1640,7 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
    "/ - search for a hunk matching the given regex\n"
    "s - split the current hunk into smaller hunks\n"
    "e - manually edit the current hunk\n"
+   "w - print the current hunk with word-diff\n"
    "p - print the current hunk\n"
    "P - print the current hunk using the pager\n"
    "> - go to the next file, roll over at the bottom\n"
@@ -1731,7 +1832,7 @@ static size_t patch_update_file(struct add_p_state *s,
 				permitted |= ALLOW_GOTO_PREVIOUS_FILE;
 				strbuf_addstr(&s->buf, ",<");
 			}
-			strbuf_addstr(&s->buf, ",p,P");
+			strbuf_addstr(&s->buf, ",w,p,P");
 		}
 		if (file_diff->deleted)
 			prompt_mode_type = PROMPT_DELETION;
@@ -1953,6 +2054,8 @@ static size_t patch_update_file(struct add_p_state *s,
 				hunk->use = USE_HUNK;
 				goto soft_increment;
 			}
+		} else if (s->answer.buf[0] == 'w') {
+			show_hunk_word_diff(s, hunk, colored);
 		} else if (ch == 'p') {
 			rendered_hunk_index = -1;
 			use_pager = (s->answer.buf[0] == 'P') ? 1 : 0;
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 6e120a4001..e1ce98d62b 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -48,8 +48,8 @@ test_expect_success 'unknown command' '
 	git add -N command &&
 	git diff command >expect &&
 	cat >>expect <<-EOF &&
-	(1/1) Stage addition [y,n,q,a,d,e,p,P,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
-	(1/1) Stage addition [y,n,q,a,d,e,p,P,?]?$SP
+	(1/1) Stage addition [y,n,q,a,d,e,w,p,P,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
+	(1/1) Stage addition [y,n,q,a,d,e,w,p,P,?]?$SP
 	EOF
 	git add -p -- command <command >actual 2>&1 &&
 	test_cmp expect actual
@@ -332,9 +332,9 @@ test_expect_success 'different prompts for mode change/deleted' '
 	git -c core.filemode=true add -p >actual &&
 	sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
 	cat >expect <<-\EOF &&
-	(1/1) Stage deletion [y,n,q,a,d,p,P,?]?
-	(1/2) Stage mode change [y,n,q,a,d,k,K,j,J,g,/,p,P,?]?
-	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]?
+	(1/1) Stage deletion [y,n,q,a,d,w,p,P,?]?
+	(1/2) Stage mode change [y,n,q,a,d,k,K,j,J,g,/,w,p,P,?]?
+	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]?
 	EOF
 	test_cmp expect actual.filtered
 '
@@ -521,13 +521,13 @@ test_expect_success 'split hunk setup' '
 test_expect_success 'goto hunk 1 with "g 1"' '
 	test_when_finished "git reset" &&
 	tr _ " " >expect <<-EOF &&
-	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? + 1:  -1,2 +1,3          +15
+	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? + 1:  -1,2 +1,3          +15
 	_ 2:  -2,4 +3,8          +21
 	go to which hunk? @@ -1,2 +1,3 @@
 	_10
 	+15
 	_20
-	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
+	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
 	EOF
 	test_write_lines s y g 1 | git add -p >actual &&
 	tail -n 7 <actual >actual.trimmed &&
@@ -540,7 +540,7 @@ test_expect_success 'goto hunk 1 with "g1"' '
 	_10
 	+15
 	_20
-	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
+	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
 	EOF
 	test_write_lines s y g1 | git add -p >actual &&
 	tail -n 4 <actual >actual.trimmed &&
@@ -550,11 +550,11 @@ test_expect_success 'goto hunk 1 with "g1"' '
 test_expect_success 'navigate to hunk via regex /pattern' '
 	test_when_finished "git reset" &&
 	tr _ " " >expect <<-EOF &&
-	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? @@ -1,2 +1,3 @@
+	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? @@ -1,2 +1,3 @@
 	_10
 	+15
 	_20
-	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
+	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
 	EOF
 	test_write_lines s y /1,2 | git add -p >actual &&
 	tail -n 5 <actual >actual.trimmed &&
@@ -567,7 +567,7 @@ test_expect_success 'navigate to hunk via regex / pattern' '
 	_10
 	+15
 	_20
-	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
+	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
 	EOF
 	test_write_lines s y / 1,2 | git add -p >actual &&
 	tail -n 4 <actual >actual.trimmed &&
@@ -579,27 +579,42 @@ test_expect_success 'print again the hunk' '
 	tr _ " " >expect <<-EOF &&
 	+15
 	 20
-	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? @@ -1,2 +1,3 @@
+	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? @@ -1,2 +1,3 @@
 	 10
 	+15
 	 20
-	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
+	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
 	EOF
 	test_write_lines s y g 1 p | git add -p >actual &&
 	tail -n 7 <actual >actual.trimmed &&
 	test_cmp expect actual.trimmed
 '
 
+test_expect_success 'print hunk with word-diff' '
+	test_when_finished "rm -rf word-diff-repo" &&
+	git init word-diff-repo &&
+	(
+		cd word-diff-repo &&
+		test_write_lines "alpha old beta" context >word-diff &&
+		git add word-diff &&
+		git commit -m word-diff &&
+		test_write_lines "alpha new beta" context >word-diff &&
+		test_write_lines w n | git add -p word-diff >actual &&
+		test_grep "alpha \\[-old-\\]{+new+} beta" actual &&
+		git diff --cached --exit-code
+	)
+'
+
 test_expect_success TTY 'print again the hunk (PAGER)' '
 	test_when_finished "git reset" &&
 	cat >expect <<-EOF &&
 	<GREEN>+<RESET><GREEN>15<RESET>
 	 20<RESET>
-	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
+	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
 	PAGER  10<RESET>
 	PAGER <GREEN>+<RESET><GREEN>15<RESET>
 	PAGER  20<RESET>
-	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>
+	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>
 	EOF
 	test_write_lines s y g 1 P |
 	(
@@ -796,21 +811,21 @@ test_expect_success 'colors can be overridden' '
 	<BLUE>+<RESET><BLUE>new<RESET>
 	<CYAN> more-context<RESET>
 	<BLUE>+<RESET><BLUE>another-one<RESET>
-	<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,P,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
+	<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,w,p,P,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
 	<MAGENTA>@@ -1,3 +1,3 @@<RESET>
 	<CYAN> context<RESET>
 	<BOLD>-old<RESET>
 	<BLUE>+<RESET><BLUE>new<RESET>
 	<CYAN> more-context<RESET>
-	<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
+	<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
 	<CYAN> more-context<RESET>
 	<BLUE>+<RESET><BLUE>another-one<RESET>
-	<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
+	<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
 	<CYAN> context<RESET>
 	<BOLD>-old<RESET>
 	<BLUE>+new<RESET>
 	<CYAN> more-context<RESET>
-	<YELLOW>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>
+	<YELLOW>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>
 	EOF
 	test_cmp expect actual
 '
@@ -1424,9 +1439,9 @@ test_expect_success 'invalid option s is rejected' '
 	test_write_lines j s q | git add -p >out &&
 	sed -ne "s/ @@.*//" -e "s/ \$//" -e "/^(/p" <out >actual &&
 	cat >expect <<-EOF &&
-	(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,p,P,?]?
-	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? Sorry, cannot split this hunk
-	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?
+	(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,w,p,P,?]?
+	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? Sorry, cannot split this hunk
+	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?
 	EOF
 	test_cmp expect actual
 '
-- 
2.54.0
PabloMay 7, 2026, 07:55 UTC in reply to Javier Bassi on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

El jue, 7 may 2026 a las 1:58, Javier Bassi (<javierbassi@gmail.com>) escribió:
Show 5 quoted lines
>
> When using `git add --patch`, reviewing changes in long lines can be
> difficult with the default line-based diff. This is particularly
> noticeable in formats such as JSONP, CSV, LaTeX, Markdown, or other
> plain text where small inline edits can be hard to spot.
Very good explanation. I think it's a good idea.
>
> Added `w - print the current hunk with word-diff` during hunk selection
  ^^^^^^^
Nit: following Documentation/SubmittingPatches, try to use an
imperative mood to write what has been done.
Show 52 quoted lines
> to re-display the current hunk using `--word-diff`. This provides a
> clearer inline view of changes without modifying the hunk or affecting
> how patches are applied or staged.
>
> Signed-off-by: Javier Bassi <javierbassi@gmail.com>
> ---
>  Documentation/git-add.adoc |   1 +
>  add-patch.c                | 105 ++++++++++++++++++++++++++++++++++++-
>  t/t3701-add-interactive.sh |  59 +++++++++++++--------
>  3 files changed, 142 insertions(+), 23 deletions(-)
>
> diff --git a/Documentation/git-add.adoc b/Documentation/git-add.adoc
> index 941135dc63..d2ee1cf9a9 100644
> --- a/Documentation/git-add.adoc
> +++ b/Documentation/git-add.adoc
> @@ -351,6 +351,7 @@ patch::
>         K - go to the previous hunk, roll over at the top
>         s - split the current hunk into smaller hunks
>         e - manually edit the current hunk
> +       w - print the current hunk with word-diff
>         p - print the current hunk
>         P - print the current hunk using the pager
>         ? - print help
> diff --git a/add-patch.c b/add-patch.c
> index f27edcbe8d..0364f4bc97 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -7,6 +7,7 @@
>  #include "commit.h"
>  #include "config.h"
>  #include "diff.h"
> +#include "diffcore.h"
>  #include "editor.h"
>  #include "environment.h"
>  #include "gettext.h"
> @@ -1508,6 +1509,105 @@ static void summarize_hunk(struct add_p_state *s, struct hunk *hunk,
>         strbuf_complete_line(out);
>  }
>
> +static void trim_trailing_lf(struct strbuf *buf)
> +{
> +       if (buf->len && buf->buf[buf->len - 1] == '\n')
> +               strbuf_setlen(buf, buf->len - 1);
> +}
> +
> +static void add_word_diff_line(struct strbuf *old, struct strbuf *new,
> +                              const char *line, size_t len, char marker)
> +{
> +       if (marker == '-' || marker == '+' || *line == ' ') {
> +               line++;
> +               len--;
> +       }

Maybe a tiny comment here would help, to know why '*line' is being checked here instead of 'marker'. They seem the same and one has to go to marker declaration and see the comment at 'normalize_marker()'

  /* Empty context lines may omit the leading ' ' */
Show 24 quoted lines
> +
> +       if (marker != '+')
> +               strbuf_add(old, line, len);
> +       if (marker != '-')
> +               strbuf_add(new, line, len);
> +}
> +
> +static void build_word_diff_files(struct add_p_state *s, struct hunk *hunk,
> +                                 struct strbuf *old, struct strbuf *new)
> +{
> +       size_t i;
> +       char last_marker = '\0';
> +
> +       for (i = hunk->start; i < hunk->end; i = find_next_line(&s->plain, i)) {
> +               size_t next = find_next_line(&s->plain, i);
> +               char marker = normalize_marker(s->plain.buf + i);
> +
> +               if (marker == '\\') {
> +                       if (last_marker != '+')
> +                               trim_trailing_lf(old);
> +                       if (last_marker != '-')
> +                               trim_trailing_lf(new);
> +                       continue;
> +               }

Here we check about "\No newline at end of file", after this point I believe that 'buf->buf[buf->len - 1] == '\n'' will always be true. Same should be for 'buf->len' because "\No newline at end of file" shouldn't come first and a '+' '-' line should have been added on a previous iteration, but the check it's fine, just in case I'm wrong.

What I want to point out is, is the 'trim_trailing_lf' function necessary? It's only called in the same place and it carries a check that could be on the caller instead, leaving the function only with 'strbuf_setlen(buf, buf->len - 1);" making sense to inline it at this point. You could keep the buf->len check:

  if (marker == '\\') {
          if (last_marker != '+' && old->len)
                   strbuf_setlen(old, old->len - 1);
          if (last_marker != '-' && new->len)
                   strbuf_setlen(new, new->len - 1);
          continue;
  }
Show 35 quoted lines
> +
> +               if (marker != ' ' && marker != '-' && marker != '+')
> +                       BUG("unhandled diff marker: '%c'", marker);
> +
> +               add_word_diff_line(old, new, s->plain.buf + i, next - i,
> +                                  marker);
> +               last_marker = marker;
> +       }
> +}
> +
> +static struct diff_filespec *word_diff_filespec(struct repository *r,
> +                                               const char *name,
> +                                               struct strbuf *buf)
> +{
> +       struct diff_filespec *spec = alloc_filespec(name);
> +       size_t size;
> +
> +       fill_filespec(spec, null_oid(r->hash_algo), 0, 0100644);
> +       spec->data = strbuf_detach(buf, &size);
> +       spec->size = size;
> +       spec->should_free = 1;
> +       spec->is_stdin = 1;
> +
> +       return spec;
> +}
> +
> +static void show_hunk_word_diff(struct add_p_state *s, struct hunk *hunk,
> +                               int colored)
> +{
> +       struct hunk_header *header = &hunk->header;
> +       struct strbuf old = STRBUF_INIT, new = STRBUF_INIT;
> +       struct diff_options opts;
> +       struct diff_queue_struct queue;
> +
> +       if (!header->old_offset && !header->new_offset) {
Nit: a comment here would help to understand this early return
Show 222 quoted lines
> +               strbuf_reset(&s->buf);
> +               render_hunk(s, hunk, 0, colored, &s->buf);
> +               fputs(s->buf.buf, stdout);
> +               return;
> +       }
> +
> +       build_word_diff_files(s, hunk, &old, &new);
> +
> +       repo_diff_setup(s->r, &opts);
> +       opts.output_format = DIFF_FORMAT_PATCH;
> +       opts.use_color = colored ? s->cfg.use_color_diff : GIT_COLOR_NEVER;
> +       opts.word_diff = DIFF_WORDS_PLAIN;
> +       opts.context = header->old_count > header->new_count ?
> +               header->old_count : header->new_count;
> +       opts.flags.suppress_diff_headers = 1;
> +       diff_setup_done(&opts);
> +
> +       memcpy(&queue, &diff_queued_diff, sizeof(diff_queued_diff));
> +       diff_queue_init(&diff_queued_diff);
> +       diff_queue(&diff_queued_diff,
> +                  word_diff_filespec(s->r, "a", &old),
> +                  word_diff_filespec(s->r, "b", &new));
> +       diffcore_std(&opts);
> +       diff_flush(&opts);
> +       memcpy(&diff_queued_diff, &queue, sizeof(diff_queued_diff));
> +}
> +
>  #define DISPLAY_HUNKS_LINES 20
>  static size_t display_hunks(struct add_p_state *s,
>                             struct file_diff *file_diff, size_t start_index)
> @@ -1540,6 +1640,7 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
>     "/ - search for a hunk matching the given regex\n"
>     "s - split the current hunk into smaller hunks\n"
>     "e - manually edit the current hunk\n"
> +   "w - print the current hunk with word-diff\n"
>     "p - print the current hunk\n"
>     "P - print the current hunk using the pager\n"
>     "> - go to the next file, roll over at the bottom\n"
> @@ -1731,7 +1832,7 @@ static size_t patch_update_file(struct add_p_state *s,
>                                 permitted |= ALLOW_GOTO_PREVIOUS_FILE;
>                                 strbuf_addstr(&s->buf, ",<");
>                         }
> -                       strbuf_addstr(&s->buf, ",p,P");
> +                       strbuf_addstr(&s->buf, ",w,p,P");
>                 }
>                 if (file_diff->deleted)
>                         prompt_mode_type = PROMPT_DELETION;
> @@ -1953,6 +2054,8 @@ static size_t patch_update_file(struct add_p_state *s,
>                                 hunk->use = USE_HUNK;
>                                 goto soft_increment;
>                         }
> +               } else if (s->answer.buf[0] == 'w') {
> +                       show_hunk_word_diff(s, hunk, colored);
>                 } else if (ch == 'p') {
>                         rendered_hunk_index = -1;
>                         use_pager = (s->answer.buf[0] == 'P') ? 1 : 0;
> diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> index 6e120a4001..e1ce98d62b 100755
> --- a/t/t3701-add-interactive.sh
> +++ b/t/t3701-add-interactive.sh
> @@ -48,8 +48,8 @@ test_expect_success 'unknown command' '
>         git add -N command &&
>         git diff command >expect &&
>         cat >>expect <<-EOF &&
> -       (1/1) Stage addition [y,n,q,a,d,e,p,P,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
> -       (1/1) Stage addition [y,n,q,a,d,e,p,P,?]?$SP
> +       (1/1) Stage addition [y,n,q,a,d,e,w,p,P,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
> +       (1/1) Stage addition [y,n,q,a,d,e,w,p,P,?]?$SP
>         EOF
>         git add -p -- command <command >actual 2>&1 &&
>         test_cmp expect actual
> @@ -332,9 +332,9 @@ test_expect_success 'different prompts for mode change/deleted' '
>         git -c core.filemode=true add -p >actual &&
>         sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
>         cat >expect <<-\EOF &&
> -       (1/1) Stage deletion [y,n,q,a,d,p,P,?]?
> -       (1/2) Stage mode change [y,n,q,a,d,k,K,j,J,g,/,p,P,?]?
> -       (2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]?
> +       (1/1) Stage deletion [y,n,q,a,d,w,p,P,?]?
> +       (1/2) Stage mode change [y,n,q,a,d,k,K,j,J,g,/,w,p,P,?]?
> +       (2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]?
>         EOF
>         test_cmp expect actual.filtered
>  '
> @@ -521,13 +521,13 @@ test_expect_success 'split hunk setup' '
>  test_expect_success 'goto hunk 1 with "g 1"' '
>         test_when_finished "git reset" &&
>         tr _ " " >expect <<-EOF &&
> -       (2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? + 1:  -1,2 +1,3          +15
> +       (2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? + 1:  -1,2 +1,3          +15
>         _ 2:  -2,4 +3,8          +21
>         go to which hunk? @@ -1,2 +1,3 @@
>         _10
>         +15
>         _20
> -       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>         EOF
>         test_write_lines s y g 1 | git add -p >actual &&
>         tail -n 7 <actual >actual.trimmed &&
> @@ -540,7 +540,7 @@ test_expect_success 'goto hunk 1 with "g1"' '
>         _10
>         +15
>         _20
> -       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>         EOF
>         test_write_lines s y g1 | git add -p >actual &&
>         tail -n 4 <actual >actual.trimmed &&
> @@ -550,11 +550,11 @@ test_expect_success 'goto hunk 1 with "g1"' '
>  test_expect_success 'navigate to hunk via regex /pattern' '
>         test_when_finished "git reset" &&
>         tr _ " " >expect <<-EOF &&
> -       (2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? @@ -1,2 +1,3 @@
> +       (2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? @@ -1,2 +1,3 @@
>         _10
>         +15
>         _20
> -       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>         EOF
>         test_write_lines s y /1,2 | git add -p >actual &&
>         tail -n 5 <actual >actual.trimmed &&
> @@ -567,7 +567,7 @@ test_expect_success 'navigate to hunk via regex / pattern' '
>         _10
>         +15
>         _20
> -       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>         EOF
>         test_write_lines s y / 1,2 | git add -p >actual &&
>         tail -n 4 <actual >actual.trimmed &&
> @@ -579,27 +579,42 @@ test_expect_success 'print again the hunk' '
>         tr _ " " >expect <<-EOF &&
>         +15
>          20
> -       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? @@ -1,2 +1,3 @@
> +       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? @@ -1,2 +1,3 @@
>          10
>         +15
>          20
> -       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +       (1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>         EOF
>         test_write_lines s y g 1 p | git add -p >actual &&
>         tail -n 7 <actual >actual.trimmed &&
>         test_cmp expect actual.trimmed
>  '
>
> +test_expect_success 'print hunk with word-diff' '
> +       test_when_finished "rm -rf word-diff-repo" &&
> +       git init word-diff-repo &&
> +       (
> +               cd word-diff-repo &&
> +               test_write_lines "alpha old beta" context >word-diff &&
> +               git add word-diff &&
> +               git commit -m word-diff &&
> +               test_write_lines "alpha new beta" context >word-diff &&
> +               test_write_lines w n | git add -p word-diff >actual &&
> +               test_grep "alpha \\[-old-\\]{+new+} beta" actual &&
> +               git diff --cached --exit-code
> +       )
> +'
> +
>  test_expect_success TTY 'print again the hunk (PAGER)' '
>         test_when_finished "git reset" &&
>         cat >expect <<-EOF &&
>         <GREEN>+<RESET><GREEN>15<RESET>
>          20<RESET>
> -       <BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
> +       <BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
>         PAGER  10<RESET>
>         PAGER <GREEN>+<RESET><GREEN>15<RESET>
>         PAGER  20<RESET>
> -       <BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>
> +       <BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>
>         EOF
>         test_write_lines s y g 1 P |
>         (
> @@ -796,21 +811,21 @@ test_expect_success 'colors can be overridden' '
>         <BLUE>+<RESET><BLUE>new<RESET>
>         <CYAN> more-context<RESET>
>         <BLUE>+<RESET><BLUE>another-one<RESET>
> -       <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,P,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
> +       <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,w,p,P,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
>         <MAGENTA>@@ -1,3 +1,3 @@<RESET>
>         <CYAN> context<RESET>
>         <BOLD>-old<RESET>
>         <BLUE>+<RESET><BLUE>new<RESET>
>         <CYAN> more-context<RESET>
> -       <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
> +       <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
>         <CYAN> more-context<RESET>
>         <BLUE>+<RESET><BLUE>another-one<RESET>
> -       <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
> +       <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
>         <CYAN> context<RESET>
>         <BOLD>-old<RESET>
>         <BLUE>+new<RESET>
>         <CYAN> more-context<RESET>
> -       <YELLOW>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>
> +       <YELLOW>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>
>         EOF
>         test_cmp expect actual
>  '
> @@ -1424,9 +1439,9 @@ test_expect_success 'invalid option s is rejected' '
>         test_write_lines j s q | git add -p >out &&
>         sed -ne "s/ @@.*//" -e "s/ \$//" -e "/^(/p" <out >actual &&
>         cat >expect <<-EOF &&
> -       (1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,p,P,?]?
> -       (2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? Sorry, cannot split this hunk
> -       (2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?
> +       (1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,w,p,P,?]?
> +       (2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? Sorry, cannot split this hunk
> +       (2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?
>         EOF
>         test_cmp expect actual
>  '
> --
> 2.54.0
>
>

I tried and found out that if a character is contiguous to a word diff it will be carried as part of the diff. e.g.:

  diff --git a/file b/file
  index f33f47f..93dd39c 100644
  --- a/file
  +++ b/file
  @@ -1 +1 @@
  -foo, bar
  +baz, bar
  (1/1) Stage this hunk [y,n,q,a,d,e,w,p,P,?]? w
  @@ -1 +1 @@
  [-foo,-]{+baz,+} bar
the ',' remains at the same place but became part of the diff which adds noise.

Regards, -- Pablo

Phillip WoodMay 7, 2026, 13:24 UTC in reply to Javier Bassi on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

Hi Javier
On 07/05/2026 00:54, Javier Bassi wrote:
Show 9 quoted lines
> When using `git add --patch`, reviewing changes in long lines can be
> difficult with the default line-based diff. This is particularly
> noticeable in formats such as JSONP, CSV, LaTeX, Markdown, or other
> plain text where small inline edits can be hard to spot.
> 
> Added `w - print the current hunk with word-diff` during hunk selection
> to re-display the current hunk using `--word-diff`. This provides a
> clearer inline view of changes without modifying the hunk or affecting
> how patches are applied or staged.

I think this is useful. While one can set interactive.diffFilter to highlight intraline differences with "diff-highlight" or "delta" they do not cope well if the text has been reflowed.

I think the approach of using the current hunk text, rather than running "git diff --diff-words" is sensible because it works correctly if the user has edited the hunk and it makes it easy to handle hunks that have been split. It would be good to propagate the filename when creating the filespec so that we use the correct word diff regex for the file. We should also propagate the context and inter-hunk context settings from struct interactive_options. It would also be a good idea to reuse the hunk header from the unified diff so that the line numbers and hunk fragment are correct.

Thanks
Phillip
Show 325 quoted lines
> Signed-off-by: Javier Bassi <javierbassi@gmail.com>
> ---
>   Documentation/git-add.adoc |   1 +
>   add-patch.c                | 105 ++++++++++++++++++++++++++++++++++++-
>   t/t3701-add-interactive.sh |  59 +++++++++++++--------
>   3 files changed, 142 insertions(+), 23 deletions(-)
> 
> diff --git a/Documentation/git-add.adoc b/Documentation/git-add.adoc
> index 941135dc63..d2ee1cf9a9 100644
> --- a/Documentation/git-add.adoc
> +++ b/Documentation/git-add.adoc
> @@ -351,6 +351,7 @@ patch::
>          K - go to the previous hunk, roll over at the top
>          s - split the current hunk into smaller hunks
>          e - manually edit the current hunk
> +       w - print the current hunk with word-diff
>          p - print the current hunk
>          P - print the current hunk using the pager
>          ? - print help
> diff --git a/add-patch.c b/add-patch.c
> index f27edcbe8d..0364f4bc97 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -7,6 +7,7 @@
>   #include "commit.h"
>   #include "config.h"
>   #include "diff.h"
> +#include "diffcore.h"
>   #include "editor.h"
>   #include "environment.h"
>   #include "gettext.h"
> @@ -1508,6 +1509,105 @@ static void summarize_hunk(struct add_p_state *s, struct hunk *hunk,
>   	strbuf_complete_line(out);
>   }
>   
> +static void trim_trailing_lf(struct strbuf *buf)
> +{
> +	if (buf->len && buf->buf[buf->len - 1] == '\n')
> +		strbuf_setlen(buf, buf->len - 1);
> +}
> +
> +static void add_word_diff_line(struct strbuf *old, struct strbuf *new,
> +			       const char *line, size_t len, char marker)
> +{
> +	if (marker == '-' || marker == '+' || *line == ' ') {
> +		line++;
> +		len--;
> +	}
> +
> +	if (marker != '+')
> +		strbuf_add(old, line, len);
> +	if (marker != '-')
> +		strbuf_add(new, line, len);
> +}
> +
> +static void build_word_diff_files(struct add_p_state *s, struct hunk *hunk,
> +				  struct strbuf *old, struct strbuf *new)
> +{
> +	size_t i;
> +	char last_marker = '\0';
> +
> +	for (i = hunk->start; i < hunk->end; i = find_next_line(&s->plain, i)) {
> +		size_t next = find_next_line(&s->plain, i);
> +		char marker = normalize_marker(s->plain.buf + i);
> +
> +		if (marker == '\\') {
> +			if (last_marker != '+')
> +				trim_trailing_lf(old);
> +			if (last_marker != '-')
> +				trim_trailing_lf(new);
> +			continue;
> +		}
> +
> +		if (marker != ' ' && marker != '-' && marker != '+')
> +			BUG("unhandled diff marker: '%c'", marker);
> +
> +		add_word_diff_line(old, new, s->plain.buf + i, next - i,
> +				   marker);
> +		last_marker = marker;
> +	}
> +}
> +
> +static struct diff_filespec *word_diff_filespec(struct repository *r,
> +						const char *name,
> +						struct strbuf *buf)
> +{
> +	struct diff_filespec *spec = alloc_filespec(name);
> +	size_t size;
> +
> +	fill_filespec(spec, null_oid(r->hash_algo), 0, 0100644);
> +	spec->data = strbuf_detach(buf, &size);
> +	spec->size = size;
> +	spec->should_free = 1;
> +	spec->is_stdin = 1;
> +
> +	return spec;
> +}
> +
> +static void show_hunk_word_diff(struct add_p_state *s, struct hunk *hunk,
> +				int colored)
> +{
> +	struct hunk_header *header = &hunk->header;
> +	struct strbuf old = STRBUF_INIT, new = STRBUF_INIT;
> +	struct diff_options opts;
> +	struct diff_queue_struct queue;
> +
> +	if (!header->old_offset && !header->new_offset) {
> +		strbuf_reset(&s->buf);
> +		render_hunk(s, hunk, 0, colored, &s->buf);
> +		fputs(s->buf.buf, stdout);
> +		return;
> +	}
> +
> +	build_word_diff_files(s, hunk, &old, &new);
> +
> +	repo_diff_setup(s->r, &opts);
> +	opts.output_format = DIFF_FORMAT_PATCH;
> +	opts.use_color = colored ? s->cfg.use_color_diff : GIT_COLOR_NEVER;
> +	opts.word_diff = DIFF_WORDS_PLAIN;
> +	opts.context = header->old_count > header->new_count ?
> +		header->old_count : header->new_count;
> +	opts.flags.suppress_diff_headers = 1;
> +	diff_setup_done(&opts);
> +
> +	memcpy(&queue, &diff_queued_diff, sizeof(diff_queued_diff));
> +	diff_queue_init(&diff_queued_diff);
> +	diff_queue(&diff_queued_diff,
> +		   word_diff_filespec(s->r, "a", &old),
> +		   word_diff_filespec(s->r, "b", &new));
> +	diffcore_std(&opts);
> +	diff_flush(&opts);
> +	memcpy(&diff_queued_diff, &queue, sizeof(diff_queued_diff));
> +}
> +
>   #define DISPLAY_HUNKS_LINES 20
>   static size_t display_hunks(struct add_p_state *s,
>   			    struct file_diff *file_diff, size_t start_index)
> @@ -1540,6 +1640,7 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
>      "/ - search for a hunk matching the given regex\n"
>      "s - split the current hunk into smaller hunks\n"
>      "e - manually edit the current hunk\n"
> +   "w - print the current hunk with word-diff\n"
>      "p - print the current hunk\n"
>      "P - print the current hunk using the pager\n"
>      "> - go to the next file, roll over at the bottom\n"
> @@ -1731,7 +1832,7 @@ static size_t patch_update_file(struct add_p_state *s,
>   				permitted |= ALLOW_GOTO_PREVIOUS_FILE;
>   				strbuf_addstr(&s->buf, ",<");
>   			}
> -			strbuf_addstr(&s->buf, ",p,P");
> +			strbuf_addstr(&s->buf, ",w,p,P");
>   		}
>   		if (file_diff->deleted)
>   			prompt_mode_type = PROMPT_DELETION;
> @@ -1953,6 +2054,8 @@ static size_t patch_update_file(struct add_p_state *s,
>   				hunk->use = USE_HUNK;
>   				goto soft_increment;
>   			}
> +		} else if (s->answer.buf[0] == 'w') {
> +			show_hunk_word_diff(s, hunk, colored);
>   		} else if (ch == 'p') {
>   			rendered_hunk_index = -1;
>   			use_pager = (s->answer.buf[0] == 'P') ? 1 : 0;
> diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> index 6e120a4001..e1ce98d62b 100755
> --- a/t/t3701-add-interactive.sh
> +++ b/t/t3701-add-interactive.sh
> @@ -48,8 +48,8 @@ test_expect_success 'unknown command' '
>   	git add -N command &&
>   	git diff command >expect &&
>   	cat >>expect <<-EOF &&
> -	(1/1) Stage addition [y,n,q,a,d,e,p,P,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
> -	(1/1) Stage addition [y,n,q,a,d,e,p,P,?]?$SP
> +	(1/1) Stage addition [y,n,q,a,d,e,w,p,P,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
> +	(1/1) Stage addition [y,n,q,a,d,e,w,p,P,?]?$SP
>   	EOF
>   	git add -p -- command <command >actual 2>&1 &&
>   	test_cmp expect actual
> @@ -332,9 +332,9 @@ test_expect_success 'different prompts for mode change/deleted' '
>   	git -c core.filemode=true add -p >actual &&
>   	sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
>   	cat >expect <<-\EOF &&
> -	(1/1) Stage deletion [y,n,q,a,d,p,P,?]?
> -	(1/2) Stage mode change [y,n,q,a,d,k,K,j,J,g,/,p,P,?]?
> -	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]?
> +	(1/1) Stage deletion [y,n,q,a,d,w,p,P,?]?
> +	(1/2) Stage mode change [y,n,q,a,d,k,K,j,J,g,/,w,p,P,?]?
> +	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]?
>   	EOF
>   	test_cmp expect actual.filtered
>   '
> @@ -521,13 +521,13 @@ test_expect_success 'split hunk setup' '
>   test_expect_success 'goto hunk 1 with "g 1"' '
>   	test_when_finished "git reset" &&
>   	tr _ " " >expect <<-EOF &&
> -	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? + 1:  -1,2 +1,3          +15
> +	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? + 1:  -1,2 +1,3          +15
>   	_ 2:  -2,4 +3,8          +21
>   	go to which hunk? @@ -1,2 +1,3 @@
>   	_10
>   	+15
>   	_20
> -	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>   	EOF
>   	test_write_lines s y g 1 | git add -p >actual &&
>   	tail -n 7 <actual >actual.trimmed &&
> @@ -540,7 +540,7 @@ test_expect_success 'goto hunk 1 with "g1"' '
>   	_10
>   	+15
>   	_20
> -	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>   	EOF
>   	test_write_lines s y g1 | git add -p >actual &&
>   	tail -n 4 <actual >actual.trimmed &&
> @@ -550,11 +550,11 @@ test_expect_success 'goto hunk 1 with "g1"' '
>   test_expect_success 'navigate to hunk via regex /pattern' '
>   	test_when_finished "git reset" &&
>   	tr _ " " >expect <<-EOF &&
> -	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? @@ -1,2 +1,3 @@
> +	(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? @@ -1,2 +1,3 @@
>   	_10
>   	+15
>   	_20
> -	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>   	EOF
>   	test_write_lines s y /1,2 | git add -p >actual &&
>   	tail -n 5 <actual >actual.trimmed &&
> @@ -567,7 +567,7 @@ test_expect_success 'navigate to hunk via regex / pattern' '
>   	_10
>   	+15
>   	_20
> -	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>   	EOF
>   	test_write_lines s y / 1,2 | git add -p >actual &&
>   	tail -n 4 <actual >actual.trimmed &&
> @@ -579,27 +579,42 @@ test_expect_success 'print again the hunk' '
>   	tr _ " " >expect <<-EOF &&
>   	+15
>   	 20
> -	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? @@ -1,2 +1,3 @@
> +	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? @@ -1,2 +1,3 @@
>   	 10
>   	+15
>   	 20
> -	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?_
> +	(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?_
>   	EOF
>   	test_write_lines s y g 1 p | git add -p >actual &&
>   	tail -n 7 <actual >actual.trimmed &&
>   	test_cmp expect actual.trimmed
>   '
>   
> +test_expect_success 'print hunk with word-diff' '
> +	test_when_finished "rm -rf word-diff-repo" &&
> +	git init word-diff-repo &&
> +	(
> +		cd word-diff-repo &&
> +		test_write_lines "alpha old beta" context >word-diff &&
> +		git add word-diff &&
> +		git commit -m word-diff &&
> +		test_write_lines "alpha new beta" context >word-diff &&
> +		test_write_lines w n | git add -p word-diff >actual &&
> +		test_grep "alpha \\[-old-\\]{+new+} beta" actual &&
> +		git diff --cached --exit-code
> +	)
> +'
> +
>   test_expect_success TTY 'print again the hunk (PAGER)' '
>   	test_when_finished "git reset" &&
>   	cat >expect <<-EOF &&
>   	<GREEN>+<RESET><GREEN>15<RESET>
>   	 20<RESET>
> -	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
> +	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
>   	PAGER  10<RESET>
>   	PAGER <GREEN>+<RESET><GREEN>15<RESET>
>   	PAGER  20<RESET>
> -	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>
> +	<BOLD;BLUE>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>
>   	EOF
>   	test_write_lines s y g 1 P |
>   	(
> @@ -796,21 +811,21 @@ test_expect_success 'colors can be overridden' '
>   	<BLUE>+<RESET><BLUE>new<RESET>
>   	<CYAN> more-context<RESET>
>   	<BLUE>+<RESET><BLUE>another-one<RESET>
> -	<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,p,P,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
> +	<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,w,p,P,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
>   	<MAGENTA>@@ -1,3 +1,3 @@<RESET>
>   	<CYAN> context<RESET>
>   	<BOLD>-old<RESET>
>   	<BLUE>+<RESET><BLUE>new<RESET>
>   	<CYAN> more-context<RESET>
> -	<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
> +	<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
>   	<CYAN> more-context<RESET>
>   	<BLUE>+<RESET><BLUE>another-one<RESET>
> -	<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,P,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
> +	<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,w,p,P,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
>   	<CYAN> context<RESET>
>   	<BOLD>-old<RESET>
>   	<BLUE>+new<RESET>
>   	<CYAN> more-context<RESET>
> -	<YELLOW>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? <RESET>
> +	<YELLOW>(1/2) Stage this hunk (was: y) [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? <RESET>
>   	EOF
>   	test_cmp expect actual
>   '
> @@ -1424,9 +1439,9 @@ test_expect_success 'invalid option s is rejected' '
>   	test_write_lines j s q | git add -p >out &&
>   	sed -ne "s/ @@.*//" -e "s/ \$//" -e "/^(/p" <out >actual &&
>   	cat >expect <<-EOF &&
> -	(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,p,P,?]?
> -	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]? Sorry, cannot split this hunk
> -	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?
> +	(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,s,e,w,p,P,?]?
> +	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]? Sorry, cannot split this hunk
> +	(2/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,w,p,P,?]?
>   	EOF
>   	test_cmp expect actual
>   '
Phillip WoodMay 7, 2026, 13:24 UTC in reply to Pablo on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

On 07/05/2026 08:55, Pablo wrote:
Show 15 quoted lines
> El jue, 7 may 2026 a las 1:58, Javier Bassi (<javierbassi@gmail.com>) escribió:
>>
>> +static void add_word_diff_line(struct strbuf *old, struct strbuf *new,
>> +                              const char *line, size_t len, char marker)
>> +{
>> +       if (marker == '-' || marker == '+' || *line == ' ') {
>> +               line++;
>> +               len--;
>> +       }
> 
> Maybe a tiny comment here would help, to know why '*line' is being
> checked here instead of 'marker'. They seem the same and one has to go
> to marker declaration and see the comment at 'normalize_marker()'
> 
>    /* Empty context lines may omit the leading ' ' */
That's a good point - it might be clearer to use
	if (marker == *line) {
		line++;
		len--;
	}

instead. That also trims lines starting with '\' but that shouldn't matter as the code should be checking "marker" rather than "line".

Show 29 quoted lines
>> +
>> +               if (marker == '\\') {
>> +                       if (last_marker != '+')
>> +                               trim_trailing_lf(old);
>> +                       if (last_marker != '-')
>> +                               trim_trailing_lf(new);
>> +                       continue;
>> +               }
> 
> Here we check about "\No newline at end of file", after this point I
> believe that 'buf->buf[buf->len - 1] == '\n'' will always be true.
> Same should be for 'buf->len' because "\No newline at end of file"
> shouldn't come first and a '+' '-' line should have been added on a
> previous iteration, but the check it's fine, just in case I'm wrong.
> 
> What I want to point out is, is the 'trim_trailing_lf' function
> necessary? It's only called in the same place and it carries a check
> that could be on the caller instead, leaving  the function only with
> 'strbuf_setlen(buf, buf->len - 1);" making sense to inline it at this
> point.
> You could keep the buf->len check:
> 
>    if (marker == '\\') {
>            if (last_marker != '+' && old->len)
>                     strbuf_setlen(old, old->len - 1);
>            if (last_marker != '-' && new->len)
>                     strbuf_setlen(new, new->len - 1);
>            continue;
>    }
Should we be trimming '\r\n' if the file has dos style line endings?
Thanks
Phillip
D. Ben KnobleMay 7, 2026, 14:39 UTC in reply to Phillip Wood on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

On Thu, May 7, 2026 at 9:28 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 17 quoted lines
>
> Hi Javier
>
> On 07/05/2026 00:54, Javier Bassi wrote:
> > When using `git add --patch`, reviewing changes in long lines can be
> > difficult with the default line-based diff. This is particularly
> > noticeable in formats such as JSONP, CSV, LaTeX, Markdown, or other
> > plain text where small inline edits can be hard to spot.
> >
> > Added `w - print the current hunk with word-diff` during hunk selection
> > to re-display the current hunk using `--word-diff`. This provides a
> > clearer inline view of changes without modifying the hunk or affecting
> > how patches are applied or staged.
>
> I think this is useful. While one can set interactive.diffFilter to
> highlight intraline differences with "diff-highlight" or "delta" they do
> not cope well if the text has been reflowed.

I concur, though I wonder what other diff options would be useful. I can imagine a world where "add --patch" allows to reshow the patch with any set of custom options. Spitballing…

     :show <opts>

…could work, since we have room to allow long-form commands still. This could be extended to arbitrary git-* commands, and ":!cmd" could be used for shelling out.

(Can you tell I use Vim?)

Anyway, what I like about this is we don't have to worry about running out of (or thinking up new mnemonics for) single letters for different combinations of diff options, so it's a bit more extensible.

Show 13 quoted lines
> I think the approach of using the current hunk text, rather than running
> "git diff --diff-words" is sensible because it works correctly if the
> user has edited the hunk and it makes it easy to handle hunks that have
> been split. It would be good to propagate the filename when creating the
> filespec so that we use the correct word diff regex for the file. We
> should also propagate the context and inter-hunk context settings from
> struct interactive_options. It would also be a good idea to reuse the
> hunk header from the unified diff so that the line numbers and hunk
> fragment are correct.
>
> Thanks
>
> Phillip
-- 
D. Ben Knoble
PabloMay 7, 2026, 14:53 UTC in reply to Phillip Wood on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

El jue, 7 may 2026 a las 15:24, Phillip Wood (<phillip.wood123@gmail.com>) escribió:

Show 27 quoted lines
>
> On 07/05/2026 08:55, Pablo wrote:
> > El jue, 7 may 2026 a las 1:58, Javier Bassi (<javierbassi@gmail.com>) escribió:
> >>
> >> +static void add_word_diff_line(struct strbuf *old, struct strbuf *new,
> >> +                              const char *line, size_t len, char marker)
> >> +{
> >> +       if (marker == '-' || marker == '+' || *line == ' ') {
> >> +               line++;
> >> +               len--;
> >> +       }
> >
> > Maybe a tiny comment here would help, to know why '*line' is being
> > checked here instead of 'marker'. They seem the same and one has to go
> > to marker declaration and see the comment at 'normalize_marker()'
> >
> >    /* Empty context lines may omit the leading ' ' */
>
> That's a good point - it might be clearer to use
>
>         if (marker == *line) {
>                 line++;
>                 len--;
>         }
>
> instead. That also trims lines starting with '\' but that shouldn't
> matter as the code should be checking "marker" rather than "line".

Yeah, I like "marker == *line". About lines starting with '\', "add_word_diff_line()" is called after "marker == '\\'" block which ends in "continue;" skipping "add_word_diff_line()" call when marker == '\\'.

Show 32 quoted lines
>
> >> +
> >> +               if (marker == '\\') {
> >> +                       if (last_marker != '+')
> >> +                               trim_trailing_lf(old);
> >> +                       if (last_marker != '-')
> >> +                               trim_trailing_lf(new);
> >> +                       continue;
> >> +               }
> >
> > Here we check about "\No newline at end of file", after this point I
> > believe that 'buf->buf[buf->len - 1] == '\n'' will always be true.
> > Same should be for 'buf->len' because "\No newline at end of file"
> > shouldn't come first and a '+' '-' line should have been added on a
> > previous iteration, but the check it's fine, just in case I'm wrong.
> >
> > What I want to point out is, is the 'trim_trailing_lf' function
> > necessary? It's only called in the same place and it carries a check
> > that could be on the caller instead, leaving  the function only with
> > 'strbuf_setlen(buf, buf->len - 1);" making sense to inline it at this
> > point.
> > You could keep the buf->len check:
> >
> >    if (marker == '\\') {
> >            if (last_marker != '+' && old->len)
> >                     strbuf_setlen(old, old->len - 1);
> >            if (last_marker != '-' && new->len)
> >                     strbuf_setlen(new, new->len - 1);
> >            continue;
> >    }
>
> Should we be trimming '\r\n' if the file has dos style line endings?

True, I haven't thought of that. Then I take back what I said about inlining it, I'd keep the helper just as is (same for the if (marker == '\\' block)) adding a check for '\r' after trimming '\n'.

-- Pablo

Show 5 quoted lines
>
> Thanks
>
> Phillip
>
Junio C HamanoMay 11, 2026, 00:16 UTC in reply to D. Ben Knoble on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

"D. Ben Knoble" <ben.knoble@gmail.com> writes:
Show 14 quoted lines
> I concur, though I wonder what other diff options would be useful. I
> can imagine a world where "add --patch" allows to reshow the patch
> with any set of custom options. Spitballing…
>
>      :show <opts>
>
> …could work, since we have room to allow long-form commands still.
> This could be extended to arbitrary git-* commands, and ":!cmd" could
> be used for shelling out.
> (Can you tell I use Vim?)
>
> Anyway, what I like about this is we don't have to worry about running
> out of (or thinking up new mnemonics for) single letters for different
> combinations of diff options, so it's a bit more extensible.

If you mean "don't waste a new letter, like 'w', every time you come up with a use case to show the patch hunk differently", I had the same reaction to suggest reusing 'p' but allow options after it, just like the output from "git diff" can be modified by giving it an option "--word-diff". But how would it work for single-key folks (I am not one of them so I have less sympathy than I should in this case, but still...).

D. Ben KnobleMay 11, 2026, 19:16 UTC in reply to Junio C Hamano on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

On Sun, May 10, 2026 at 8:16 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 21 quoted lines
>
> "D. Ben Knoble" <ben.knoble@gmail.com> writes:
>
> > I concur, though I wonder what other diff options would be useful. I
> > can imagine a world where "add --patch" allows to reshow the patch
> > with any set of custom options. Spitballing…
> >
> >      :show <opts>
> >
> > …could work, since we have room to allow long-form commands still.
> > This could be extended to arbitrary git-* commands, and ":!cmd" could
> > be used for shelling out.
> > (Can you tell I use Vim?)
> >
> > Anyway, what I like about this is we don't have to worry about running
> > out of (or thinking up new mnemonics for) single letters for different
> > combinations of diff options, so it's a bit more extensible.
>
>
> If you mean "don't waste a new letter, like 'w', every time you come
> up with a use case to show the patch hunk differently",
Precisely
Show 6 quoted lines
> I had the
> same reaction to suggest reusing 'p' but allow options after it,
> just like the output from "git diff" can be modified by giving it an
> option "--word-diff".  But how would it work for single-key folks (I
> am not one of them so I have less sympathy than I should in this
> case, but still...).
Ah, I hadn't remembered we already had p/P. Perhaps
- in non-single-key mode, p/P can take options.
and/or
- ":" is an exception to single-key mode and always allows more text
to compose a longer-form "command". The first command could be "print"
which takes options of this kind.

Since ":" would be new, exempting it from single-key mode shouldn't be too jarring I guess.

-- 
D. Ben Knoble
Junio C HamanoMay 12, 2026, 00:04 UTC in reply to D. Ben Knoble on lore

Re: [PATCH] add -p: introduce 'w' command to view hunk with --word-diff

"D. Ben Knoble" <ben.knoble@gmail.com> writes:
Show 8 quoted lines
>> >      :show <opts>
> ...
> - ":" is an exception to single-key mode and always allows more text
> to compose a longer-form "command". The first command could be "print"
> which takes options of this kind.
>
> Since ":" would be new, exempting it from single-key mode shouldn't be
> too jarring I guess.

OK. And we can use ':blah' to rarely used actions and give them descriptive names. Makes sense. word-diff sounds like a good candidate but it is not immediately obvious to me if machinery to set up the "colon is special and handles longhand commands with possible arguments" is worth doing.

Back to recent threads