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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Apr 4, 2017 at 17:21 UTC
0c064d907b5d96cb4f68c9f1f3214b115d43436e
1 file changed
+32
-32
refs.c
+32
-32
@@ -1450,32 +1450,32 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
1450
return 0;
1451
}
1452
1453
-struct submodule_hash_entry
1453
+struct ref_store_hash_entry
1454
{
1455
struct hashmap_entry ent; /* must be the first member! */
1456
1457
struct ref_store *refs;
1458
1459
- /* NUL-terminated name of submodule: */
1460
- char submodule[FLEX_ARRAY];
1459
+ /* NUL-terminated identifier of the ref store: */
1460
+ char name[FLEX_ARRAY];
1461
};
1462
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,
1464
const void *keydata)
1465
{
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;
1468
1469
- return strcmp(e1->submodule, submodule);
1469
+ return strcmp(e1->name, name);
1470
}
1471
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)
1474
{
1475
- struct submodule_hash_entry *entry;
1475
+ struct ref_store_hash_entry *entry;
1476
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));
1479
entry->refs = refs;
1480
return entry;
1481
}
@@ -1487,19 +1487,19 @@ static struct ref_store *main_ref_store;
1487
static struct hashmap submodule_ref_stores;
1488
1489
/*
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.
1492
*/
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)
1495
{
1495
- struct submodule_hash_entry *entry;
1496
+ struct ref_store_hash_entry *entry;
1497
1497
- if (!submodule_ref_stores.tablesize)
1498
+ if (!map->tablesize)
1499
/* It's initialized on demand in register_ref_store(). */
1500
return NULL;
1501
1501
- entry = hashmap_get_from_hash(&submodule_ref_stores,
1502
- strhash(submodule), submodule);
1502
+ entry = hashmap_get_from_hash(map, strhash(name), name);
1503
return entry ? entry->refs : NULL;
1504
}
1505
@@ -1535,20 +1535,19 @@ struct ref_store *get_main_ref_store(void)
1535
}
1536
1537
/*
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.
1540
*/
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)
1545
{
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);
1548
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);
1551
}
1552
1553
struct ref_store *get_submodule_ref_store(const char *submodule)
@@ -1565,7 +1564,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
1564
return get_main_ref_store();
1565
}
1566
1568
- refs = lookup_submodule_ref_store(submodule);
1567
+ refs = lookup_ref_store_map(&submodule_ref_stores, submodule);
1568
if (refs)
1569
return refs;
1570
@@ -1584,7 +1583,8 @@ struct ref_store *get_submodule_ref_store(const char *submodule)
1583
/* assume that add_submodule_odb() has been called */
1584
refs = ref_store_init(submodule_sb.buf,
1585
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);
1588
1589
strbuf_release(&submodule_sb);
1590
return refs;