register_ref_store(): new function

Move the responsibility for registering the ref_store for a submodule from base_ref_store_init() to a new function, register_ref_store(). Call the latter from ref_store_init(). Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Feb 10, 2017 at 12:16 UTC ba88add58176a883e4137ee9bb5e66235799a9d9
1 file changed +29 -14
refs.c
+29 -14
@@ -1378,6 +1378,29 @@ static struct ref_store *lookup_ref_store(const char *submodule)
1378 return NULL;
1379 }
1380
1381 +/*
1382 + * Register the specified ref_store to be the one that should be used
1383 + * for submodule (or the main repository if submodule is NULL). It is
1384 + * a fatal error to call this function twice for the same submodule.
1385 + */
1386 +static void register_ref_store(struct ref_store *refs, const char *submodule)
1387 +{
1388 + if (!submodule) {
1389 + if (main_ref_store)
1390 + die("BUG: main_ref_store initialized twice");
1391 +
1392 + refs->next = NULL;
1393 + main_ref_store = refs;
1394 + } else {
1395 + if (lookup_ref_store(submodule))
1396 + die("BUG: ref_store for submodule '%s' initialized twice",
1397 + submodule);
1398 +
1399 + refs->next = submodule_ref_stores;
1400 + submodule_ref_stores = refs;
1401 + }
1402 +}
1403 +
1404 /*
1405 * Create, record, and return a ref_store instance for the specified
1406 * submodule (or the main repository if submodule is NULL).
@@ -1386,11 +1409,14 @@ static struct ref_store *ref_store_init(const char *submodule)
1409 {
1410 const char *be_name = "files";
1411 struct ref_storage_be *be = find_ref_storage_backend(be_name);
1412 + struct ref_store *refs;
1413
1414 if (!be)
1415 die("BUG: reference backend %s is unknown", be_name);
1416
1393 - return be->init(submodule);
1417 + refs = be->init(submodule);
1418 + register_ref_store(refs, submodule);
1419 + return refs;
1420 }
1421
1422 struct ref_store *get_ref_store(const char *submodule)
@@ -1423,22 +1449,11 @@ void base_ref_store_init(struct ref_store *refs,
1449 const char *submodule)
1450 {
1451 refs->be = be;
1426 - if (!submodule) {
1427 - if (main_ref_store)
1428 - die("BUG: main_ref_store initialized twice");
1452
1453 + if (!submodule)
1454 refs->submodule = "";
1431 - refs->next = NULL;
1432 - main_ref_store = refs;
1433 - } else {
1434 - if (lookup_ref_store(submodule))
1435 - die("BUG: ref_store for submodule '%s' initialized twice",
1436 - submodule);
1437 -
1455 + else
1456 refs->submodule = xstrdup(submodule);
1439 - refs->next = submodule_ref_stores;
1440 - submodule_ref_stores = refs;
1441 - }
1457 }
1458
1459 void assert_main_repository(struct ref_store *refs, const char *caller)