Git development
 help / color / mirror / Atom feed
* [PATCH 00/12] revision: learn -Z for NUL termination
@ 2026-12-02 22:14 kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 01/12] t4205: use correct 'setup' keyword kristofferhaugsbakk
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

Topic name: kh/revision-nul-termination

Topic summary: Commands in the git-rev-list(1) family support NUL
delimiting commits. NUL delimiting is handy when you want to process each
commit at a time (cf. just run through all via a pipeline). But this NUL
delimit support lacks in ergonomics, is fragile, and inconsistent.
Introduce `-Z` which NUL terminates commits and is decoupled
from the NUL handling internal to diffs.

This started when I wanted to replace git-format-rev(1) with simply using
git-diff-tree(1) with some options that I would have never guessed. But I
found some problems:[1]

    But I was wrong again. git-diff-tree(1) supports NUL for pretty printing
    (`-v`). I was only looking at one single output. But with “separator”
    delimiters (either newline or NUL) there is no delimiter for one single
    record (output). That’s why it didn’t show up for me. (thanks `cat -A`)

    But the separator behavior is a problem. Because when consuming input
    piecemeal (like reading from a shell coproc) you need a terminator. But
    the `log-tree.c` functionality very diligently keeps itself to using
    separators:

    1. You feed one commit: you get the output with no delimiter
    2. You feed another one: now there are two output records so a delimiter
       is needed. *Then* a separator is printed right before the record
    3. Repeat
    4. Of course the final record will not have a delimiter at the end

    I experimented with forcing terminator behavior but it seems that
    git-diff-tree(1) is just unwilling.

This patch series adds `-Z`:

• NUL delimiter
• Termination. This is needed when you want to read each record and act on
  it. Reading up until NUL avoids deadlock risks.[2]
  • Cf. separator which does not add a delimiter after the final record
  • Also, every separator is emitted “lazily”, at the start of the *next*
    record
  • The standard logic chooses separator if no custom format and terminator
    for custom format. (You can also get separator with a custom format with
    `--pretty=format:<fmt>`.) So you effectively cannot get termination
    with the built-in formats.
• Both the commit message as well as the commit acts as a record. Or just
  the diff, of course (git-diff-tree(1))
  • Things like `git log --format=%s --patch -z` terminate after the
    commit subject, then the diff, then the commit subject, termination,
    and repeat. This is inconsistent with plain `git log -z` (default
    format), which delimits the commit message and diff as a
    single unit (record) (see abd4e222 (Fix "git log -z" behaviour, 2007-02-07)
• Decoupled from the `-z` diff behavior, which adds internal NUL to the
  non-patch diff formats like `--name-only` and `--raw`
  • But you can pass `-Z -z` and get both

This is a new `revision.c:handle_revision_opt` option, but the utility is
focused around git-diff-tree(1).

Also no backticks in the commit messages.[4] And less code-quoting overall.

🔗 1: https://lore.kernel.org/git/238e5652-e95b-4d44-a900-e16aa6ab81f0@app.fastmail.com/
† 2: I learned this while working on git-format-rev(1)[3]
🔗 3: https://lore.kernel.org/git/e8bb57a2-8eff-4f72-96ca-72d8880901e0@gmail.com/
🔗 4: https://lore.kernel.org/git/xmqq33sme4u5.fsf@gitster.g/

[01/12] t4205: use correct 'setup' keyword
[02/12] t4205: add test coverage for NUL separator and --patch
[03/12] t4205: test blank line between commit and diff explicitly
[04/12] t4013-diff-various.sh: test --stdin -z --patch
[05/12] log-tree: terminate cmt+diff when -z --patch
[06/12] t4205: test --diff-merges
[07/12] revision: learn -Z for NUL termination
[08/12] revision: split out termination character
[09/12] diff-tree: flush each diff when --stdin
[10/12] doc: diff-tree: tell about --no-commit-id in Description
[11/12] doc: diff-tree: add Examples section and mention --stdin
[12/12] doc: rev-list: complete diff-tree by mentioning --always

 Documentation/git-diff-tree.adoc    |  43 ++++++++++
 Documentation/git-rev-list.adoc     |   3 +
 Documentation/rev-list-options.adoc |  12 +++
 builtin/diff-tree.c                 |   4 +
 log-tree.c                          |  35 ++++++--
 log-tree.h                          |   5 ++
 pretty.c                            |   8 +-
 revision.c                          |   7 ++
 revision.h                          |   7 ++
 t/t4013-diff-various.sh             |  72 ++++++++++++++++
 t/t4205-log-pretty-formats.sh       | 127 +++++++++++++++++++++++++++-
 11 files changed, 312 insertions(+), 11 deletions(-)


base-commit: 1891de46f79dc444a47db1c87bd0c8c76b5463f2
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 01/12] t4205: use correct 'setup' keyword
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 02/12] t4205: add test coverage for NUL separator and --patch kristofferhaugsbakk
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

The first test sets up the repository. But it uses 'set up' and not
the 'setup' keyword, which means that it won’t be run under
'--run=setup,...'.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 t/t4205-log-pretty-formats.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 01b97c88888..3ef184d7879 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -31,7 +31,7 @@ commit_msg () {
 	fi
 }
 
-test_expect_success 'set up basic repos' '
+test_expect_success 'setup basic repos' '
 	>foo &&
 	>bar &&
 	git add foo &&
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 02/12] t4205: add test coverage for NUL separator and --patch
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 01/12] t4205: use correct 'setup' keyword kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 03/12] t4205: test blank line between commit and diff explicitly kristofferhaugsbakk
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

