]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs/files: skip creation of "refs/{heads,tags}" for worktrees
authorPatrick Steinhardt <ps@pks.im>
Mon, 8 Jan 2024 10:05:35 +0000 (11:05 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Jan 2024 21:17:30 +0000 (13:17 -0800)
The files ref backend will create both "refs/heads" and "refs/tags" in
the Git directory. While this logic makes sense for normal repositories,
it does not for worktrees because those refs are "common" refs that
would always be contained in the main repository's ref database.

Introduce a new flag telling the backend that it is expected to create a
per-worktree ref database and skip creation of these dirs in the files
backend when the flag is set. No other backends (currently) need
worktree-specific logic, so this is the only required change to start
creating per-worktree ref databases via `refs_init_db()`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.h
refs/files-backend.c

diff --git a/refs.h b/refs.h
index 114caa272a37c00d6fad8fe93f979e0a3da29c5c..c2dfe451a105c6d5e6be0c16ffd5de5db273f6b6 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -126,6 +126,8 @@ int should_autocreate_reflog(const char *refname);
 
 int is_branch(const char *refname);
 
+#define REFS_INIT_DB_IS_WORKTREE (1 << 0)
+
 int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err);
 
 /*
index 054ecdbca34886cb59aef0b3662290147354455c..6dae37e351a78210aac08d94f569d7d768c741c8 100644 (file)
@@ -3221,7 +3221,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
 }
 
 static int files_init_db(struct ref_store *ref_store,
-                        int flags UNUSED,
+                        int flags,
                         struct strbuf *err UNUSED)
 {
        struct files_ref_store *refs =
@@ -3245,15 +3245,21 @@ static int files_init_db(struct ref_store *ref_store,
        adjust_shared_perm(sb.buf);
 
        /*
-        * Create .git/refs/{heads,tags}
+        * There is no need to create directories for common refs when creating
+        * a worktree ref store.
         */
-       strbuf_reset(&sb);
-       files_ref_path(refs, &sb, "refs/heads");
-       safe_create_dir(sb.buf, 1);
+       if (!(flags & REFS_INIT_DB_IS_WORKTREE)) {
+               /*
+                * Create .git/refs/{heads,tags}
+                */
+               strbuf_reset(&sb);
+               files_ref_path(refs, &sb, "refs/heads");
+               safe_create_dir(sb.buf, 1);
 
-       strbuf_reset(&sb);
-       files_ref_path(refs, &sb, "refs/tags");
-       safe_create_dir(sb.buf, 1);
+               strbuf_reset(&sb);
+               files_ref_path(refs, &sb, "refs/tags");
+               safe_create_dir(sb.buf, 1);
+       }
 
        strbuf_release(&sb);
        return 0;