refs/files: skip creation of "refs/{heads,tags}" for worktrees

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>

Patrick Steinhardt committed Jan 8, 2024 at 11:05 UTC 2eb1d0c45271faf149a13b61bb74af52abd7b3aa
2 files changed +16 -8
refs.h
+2
@@ -126,6 +126,8 @@ int should_autocreate_reflog(const char *refname);
126
127 int is_branch(const char *refname);
128
129 +#define REFS_INIT_DB_IS_WORKTREE (1 << 0)
130 +
131 int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err);
132
133 /*
refs/files-backend.c
+14 -8
@@ -3221,7 +3221,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
3221 }
3222
3223 static int files_init_db(struct ref_store *ref_store,
3224 - int flags UNUSED,
3224 + int flags,
3225 struct strbuf *err UNUSED)
3226 {
3227 struct files_ref_store *refs =
@@ -3245,15 +3245,21 @@ static int files_init_db(struct ref_store *ref_store,
3245 adjust_shared_perm(sb.buf);
3246
3247 /*
3248 - * Create .git/refs/{heads,tags}
3248 + * There is no need to create directories for common refs when creating
3249 + * a worktree ref store.
3250 */
3250 - strbuf_reset(&sb);
3251 - files_ref_path(refs, &sb, "refs/heads");
3252 - safe_create_dir(sb.buf, 1);
3251 + if (!(flags & REFS_INIT_DB_IS_WORKTREE)) {
3252 + /*
3253 + * Create .git/refs/{heads,tags}
3254 + */
3255 + strbuf_reset(&sb);
3256 + files_ref_path(refs, &sb, "refs/heads");
3257 + safe_create_dir(sb.buf, 1);
3258
3254 - strbuf_reset(&sb);
3255 - files_ref_path(refs, &sb, "refs/tags");
3256 - safe_create_dir(sb.buf, 1);
3259 + strbuf_reset(&sb);
3260 + files_ref_path(refs, &sb, "refs/tags");
3261 + safe_create_dir(sb.buf, 1);
3262 + }
3263
3264 strbuf_release(&sb);
3265 return 0;