There is no test coverage for option -z combined with the default commit
format (medium) and user-defined pretty formats using separator.

This is relevant since we are about to change how -z, --patch, and
NUL *termination* works in an upcoming commit.[1]

† 1: commit “log-tree: terminate cmt+diff when -z --patch”

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 t/t4205-log-pretty-formats.sh | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 3ef184d7879..5048c3b4c39 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -138,6 +138,22 @@ test_expect_success 'NUL separation with --stat' '
 	test_cmp expected actual
 '
 
+test_expect_success 'NUL separation with --patch' '
+	diff0_part=$(git diff HEAD^ HEAD) &&
+	diff1_part=$(git diff-tree --no-commit-id --patch --root HEAD^) &&
+	printf "add bar\n${diff0_part}\n\0$(commit_msg)\n${diff1_part}\n" >expected &&
+	git log -z --patch --pretty="format:%s" >actual &&
+	test_cmp_bin expected actual
+'
+
+test_expect_success 'NUL separation with --format=medium --patch' '
+	git show HEAD >expected &&
+	printf "\0" >>expected &&
+	git show HEAD^ >>expected &&
+	git log -z --patch >actual &&
+	test_cmp_bin expected actual
+'
+
 test_expect_failure 'NUL termination with --stat' '
 	stat0_part=$(git diff --stat HEAD^ HEAD) &&
 	stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 03/12] t4205: test blank line between commit and diff explicitly
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 01/12] t4205: use correct 'setup' keyword kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 02/12] t4205: add test coverage for NUL separator and --patch kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 04/12] t4013-diff-various.sh: test --stdin -z --patch kristofferhaugsbakk
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

This comment and code in log-tree.c:

    When showing a verbose header (i.e. log message), [...] we would
    want an extra newline between the end of log and the diff/diffstat
    output for readability.

is not directly tested by this file. There are two cases:

1. Terminators
2. Separator (--pretty=format:<fmt>)

Case (1) is tested by 'NUL separation with --stat'. Let’s add an explicit
test for that which doesn’t use special flags like -z.

Case (2) does not adhere to the comment above, though. There is no
blank line between the commit message and the diff:

    $ git log --patch --pretty=format:<fmt>
    <subject>
    <diff>

The reason is that terminators are added after show_log(...), i.e.
after the log message, while separators are added at the beginning
of show_log(...). This is how newlines work for terminators:

1. Custom format is printed; missing newline
2. The code below this comment terminates the commit log
3. The terminator newline adds a blank line
4. The diff is printed

While the separator case is missing step (3).

That might be a bug judging by this code comment. But changing it
would break several *other* tests. Let’s just add a regression test
at this point.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 t/t4205-log-pretty-formats.sh | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 5048c3b4c39..b03dd647ee4 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -118,6 +118,21 @@ test_expect_success 'alias loop' '
 	test_must_fail git log --pretty=test-foo
 '
 
+test_expect_success 'log --format=%s --patch blank line between commit and diff' '
+	git log --format=%s -1 >expect &&
+	echo >>expect &&
+	git diff HEAD^ HEAD >>expect &&
+	git log --format=%s --patch -1 >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=format:%s --patch no blank line between commit and diff' '
+	git log --format=%s -1 >expect &&
+	git diff HEAD^ HEAD >>expect &&
+	git log --pretty=format:%s --patch -1 >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'NUL separation' '
 	printf "add bar\0$(commit_msg)" >expected &&
 	git log -z --pretty="format:%s" >actual &&
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 04/12] t4013-diff-various.sh: test --stdin -z --patch
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (2 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 03/12] t4205: test blank line between commit and diff explicitly kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 05/12] log-tree: terminate cmt+diff when " kristofferhaugsbakk
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

There aren’t any explicit tests for 'git diff-tree --stdin -z
--patch'. Let’s add a couple of regression tests now since we are about
to change the log-tree.c code path.[1]

† 1: Upcoming commit “log-tree: terminate cmt+diff when -z --patch”

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 t/t4013-diff-various.sh | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh
index 21c9ad7c5d0..928a07a91bd 100755
--- a/t/t4013-diff-various.sh
+++ b/t/t4013-diff-various.sh
@@ -590,6 +590,42 @@ test_expect_success 'diff-tree --stdin with pathspec' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup: diff-tree --stdin -z etc.' '
+	printf "%s" $(git rev-parse main~1) >oid1 &&
+	printf "%s" $(git rev-parse main~2) >oid2 &&
+	cat oid1 >commits &&
+	echo >>commits &&
+	cat oid2 >>commits &&
+	echo >>commits &&
+	test_line_count = 2 commits &&
+	git rev-parse main >commits2 &&
+	git rev-parse main~1 >>commits2 &&
+	test_line_count = 2 commits2 &&
+	git diff main~2 main~1 >diff1 &&
+	git diff main~3 main~2 >diff2
+'
+
+test_expect_success 'diff-tree --stdin --patch -z' '
+	cat oid1 >expect &&
+	printf "\0" >>expect &&
+	cat diff1 >>expect &&
+	cat oid2 >>expect &&
+	printf "\0" >>expect &&
+	cat diff2 >>expect &&
+	git diff-tree --stdin --patch -z <commits >actual &&
+	test_cmp_bin expect actual
+'
+
+test_expect_success 'diff-tree --stdin --patch -z --always with one empty diff' '
+	printf "%s" $(git rev-parse main) >expect &&
+	printf "\0" >>expect &&
+	printf "%s" $(git rev-parse main~1) >>expect &&
+	printf "\0" >>expect &&
+	cat diff1 >>expect &&
+	git diff-tree --stdin --patch -z --always <commits2 >actual &&
+	test_cmp_bin expect actual
+'
+
 test_expect_success 'show A B ... -- <pathspec>' '
 	# side touches dir/sub, file0, and file3
 	# main^ touches dir/sub, and file1
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 05/12] log-tree: terminate cmt+diff when -z --patch
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (3 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 04/12] t4013-diff-various.sh: test --stdin -z --patch kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 06/12] t4205: test --diff-merges kristofferhaugsbakk
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

Terminate the whole log record, i.e. the commit and the diff,
with NUL when e.g. 'git log -z --patch' or 'git log -z --stat' is used
and records use terminators. This is in line with how these commands
work with separators. This fixes test case 'NUL termination with --stat'
in t4205.

This behavior is useful because a program can read one record at a time
by looking for NUL. And, unlike the separator case, it can be read
piecemeal; in the separator case, you feed one commit to log-tree.c
and get a record without any delimiter. You have to feed the *next*
commit to get the delimiter and then the next record.

The separator case was fixed in abd4e222 (Fix "git log -z" behaviour,
2007-02-07). It tells us this:

   For commit messages, we should really put the "line_termination" when we
   output the character in between different commits, *not* between the
   commit and the diff. The diff goes hand-in-hand with the commit, it
   shouldn't be separated from it with the termination character.

   So this:
    - uses the termination character for true inter-commit spacing
    - uses a regular newline between the commit log and the diff

   [...]

This is the problem that the terminator case has right now.

The reason is that show_log(...) handles record termination at the
end of the function. At that point we have only printed the commit
message.

Let’s put off the NUL termination case to the end of
log_tree_commit. That covers all cases: no diff, regular diff,
and diff on merge commits.

Now, as to the the commit ID and diff case:[1]

    git diff-tree -z --patch --stdin

It would be nice to uniformly have NUL termination for the whole record
by using a newline between the commit ID and the diff. But it might seem
that backwards compatibility could be a problem here. Fortunately it
isn’t, since there isn’t a way to get both NUL delimiting and record
termination; record termination is turned on by using custom pretty
formats, and custom pretty formats turn off the plain commit ID
display. So we are free to define this behavior.

† 1: log-tree.c:767

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 builtin/diff-tree.c           |  1 +
 log-tree.c                    | 28 ++++++++++++++++++++++++++--
 log-tree.h                    |  5 +++++
 t/t4205-log-pretty-formats.sh | 10 +++++++++-
 4 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c
index 8b8f8b54e40..c7734f7be63 100644
--- a/builtin/diff-tree.c
+++ b/builtin/diff-tree.c
@@ -59,6 +59,7 @@ static int stdin_diff_trees(struct tree *tree1, const char *p)
 	diff_tree_oid(&tree1->object.oid, &tree2->object.oid,
 		      "", &log_tree_opt.diffopt);
 	log_tree_diff_flush(&log_tree_opt);
+	log_tree_nul_terminate(&log_tree_opt);
 	return 0;
 }
 
diff --git a/log-tree.c b/log-tree.c
index 361dfadbb36..5dcee870be0 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -764,7 +764,18 @@ void show_log(struct rev_info *opt)
 			putc('\n', opt->diffopt.file);
 			graph_show_remainder(opt->graph);
 		}
-		putc(opt->diffopt.line_termination, opt->diffopt.file);
+		if (opt->use_terminator &&
+		    opt->diffopt.line_termination == '\0') {
+			/*
+			 * NUL termination means that we postpone the terminator.
+			 * But we do want a newline between the commit ID and
+			 * the diff.
+			 */
+			if (opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
+				putc('\n', opt->diffopt.file);
+		} else {
+			putc(opt->diffopt.line_termination, opt->diffopt.file);
+		}
 		return;
 	}
 
@@ -916,7 +927,12 @@ void show_log(struct rev_info *opt)
 	if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
 		if (!opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putc(opt->diffopt.line_termination, opt->diffopt.file);
+		/*
+		 * Termination is postponed for NUL in case there is
+		 * diff output after the log message.
+		 */
+		if (opt->diffopt.line_termination != '\0')
+			putc(opt->diffopt.line_termination, opt->diffopt.file);
 	}
 
 	strbuf_release(&msgbuf);
@@ -973,6 +989,12 @@ int log_tree_diff_flush(struct rev_info *opt)
 	return 1;
 }
 
+void log_tree_nul_terminate(struct rev_info *opt) {
+	if (opt->use_terminator &&
+	    opt->diffopt.line_termination == '\0')
+		putc(opt->diffopt.line_termination, opt->diffopt.file);
+}
+
 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
 {
 	diff_tree_combined_merge(commit, opt);
@@ -1312,6 +1334,8 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
 		}
 	}
 
+	log_tree_nul_terminate(opt);
+
 	opt->loginfo = NULL;
 	maybe_flush_or_die(opt->diffopt.file, "stdout");
 	opt->diffopt.no_free = no_free;
