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

[PATCH 32/43] refs.c: add method for initializing refs db

From
David Turner <dturner@twopensource.com>
Date
Sep 3, 2015, 01:55 UTC
Message-ID
<1441245313-11907-33-git-send-email-dturner@twopensource.com>
In-Reply-To
<1441245313-11907-1-git-send-email-dturner@twopensource.com>

Alternate refs backends might not need refs/heads and so on, so we make ref db initialization part of the backend.

Signed-off-by: David Turner <dturner@twopensource.com>
---
 builtin/init-db.c | 14 ++++----------
 refs-be-files.c   | 17 +++++++++++++++++
 refs.c            |  5 +++++
 refs.h            |  4 ++++
 4 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 06f8cca..0ce2baf 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -196,6 +196,7 @@ static int create_default_files(const char *template_path)
 	char junk[2];
 	int reinit;
 	int filemode;
+	struct strbuf err = STRBUF_INIT;
 
 	if (len > sizeof(path)-50)
 		die(_("insane git directory %s"), git_dir);
@@ -204,13 +205,6 @@ static int create_default_files(const char *template_path)
 	if (len && path[len-1] != '/')
 		path[len++] = '/';
 
-	/*
-	 * Create .git/refs/{heads,tags}
-	 */
-	safe_create_dir(git_path("refs"), 1);
-	safe_create_dir(git_path("refs/heads"), 1);
-	safe_create_dir(git_path("refs/tags"), 1);
-
 	/* Just look for `init.templatedir` */
 	git_config(git_init_db_config, NULL);
 
@@ -233,11 +227,11 @@ static int create_default_files(const char *template_path)
 	 */
 	if (shared_repository) {
 		adjust_shared_perm(get_git_dir());
-		adjust_shared_perm(git_path("refs"));
-		adjust_shared_perm(git_path("refs/heads"));
-		adjust_shared_perm(git_path("refs/tags"));
 	}
 
+	if (refs_initdb(&err, shared_repository))
+		die("failed to set up refs db: %s", err.buf);
+
 	/*
 	 * Create the default symlink from ".git/HEAD" to the "master"
 	 * branch, if it does not exist yet.
diff --git a/refs-be-files.c b/refs-be-files.c
index c49d44e..acf35b5 100644
--- a/refs-be-files.c
+++ b/refs-be-files.c
@@ -3722,10 +3722,27 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
 	return -1;
 }
 
+static int files_initdb(struct strbuf *err, int shared)
+{
+	/*
+	 * Create .git/refs/{heads,tags}
+	 */
+	safe_create_dir(git_path("refs"), 1);
+	safe_create_dir(git_path("refs/heads"), 1);
+	safe_create_dir(git_path("refs/tags"), 1);
+	if (shared) {
+		adjust_shared_perm(git_path("refs"));
+		adjust_shared_perm(git_path("refs/heads"));
+		adjust_shared_perm(git_path("refs/tags"));
+	}
+	return 0;
+}
+
 struct ref_be refs_be_files = {
 	NULL,
 	"files",
 	NULL,
+	files_initdb,
 	files_transaction_begin,
 	files_transaction_update,
 	files_transaction_create,
diff --git a/refs.c b/refs.c
index 002d24d..72dd66c 100644
--- a/refs.c
+++ b/refs.c
@@ -995,6 +995,11 @@ enum peel_status peel_object(const unsigned char *name, unsigned char *sha1)
 }
 
 /* backend functions */
