Skip to content

Commit 442b7e0

Browse files
committed
Merge branch 'ps/setup-reinit-fixes'
"git init" to reinitialize a repository that already exists cannot change the hash function and ref backends; such a request is silently ignored now. * ps/setup-reinit-fixes: setup: fix reinit of repos with incompatible GIT_DEFAULT_HASH setup: fix reinit of repos with incompatible GIT_DEFAULT_REF_FORMAT t0001: remove duplicate test
2 parents 9520f7d + 7e88640 commit 442b7e0

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

setup.c

Lines changed: 6 additions & 2 deletions
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
}
@@ -2534,7 +2536,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
25342536
ref_format = ref_storage_format_by_name(env);
25352537
if (ref_format == REF_STORAGE_FORMAT_UNKNOWN)
25362538
die(_("unknown ref storage format '%s'"), env);
2537-
repo_fmt->ref_storage_format = ref_format;
2539+
if (repo_fmt->version < 0 ||
2540+
repo_fmt->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
2541+
repo_fmt->ref_storage_format = ref_format;
25382542
} else if (cfg.ref_format != REF_STORAGE_FORMAT_UNKNOWN) {
25392543
repo_fmt->ref_storage_format = cfg.ref_format;
25402544
}

t/t0001-init.sh

Lines changed: 21 additions & 9 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 &&
@@ -697,6 +709,15 @@ do
697709
git -C refformat rev-parse --show-ref-format >actual &&
698710
test_cmp expect actual
699711
'
712+
713+
test_expect_success "reinit repository with GIT_DEFAULT_REF_FORMAT=$format does not change format" '
714+
test_when_finished "rm -rf refformat" &&
715+
git init refformat &&
716+
git -C refformat rev-parse --show-ref-format >expect &&
717+
GIT_DEFAULT_REF_FORMAT=$format git init refformat &&
718+
git -C refformat rev-parse --show-ref-format >actual &&
719+
test_cmp expect actual
720+
'
700721
done
701722

702723
test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" '
@@ -861,15 +882,6 @@ test_expect_success 're-init with includeIf.onbranch condition' '
861882
test_cmp expect actual
862883
'
863884

864-
test_expect_success 're-init with includeIf.onbranch condition' '
865-
test_when_finished "rm -rf repo" &&
866-
git init repo &&
867-
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
868-
echo $GIT_DEFAULT_REF_FORMAT >expect &&
869-
git -C repo rev-parse --show-ref-format >actual &&
870-
test_cmp expect actual
871-
'
872-
873885
test_expect_success 're-init skips non-matching includeIf.onbranch' '
874886
test_when_finished "rm -rf repo config" &&
875887
cat >config <<-EOF &&

0 commit comments

Comments
 (0)