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>

Nguyễn Thái Ngọc Duy committed Mar 26, 2017 at 09:42 UTC 5d0bc90e5de37c708f0856aef2c7b353a9d5030b
3 files changed +20 -32
refs.c
+14 -5
@@ -9,6 +9,7 @@
9 #include "refs/refs-internal.h"
10 #include "object.h"
11 #include "tag.h"
12 +#include "submodule.h"
13
14 /*
15 * List of all available backends
@@ -1413,9 +1414,9 @@ static struct ref_store *lookup_submodule_ref_store(const char *submodule)
1414
1415 /*
1416 * Create, record, and return a ref_store instance for the specified
1416 - * submodule (or the main repository if submodule is NULL).
1417 + * gitdir.
1418 */
1418 -static struct ref_store *ref_store_init(const char *submodule)
1419 +static struct ref_store *ref_store_init(const char *gitdir)
1420 {
1421 const char *be_name = "files";
1422 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)
1425 if (!be)
1426 die("BUG: reference backend %s is unknown", be_name);
1427
1427 - refs = be->init(submodule);
1428 + refs = be->init(gitdir);
1429 return refs;
1430 }
1431
@@ -1433,7 +1434,7 @@ struct ref_store *get_main_ref_store(void)
1434 if (main_ref_store)
1435 return main_ref_store;
1436
1436 - main_ref_store = ref_store_init(NULL);
1437 + main_ref_store = ref_store_init(get_git_dir());
1438 return main_ref_store;
1439 }
1440
@@ -1474,8 +1475,16 @@ struct ref_store *get_ref_store(const char *submodule)
1475 if (!ret)
1476 return NULL;
1477
1477 - refs = ref_store_init(submodule);
1478 + ret = submodule_to_gitdir(&submodule_sb, submodule);
1479 + if (ret) {
1480 + strbuf_release(&submodule_sb);
1481 + return NULL;
1482 + }
1483 +
1484 + refs = ref_store_init(submodule_sb.buf);
1485 register_submodule_ref_store(refs, submodule);
1486 +
1487 + strbuf_release(&submodule_sb);
1488 return refs;
1489 }
1490
refs/files-backend.c
+2 -22
@@ -917,12 +917,6 @@ struct packed_ref_cache {
917 struct files_ref_store {
918 struct ref_store base;
919
920 - /*
921 - * The name of the submodule represented by this object, or
922 - * NULL if it represents the main repository's reference
923 - * store:
924 - */
925 - const char *submodule;
920 char *gitdir;
921 char *gitcommondir;
922 char *packed_refs_path;
@@ -982,22 +976,14 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
976 * Create a new submodule ref cache and add it to the internal
977 * set of caches.
978 */
985 -static struct ref_store *files_ref_store_create(const char *submodule)
979 +static struct ref_store *files_ref_store_create(const char *gitdir)
980 {
981 struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
982 struct ref_store *ref_store = (struct ref_store *)refs;
983 struct strbuf sb = STRBUF_INIT;
990 - const char *gitdir = get_git_dir();
984
985 base_ref_store_init(ref_store, &refs_be_files);
986
994 - if (submodule) {
995 - refs->submodule = xstrdup(submodule);
996 - refs->packed_refs_path = git_pathdup_submodule(
997 - refs->submodule, "packed-refs");
998 - return ref_store;
999 - }
1000 -
987 refs->gitdir = xstrdup(gitdir);
988 get_common_dir_noenv(&sb, gitdir);
989 refs->gitcommondir = strbuf_detach(&sb, NULL);
@@ -1014,8 +1000,7 @@ static struct ref_store *files_ref_store_create(const char *submodule)
1000 static void files_assert_main_repository(struct files_ref_store *refs,
1001 const char *caller)
1002 {
1017 - if (refs->submodule)
1018 - die("BUG: %s called for a submodule", caller);
1003 + /* This function is to be fixed up in the next patch */
1004 }
1005
1006 /*
@@ -1206,11 +1191,6 @@ static void files_ref_path(struct files_ref_store *refs,
1191 struct strbuf *sb,
1192 const char *refname)
1193 {
1209 - if (refs->submodule) {
1210 - strbuf_git_path_submodule(sb, refs->submodule, "%s", refname);
1211 - return;
1212 - }
1213 -
1194 switch (ref_type(refname)) {
1195 case REF_TYPE_PER_WORKTREE:
1196 case REF_TYPE_PSEUDOREF:
refs/refs-internal.h
+4 -5
@@ -482,12 +482,11 @@ struct ref_store;
482 /* refs backends */
483
484 /*
485 - * Initialize the ref_store for the specified submodule, or for the
486 - * main repository if submodule == NULL. These functions should call
487 - * base_ref_store_init() to initialize the shared part of the
488 - * ref_store and to record the ref_store for later lookup.
485 + * Initialize the ref_store for the specified gitdir. These functions
486 + * should call base_ref_store_init() to initialize the shared part of
487 + * the ref_store and to record the ref_store for later lookup.
488 */
490 -typedef struct ref_store *ref_store_init_fn(const char *submodule);
489 +typedef struct ref_store *ref_store_init_fn(const char *gitdir);
490
491 typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);
492