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

[PATCH 3/3] setup: fix reinit of repos with incompatible GIT_DEFAULT_HASH

From
Patrick Steinhardt <ps@pks.im>
Date
Jan 30, 2025, 16:24 UTC
Message-ID
<20250130-b4-pks-reinit-default-ref-format-v1-3-d2769ca01207@pks.im>
In-Reply-To
<20250130-b4-pks-reinit-default-ref-format-v1-0-d2769ca01207@pks.im>

The exact same issue as described in the preceding commit also exists for GIT_DEFAULT_HASH. Thus, reinitializing a repository that e.g. uses SHA1 with `GIT_DEFAULT_HASH=sha256 git init` will cause the object format of that repository to change to SHA256. This is of course bogus as any existing objects and refs will not be converted, thus causing repository corruption:

    $ git init repo
    Initialized empty Git repository in /tmp/repo/.git/
    $ cd repo/
    $ git commit --allow-empty -m message
    [main (root-commit) 35a7344] message
    $ GIT_DEFAULT_HASH=sha256 git init
    Reinitialized existing Git repository in /tmp/repo/.git/
    $ git show
    fatal: your current branch appears to be broken

Fix the issue by ignoring the environment variable in case the repo has already been initialized with an object hash.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 setup.c         |  4 +++-
 t/t0001-init.sh | 12 ++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/setup.c b/setup.c
index 53ffeabc5b..7da7aa8984 100644
--- a/setup.c
+++ b/setup.c
@@ -2517,7 +2517,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 		int env_algo = hash_algo_by_name(env);
 		if (env_algo == GIT_HASH_UNKNOWN)
 			die(_("unknown hash algorithm '%s'"), env);
-		repo_fmt->hash_algo = env_algo;
+		if (repo_fmt->version < 0 ||
+		    repo_fmt->hash_algo == GIT_HASH_UNKNOWN)
+			repo_fmt->hash_algo = env_algo;
 	} else if (cfg.hash != GIT_HASH_UNKNOWN) {
 		repo_fmt->hash_algo = cfg.hash;
 	}
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 6dff8b75f1..c49d9e0d38 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -586,6 +586,18 @@ test_expect_success 'GIT_DEFAULT_HASH overrides init.defaultObjectFormat' '
 	echo sha256 >expected
 '
 
+for hash in sha1 sha256
+do
+	test_expect_success "reinit repository with GIT_DEFAULT_HASH=$hash does not change format" '
+		test_when_finished "rm -rf repo" &&
+		git init repo &&
+		git -C repo rev-parse --show-object-format >expect &&
+		GIT_DEFAULT_HASH=$hash git init repo &&
+		git -C repo rev-parse --show-object-format >actual &&
+		test_cmp expect actual
+	'
+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 &&
-- 
2.48.1.468.gbf5f394be8.dirty
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 3 of 11 in “setup: fix reinit of repos with different formats”
  1. 0/3 setup: fix reinit of repos with different formatsPatrick Steinhardt, Jan 30, 2025
  2. 1/3 t0001: remove duplicate testPatrick Steinhardt, Jan 30, 2025
  3. 3/3 setup: fix reinit of repos with incompatible GIT_DEFAULT_HASHPatrick Steinhardt, Jan 30, 2025
  4. Junio C HamanoJan 31, 2025
  5. 2/3 setup: fix reinit of repos with incompatible GIT_DEFAULT_REF_FORMATPatrick Steinhardt, Jan 30, 2025
  6. Junio C HamanoJan 30, 2025
  7. Junio C HamanoJan 31, 2025
  8. Patrick SteinhardtFeb 3, 2025
  9. Junio C HamanoFeb 3, 2025
  10. Patrick SteinhardtFeb 3, 2025
  11. brian m. carlsonJan 30, 2025

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.