submodule: extract get_fetch_task()

get_next_submodule() configures the parallel submodule fetch by performing two functions: * iterate the index to find submodules * configure the child processes to fetch the submodules found in the previous step Extract the index iterating code into an iterator function, get_fetch_task(), so that get_next_submodule() is agnostic of how to find submodules. This prepares for a subsequent commit will teach the fetch machinery to also iterate through the list of changed submodules (in addition to the index). Signed-off-by: Glen Choo <chooglen@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Glen Choo committed Mar 7, 2022 at 16:14 UTC 73bc90d7e19c471318e799624b6d4c6d449c655d
1 file changed +36 -25
submodule.c
+36 -25
@@ -1389,6 +1389,7 @@ struct fetch_task {
1389 struct repository *repo;
1390 const struct submodule *sub;
1391 unsigned free_sub : 1; /* Do we need to free the submodule? */
1392 + const char *default_argv; /* The default fetch mode. */
1393
1394 struct oid_array *commits; /* Ensure these commits are fetched */
1395 };
@@ -1466,14 +1467,11 @@ static struct repository *get_submodule_repo_for(struct repository *r,
1467 return ret;
1468 }
1469
1469 -static int get_next_submodule(struct child_process *cp,
1470 - struct strbuf *err, void *data, void **task_cb)
1470 +static struct fetch_task *
1471 +get_fetch_task(struct submodule_parallel_fetch *spf, struct strbuf *err)
1472 {
1472 - struct submodule_parallel_fetch *spf = data;
1473 -
1473 for (; spf->count < spf->r->index->cache_nr; spf->count++) {
1474 const struct cache_entry *ce = spf->r->index->cache[spf->count];
1476 - const char *default_argv;
1475 struct fetch_task *task;
1476
1477 if (!S_ISGITLINK(ce->ce_mode))
@@ -1493,10 +1491,10 @@ static int get_next_submodule(struct child_process *cp,
1491 &spf->changed_submodule_names,
1492 task->sub->name))
1493 continue;
1496 - default_argv = "on-demand";
1494 + task->default_argv = "on-demand";
1495 break;
1496 case RECURSE_SUBMODULES_ON:
1499 - default_argv = "yes";
1497 + task->default_argv = "yes";
1498 break;
1499 case RECURSE_SUBMODULES_OFF:
1500 continue;
@@ -1504,29 +1502,12 @@ static int get_next_submodule(struct child_process *cp,
1502
1503 task->repo = get_submodule_repo_for(spf->r, task->sub->path, null_oid());
1504 if (task->repo) {
1507 - struct strbuf submodule_prefix = STRBUF_INIT;
1508 - child_process_init(cp);
1509 - cp->dir = task->repo->gitdir;
1510 - prepare_submodule_repo_env_in_gitdir(&cp->env_array);
1511 - cp->git_cmd = 1;
1505 if (!spf->quiet)
1506 strbuf_addf(err, _("Fetching submodule %s%s\n"),
1507 spf->prefix, ce->name);
1515 - strvec_init(&cp->args);
1516 - strvec_pushv(&cp->args, spf->args.v);
1517 - strvec_push(&cp->args, default_argv);
1518 - strvec_push(&cp->args, "--submodule-prefix");
1519 -
1520 - strbuf_addf(&submodule_prefix, "%s%s/",
1521 - spf->prefix,
1522 - task->sub->path);
1523 - strvec_push(&cp->args, submodule_prefix.buf);
1508
1509 spf->count++;
1526 - *task_cb = task;
1527 -
1528 - strbuf_release(&submodule_prefix);
1529 - return 1;
1510 + return task;
1511 } else {
1512 struct strbuf empty_submodule_path = STRBUF_INIT;
1513
@@ -1550,6 +1531,36 @@ static int get_next_submodule(struct child_process *cp,
1531 strbuf_release(&empty_submodule_path);
1532 }
1533 }
1534 + return NULL;
1535 +}
1536 +
1537 +static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1538 + void *data, void **task_cb)
1539 +{
1540 + struct submodule_parallel_fetch *spf = data;
1541 + struct fetch_task *task = get_fetch_task(spf, err);
1542 +
1543 + if (task) {
1544 + struct strbuf submodule_prefix = STRBUF_INIT;
1545 +
1546 + child_process_init(cp);
1547 + cp->dir = task->repo->gitdir;
1548 + prepare_submodule_repo_env_in_gitdir(&cp->env_array);
1549 + cp->git_cmd = 1;
1550 + strvec_init(&cp->args);
1551 + strvec_pushv(&cp->args, spf->args.v);
1552 + strvec_push(&cp->args, task->default_argv);
1553 + strvec_push(&cp->args, "--submodule-prefix");
1554 +
1555 + strbuf_addf(&submodule_prefix, "%s%s/",
1556 + spf->prefix,
1557 + task->sub->path);
1558 + strvec_push(&cp->args, submodule_prefix.buf);
1559 + *task_cb = task;
1560 +
1561 + strbuf_release(&submodule_prefix);
1562 + return 1;
1563 + }
1564
1565 if (spf->oid_fetch_tasks_nr) {
1566 struct fetch_task *task =