Git development
 help / color / mirror / Atom feed
From: Aman Ganapathy <amanganapathy@gmail.com>
To: git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>
Subject: [PATCH] mktree, bisect, remote: load the default config
Date: Fri,  4 Dec 2026 14:56:41 -0800	[thread overview]
Message-ID: <20261204225641.12294-1-amanganapathy@gmail.com> (raw)

When core.fsync is set, the expected result is that whatever components
it lists get synced to disk, no matter which command writes them.
Implementation wise, core.fsync is only parsed when a command loads the
default config with git_default_config(). "git mktree", "git bisect"
and "git remote" never do this, so they keep the built-in default,
which doesn't sync loose objects or refs. What this means is that even
with core.fsync=all, mktree doesn't sync the tree it writes, and bisect
and "remote rename" don't sync the refs they write.

mktree is where this hurts the most. A script that builds a commit with
hash-object, mktree, commit-tree and update-ref would expect all of it
to be synced, but the tree isn't. On XFS and ext4, a crash shortly
after can leave the branch pointing at a commit whose tree is an empty
file, and "git status" then fails with "loose object ... is corrupt".

Fix this by loading the default config in all three, like 29c83fc23f
(interpret-trailers: load default config, 2019-06-19) did.

Signed-off-by: Aman Ganapathy <amanganapathy@gmail.com>
---
I was testing crashes with core.fsync=all, and a commit built with
plumbing came back with an empty tree, even though the commit and the
branch were fine. Here's roughly what I ran on an XFS loop mount:

    git config core.fsync all
    git commit -q --allow-empty -m one && sync
    blob=$(echo hi | git hash-object -w --stdin)
    tree=$(printf '100644 blob %s\tf\n' $blob | git mktree)
    git update-ref HEAD $(echo two | git commit-tree $tree -p HEAD)
    git update-ref refs/heads/other HEAD
    xfs_io -x -c shutdown /mnt

The second update-ref is just any later command that syncs something,
without it the branch goes back to "one". After remounting, "git
status" fails on the empty tree. With the patch the tree is fine.

These were the only three I found, but I only traced a dozen or so
commands, so there could be others. I can also split this into one patch per command if that's preferred!

 builtin/bisect.c            |  4 ++++
 builtin/mktree.c            |  3 +++
 builtin/remote.c            |  2 ++
 t/t1010-mktree.sh           | 11 +++++++++++
 t/t5505-remote.sh           | 13 +++++++++++++
 t/t6030-bisect-porcelain.sh | 10 ++++++++++
 6 files changed, 43 insertions(+)

diff --git a/builtin/bisect.c b/builtin/bisect.c
index 1cfb8a794b..9bb886a9bf 100644
--- a/builtin/bisect.c
+++ b/builtin/bisect.c
@@ -2,6 +2,7 @@
 #define DISABLE_SIGN_COMPARE_WARNINGS
 
 #include "builtin.h"
+#include "config.h"
 #include "copy.h"
 #include "environment.h"
 #include "gettext.h"
@@ -1615,6 +1616,9 @@ int cmd_bisect(int argc,
 		OPT_SUBCOMMAND("run", &fn, cmd_bisect__run),
 		OPT_END()
 	};
+
+	repo_config(repo, git_default_config, NULL);
+
 	argc = parse_options(argc, argv, prefix, options, git_bisect_usage,
 			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
 
diff --git a/builtin/mktree.c b/builtin/mktree.c
index 45ae2af3b5..1b4d9d8ccc 100644
--- a/builtin/mktree.c
+++ b/builtin/mktree.c
@@ -4,6 +4,7 @@
  * Copyright (c) Junio C Hamano, 2006, 2009
  */
 #include "builtin.h"
+#include "config.h"
 #include "gettext.h"
 #include "hex.h"
 #include "quote.h"
@@ -170,6 +171,8 @@ int cmd_mktree(int ac,
 		OPT_END()
 	};
 
+	repo_config(repo, git_default_config, NULL);
+
 	ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
 	getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
 
diff --git a/builtin/remote.c b/builtin/remote.c
index fb7e5b114f..e53e9db3fa 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -1952,6 +1952,8 @@ int cmd_remote(int argc,
 		OPT_END()
 	};
 
+	repo_config(repo, git_default_config, NULL);
+
 	argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
 			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
 
diff --git a/t/t1010-mktree.sh b/t/t1010-mktree.sh
index cecba55d45..893b6e4628 100755
--- a/t/t1010-mktree.sh
+++ b/t/t1010-mktree.sh
@@ -117,4 +117,15 @@ test_expect_success PIPE 'mktree --batch survives a concurrent repack retiring a
 	)
 '
 
+test_expect_success 'mktree syncs the tree' '
+	test_when_finished "rm -f input trace2.txt" &&
+	blob=$(echo sync | git hash-object -w --stdin) &&
+	printf "100644 blob %s\tsync\n" "$blob" >input &&
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+	GIT_TEST_FSYNC=true \
+		git -c core.fsync=loose-object -c core.fsyncMethod=fsync \
+		mktree <input &&
+	test_grep "\"name\":\"hardware-flush\",\"count\":1}" trace2.txt
+'
+
 test_done
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index 8b4c7006ec..745687a74b 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -1883,4 +1883,17 @@ test_expect_success 'rename moves around the reflog' '
 	test_cmp expect reflog-entries-new
 '
 
+test_expect_success 'remote rename syncs the renamed refs' '
+	test_when_finished "rm -rf fsync trace2.txt" &&
+	git init fsync &&
+	test_commit -C fsync one &&
+	git -C fsync remote add origin ../nowhere &&
+	git -C fsync update-ref refs/remotes/origin/main HEAD &&
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+	GIT_TEST_FSYNC=true \
+		git -C fsync -c core.fsync=reference -c core.fsyncMethod=fsync \
+		remote rename origin upstream &&
+	test_grep "\"name\":\"hardware-flush\",\"count\":1}" trace2.txt
+'
+
 test_done
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index d5e7987f56..1e25b5d493 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -1392,4 +1392,14 @@ test_expect_success 'verify correct error message' '
 	test_grep "git bisect good.*exited with error code" error
 '
 
+test_expect_success 'bisect syncs its refs' '
+	test_when_finished "git bisect reset && rm -f trace2.txt" &&
+	git bisect start &&
+	GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+	GIT_TEST_FSYNC=true \
+		git -c core.fsync=reference -c core.fsyncMethod=fsync \
+		bisect bad &&
+	test_grep "\"name\":\"hardware-flush\",\"count\":1}" trace2.txt
+'
+
 test_done
-- 
2.39.5 (Apple Git-154)


                 reply	other threads:[~2026-12-04 22:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261204225641.12294-1-amanganapathy@gmail.com \
    --to=amanganapathy@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    /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