All of lore.kernel.org
 help / color / mirror / Atom feed
From: kristofferhaugsbakk@fastmail.com
To: git@vger.kernel.org
Cc: Kristoffer Haugsbakk <code@khaugsbakk.name>
Subject: [PATCH 08/12] revision: split out termination character
Date: Wed,  2 Dec 2026 23:14:16 +0100	[thread overview]
Message-ID: <split_out_termination.df7@m5gid.xyz> (raw)
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


  parent reply	other threads:[~2026-12-02 22:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` kristofferhaugsbakk [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=split_out_termination.df7@m5gid.xyz \
    --to=kristofferhaugsbakk@fastmail.com \
    --cc=code@khaugsbakk.name \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.