Git development
 help / color / mirror / Atom feed
From: kristofferhaugsbakk@fastmail.com
To: git@vger.kernel.org
Cc: Kristoffer Haugsbakk <code@khaugsbakk.name>
Subject: [PATCH 05/12] log-tree: terminate cmt+diff when -z --patch
Date: Wed,  2 Dec 2026 23:14:13 +0100	[thread overview]
Message-ID: <terminate_cmt~pldiff_etc..df4@m5gid.xyz> (raw)
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


  parent reply	other threads:[~2026-12-02 22:15 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 ` kristofferhaugsbakk [this message]
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

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='terminate_cmt~pldiff_etc..df4@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox