refs: move submodule code out of files-backend.c

files-backend is now initialized with a $GIT_DIR. Converting a submodule
path to where real submodule gitdir is located is done in get_ref_store().

This gives a slight performance improvement for submodules since we
don't convert submodule path to gitdir at every backend call like
before. We pay that once at ref-store creation.

More cleanup in files_downcast() and files_assert_main_repository()
follows shortly. It's separate to keep noises from this patch.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2017-03-26 09:42:31 +07:00 committed by Junio C Hamano
parent bbbb7de7ac
commit 5d0bc90e5d
3 changed files with 20 additions and 32 deletions

19
refs.c
View File

@ -9,6 +9,7 @@
#include "refs/refs-internal.h" #include "refs/refs-internal.h"
#include "object.h" #include "object.h"
#include "tag.h" #include "tag.h"
#include "submodule.h"
/* /*
* List of all available backends * List of all available backends
@ -1413,9 +1414,9 @@ static struct ref_store *lookup_submodule_ref_store(const char *submodule)
/* /*
* Create, record, and return a ref_store instance for the specified * Create, record, and return a ref_store instance for the specified
* submodule (or the main repository if submodule is NULL). * gitdir.
*/ */
static struct ref_store *ref_store_init(const char *submodule) static struct ref_store *ref_store_init(const char *gitdir)
{ {
const char *be_name = "files"; const char *be_name = "files";
struct ref_storage_be *be = find_ref_storage_backend(be_name); struct ref_storage_be *be = find_ref_storage_backend(be_name);
@ -1424,7 +1425,7 @@ static struct ref_store *ref_store_init(const char *submodule)
if (!be) if (!be)
die("BUG: reference backend %s is unknown", be_name); die("BUG: reference backend %s is unknown", be_name);
refs = be->init(submodule); refs = be->init(gitdir);
return refs; return refs;
} }
@ -1433,7 +1434,7 @@ struct ref_store *get_main_ref_store(void)
if (main_ref_store) if (main_ref_store)
return main_ref_store; return main_ref_store;
main_ref_store = ref_store_init(NULL); main_ref_store = ref_store_init(get_git_dir());
return main_ref_store; return main_ref_store;
} }
@ -1474,8 +1475,16 @@ struct ref_store *get_ref_store(const char *submodule)
if (!ret) if (!ret)
return NULL; return NULL;
refs = ref_store_init(submodule); ret = submodule_to_gitdir(&submodule_sb, submodule);
if (ret) {
strbuf_release(&submodule_sb);
return NULL;
}
refs = ref_store_init(submodule_sb.buf);
register_submodule_ref_store(refs, submodule); register_submodule_ref_store(refs, submodule);
strbuf_release(&submodule_sb);
return refs; return refs;
} }

View File

@ -917,12 +917,6 @@ struct packed_ref_cache {
struct files_ref_store { struct files_ref_store {
struct ref_store base; struct ref_store base;
/*
* The name of the submodule represented by this object, or
* NULL if it represents the main repository's reference
* store:
*/
const char *submodule;
char *gitdir; char *gitdir;
char *gitcommondir; char *gitcommondir;
char *packed_refs_path; char *packed_refs_path;
@ -982,22 +976,14 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
* Create a new submodule ref cache and add it to the internal * Create a new submodule ref cache and add it to the internal
* set of caches. * set of caches.
*/ */
static struct ref_store *files_ref_store_create(const char *submodule) static struct ref_store *files_ref_store_create(const char *gitdir)
{ {
struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
struct ref_store *ref_store = (struct ref_store *)refs; struct ref_store *ref_store = (struct ref_store *)refs;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
const char *gitdir = get_git_dir();
base_ref_store_init(ref_store, &refs_be_files); base_ref_store_init(ref_store, &refs_be_files);
if (submodule) {
refs->submodule = xstrdup(submodule);
refs->packed_refs_path = git_pathdup_submodule(
refs->submodule, "packed-refs");
return ref_store;
}
refs->gitdir = xstrdup(gitdir); refs->gitdir = xstrdup(gitdir);
get_common_dir_noenv(&sb, gitdir); get_common_dir_noenv(&sb, gitdir);
refs->gitcommondir = strbuf_detach(&sb, NULL); refs->gitcommondir = strbuf_detach(&sb, NULL);
@ -1014,8 +1000,7 @@ static struct ref_store *files_ref_store_create(const char *submodule)
static void files_assert_main_repository(struct files_ref_store *refs, static void files_assert_main_repository(struct files_ref_store *refs,
const char *caller) const char *caller)
{ {
if (refs->submodule) /* This function is to be fixed up in the next patch */
die("BUG: %s called for a submodule", caller);
} }
/* /*
@ -1206,11 +1191,6 @@ static void files_ref_path(struct files_ref_store *refs,
struct strbuf *sb, struct strbuf *sb,
const char *refname) const char *refname)
{ {
if (refs->submodule) {
strbuf_git_path_submodule(sb, refs->submodule, "%s", refname);
return;
}
switch (ref_type(refname)) { switch (ref_type(refname)) {
case REF_TYPE_PER_WORKTREE: case REF_TYPE_PER_WORKTREE:
case REF_TYPE_PSEUDOREF: case REF_TYPE_PSEUDOREF:

View File

@ -482,12 +482,11 @@ struct ref_store;
/* refs backends */ /* refs backends */
/* /*
* Initialize the ref_store for the specified submodule, or for the * Initialize the ref_store for the specified gitdir. These functions
* main repository if submodule == NULL. These functions should call * should call base_ref_store_init() to initialize the shared part of
* base_ref_store_init() to initialize the shared part of the * the ref_store and to record the ref_store for later lookup.
* ref_store and to record the ref_store for later lookup.
*/ */
typedef struct ref_store *ref_store_init_fn(const char *submodule); typedef struct ref_store *ref_store_init_fn(const char *gitdir);
typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err); typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);