diff --git a/log-tree.h b/log-tree.h
index e8679b6c4aa..c5eec0459d8 100644
--- a/log-tree.h
+++ b/log-tree.h
@@ -26,6 +26,11 @@ struct decoration_options {
 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
 int log_tree_diff_flush(struct rev_info *);
 int log_tree_commit(struct rev_info *, struct commit *);
+/*
+ * Terminates the record if 'opt->use_terminator' and if
+ * 'opt->line_termination' is NUL.
+ */
+void log_tree_nul_terminate(struct rev_info *opt);
 void release_follow_pathspec_slab(struct rev_info *);
 void show_log(struct rev_info *opt);
 void format_decorations(struct strbuf *sb, const struct commit *commit,
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index b03dd647ee4..a9a470606cf 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -169,7 +169,7 @@ test_expect_success 'NUL separation with --format=medium --patch' '
 	test_cmp_bin expected actual
 '
 
-test_expect_failure 'NUL termination with --stat' '
+test_expect_success 'NUL termination with --stat' '
 	stat0_part=$(git diff --stat HEAD^ HEAD) &&
 	stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
 	printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n\0" >expected &&
@@ -177,6 +177,14 @@ test_expect_failure 'NUL termination with --stat' '
 	test_cmp expected actual
 '
 
+test_expect_success 'NUL termination with --patch' '
+	diff0_part=$(git diff HEAD^ HEAD) &&
+	diff1_part=$(git diff-tree --no-commit-id --patch --root HEAD^) &&
+	printf "add bar\n${diff0_part}\n\0$(commit_msg)\n${diff1_part}\n\0" >expected &&
+	git log -z --patch --pretty="tformat:%s" >actual &&
+	test_cmp_bin expected actual
+'
+
 for p in short medium full fuller email raw
 do
 	test_expect_success "NUL termination with --reflog --pretty=$p" '
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 06/12] t4205: test --diff-merges
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (4 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 05/12] log-tree: terminate cmt+diff when " kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 07/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

There are no test cases for diff output on merge commits. Let’s add a
few. In particular, we need the 'log --diff-merges NUL terminated' to
test this code path according to the previous commit.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 t/t4205-log-pretty-formats.sh | 57 +++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index a9a470606cf..ece4def4cfd 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -215,6 +215,63 @@ test_expect_success 'NUL termination with --reflog --pretty=oneline' '
 	test_cmp expect actual
 '
 
+test_expect_success 'setup merge' '
+	original=$(git branch --show-current) &&
+	git checkout -b merge-branch &&
+	git checkout -b f &&
+	echo on-a-branch >feature.txt &&
+	git add feature.txt &&
+	test_commit --no-tag on-another-branch feature.txt &&
+	git checkout merge-branch &&
+	git merge --no-ff f &&
+	test_merge merge-commit --no-ff f &&
+	git checkout -b f1 &&
+	echo on-f1 >f1.txt &&
+	git add f1.txt &&
+	test_commit --no-tag f1 f1.txt &&
+	git checkout merge-branch &&
+	git checkout -b f2 &&
+	echo on-f2 >f2.txt &&
+	git add f2.txt &&
+	test_commit --no-tag f2 f2.txt &&
+	git checkout merge-branch &&
+	test_merge octopus-merge --no-ff f1 f2 &&
+	git branch -D f f1 f2 &&
+	git checkout "$original"
+'
+
+test_expect_success 'log --diff-merges' '
+	git log -1 merge-commit >expect &&
+	echo >>expect &&
+	git diff merge-commit^1 merge-commit >>expect &&
+	git log -1 --diff-merges=first-parent \
+		merge-commit >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --diff-merges NUL terminated' '
+	git log -1 --format=%s merge-commit >expect &&
+	git diff merge-commit^1 merge-commit >>expect &&
+	printf "\0" >>expect &&
+	git log -1 -z --format=%s --diff-merges=first-parent \
+		merge-commit >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --remerge-diff octopus merge' '
+	git log -1 octopus-merge >expect &&
+	echo "diff: warning: Skipping remerge-diff for octopus merges." \
+		>>expect &&
+	git log -1 --remerge-diff octopus-merge >actual &&
+	test_cmp expect actual
+'
+
+# Later tests depend on what tags you have
+test_expect_success 'setup: clean up --diff-merges' '
+	git tag -d merge-commit &&
+	git tag -d octopus-merge
+'
+
 test_expect_success 'setup more commits' '
 	test_commit "message one" one one message-one &&
 	test_commit "message two" two two message-two &&
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 07/12] revision: learn -Z for NUL termination
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (5 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 06/12] t4205: test --diff-merges kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 08/12] revision: split out termination character kristofferhaugsbakk
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

Add a new option -Z which turns on NUL terminators for commit records.

Separator delimiting is the default, while terminators are turned on for
user-defined pretty formats. But that means that you cannot get
terminator behavior for built-in pretty formats. This options fills in
that gap.

Combining NUL delimiting with termination behavior means that the user
can read and act on each record immediately without risking deadlocks.

However, the picture is not as simple as just splitting on NUL.
The terminator/delimiter character is taken from struct diff_options,
so you have to take the diff behavior with the git-log(1) behavior.
But we will split these up in the next comit so that you use -Z
without getting NUL in any diff format.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 Documentation/rev-list-options.adoc | 12 ++++++++++++
 pretty.c                            |  8 +++++---
 revision.c                          |  4 ++++
 revision.h                          |  5 +++++
 t/t4013-diff-various.sh             | 23 +++++++++++++++++++++++
 t/t4205-log-pretty-formats.sh       | 27 +++++++++++++++++++++++++++
 6 files changed, 76 insertions(+), 3 deletions(-)

diff --git a/Documentation/rev-list-options.adoc b/Documentation/rev-list-options.adoc
index c234e865cb1..a4bf96e7d0a 100644
--- a/Documentation/rev-list-options.adoc
+++ b/Documentation/rev-list-options.adoc
@@ -396,6 +396,18 @@ This mode is only compatible with the `--objects`, `--boundary`, and
 `--missing` output options.
 endif::git-rev-list[]
 
