3
3
*/
4
4
5
5
#include "cache.h"
6
+ #include "hashmap.h"
6
7
#include "lockfile.h"
7
8
#include "refs.h"
8
9
#include "refs/refs-internal.h"
@@ -1352,11 +1353,41 @@ int resolve_gitlink_ref(const char *submodule, const char *refname,
1352
1353
return 0 ;
1353
1354
}
1354
1355
1356
+ struct submodule_hash_entry
1357
+ {
1358
+ struct hashmap_entry ent ; /* must be the first member! */
1359
+
1360
+ struct ref_store * refs ;
1361
+
1362
+ /* NUL-terminated name of submodule: */
1363
+ char submodule [FLEX_ARRAY ];
1364
+ };
1365
+
1366
+ static int submodule_hash_cmp (const void * entry , const void * entry_or_key ,
1367
+ const void * keydata )
1368
+ {
1369
+ const struct submodule_hash_entry * e1 = entry , * e2 = entry_or_key ;
1370
+ const char * submodule = keydata ? keydata : e2 -> submodule ;
1371
+
1372
+ return strcmp (e1 -> submodule , submodule );
1373
+ }
1374
+
1375
+ static struct submodule_hash_entry * alloc_submodule_hash_entry (
1376
+ const char * submodule , struct ref_store * refs )
1377
+ {
1378
+ struct submodule_hash_entry * entry ;
1379
+
1380
+ FLEX_ALLOC_STR (entry , submodule , submodule );
1381
+ hashmap_entry_init (entry , strhash (submodule ));
1382
+ entry -> refs = refs ;
1383
+ return entry ;
1384
+ }
1385
+
1355
1386
/* A pointer to the ref_store for the main repository: */
1356
1387
static struct ref_store * main_ref_store ;
1357
1388
1358
- /* A linked list of ref_stores for submodules : */
1359
- static struct ref_store * submodule_ref_stores ;
1389
+ /* A hashmap of ref_stores, stored by submodule name : */
1390
+ static struct hashmap submodule_ref_stores ;
1360
1391
1361
1392
/*
1362
1393
* Return the ref_store instance for the specified submodule (or the
@@ -1365,17 +1396,18 @@ static struct ref_store *submodule_ref_stores;
1365
1396
*/
1366
1397
static struct ref_store * lookup_ref_store (const char * submodule )
1367
1398
{
1368
- struct ref_store * refs ;
1399
+ struct submodule_hash_entry * entry ;
1369
1400
1370
1401
if (!submodule )
1371
1402
return main_ref_store ;
1372
1403
1373
- for (refs = submodule_ref_stores ; refs ; refs = refs -> next ) {
1374
- if (!strcmp (submodule , refs -> submodule ))
1375
- return refs ;
1376
- }
1404
+ if (!submodule_ref_stores .tablesize )
1405
+ /* It's initialized on demand in register_ref_store(). */
1406
+ return NULL ;
1377
1407
1378
- return NULL ;
1408
+ entry = hashmap_get_from_hash (& submodule_ref_stores ,
1409
+ strhash (submodule ), submodule );
1410
+ return entry ? entry -> refs : NULL ;
1379
1411
}
1380
1412
1381
1413
/*
@@ -1389,15 +1421,15 @@ static void register_ref_store(struct ref_store *refs, const char *submodule)
1389
1421
if (main_ref_store )
1390
1422
die ("BUG: main_ref_store initialized twice" );
1391
1423
1392
- refs -> next = NULL ;
1393
1424
main_ref_store = refs ;
1394
1425
} else {
1395
- if (lookup_ref_store (submodule ))
1426
+ if (!submodule_ref_stores .tablesize )
1427
+ hashmap_init (& submodule_ref_stores , submodule_hash_cmp , 0 );
1428
+
1429
+ if (hashmap_put (& submodule_ref_stores ,
1430
+ alloc_submodule_hash_entry (submodule , refs )))
1396
1431
die ("BUG: ref_store for submodule '%s' initialized twice" ,
1397
1432
submodule );
1398
-
1399
- refs -> next = submodule_ref_stores ;
1400
- submodule_ref_stores = refs ;
1401
1433
}
1402
1434
}
1403
1435
0 commit comments