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

[PATCH v2] setup: fix hash algorithm detection

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Nov 13, 2026, 22:17 UTC
Message-ID
<20261113221753.3408862-1-sandals@crustytoothpaste.net>
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.

Reported-by: Adam Dinwoodie <adam@dinwoodie.org>
Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 setup.c         | 22 +++++++++++--------
 t/t0001-init.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 9 deletions(-)
diff --git a/setup.c b/setup.c
index 61bcdafe82..14c6e1a67d 100644
--- a/setup.c
+++ b/setup.c
@@ -2817,19 +2817,20 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 	 * 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;
 	}
 
 	/*
@@ -2899,6 +2900,8 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 	 * current ref storage format does 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) &&
@@ -2908,6 +2911,7 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 	}
 
 	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 f1d227f271..d8244a2dec 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -516,6 +516,22 @@ 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 &&
+		if test_have_prereq WITH_BREAKING_CHANGES
+		then
+			echo sha256
+		else
+			echo sha1
+		fi >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 +618,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 &&
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 9 of 12 in “Default object format for new repositories in Git v3”
  1. Adam DinwoodieNov 6, 2026
  2. brian m. carlsonNov 6, 2026
  3. setup: default hash algo to GIT_HASH_DEFAULTbrian m. carlson, Nov 8, 2026
  4. Junio C HamanoNov 8, 2026
  5. brian m. carlsonNov 8, 2026
  6. Junio C HamanoNov 8, 2026
  7. Junio C HamanoNov 9, 2026
  8. Patrick SteinhardtNov 9, 2026
  9. setup: fix hash algorithm detectionbrian m. carlson, Nov 13, 2026
  10. Junio C HamanoNov 14, 2026
  11. Patrick SteinhardtNov 16, 2026
  12. setup: fix hash algorithm detectionbrian m. carlson, Nov 19, 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.