+`-Z`::
+	Use NUL terminator to delimit records. The _record_ is the
+	commit and whatever extra information about it, like for example
+	`--patch` or `--stat`.
++
+Note that separator/terminator behavior is usually set when using
+`--pretty` or `--format`. These have no effect when this option is
+given.
++
+This implies `-z`, so diff output formats like `--raw` and `--numstat`
+will also contain _NUL_ internally.
+
 History Simplification
 ~~~~~~~~~~~~~~~~~~~~~~
 
diff --git a/pretty.c b/pretty.c
index d8a9f370f6c..019e1f88511 100644
--- a/pretty.c
+++ b/pretty.c
@@ -54,7 +54,7 @@ static void save_user_format(struct rev_info *rev, const char *cp, int is_tforma
 {
 	free(user_format);
 	user_format = xstrdup(cp);
-	if (is_tformat)
+	if (!rev->want_terminator && is_tformat)
 		rev->use_terminator = 1;
 	rev->commit_format = CMIT_FMT_USERFORMAT;
 }
@@ -191,7 +191,8 @@ void get_commit_format(const char *arg, struct rev_info *rev)
 {
 	struct cmt_fmt_map *commit_format;
 
-	rev->use_terminator = 0;
+	if (!rev->want_terminator)
+		rev->use_terminator = 0;
 	if (!arg) {
 		rev->commit_format = CMIT_FMT_DEFAULT;
 		return;
@@ -211,7 +212,8 @@ void get_commit_format(const char *arg, struct rev_info *rev)
 		die("invalid --pretty format: %s", arg);
 
 	rev->commit_format = commit_format->format;
-	rev->use_terminator = commit_format->is_tformat;
+	if (!rev->want_terminator)
+		rev->use_terminator = commit_format->is_tformat;
 	rev->expand_tabs_in_log_default = commit_format->expand_tabs_in_log;
 	if (!rev->date_mode_explicit && commit_format->default_date_mode_type)
 		rev->date_mode.type = commit_format->default_date_mode_type;
diff --git a/revision.c b/revision.c
index a83e499047b..1d2a10267ca 100644
--- a/revision.c
+++ b/revision.c
@@ -2755,6 +2755,10 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		if (revs->repo->fetch_if_missing)
 			BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
 		revs->exclude_promisor_objects = 1;
+	} else if (!strcmp(arg, "-Z")) {
+		revs->want_terminator = 1;
+		revs->use_terminator = 1;
+		revs->diffopt.line_termination = '\0';
 	} else {
 		int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
 		if (!opts)
diff --git a/revision.h b/revision.h
index e5dabd18ce1..c6598ba6f22 100644
--- a/revision.h
+++ b/revision.h
@@ -272,6 +272,11 @@ struct rev_info {
 			abbrev_commit:1,
 			abbrev_commit_given:1,
 			zero_commit:1,
+			/*
+			 * pretty.c should not set 'use_terminator' if
+			 * this is true.
+			 */
+			want_terminator:1,
 			use_terminator:1,
 			missing_newline:1,
 			date_mode_explicit:1,
diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh
index 928a07a91bd..207bd2df423 100755
--- a/t/t4013-diff-various.sh
+++ b/t/t4013-diff-various.sh
@@ -626,6 +626,29 @@ test_expect_success 'diff-tree --stdin --patch -z --always with one empty diff'
 	test_cmp_bin expect actual
 '
 
+test_expect_success 'diff-tree --stdin --patch -Z' '
+	cat oid1 >expect &&
+	echo >>expect &&
+	cat diff1 >>expect &&
+	printf "\0" >>expect &&
+	cat oid2 >>expect &&
+	echo >>expect &&
+	cat diff2 >>expect &&
+	printf "\0" >>expect &&
+	git diff-tree --stdin --patch -Z <commits >actual &&
+	test_cmp_bin expect actual
+'
+
+test_expect_success 'diff-tree --stdin --patch -Z --always with one empty diff' '
+	git rev-parse main >expect &&
+	printf "\0" >>expect &&
+	git rev-parse main~1 >>expect &&
+	cat diff1 >>expect &&
+	printf "\0" >>expect &&
+	git diff-tree --stdin --patch -Z --always <commits2 >actual &&
+	test_cmp_bin expect actual
+'
+
 test_expect_success 'show A B ... -- <pathspec>' '
 	# side touches dir/sub, file0, and file3
 	# main^ touches dir/sub, and file1
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index ece4def4cfd..2d6cca46fff 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -169,6 +169,33 @@ test_expect_success 'NUL separation with --format=medium --patch' '
 	test_cmp_bin expected actual
 '
 
+test_expect_success 'NUL with -Z and --format=medium --patch' '
+	git show HEAD >expected &&
+	printf "\0" >>expected &&
+	git show HEAD^ >>expected &&
+	printf "\0" >>expected &&
+	git log -Z --patch >actual &&
+	test_cmp_bin expected actual
+'
+
+test_expect_success 'NUL and terminator behavior with -Z' '
+	git show HEAD >expected &&
+	printf "\0" >>expected &&
+	git show HEAD^ >>expected &&
+	printf "\0" >>expected &&
+	git log -Z --patch >actual &&
+	test_cmp_bin expected actual
+'
+
+test_expect_success '-Z --format=medium still uses terminator' '
+	git show HEAD >expected &&
+	printf "\0" >>expected &&
+	git show HEAD^ >>expected &&
+	printf "\0" >>expected &&
+	git log -Z --format=medium --patch >actual &&
+	test_cmp_bin expected actual
+'
+
 test_expect_success 'NUL termination with --stat' '
 	stat0_part=$(git diff --stat HEAD^ HEAD) &&
 	stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 08/12] revision: split out termination character
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (6 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 07/12] revision: learn -Z for NUL termination kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 09/12] diff-tree: flush each diff when --stdin kristofferhaugsbakk
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

log-tree.c uses struct diff_options.line_termination for the commit
record termination. That means that options -z and -Z give you both NUL
commit record delimiting as well as NUL in non-patch diff formats.

The -z behavior has to stay that way for backwards compatibility.
But with the new -Z option we have an opportunity to split out this
behavior so that -Z only means delimiting commit records and does not
affect diffs; if you want to NUL-terminate diffs as well you can use
both -Z and -z.

Let’s store 'delimiter' in struct rev_info and use that in
log-tree.c. Let’s also add -z to handle_revision_opt(...) that acts
like -z in diff.c for struct diffopt and also sets 'delimiter'
appropriately.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 Documentation/rev-list-options.adoc |  4 ++--
 log-tree.c                          | 21 ++++++++++-----------
 revision.c                          |  5 ++++-
 revision.h                          |  2 ++
 t/t4013-diff-various.sh             | 13 +++++++++++++
 5 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/Documentation/rev-list-options.adoc b/Documentation/rev-list-options.adoc
index a4bf96e7d0a..4ba410c5e06 100644
--- a/Documentation/rev-list-options.adoc
+++ b/Documentation/rev-list-options.adoc
@@ -405,8 +405,8 @@ Note that separator/terminator behavior is usually set when using
 `--pretty` or `--format`. These have no effect when this option is
 given.
 +
-This implies `-z`, so diff output formats like `--raw` and `--numstat`
-will also contain _NUL_ internally.
+This does not imply `-z`, but `-z` can be passed as well in order to use
+_NUL_ internally for diff formats like `--raw` and `--numstat`.
 
 History Simplification
 ~~~~~~~~~~~~~~~~~~~~~~
diff --git a/log-tree.c b/log-tree.c
index 5dcee870be0..e9ea5620e9b 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -764,8 +764,7 @@ void show_log(struct rev_info *opt)
 			putc('\n', opt->diffopt.file);
 			graph_show_remainder(opt->graph);
 		}
-		if (opt->use_terminator &&
-		    opt->diffopt.line_termination == '\0') {
+		if (opt->use_terminator && opt->delimiter == '\0') {
 			/*
 			 * NUL termination means that we postpone the terminator.
 			 * But we do want a newline between the commit ID and
@@ -774,7 +773,7 @@ void show_log(struct rev_info *opt)
 			if (opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
 				putc('\n', opt->diffopt.file);
 		} else {
-			putc(opt->diffopt.line_termination, opt->diffopt.file);
+			putc(opt->delimiter, opt->diffopt.file);
 		}
 		return;
 	}
@@ -782,8 +781,8 @@ void show_log(struct rev_info *opt)
 	/*
 	 * If use_terminator is set, we already handled any record termination
 	 * at the end of the last record.
-	 * Otherwise, add a diffopt.line_termination character before all
-	 * entries but the first.  (IOW, as a separator between entries)
+	 * Otherwise, add a ''delimiter' character before all entries
+	 * but the first.  (IOW, as a separator between entries)
 	 */
 	if (opt->shown_one && !opt->use_terminator) {
 		/*
@@ -798,10 +797,10 @@ void show_log(struct rev_info *opt)
 		 * never want the extra graph output before the entry
 		 * separator.
 		 */
-		if (opt->diffopt.line_termination == '\n' &&
+		if (opt->delimiter == '\n' &&
 		    !opt->missing_newline)
 			graph_show_padding(opt->graph);
-		putc(opt->diffopt.line_termination, opt->diffopt.file);
+		putc(opt->delimiter, opt->diffopt.file);
 	}
 	opt->shown_one = 1;
 
@@ -931,8 +930,8 @@ void show_log(struct rev_info *opt)
 		 * Termination is postponed for NUL in case there is
 		 * diff output after the log message.
 		 */
-		if (opt->diffopt.line_termination != '\0')
-			putc(opt->diffopt.line_termination, opt->diffopt.file);
+		if (opt->delimiter != '\0')
+			putc(opt->delimiter, opt->diffopt.file);
 	}
 
 	strbuf_release(&msgbuf);
@@ -991,8 +990,8 @@ int log_tree_diff_flush(struct rev_info *opt)
 
 void log_tree_nul_terminate(struct rev_info *opt) {
 	if (opt->use_terminator &&
-	    opt->diffopt.line_termination == '\0')
-		putc(opt->diffopt.line_termination, opt->diffopt.file);
+	    opt->delimiter == '\0')
+		putc(opt->delimiter, opt->diffopt.file);
 }
 
 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
diff --git a/revision.c b/revision.c
index 1d2a10267ca..e9c10c354e0 100644
--- a/revision.c
+++ b/revision.c
@@ -2755,10 +2755,13 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		if (revs->repo->fetch_if_missing)
 			BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
 		revs->exclude_promisor_objects = 1;
+	} else if (!strcmp(arg, "-z")) {
+		revs->delimiter = '\0';
+		revs->diffopt.line_termination = '\0';
 	} else if (!strcmp(arg, "-Z")) {
 		revs->want_terminator = 1;
 		revs->use_terminator = 1;
-		revs->diffopt.line_termination = '\0';
+		revs->delimiter = '\0';
 	} else {
 		int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
 		if (!opts)
diff --git a/revision.h b/revision.h
index c6598ba6f22..337ba08e553 100644
--- a/revision.h
+++ b/revision.h
@@ -284,6 +284,7 @@ struct rev_info {
 			force_in_body_from:1,
 			encode_email_headers:1,
 			include_header:1;
+	char		delimiter;
 	unsigned int	disable_stdin:1;
 	/* --show-linear-break */
 	unsigned int	track_linear:1,
@@ -444,6 +445,7 @@ struct rev_info {
 	.max_parents = -1, \
 	.expand_tabs_in_log = -1, \
 	.commit_format = CMIT_FMT_DEFAULT, \
+	.delimiter = '\n', \
 	.expand_tabs_in_log_default = 8, \
 	.rdiff_log_arg = STRVEC_INIT, \
 	.argv_to_free = STRVEC_INIT, \
diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh
index 207bd2df423..b6974b9b64d 100755
--- a/t/t4013-diff-various.sh
+++ b/t/t4013-diff-various.sh
@@ -649,6 +649,19 @@ test_expect_success 'diff-tree --stdin --patch -Z --always with one empty diff'
 	test_cmp_bin expect actual
 '
 
+test_expect_success 'diff-tree --stdin -Z' '
+	cat oid1 >expect &&
+	echo >>expect &&
+	git diff-tree main~2 main~1 >>expect &&
+	printf "\0" >>expect &&
+	cat oid2 >>expect &&
+	echo >>expect &&
+	git diff-tree main~3 main~2 >>expect &&
+	printf "\0" >>expect &&
+	git diff-tree --stdin -Z <commits >actual &&
+	test_cmp_bin expect actual
+'
+
 test_expect_success 'show A B ... -- <pathspec>' '
 	# side touches dir/sub, file0, and file3
 	# main^ touches dir/sub, and file1
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 09/12] diff-tree: flush each diff when --stdin
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (7 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 08/12] revision: split out termination character kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 10/12] doc: diff-tree: tell about --no-commit-id in Description kristofferhaugsbakk
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

'git diff-tree --stdin' handles both commits and diffs. The commit case
is handled by log-tree.c:log_tree_commit. That method calls
maybe_flush_or_die(...). That means that with e.g.:

    git diff-tree --stdin --patch -Z

We can feed a commit to the commit and read it back in a loop without
riskig deadlocks.

The diff case, on the other hand, calls log-tree.c:
log_tree_diff_flush(...). That function does not call the
IO flush function.[1]

Let’s call the IO flush function so that we are guaranteed to read back
a diff after each input immediately, just like with commits.

† 1: The “flush” in the name is because it calls diff.c:diff_flush

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 builtin/diff-tree.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c
index c7734f7be63..d516c496521 100644
--- a/builtin/diff-tree.c
+++ b/builtin/diff-tree.c
@@ -11,6 +11,7 @@
 #include "revision.h"
 #include "tmp-objdir.h"
 #include "tree.h"
+#include "write-or-die.h"
 
 static struct rev_info log_tree_opt;
 
@@ -60,6 +61,8 @@ static int stdin_diff_trees(struct tree *tree1, const char *p)
 		      "", &log_tree_opt.diffopt);
 	log_tree_diff_flush(&log_tree_opt);
 	log_tree_nul_terminate(&log_tree_opt);
+	maybe_flush_or_die(log_tree_opt.diffopt.file, "stdout");
+
 	return 0;
 }
 
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 10/12] doc: diff-tree: tell about --no-commit-id in Description
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (8 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 09/12] diff-tree: flush each diff when --stdin kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 11/12] doc: diff-tree: add Examples section and mention --stdin kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 12/12] doc: rev-list: complete diff-tree by mentioning --always kristofferhaugsbakk
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

