git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Subject: [PATCH 2/6] setup: move creation of "refs/" into the files backend
Date: Thu, 28 Dec 2023 10:59:56 +0100	[thread overview]
Message-ID: <ae013eaa4aba0d68172ff03dbe9f2c2bca596285.1703754513.git.ps@pks.im> (raw)
In-Reply-To: <cover.1703754513.git.ps@pks.im>

[-- Attachment #1: Type: text/plain, Size: 3400 bytes --]

When creating the ref database we unconditionally create the "refs/"
directory in "setup.c". This is a mandatory prerequisite for all Git
repositories regardless of the ref backend in use, because Git will be
unable to detect the directory as a repository if "refs/" doesn't exist.

We are about to add another new caller that will want to create a ref
database when creating worktrees. We would require the same logic to
create the "refs/" directory even though the caller really should not
care about such low-level details. Ideally, the ref database should be
fully initialized after calling `refs_init_db()`.

Move the code to create the directory into the files backend itself to
make it so. This means that future ref backends will also need to have
equivalent logic around to ensure that the directory exists, but it
seems a lot more sensible to have it this way round than to require
callers to create the directory themselves.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 refs/files-backend.c | 17 +++++++++++++++++
 setup.c              | 15 ---------------
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/refs/files-backend.c b/refs/files-backend.c
index 387eeb5037..ed47c5dc08 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3228,9 +3228,26 @@ static int files_init_db(struct ref_store *ref_store,
 		files_downcast(ref_store, REF_STORE_WRITE, "init_db");
 	struct strbuf sb = STRBUF_INIT;
 
+	/*
+	 * We need to create a "refs" dir in any case so that older versions of
+	 * Git can tell that this is a repository. This serves two main purposes:
+	 *
+	 * - Clients will know to stop walking the parent-directory chain when
+	 *   detecting the Git repository. Otherwise they may end up detecting
+	 *   a Git repository in a parent directory instead.
+	 *
+	 * - Instead of failing to detect a repository with unknown reference
+	 *   format altogether, old clients will print an error saying that
+	 *   they do not understand the reference format extension.
+	 */
+	strbuf_addf(&sb, "%s/refs", ref_store->gitdir);
+	safe_create_dir(sb.buf, 1);
+	adjust_shared_perm(sb.buf);
+
 	/*
 	 * Create .git/refs/{heads,tags}
 	 */
+	strbuf_reset(&sb);
 	files_ref_path(refs, &sb, "refs/heads");
 	safe_create_dir(sb.buf, 1);
 
diff --git a/setup.c b/setup.c
index a4eb2a38ac..f2d55994e2 100644
--- a/setup.c
+++ b/setup.c
@@ -1904,21 +1904,6 @@ void create_reference_database(const char *initial_branch, int quiet)
 	struct strbuf err = STRBUF_INIT;
 	int reinit = is_reinit();
 
-	/*
-	 * We need to create a "refs" dir in any case so that older versions of
-	 * Git can tell that this is a repository. This serves two main purposes:
-	 *
-	 * - Clients will know to stop walking the parent-directory chain when
-	 *   detecting the Git repository. Otherwise they may end up detecting
-	 *   a Git repository in a parent directory instead.
-	 *
-	 * - Instead of failing to detect a repository with unknown reference
-	 *   format altogether, old clients will print an error saying that
-	 *   they do not understand the reference format extension.
-	 */
-	safe_create_dir(git_path("refs"), 1);
-	adjust_shared_perm(git_path("refs"));
-
 	if (refs_init_db(get_main_ref_store(the_repository), 0, &err))
 		die("failed to set up refs db: %s", err.buf);
 
-- 
2.43.GIT


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2023-12-28  9:59 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-28  9:59 [PATCH 0/6] worktree: initialize refdb via ref backends Patrick Steinhardt
2023-12-28  9:59 ` [PATCH 1/6] refs: prepare `refs_init_db()` for initializing worktree refs Patrick Steinhardt
2023-12-28  9:59 ` Patrick Steinhardt [this message]
2024-01-02 13:23   ` [PATCH 2/6] setup: move creation of "refs/" into the files backend Karthik Nayak
2024-01-03  8:33     ` Patrick Steinhardt
2023-12-28 10:00 ` [PATCH 3/6] refs/files: skip creation of "refs/{heads,tags}" for worktrees Patrick Steinhardt
2023-12-29 10:35   ` Eric Sunshine
2023-12-28 10:00 ` [PATCH 4/6] builtin/worktree: move setup of commondir file earlier Patrick Steinhardt
2023-12-28 10:00 ` [PATCH 5/6] worktree: expose interface to look up worktree by name Patrick Steinhardt
2023-12-28 10:00 ` [PATCH 6/6] builtin/worktree: create refdb via ref backend Patrick Steinhardt
2023-12-28 18:11 ` [PATCH 0/6] worktree: initialize refdb via ref backends Junio C Hamano
2023-12-28 19:57   ` Patrick Steinhardt
2024-01-08 10:05 ` [PATCH v2 " Patrick Steinhardt
2024-01-08 10:05   ` [PATCH v2 1/6] refs: prepare `refs_init_db()` for initializing worktree refs Patrick Steinhardt
2024-01-08 10:05   ` [PATCH v2 2/6] setup: move creation of "refs/" into the files backend Patrick Steinhardt
2024-01-08 10:05   ` [PATCH v2 3/6] refs/files: skip creation of "refs/{heads,tags}" for worktrees Patrick Steinhardt
2024-01-08 10:05   ` [PATCH v2 4/6] builtin/worktree: move setup of commondir file earlier Patrick Steinhardt
2024-01-08 10:05   ` [PATCH v2 5/6] worktree: expose interface to look up worktree by name Patrick Steinhardt
2024-01-08 10:05   ` [PATCH v2 6/6] builtin/worktree: create refdb via ref backend Patrick Steinhardt
2024-01-16  9:17   ` [PATCH v2 0/6] worktree: initialize refdb via ref backends Karthik Nayak
2024-01-16 17:53     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ae013eaa4aba0d68172ff03dbe9f2c2bca596285.1703754513.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).