git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] setup: fix reinit of repos with incompatible GIT_DEFAULT_REF_FORMAT

From
Patrick Steinhardt <ps@pks.im>
Date
Jan 30, 2025, 16:24 UTC
Message-ID
<20250130-b4-pks-reinit-default-ref-format-v1-2-d2769ca01207@pks.im>
In-Reply-To
<20250130-b4-pks-reinit-default-ref-format-v1-0-d2769ca01207@pks.im>

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>
---
 setup.c         | 4 +++-
 t/t0001-init.sh | 9 +++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/setup.c b/setup.c
index 8a488f3e7c..53ffeabc5b 100644
--- 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;
 	}
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 213d5984b1..6dff8b75f1 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -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" '
-- 
2.48.1.468.gbf5f394be8.dirty
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 11 in “setup: fix reinit of repos with different formats”
  1. 0/3 setup: fix reinit of repos with different formatsPatrick Steinhardt, Jan 30, 2025
  2. 1/3 t0001: remove duplicate testPatrick Steinhardt, Jan 30, 2025
  3. 3/3 setup: fix reinit of repos with incompatible GIT_DEFAULT_HASHPatrick Steinhardt, Jan 30, 2025
  4. Junio C HamanoJan 31, 2025
  5. 2/3 setup: fix reinit of repos with incompatible GIT_DEFAULT_REF_FORMATPatrick Steinhardt, Jan 30, 2025
  6. Junio C HamanoJan 30, 2025
  7. Junio C HamanoJan 31, 2025
  8. Patrick SteinhardtFeb 3, 2025
  9. Junio C HamanoFeb 3, 2025
  10. Patrick SteinhardtFeb 3, 2025
  11. brian m. carlsonJan 30, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.