This doc has many options. 'git help --man diff-tree' tells us that
it is 1346 lines long. In turn the dedicated options are not skimmable.
This options is important if you for example just want regular diff
output. So let’s mention it in the Description section as well.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 Documentation/git-diff-tree.adoc | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Documentation/git-diff-tree.adoc b/Documentation/git-diff-tree.adoc
index f1e3134bdef..084be99e7cf 100644
--- a/Documentation/git-diff-tree.adoc
+++ b/Documentation/git-diff-tree.adoc
@@ -20,6 +20,9 @@ Compare the content and mode of blobs found via two tree objects.
 If there is only one <tree-ish> given, the commit is compared with its parents
 (see --stdin below).
 
+This command outputs a line with the commit ID when applicable. Use
+`--no-commit-id` to suppress this behavior.
+
 Note that 'git diff-tree' can use the tree encapsulated in a commit object.
 
 OPTIONS
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 11/12] doc: diff-tree: add Examples section and mention --stdin
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (9 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 10/12] doc: diff-tree: tell about --no-commit-id in Description kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  2026-12-02 22:14 ` [PATCH 12/12] doc: rev-list: complete diff-tree by mentioning --always kristofferhaugsbakk
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

We added option -Z a few commits ago.[1] This option can be used by
any command that uses revision.c:handle_revision_opt(...), but I had in
mind git-diff-tree(1) in particular. That is because 'git diff-tree
--stdin' outputs each record (commit or diff or both) immediately after
it is processed. That is unlike git-rev-list(1)/git-show(1)/git-log(1)
which wait until stdin is closed to and then outputs everything.

† 1: commit “revision: learn -Z (NUL termination)”

And this behavior is useful for feeding commit IDs, getting back some
output, acting on it, and then feeding the next commit ID. This is
facilitated by using -z so that you know a record is done after you
read it. However, this does not work well for the default separator
case; the delimiter is added *between* records, an in turn you have to
give the Nth commit in order to get the NUL delimiter for the N-1
commit. Also, the last commit is not terminated.

However, with the new -Z option you can pretty-print
commits on demand just like 'git format-rev --stdin-mode=revs':

    git cat-file --batch-command='%(objectname)' |
    git diff-tree --no-patch --format=<fmt> --always -Z --stdin

And you also get log-tree.c formatted output (e.g. 'Notes:' in
--format=medium) just like commands like git-log(1). And you can do
the same for diffs, or combine commits and diffs with --format=<fmt>
--patch.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 Documentation/git-diff-tree.adoc | 40 ++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/Documentation/git-diff-tree.adoc b/Documentation/git-diff-tree.adoc
index 084be99e7cf..b73cf4d268d 100644
--- a/Documentation/git-diff-tree.adoc
+++ b/Documentation/git-diff-tree.adoc
@@ -25,6 +25,8 @@ This command outputs a line with the commit ID when applicable. Use
 
 Note that 'git diff-tree' can use the tree encapsulated in a commit object.
 
+See the "EXAMPLES" section at the end for a few examples.
+
 OPTIONS
 -------
 include::diff-options.adoc[]
@@ -129,6 +131,44 @@ include::pretty-formats.adoc[]
 
 include::diff-format.adoc[]
 
+EXAMPLES
+--------
+
+The default behavior is to show the commit ID and then the raw diff (see
+`--raw`):
+
+----
+$ git diff-tree HEAD
+00645eb9f4e515533cdf67376d481ffb4a796bcc
+:040000 040000 ac1844364599b5671e9e4da68eab0817cab2ac2d b39de8e87a84787fb7bc2b38e015540e6d336eae M	Documentation
+----
+
+But you can use this command to show normal diffs (`--patch`) and commit
+messages as well. You can for example get the same output as `git log`
+with:
+
+----
+$ git rev-list HEAD |
+git diff-tree --no-patch --stdin --always --format=medium
+----
+
+The above is of course longer to type and uses two processes instead
+of two. But this longer form with `--stdin` is more flexible than `git
+log --stdin`. That is because the linkgit:git-log[1] form only outputs
+anything after you close standard in (stdin). Meanwhile, `git diff-tree
+--stdin ...` outputs each commit or diff (or both) immediately. This
+means that you can use `-Z` to feed the commit, handle the output, and
+then continue with the next commit. The above can be tweaked to for
+example show the diff as well with `--patch`.
+
+----
+$ git diff-tree --no-patch --stdin --always --format=<fmt> -Z
+     [rev-list options]
+----
+
+The command `git diff-tree --stdin --patch` can be used together with
+linkgit:git-patch-id[1] to create a map from commit IDs to pach IDs.
+
 GIT
 ---
 Part of the linkgit:git[1] suite
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 12/12] doc: rev-list: complete diff-tree by mentioning --always
  2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
                   ` (10 preceding siblings ...)
  2026-12-02 22:14 ` [PATCH 11/12] doc: diff-tree: add Examples section and mention --stdin kristofferhaugsbakk
@ 2026-12-02 22:14 ` kristofferhaugsbakk
  11 siblings, 0 replies; 13+ messages in thread