+int refs_initdb(struct strbuf *err, int shared)
+{
+	return the_refs_backend->initdb(err, shared);
+}
+
 struct ref_transaction *ref_transaction_begin(struct strbuf *err)
 {
 	return the_refs_backend->transaction_begin(err);
diff --git a/refs.h b/refs.h
index da97a14..3134a28 100644
--- a/refs.h
+++ b/refs.h
@@ -68,6 +68,8 @@ extern int ref_exists(const char *refname);
 
 extern int is_branch(const char *refname);
 
+extern int refs_initdb(struct strbuf *err, int shared);
+
 /*
  * If refname is a non-symbolic reference that refers to a tag object,
  * and the tag can be (recursively) dereferenced to a non-tag object,
@@ -587,6 +589,7 @@ extern int reflog_expire(const char *refname, const unsigned char *sha1,
 
 /* refs backends */
 typedef void (*ref_backend_init_fn)(void *data);
+typedef int (*ref_backend_initdb_fn)(struct strbuf *err, int shared);
 typedef struct ref_transaction *(*ref_transaction_begin_fn)(struct strbuf *err);
 typedef int (*ref_transaction_update_fn)(struct ref_transaction *transaction,
 		const char *refname, const unsigned char *new_sha1,
@@ -653,6 +656,7 @@ struct ref_be {
 	struct ref_be *next;
 	const char *name;
 	ref_backend_init_fn init_backend;
+	ref_backend_initdb_fn initdb;
 	ref_transaction_begin_fn transaction_begin;
 	ref_transaction_update_fn transaction_update;
 	ref_transaction_create_fn transaction_create;
-- 
2.0.4.315.gad8727a-twtrsrc
Previous: David TurnerNext: David Turner
Message 38 of 54 in “refs lmdb backend”
  1. 00/43 refs lmdb backendDavid Turner, Sep 3, 2015
  2. 01/43 refs.c: create a public version of verify_refname_availableDavid Turner, Sep 3, 2015
  3. Junio C HamanoSep 3, 2015
  4. David TurnerSep 4, 2015
  5. 02/43 refs: make repack_without_refs and is_branch publicDavid Turner, Sep 3, 2015
  6. 04/43 refs.c: add a new refs.c file to hold all common refs codeDavid Turner, Sep 3, 2015
  7. 05/43 refs.c: move update_ref to refs.cDavid Turner, Sep 3, 2015
  8. 06/43 refs.c: move delete_ref and delete_refs to the common codeDavid Turner, Sep 3, 2015
  9. 07/43 refs.c: move read_ref_at to the common refs fileDavid Turner, Sep 3, 2015
  10. 08/43 refs.c: move the hidden refs functions to the common codeDavid Turner, Sep 3, 2015
  11. 09/43 refs.c: move dwim and friend functions to the common refs codeDavid Turner, Sep 3, 2015
  12. 10/43 refs.c: move warn_if_dangling_symref* to the common codeDavid Turner, Sep 3, 2015
  13. 11/43 refs.c: move read_ref, read_ref_full and ref_exists to the common codeDavid Turner, Sep 3, 2015
  14. 12/43 refs.c: move resolve_refdup to commonDavid Turner, Sep 3, 2015
  15. 13/43 refs.c: move check_refname_format to the common codeDavid Turner, Sep 3, 2015
  16. 14/43 refs.c: move is_branch to the common codeDavid Turner, Sep 3, 2015
  17. 15/43 refs.c: move prettify_refname to the common codeDavid Turner, Sep 3, 2015
  18. 16/43 refs.c: move ref iterators to the common codeDavid Turner, Sep 3, 2015
  19. 17/43 refs.c: move head_ref_namespaced to the common codeDavid Turner, Sep 3, 2015
  20. 18/43 refs-be-files.c: add a backend method structure with transaction functionsDavid Turner, Sep 3, 2015
  21. 19/43 refs-be-files.c: add methods for misc ref operationsDavid Turner, Sep 3, 2015
  22. 20/43 refs-be-files.c: add methods for the ref iteratorsDavid Turner, Sep 3, 2015
  23. Junio C HamanoSep 25, 2015
  24. David TurnerSep 25, 2015
  25. Junio C HamanoSep 25, 2015
  26. 21/43 refs-be-files.c: add method for for_each_reftype_...David Turner, Sep 3, 2015
  27. 22/43 refs-be-files.c: add do_for_each_per_worktree_refDavid Turner, Sep 3, 2015
  28. 23/43 refs.c: move refname_is_safe to the common codeDavid Turner, Sep 3, 2015
  29. 24/43 refs.h: document make refname_is_safe and add it to headerDavid Turner, Sep 3, 2015
  30. David TurnerSep 3, 2015
  31. 25/43 refs.c: move copy_msg to the common codeDavid Turner, Sep 3, 2015
  32. 26/43 refs.c: move peel_object to the common codeDavid Turner, Sep 3, 2015
  33. 27/43 refs.c: move should_autocreate_reflog to common codeDavid Turner, Sep 3, 2015
  34. 28/43 refs.c: add ref backend init functionDavid Turner, Sep 3, 2015
  35. 29/43 refs.c: add methods for reflogDavid Turner, Sep 3, 2015
  36. 30/43 refs.c: add method for initial ref transaction commitDavid Turner, Sep 3, 2015
  37. 31/43 initdb: move safe_create_dir into common codeDavid Turner, Sep 3, 2015
  38. 32/43 refs.c: add method for initializing refs dbDavid Turner, Sep 3, 2015
  39. 33/43 refs.c: make struct ref_transaction genericDavid Turner, Sep 3, 2015
  40. 34/43 refs-be-files.c: add method to rename refsDavid Turner, Sep 3, 2015
  41. 35/43 refs-be-files.c: add method to expire reflogsDavid Turner, Sep 3, 2015
  42. 36/43 run-command: track total number of commands runDavid Turner, Sep 3, 2015
  43. 37/43 refs: move some defines from refs-be-files.c to refs.hDavid Turner, Sep 3, 2015
  44. 38/43 refs: make some files backend functions publicDavid Turner, Sep 3, 2015
  45. 39/43 refs: break out a ref conflict checkDavid Turner, Sep 3, 2015
  46. 40/43 refs: allow ref backend to be set for cloneDavid Turner, Sep 3, 2015
  47. 41/43 refs: add register_refs_backendDavid Turner, Sep 3, 2015
  48. 42/43 refs: add LMDB refs backendDavid Turner, Sep 3, 2015
  49. Junio C HamanoSep 3, 2015
  50. 43/43 refs: tests for db backendDavid Turner, Sep 3, 2015
  51. Junio C HamanoSep 3, 2015
  52. David TurnerSep 4, 2015
  53. David TurnerSep 17, 2015
  54. Junio C HamanoSep 17, 2015

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.