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
This commit is contained in:
Junio C Hamano
2025-02-10 10:18:29 -08:00
2 changed files with 27 additions and 11 deletions

View File

@@ -2517,7 +2517,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
int env_algo = hash_algo_by_name(env); int env_algo = hash_algo_by_name(env);
if (env_algo == GIT_HASH_UNKNOWN) if (env_algo == GIT_HASH_UNKNOWN)
die(_("unknown hash algorithm '%s'"), env); 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) { } else if (cfg.hash != GIT_HASH_UNKNOWN) {
repo_fmt->hash_algo = cfg.hash; repo_fmt->hash_algo = cfg.hash;
} }
@@ -2534,7 +2536,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
ref_format = ref_storage_format_by_name(env); ref_format = ref_storage_format_by_name(env);
if (ref_format == REF_STORAGE_FORMAT_UNKNOWN) if (ref_format == REF_STORAGE_FORMAT_UNKNOWN)
die(_("unknown ref storage format '%s'"), env); die(_("unknown ref storage format '%s'"), env);
repo_fmt->ref_storage_format = ref_format; if (repo_fmt->version < 0 ||
repo_fmt->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
repo_fmt->ref_storage_format = ref_format;
} else if (cfg.ref_format != REF_STORAGE_FORMAT_UNKNOWN) { } else if (cfg.ref_format != REF_STORAGE_FORMAT_UNKNOWN) {
repo_fmt->ref_storage_format = cfg.ref_format; repo_fmt->ref_storage_format = cfg.ref_format;
} }

View File

@@ -586,6 +586,18 @@ test_expect_success 'GIT_DEFAULT_HASH overrides init.defaultObjectFormat' '
echo sha256 >expected 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_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
test_when_finished "rm -rf explicit-v0" && test_when_finished "rm -rf explicit-v0" &&
git init --object-format=sha256 explicit-v0 && git init --object-format=sha256 explicit-v0 &&
@@ -697,6 +709,15 @@ do
git -C refformat rev-parse --show-ref-format >actual && git -C refformat rev-parse --show-ref-format >actual &&
test_cmp expect actual test_cmp expect actual
' '
test_expect_success "reinit repository with GIT_DEFAULT_REF_FORMAT=$format does not change format" '
test_when_finished "rm -rf refformat" &&
git init refformat &&
git -C refformat rev-parse --show-ref-format >expect &&
GIT_DEFAULT_REF_FORMAT=$format git init refformat &&
git -C refformat rev-parse --show-ref-format >actual &&
test_cmp expect actual
'
done done
test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" ' test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" '
@@ -861,15 +882,6 @@ test_expect_success 're-init with includeIf.onbranch condition' '
test_cmp expect actual test_cmp expect actual
' '
test_expect_success 're-init with includeIf.onbranch condition' '
test_when_finished "rm -rf repo" &&
git init repo &&
git -c includeIf.onbranch:nonexistent.path=/does/not/exist init repo &&
echo $GIT_DEFAULT_REF_FORMAT >expect &&
git -C repo rev-parse --show-ref-format >actual &&
test_cmp expect actual
'
test_expect_success 're-init skips non-matching includeIf.onbranch' ' test_expect_success 're-init skips non-matching includeIf.onbranch' '
test_when_finished "rm -rf repo config" && test_when_finished "rm -rf repo config" &&
cat >config <<-EOF && cat >config <<-EOF &&