From: kristofferhaugsbakk @ 2026-12-02 22:14 UTC (permalink / raw)
  To: git; +Cc: Kristoffer Haugsbakk

From: Kristoffer Haugsbakk <code@khaugsbakk.name>

This example implies that it acts a lot like some git-log(1)
invocation. The immediate thought is 'git log --patch', but that would
be wrong; the git-diff-tree(1) command does not output commits which do
not have a diff, like a merge commit. The example doesn’t say that, but
it is useful for the reader to know that they need to make this
adjustment if they want git-log(1) output. So let’s mention it after the
code example.

Of course, this is not complete; you would need to pass '--notes' or
'--show-notes-by-default' to git-diff-tree(1) in order to mimic the
git-log(1) notes behavior. And maybe other things. But those are small
details compared to not getting a commit at all.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 Documentation/git-rev-list.adoc | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Documentation/git-rev-list.adoc b/Documentation/git-rev-list.adoc
index f582491dd43..0dd6269d662 100644
--- a/Documentation/git-rev-list.adoc
+++ b/Documentation/git-rev-list.adoc
@@ -61,6 +61,9 @@ git rev-list --format=medium HEAD
 git rev-list HEAD |
 git diff-tree --stdin --format=medium -p
 ----------
++
+This will not print commits without diffs, like merge commits. Use
+`--always` to also print those commits.
 
 * Print the list of commits on the current branch that touched any
   file in the `Documentation` directory.
-- 
2.56.1.5.g155936fbb2a


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-12-02 22:17 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 22:14 [PATCH 00/12] revision: learn -Z for NUL termination kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 01/12] t4205: use correct 'setup' keyword kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 02/12] t4205: add test coverage for NUL separator and --patch kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 03/12] t4205: test blank line between commit and diff explicitly kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 04/12] t4013-diff-various.sh: test --stdin -z --patch kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 05/12] log-tree: terminate cmt+diff when " kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 06/12] t4205: test --diff-merges kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 07/12] revision: learn -Z for NUL termination kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 08/12] revision: split out termination character kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 09/12] diff-tree: flush each diff when --stdin kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 10/12] doc: diff-tree: tell about --no-commit-id in Description kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 11/12] doc: diff-tree: add Examples section and mention --stdin kristofferhaugsbakk
2026-12-02 22:14 ` [PATCH 12/12] doc: rev-list: complete diff-tree by mentioning --always kristofferhaugsbakk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox