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 <christian.couder@gmail.com> Mentored-by: Stefan Beller <sbeller@google.com> Signed-off-by: Prathamesh Chavan <pc44800@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
74a10642aa
commit
9f580a6260
@ -14,6 +14,11 @@
|
||||
#include "refs.h"
|
||||
#include "connect.h"
|
||||
|
||||
#define OPT_QUIET (1 << 0)
|
||||
|
||||
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
|
||||
void *cb_data);
|
||||
|
||||
static char *get_default_remote(void)
|
||||
{
|
||||
char *dest = NULL, *ret;
|
||||
@ -349,7 +354,23 @@ static int module_list(int argc, const char **argv, const char *prefix)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void init_submodule(const char *path, const char *prefix, int quiet)
|
||||
static void for_each_listed_submodule(const struct module_list *list,
|
||||
each_submodule_fn fn, void *cb_data)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < list->nr; i++)
|
||||
fn(list->entries[i], cb_data);
|
||||
}
|
||||
|
||||
struct init_cb {
|
||||
const char *prefix;
|
||||
unsigned int flags;
|
||||
};
|
||||
|
||||
#define INIT_CB_INIT { NULL, 0 }
|
||||
|
||||
static void init_submodule(const char *path, const char *prefix,
|
||||
unsigned int flags)
|
||||
{
|
||||
const struct submodule *sub;
|
||||
struct strbuf sb = STRBUF_INIT;
|
||||
@ -411,7 +432,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
|
||||
if (git_config_set_gently(sb.buf, url))
|
||||
die(_("Failed to register url for submodule path '%s'"),
|
||||
displaypath);
|
||||
if (!quiet)
|
||||
if (!(flags & OPT_QUIET))
|
||||
fprintf(stderr,
|
||||
_("Submodule '%s' (%s) registered for path '%s'\n"),
|
||||
sub->name, url, displaypath);
|
||||
@ -438,12 +459,18 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
|
||||
free(upd);
|
||||
}
|
||||
|
||||
static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
|
||||
{
|
||||
struct init_cb *info = cb_data;
|
||||
init_submodule(list_item->name, info->prefix, info->flags);
|
||||
}
|
||||
|
||||
static int module_init(int argc, const char **argv, const char *prefix)
|
||||
{
|
||||
struct init_cb info = INIT_CB_INIT;
|
||||
struct pathspec pathspec;
|
||||
struct module_list list = MODULE_LIST_INIT;
|
||||
int quiet = 0;
|
||||
int i;
|
||||
|
||||
struct option module_init_options[] = {
|
||||
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)
|
||||
if (!argc && git_config_get_value_multi("submodule.active"))
|
||||
module_list_active(&list);
|
||||
|
||||
for (i = 0; i < list.nr; i++)
|
||||
init_submodule(list.entries[i]->name, prefix, quiet);
|
||||
info.prefix = prefix;
|
||||
if (quiet)
|
||||
info.flags |= OPT_QUIET;
|
||||
|
||||
for_each_listed_submodule(&list, init_submodule_cb, &info);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user