Skip to content

Commit 5d0bc90

Browse files
pcloudsgitster
authored andcommitted
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>
1 parent bbbb7de commit 5d0bc90

3 files changed

Lines changed: 20 additions & 32 deletions

File tree

refs.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "refs/refs-internal.h"
1010
#include "object.h"
1111
#include "tag.h"
12+
#include "submodule.h"
1213

1314
/*
1415
* List of all available backends
@@ -1413,9 +1414,9 @@ static struct ref_store *lookup_submodule_ref_store(const char *submodule)
14131414

14141415
/*
14151416
* Create, record, and return a ref_store instance for the specified
1416-
* submodule (or the main repository if submodule is NULL).
1417+
* gitdir.
14171418
*/
1418-
static struct ref_store *ref_store_init(const char *submodule)
1419+
static struct ref_store *ref_store_init(const char *gitdir)
14191420
{
14201421
const char *be_name = "files";
14211422
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)
14241425
if (!be)
14251426
die("BUG: reference backend %s is unknown", be_name);
14261427

1427-
refs = be->init(submodule);
1428+
refs = be->init(gitdir);
14281429
return refs;
14291430
}
14301431

@@ -1433,7 +1434,7 @@ struct ref_store *get_main_ref_store(void)
14331434
if (main_ref_store)
14341435
return main_ref_store;
14351436

1436-
main_ref_store = ref_store_init(NULL);
1437+
main_ref_store = ref_store_init(get_git_dir());
14371438
return main_ref_store;
14381439
}
14391440

@@ -1474,8 +1475,16 @@ struct ref_store *get_ref_store(const char *submodule)
14741475
if (!ret)
14751476
return NULL;
14761477

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);
14781485
register_submodule_ref_store(refs, submodule);
1486+
1487+
strbuf_release(&submodule_sb);
14791488
return refs;
14801489
}
14811490

refs/files-backend.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -917,12 +917,6 @@ struct packed_ref_cache {
917917
struct files_ref_store {
918918
struct ref_store base;
919919

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;
926920
char *gitdir;
927921
char *gitcommondir;
928922
char *packed_refs_path;
@@ -982,22 +976,14 @@ static void clear_loose_ref_cache(struct files_ref_store *refs)
982976
* Create a new submodule ref cache and add it to the internal
983977
* set of caches.
984978
*/
985-
static struct ref_store *files_ref_store_create(const char *submodule)
979+
static struct ref_store *files_ref_store_create(const char *gitdir)
986980
{
987981
struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
988982
struct ref_store *ref_store = (struct ref_store *)refs;
989983
struct strbuf sb = STRBUF_INIT;
990-
const char *gitdir = get_git_dir();
991984

992985
base_ref_store_init(ref_store, &refs_be_files);
993986

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-
1001987
refs->gitdir = xstrdup(gitdir);
1002988
get_common_dir_noenv(&sb, gitdir);
1003989
refs->gitcommondir = strbuf_detach(&sb, NULL);
@@ -1014,8 +1000,7 @@ static struct ref_store *files_ref_store_create(const char *submodule)
10141000
static void files_assert_main_repository(struct files_ref_store *refs,
10151001
const char *caller)
10161002
{
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 */
10191004
}
10201005

10211006
/*
@@ -1206,11 +1191,6 @@ static void files_ref_path(struct files_ref_store *refs,
12061191
struct strbuf *sb,
12071192
const char *refname)
12081193
{
1209-
if (refs->submodule) {
1210-
strbuf_git_path_submodule(sb, refs->submodule, "%s", refname);
1211-
return;
1212-
}
1213-
12141194
switch (ref_type(refname)) {
12151195
case REF_TYPE_PER_WORKTREE:
12161196
case REF_TYPE_PSEUDOREF:

refs/refs-internal.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,11 @@ struct ref_store;
482482
/* refs backends */
483483

484484
/*
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.
489488
*/
490-
typedef struct ref_store *ref_store_init_fn(const char *submodule);
489+
typedef struct ref_store *ref_store_init_fn(const char *gitdir);
491490

492491
typedef int ref_init_db_fn(struct ref_store *refs, struct strbuf *err);
493492

0 commit comments

Comments
 (0)