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

[PATCH 05/12] log-tree: terminate cmt+diff when -z --patch

From
Kkristofferhaugsbakk@fastmail.com <kristofferhaugsbakk@fastmail.com>
Date
Dec 2, 2026, 22:14 UTC
Message-ID
<terminate_cmt~pldiff_etc..df4@m5gid.xyz>
In-Reply-To
<CV_diff-tree_NUL_termination.def@m5gid.xyz>
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
Previous: kristofferhaugsbakk@fastmail.comNext: kristofferhaugsbakk@fastmail.com
Message 6 of 13 in “revision: learn -Z for NUL termination”
  1. 00/12 revision: learn -Z for NUL terminationkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  2. 01/12 t4205: use correct 'setup' keywordkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  3. 02/12 t4205: add test coverage for NUL separator and --patchkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  4. 03/12 t4205: test blank line between commit and diff explicitlykristofferhaugsbakk@fastmail.com, Dec 2, 2026
  5. 04/12 t4013-diff-various.sh: test --stdin -z --patchkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  6. 05/12 log-tree: terminate cmt+diff when -z --patchkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  7. 06/12 t4205: test --diff-mergeskristofferhaugsbakk@fastmail.com, Dec 2, 2026
  8. 07/12 revision: learn -Z for NUL terminationkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  9. 08/12 revision: split out termination characterkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  10. 09/12 diff-tree: flush each diff when --stdinkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  11. 10/12 doc: diff-tree: tell about --no-commit-id in Descriptionkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  12. 11/12 doc: diff-tree: add Examples section and mention --stdinkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  13. 12/12 doc: rev-list: complete diff-tree by mentioning --alwayskristofferhaugsbakk@fastmail.com, Dec 2, 2026

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

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