Skip to content

Commit 0c064d9

Browse files
pcloudsgitster
authored andcommitted
refs.c: make submodule ref store hashmap generic
This removes the "submodule" from submodule_hash_entry and other function names. The goal is to reuse the same code and data structure for other ref store types. The first one is worktree ref stores. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a560d87 commit 0c064d9

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

refs.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,32 +1450,32 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
14501450
return 0;
14511451
}
14521452

1453-
struct submodule_hash_entry
1453+
struct ref_store_hash_entry
14541454
{
14551455
struct hashmap_entry ent; /* must be the first member! */
14561456

14571457
struct ref_store *refs;
14581458

1459-
/* NUL-terminated name of submodule: */
1460-
char submodule[FLEX_ARRAY];
1459+
/* NUL-terminated identifier of the ref store: */
1460+
char name[FLEX_ARRAY];
14611461
};
14621462

1463-
static int submodule_hash_cmp(const void *entry, const void *entry_or_key,
1463+
static int ref_store_hash_cmp(const void *entry, const void *entry_or_key,
14641464
const void *keydata)
14651465
{
1466-
const struct submodule_hash_entry *e1 = entry, *e2 = entry_or_key;
1467-
const char *submodule = keydata ? keydata : e2->submodule;
1466+
const struct ref_store_hash_entry *e1 = entry, *e2 = entry_or_key;
1467+
const char *name = keydata ? keydata : e2->name;
14681468

1469-
return strcmp(e1->submodule, submodule);
1469+
return strcmp(e1->name, name);
14701470
}
14711471

1472-
static struct submodule_hash_entry *alloc_submodule_hash_entry(
1473-
const char *submodule, struct ref_store *refs)
1472+
static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
1473+
const char *name, struct ref_store *refs)
14741474
{
1475-
struct submodule_hash_entry *entry;
1475+
struct ref_store_hash_entry *entry;
14761476

1477-
FLEX_ALLOC_STR(entry, submodule, submodule);
1478-
hashmap_entry_init(entry, strhash(submodule));
1477+
FLEX_ALLOC_STR(entry, name, name);
1478+
hashmap_entry_init(entry, strhash(name));
14791479
entry->refs = refs;
14801480
return entry;
14811481
}
@@ -1487,19 +1487,19 @@ static struct ref_store *main_ref_store;
14871487
static struct hashmap submodule_ref_stores;
14881488

14891489
/*
1490-
* Return the ref_store instance for the specified submodule. If that
1491-
* ref_store hasn't been initialized yet, return NULL.
1490+
* Look up a ref store by name. If that ref_store hasn't been
1491+
* registered yet, return NULL.
14921492
*/
1493-
static struct ref_store *lookup_submodule_ref_store(const char *submodule)
1493+
static struct ref_store *lookup_ref_store_map(struct hashmap *map,
1494+
const char *name)
14941495
{
1495-
struct submodule_hash_entry *entry;
1496+
struct ref_store_hash_entry *entry;
14961497

1497-
if (!submodule_ref_stores.tablesize)
1498+
if (!map->tablesize)
14981499
/* It's initialized on demand in register_ref_store(). */
14991500
return NULL;
15001501

1501-
entry = hashmap_get_from_hash(&submodule_ref_stores,
1502-
strhash(submodule), submodule);
1502+
entry = hashmap_get_from_hash(map, strhash(name), name);
15031503
return entry ? entry->refs : NULL;
15041504
}
15051505

@@ -1535,20 +1535,19 @@ struct ref_store *get_main_ref_store(void)
15351535
}
15361536

15371537
/*
1538-
* Register the specified ref_store to be the one that should be used
1539-
* for submodule. It is a fatal error to call this function twice for
1540-
* the same submodule.
1538+
* Associate a ref store with a name. It is a fatal error to call this
1539+
* function twice for the same name.
15411540
*/
1542-
static void register_submodule_ref_store(struct ref_store *refs,
1543-
const char *submodule)
1541+
static void register_ref_store_map(struct hashmap *map,
1542+
const char *type,
1543+
struct ref_store *refs,
1544+
const char *name)
15441545
{
1545-
if (!submodule_ref_stores.tablesize)
1546-
hashmap_init(&submodule_ref_stores, submodule_hash_cmp, 0);
1546+
if (!map->tablesize)
1547+
hashmap_init(map, ref_store_hash_cmp, 0);
15471548

1548-
if (hashmap_put(&submodule_ref_stores,
1549-
alloc_submodule_hash_entry(submodule, refs)))
1550-
die("BUG: ref_store for submodule '%s' initialized twice",
1551-
submodule);
1549+
if (hashmap_put(map, alloc_ref_store_hash_entry(name, refs)))
1550+
die("BUG: %s ref_store '%s' initialized twice", type, name);
15521551
}
15531552

15541553
struct ref_store *get_submodule_ref_store(const char *submodule)
@@ -1565,7 +1564,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
15651564
return get_main_ref_store();
15661565
}
15671566

1568-
refs = lookup_submodule_ref_store(submodule);
1567+
refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
15691568
if (refs)
15701569
return refs;
15711570

@@ -1584,7 +1583,8 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
15841583
/* assume that add_submodule_odb() has been called */
15851584
refs = ref_store_init(submodule_sb.buf,
15861585
REF_STORE_READ | REF_STORE_ODB);
1587-
register_submodule_ref_store(refs, submodule);
1586+
register_ref_store_map(&submodule_ref_stores, "submodule",
1587+
refs, submodule);
15881588

15891589
strbuf_release(&submodule_sb);
15901590
return refs;

0 commit comments

Comments
 (0)