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

[PATCH 1/6] refs: prepare `refs_init_db()` for initializing worktree refs

From
Patrick Steinhardt <ps@pks.im>
Date
Dec 28, 2023, 09:59 UTC
Message-ID
<6cb4e0a99fff52c35ea293f855fd3a34bcba7ab1.1703754513.git.ps@pks.im>
In-Reply-To
<cover.1703754513.git.ps@pks.im>

The purpose of `refs_init_db()` is to initialize the on-disk files of a new ref database. The function is quite inflexible right now though, as callers can neither specify the `struct ref_store` nor can they pass any flags.

Refactor the interface to accept both of these. This will be required so that we can start initializing per-worktree ref databases via the ref backend instead of open-coding the initialization in "worktree.c".

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 refs.c                | 6 ++----
 refs.h                | 2 +-
 refs/debug.c          | 4 ++--
 refs/files-backend.c  | 4 +++-
 refs/packed-backend.c | 1 +
 refs/refs-internal.h  | 4 +++-
 setup.c               | 2 +-
 7 files changed, 13 insertions(+), 10 deletions(-)
diff --git a/refs.c b/refs.c
index 16bfa21df7..87e2659db7 100644
--- a/refs.c
+++ b/refs.c
@@ -1928,11 +1928,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
 }
 
 /* backend functions */
-int refs_init_db(struct strbuf *err)
+int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err)
 {
-	struct ref_store *refs = get_main_ref_store(the_repository);
-
-	return refs->be->init_db(refs, err);
+	return refs->be->init_db(refs, flags, err);
 }
 
 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
diff --git a/refs.h b/refs.h
index ff113bb12a..6090e92578 100644
--- a/refs.h
+++ b/refs.h
@@ -123,7 +123,7 @@ int should_autocreate_reflog(const char *refname);
 
 int is_branch(const char *refname);
 
-int refs_init_db(struct strbuf *err);
+int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err);
 
 /*
  * Return the peeled value of the oid currently being iterated via
diff --git a/refs/debug.c b/refs/debug.c
index 83b7a0ba65..c061def8b5 100644
--- a/refs/debug.c
+++ b/refs/debug.c
@@ -33,10 +33,10 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor
 	return (struct ref_store *)res;
 }
 
-static int debug_init_db(struct ref_store *refs, struct strbuf *err)
+static int debug_init_db(struct ref_store *refs, int flags, struct strbuf *err)
 {
 	struct debug_ref_store *drefs = (struct debug_ref_store *)refs;
-	int res = drefs->refs->be->init_db(drefs->refs, err);
+	int res = drefs->refs->be->init_db(drefs->refs, flags, err);
 	trace_printf_key(&trace_refs, "init_db: %d\n", res);
 	return res;
 }
diff --git a/refs/files-backend.c b/refs/files-backend.c
index ad8b1d143f..387eeb5037 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -3220,7 +3220,9 @@ static int files_reflog_expire(struct ref_store *ref_store,
 	return -1;
 }
 
-static int files_init_db(struct ref_store *ref_store, struct strbuf *err UNUSED)
+static int files_init_db(struct ref_store *ref_store,
+			 int flags UNUSED,
+			 struct strbuf *err UNUSED)
 {
 	struct files_ref_store *refs =
 		files_downcast(ref_store, REF_STORE_WRITE, "init_db");
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index b9fa097a29..3f10bccf44 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -1246,6 +1246,7 @@ static const char PACKED_REFS_HEADER[] =
 	"# pack-refs with: peeled fully-peeled sorted \n";
 
 static int packed_init_db(struct ref_store *ref_store UNUSED,
+			  int flags UNUSED,
 			  struct strbuf *err UNUSED)
 {
 	/* Nothing to do. */
diff --git a/refs/refs-internal.h b/refs/refs-internal.h
index 4af83bf9a5..a33b28c19d 100644
--- a/refs/refs-internal.h
+++ b/refs/refs-internal.h
@@ -529,7 +529,9 @@ typedef struct ref_store *ref_store_init_fn(struct repository *repo,
 					    const char *gitdir,
 					    unsigned int flags);
 
-typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);
+typedef int ref_init_db_fn(struct ref_store *refs,
+			   int flags,
+			   struct strbuf *err);
 
 typedef int ref_transaction_prepare_fn(struct ref_store *refs,
 				       struct ref_transaction *transaction,
diff --git a/setup.c b/setup.c
index bc90bbd033..a4eb2a38ac 100644
--- a/setup.c
+++ b/setup.c
@@ -1919,7 +1919,7 @@ void create_reference_database(const char *initial_branch, int quiet)
 	safe_create_dir(git_path("refs"), 1);
 	adjust_shared_perm(git_path("refs"));
 
-	if (refs_init_db(&err))
+	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
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 2 of 21 in “worktree: initialize refdb via ref backends”
  1. 0/6 worktree: initialize refdb via ref backendsPatrick Steinhardt, Dec 28, 2023
  2. 1/6 refs: prepare `refs_init_db()` for initializing worktree refsPatrick Steinhardt, Dec 28, 2023
  3. 2/6 setup: move creation of "refs/" into the files backendPatrick Steinhardt, Dec 28, 2023
  4. Karthik NayakJan 2, 2024
  5. Patrick SteinhardtJan 3, 2024
  6. 3/6 refs/files: skip creation of "refs/{heads,tags}" for worktreesPatrick Steinhardt, Dec 28, 2023
  7. Eric SunshineDec 29, 2023
  8. 4/6 builtin/worktree: move setup of commondir file earlierPatrick Steinhardt, Dec 28, 2023
  9. 5/6 worktree: expose interface to look up worktree by namePatrick Steinhardt, Dec 28, 2023
  10. 6/6 builtin/worktree: create refdb via ref backendPatrick Steinhardt, Dec 28, 2023
  11. Junio C HamanoDec 28, 2023
  12. Patrick SteinhardtDec 28, 2023
  13. 0/6 worktree: initialize refdb via ref backendsPatrick Steinhardt, Jan 8, 2024
  14. 1/6 refs: prepare `refs_init_db()` for initializing worktree refsPatrick Steinhardt, Jan 8, 2024
  15. 2/6 setup: move creation of "refs/" into the files backendPatrick Steinhardt, Jan 8, 2024
  16. 3/6 refs/files: skip creation of "refs/{heads,tags}" for worktreesPatrick Steinhardt, Jan 8, 2024
  17. 4/6 builtin/worktree: move setup of commondir file earlierPatrick Steinhardt, Jan 8, 2024
  18. 5/6 worktree: expose interface to look up worktree by namePatrick Steinhardt, Jan 8, 2024
  19. 6/6 builtin/worktree: create refdb via ref backendPatrick Steinhardt, Jan 8, 2024
  20. Karthik NayakJan 16, 2024
  21. Junio C HamanoJan 16, 2024

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.