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

[PATCH 07/12] revision: learn -Z for NUL termination

From
Kkristofferhaugsbakk@fastmail.com <kristofferhaugsbakk@fastmail.com>
Date
Dec 2, 2026, 22:14 UTC
Message-ID
<learn_-Z.df6@m5gid.xyz>
In-Reply-To
<CV_diff-tree_NUL_termination.def@m5gid.xyz>
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
Previous: kristofferhaugsbakk@fastmail.comNext: kristofferhaugsbakk@fastmail.com
Message 8 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.