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

[PATCH 08/12] revision: split out termination character

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