Git development
 help / color / mirror / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>,
	Patrick Steinhardt <ps@pks.im>,
	Adam Dinwoodie <adam@dinwoodie.org>
Subject: [PATCH v3] setup: fix hash algorithm detection
Date: Thu, 19 Nov 2026 01:06:12 +0000	[thread overview]
Message-ID: <20261119010612.687733-1-sandals@crustytoothpaste.net> (raw)
In-Reply-To: <20261108011633.1772000-1-sandals@crustytoothpaste.net>

Our current hash algorithm selection code has a few different problems.
First, we do not actually default to GIT_HASH_DEFAULT.  Second, if we
have an algorithm chosen by init.defaultObjectFormat and we re-init a
repository, we will change the hash algorithm, which is very much not
desired.

Let's restructure this code slightly.  First, if an algorithm was
selected on the command line, assume that's what the user wanted.
Otherwise, the other options should only be chosen if our repository
doesn't exist (that is, repo_fmt->version is -1) or it's specifically
detected as having no hash algorithm.  In that case, first use the
environment, then the config, and finally, the default algorithm.

Then, if the algorithm does not match the repository, complain loudly
and refuse to reinitialize the repository with a different hash
algorithm.

With this change, we will choose SHA-256 in breaking changes mode and in
Git 3.0, which is what we want.  Add several tests to cover this case
and several others discovered along the way.  Remove the now-inaccurate
comment.

Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
Changes since v2:
* Remove comment.
* Use `$GIT_TEST_BUILTIN_HASH`.

 setup.c         | 27 ++++++++++++-------------
 t/t0001-init.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 14 deletions(-)

diff --git a/setup.c b/setup.c
index cedc031c54..5d7e8d590e 100644
--- a/setup.c
+++ b/setup.c
@@ -2887,25 +2887,21 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 
 	config_with_options(read_default_format_config, &cfg, NULL, NULL, &opts);
 
-	/*
-	 * If we already have an initialized repo, don't allow the user to
-	 * specify a different algorithm, as that could cause corruption.
-	 * Otherwise, if the user has specified one on the command line, use it.
-	 */
 	env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
-	if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
-		die(_("attempt to reinitialize repository with different hash"));
-	else if (hash != GIT_HASH_UNKNOWN)
-		repo_fmt->hash_algo = hash;
-	else if (env) {
+	if (hash != GIT_HASH_UNKNOWN) {
+		/* nothing to do */
+	} else if (repo_fmt->version >= 0 &&
+		   repo_fmt->hash_algo != GIT_HASH_UNKNOWN) {
+		hash = repo_fmt->hash_algo;
+	} else if (env) {
 		int env_algo = hash_algo_by_name(env);
 		if (env_algo == GIT_HASH_UNKNOWN)
 			die(_("unknown hash algorithm '%s'"), env);
-		if (repo_fmt->version < 0 ||
-		    repo_fmt->hash_algo == GIT_HASH_UNKNOWN)
-			repo_fmt->hash_algo = env_algo;
+		hash = env_algo;
 	} else if (cfg.hash != GIT_HASH_UNKNOWN) {
-		repo_fmt->hash_algo = cfg.hash;
+		hash = cfg.hash;
+	} else {
+		hash = GIT_HASH_DEFAULT;
 	}
 
 	/*
@@ -3022,6 +3018,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 	 * current storage formats do not change.
 	 */
 	if (repo_fmt->version >= 0) {
+		if (hash != repo_fmt->hash_algo)
+			die(_("attempt to reinitialize repository with different hash"));
 		if (ref_storage_format != repo_fmt->ref_storage_format)
 			die(_("attempt to reinitialize repository with different reference storage format"));
 		if ((ref_storage_payload || repo_fmt->ref_storage_payload) &&
@@ -3038,6 +3036,7 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 
 	free(cfg.ref_storage_payload);
 	free(repo_fmt->ref_storage_payload);
+	repo_fmt->hash_algo = hash;
 	repo_fmt->ref_storage_format = ref_storage_format;
 	repo_fmt->ref_storage_payload = ref_storage_payload;
 
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 9e57a06cea..d89f60bab9 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -516,6 +516,17 @@ test_expect_success 're-init from a linked worktree' '
 	)
 '
 
+test_expect_success 'git init uses default object format' '
+	test_when_finished "rm -rf repo" &&
+	(
+		sane_unset GIT_DEFAULT_HASH &&
+		git init repo &&
+		echo "$GIT_TEST_BUILTIN_HASH" >expect &&
+		git -C repo rev-parse --show-object-format >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'init honors GIT_DEFAULT_HASH' '
 	test_when_finished "rm -rf sha1 sha256" &&
 	GIT_DEFAULT_HASH=sha1 git init sha1 &&
@@ -602,6 +613,48 @@ do
 	'
 done
 
+test_expect_success 'init refuses to reinitialize with different hash' '
+	test_when_finished "rm -rf repo" &&
+	sane_unset GIT_DEFAULT_HASH &&
+	for algo in sha1 sha256
+	do
+		if test "$algo" = sha1
+		then
+			other=sha256
+		else
+			other=sha1
+		fi &&
+		git init --object-format="$algo" repo &&
+		cat >expect <<-EOF &&
+		fatal: attempt to reinitialize repository with different hash
+		EOF
+		test_must_fail git init --object-format="$other" repo 2>err &&
+		test_cmp expect err &&
+		rm -fr repo || return 1
+	done
+'
+
+test_expect_success 're-init does not modify hash from config' '
+	test_when_finished "rm -rf repo" &&
+	sane_unset GIT_DEFAULT_HASH &&
+	for algo in sha1 sha256
+	do
+		if test "$algo" = sha1
+		then
+			other=sha256
+		else
+			other=sha1
+		fi &&
+		git init --object-format="$algo" repo &&
+		test_config_global init.defaultObjectFormat "$other" &&
+		git init repo &&
+		echo "$algo" >expect &&
+		git -C repo rev-parse --show-object-format >actual &&
+		test_cmp expect actual &&
+		rm -fr repo || return 1
+	done
+'
+
 test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
 	test_when_finished "rm -rf explicit-v0" &&
 	git init --object-format=sha256 explicit-v0 &&

  parent reply	other threads:[~2026-11-19  1:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06 16:50 Default object format for new repositories in Git v3 Adam Dinwoodie
2026-11-06 20:46 ` brian m. carlson
2026-11-08  1:16 ` [PATCH] setup: default hash algo to GIT_HASH_DEFAULT brian m. carlson
2026-11-08  4:42   ` Junio C Hamano
2026-11-08 14:38     ` brian m. carlson
2026-11-08 18:22   ` Junio C Hamano
2026-11-09  2:21     ` Junio C Hamano
2026-11-09  9:54       ` Patrick Steinhardt
2026-11-13 22:17   ` [PATCH v2] setup: fix hash algorithm detection brian m. carlson
2026-11-14  0:16     ` Junio C Hamano
2026-11-16  6:22     ` Patrick Steinhardt
2026-11-19  1:06   ` brian m. carlson [this message]
2026-11-19  9:52     ` [PATCH v3] " Patrick Steinhardt
2026-11-19 16:26     ` Junio C Hamano
2026-11-19 16:38     ` Junio C Hamano

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=20261119010612.687733-1-sandals@crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=adam@dinwoodie.org \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --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