Skip to content

Commit 6fb5acf

Browse files
dturner-twgitster
authored andcommitted
refs: add methods to init refs db
Alternate refs backends might not need the refs/heads directory and so on, so we make ref db initialization part of the backend. Signed-off-by: David Turner <dturner@twopensource.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a27dcf8 commit 6fb5acf

5 files changed

Lines changed: 42 additions & 10 deletions

File tree

builtin/init-db.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,7 @@ static int create_default_files(const char *template_path)
180180
char junk[2];
181181
int reinit;
182182
int filemode;
183-
184-
/*
185-
* Create .git/refs/{heads,tags}
186-
*/
187-
safe_create_dir(git_path_buf(&buf, "refs"), 1);
188-
safe_create_dir(git_path_buf(&buf, "refs/heads"), 1);
189-
safe_create_dir(git_path_buf(&buf, "refs/tags"), 1);
183+
struct strbuf err = STRBUF_INIT;
190184

191185
/* Just look for `init.templatedir` */
192186
git_config(git_init_db_config, NULL);
@@ -210,11 +204,18 @@ static int create_default_files(const char *template_path)
210204
*/
211205
if (get_shared_repository()) {
212206
adjust_shared_perm(get_git_dir());
213-
adjust_shared_perm(git_path_buf(&buf, "refs"));
214-
adjust_shared_perm(git_path_buf(&buf, "refs/heads"));
215-
adjust_shared_perm(git_path_buf(&buf, "refs/tags"));
216207
}
217208

209+
/*
210+
* We need to create a "refs" dir in any case so that older
211+
* versions of git can tell that this is a repository.
212+
*/
213+
safe_create_dir(git_path("refs"), 1);
214+
adjust_shared_perm(git_path("refs"));
215+
216+
if (refs_init_db(&err))
217+
die("failed to set up refs db: %s", err.buf);
218+
218219
/*
219220
* Create the default symlink from ".git/HEAD" to the "master"
220221
* branch, if it does not exist yet.

refs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,14 @@ static const char *resolve_ref_recursively(struct ref_store *refs,
12921292
return NULL;
12931293
}
12941294

1295+
/* backend functions */
1296+
int refs_init_db(struct strbuf *err)
1297+
{
1298+
struct ref_store *refs = get_ref_store(NULL);
1299+
1300+
return refs->be->init_db(refs, err);
1301+
}
1302+
12951303
const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
12961304
unsigned char *sha1, int *flags)
12971305
{

refs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ int ref_exists(const char *refname);
6666

6767
int is_branch(const char *refname);
6868

69+
extern int refs_init_db(struct strbuf *err);
70+
6971
/*
7072
* If refname is a non-symbolic reference that refers to a tag object,
7173
* and the tag can be (recursively) dereferenced to a non-tag object,

refs/files-backend.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,10 +4058,28 @@ static int files_reflog_expire(struct ref_store *ref_store,
40584058
return -1;
40594059
}
40604060

4061+
static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
4062+
{
4063+
/* Check validity (but we don't need the result): */
4064+
files_downcast(ref_store, 0, "init_db");
4065+
4066+
/*
4067+
* Create .git/refs/{heads,tags}
4068+
*/
4069+
safe_create_dir(git_path("refs/heads"), 1);
4070+
safe_create_dir(git_path("refs/tags"), 1);
4071+
if (get_shared_repository()) {
4072+
adjust_shared_perm(git_path("refs/heads"));
4073+
adjust_shared_perm(git_path("refs/tags"));
4074+
}
4075+
return 0;
4076+
}
4077+
40614078
struct ref_storage_be refs_be_files = {
40624079
NULL,
40634080
"files",
40644081
files_ref_store_create,
4082+
files_init_db,
40654083
files_transaction_commit,
40664084
files_initial_transaction_commit,
40674085

refs/refs-internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ struct ref_store;
479479
*/
480480
typedef struct ref_store *ref_store_init_fn(const char *submodule);
481481

482+
typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);
483+
482484
typedef int ref_transaction_commit_fn(struct ref_store *refs,
483485
struct ref_transaction *transaction,
484486
struct strbuf *err);
@@ -583,6 +585,7 @@ struct ref_storage_be {
583585
struct ref_storage_be *next;
584586
const char *name;
585587
ref_store_init_fn *init;
588+
ref_init_db_fn *init_db;
586589
ref_transaction_commit_fn *transaction_commit;
587590
ref_transaction_commit_fn *initial_transaction_commit;
588591

0 commit comments

Comments
 (0)