Skip to content

Commit 796fda3

Browse files
pks-tgitster
authored andcommitted
setup: fix reinit of repos with incompatible GIT_DEFAULT_REF_FORMAT
The GIT_DEFAULT_REF_FORMAT environment variable can be set to influence the default ref format that new repostiories shall be initialized with. While this is the expected behaviour when creating a new repository, it is not when reinitializing a repository: we should retain the ref format currently used by it in that case. This doesn't work correctly right now: $ git init --ref-format=files repo Initialized empty Git repository in /tmp/repo/.git/ $ GIT_DEFAULT_REF_FORMAT=reftable git init repo fatal: could not open '/tmp/repo/.git/refs/heads' for writing: Is a directory Instead of retaining the current ref format, the reinitialization tries to reinitialize the repository with the different format. This action fails when git-init(1) tries to write the ".git/refs/heads" stub, which in the context of the reftable backend is always written as a file so that we can detect clients which inadvertently try to access the repo with the wrong ref format. Seems like the protection mechanism works for this case, as well. Fix the issue by ignoring the environment variable in case the repo has already been initialized with a ref storage format. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 150c31b commit 796fda3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

setup.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
25342534
ref_format = ref_storage_format_by_name(env);
25352535
if (ref_format == REF_STORAGE_FORMAT_UNKNOWN)
25362536
die(_("unknown ref storage format '%s'"), env);
2537-
repo_fmt->ref_storage_format = ref_format;
2537+
if (repo_fmt->version < 0 ||
2538+
repo_fmt->ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
2539+
repo_fmt->ref_storage_format = ref_format;
25382540
} else if (cfg.ref_format != REF_STORAGE_FORMAT_UNKNOWN) {
25392541
repo_fmt->ref_storage_format = cfg.ref_format;
25402542
}

t/t0001-init.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,15 @@ do
697697
git -C refformat rev-parse --show-ref-format >actual &&
698698
test_cmp expect actual
699699
'
700+
701+
test_expect_success "reinit repository with GIT_DEFAULT_REF_FORMAT=$format does not change format" '
702+
test_when_finished "rm -rf refformat" &&
703+
git init refformat &&
704+
git -C refformat rev-parse --show-ref-format >expect &&
705+
GIT_DEFAULT_REF_FORMAT=$format git init refformat &&
706+
git -C refformat rev-parse --show-ref-format >actual &&
707+
test_cmp expect actual
708+
'
700709
done
701710

702711
test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" '

0 commit comments

Comments
 (0)