Skip to content

Commit 7e88640

Browse files
pks-tgitster
authored andcommitted
setup: fix reinit of repos with incompatible GIT_DEFAULT_HASH
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 796fda3 commit 7e88640

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

setup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2517,7 +2517,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
25172517
int env_algo = hash_algo_by_name(env);
25182518
if (env_algo == GIT_HASH_UNKNOWN)
25192519
die(_("unknown hash algorithm '%s'"), env);
2520-
repo_fmt->hash_algo = env_algo;
2520+
if (repo_fmt->version < 0 ||
2521+
repo_fmt->hash_algo == GIT_HASH_UNKNOWN)
2522+
repo_fmt->hash_algo = env_algo;
25212523
} else if (cfg.hash != GIT_HASH_UNKNOWN) {
25222524
repo_fmt->hash_algo = cfg.hash;
25232525
}

t/t0001-init.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,18 @@ test_expect_success 'GIT_DEFAULT_HASH overrides init.defaultObjectFormat' '
586586
echo sha256 >expected
587587
'
588588

589+
for hash in sha1 sha256
590+
do
591+
test_expect_success "reinit repository with GIT_DEFAULT_HASH=$hash does not change format" '
592+
test_when_finished "rm -rf repo" &&
593+
git init repo &&
594+
git -C repo rev-parse --show-object-format >expect &&
595+
GIT_DEFAULT_HASH=$hash git init repo &&
596+
git -C repo rev-parse --show-object-format >actual &&
597+
test_cmp expect actual
598+
'
599+
done
600+
589601
test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
590602
test_when_finished "rm -rf explicit-v0" &&
591603
git init --object-format=sha256 explicit-v0 &&

0 commit comments

Comments
 (0)