Skip to content

Commit 9f580a6

Browse files
pratham-pcgitster
authored andcommitted
submodule--helper: introduce for_each_listed_submodule()
Introduce function for_each_listed_submodule() and replace a loop in module_init() with a call to it. The new function will also be used in other parts of the system in later patches. Mentored-by: Christian Couder <[email protected]> Mentored-by: Stefan Beller <[email protected]> Signed-off-by: Prathamesh Chavan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74a1064 commit 9f580a6

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

builtin/submodule--helper.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include "refs.h"
1515
#include "connect.h"
1616

17+
#define OPT_QUIET (1 << 0)
18+
19+
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
20+
void *cb_data);
21+
1722
static char *get_default_remote(void)
1823
{
1924
char *dest = NULL, *ret;
@@ -349,7 +354,23 @@ static int module_list(int argc, const char **argv, const char *prefix)
349354
return 0;
350355
}
351356

352-
static void init_submodule(const char *path, const char *prefix, int quiet)
357+
static void for_each_listed_submodule(const struct module_list *list,
358+
each_submodule_fn fn, void *cb_data)
359+
{
360+
int i;
361+
for (i = 0; i < list->nr; i++)
362+
fn(list->entries[i], cb_data);
363+
}
364+
365+
struct init_cb {
366+
const char *prefix;
367+
unsigned int flags;
368+
};
369+
370+
#define INIT_CB_INIT { NULL, 0 }
371+
372+
static void init_submodule(const char *path, const char *prefix,
373+
unsigned int flags)
353374
{
354375
const struct submodule *sub;
355376
struct strbuf sb = STRBUF_INIT;
@@ -411,7 +432,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
411432
if (git_config_set_gently(sb.buf, url))
412433
die(_("Failed to register url for submodule path '%s'"),
413434
displaypath);
414-
if (!quiet)
435+
if (!(flags & OPT_QUIET))
415436
fprintf(stderr,
416437
_("Submodule '%s' (%s) registered for path '%s'\n"),
417438
sub->name, url, displaypath);
@@ -438,12 +459,18 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
438459
free(upd);
439460
}
440461

462+
static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
463+
{
464+
struct init_cb *info = cb_data;
465+
init_submodule(list_item->name, info->prefix, info->flags);
466+
}
467+
441468
static int module_init(int argc, const char **argv, const char *prefix)
442469
{
470+
struct init_cb info = INIT_CB_INIT;
443471
struct pathspec pathspec;
444472
struct module_list list = MODULE_LIST_INIT;
445473
int quiet = 0;
446-
int i;
447474

448475
struct option module_init_options[] = {
449476
OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
@@ -468,8 +495,11 @@ static int module_init(int argc, const char **argv, const char *prefix)
468495
if (!argc && git_config_get_value_multi("submodule.active"))
469496
module_list_active(&list);
470497

471-
for (i = 0; i < list.nr; i++)
472-
init_submodule(list.entries[i]->name, prefix, quiet);
498+
info.prefix = prefix;
499+
if (quiet)
500+
info.flags |= OPT_QUIET;
501+
502+
for_each_listed_submodule(&list, init_submodule_cb, &info);
473503

474504
return 0;
475505
}

0 commit comments

Comments
 (0)