]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: fix reinit of repos with incompatible GIT_DEFAULT_REF_FORMAT
authorPatrick Steinhardt <ps@pks.im>
Thu, 30 Jan 2025 16:24:18 +0000 (17:24 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Jan 2025 22:36:40 +0000 (14:36 -0800)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
setup.c
t/t0001-init.sh

diff --git a/setup.c b/setup.c
index 8a488f3e7c74b1ffeef8d25ffb656b990a4c06a6..53ffeabc5b44d0214830f8279ec11c870f8d0a79 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -2534,7 +2534,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
                ref_format = ref_storage_format_by_name(env);
                if (ref_format == REF_STORAGE_FORMAT_UNKNOWN)
                        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) {
                repo_fmt->ref_storage_format = cfg.ref_format;
        }
index 213d5984b134435e1884f45b65c6ea3adbfb1141..6dff8b75f1f991bc7c7d3de2ce47ca26a463b1cc 100755 (executable)
@@ -697,6 +697,15 @@ do
                git -C refformat rev-parse --show-ref-format >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
 
 test_expect_success "--ref-format= overrides GIT_DEFAULT_REF_FORMAT" '