Merge branch 'ab/diff-no-index-tests'
[alt-git.git] / builtin / for-each-repo.c
blob52be64a4373a3a4fb992662aaee54e17c9d03b46
1 #include "cache.h"
2 #include "config.h"
3 #include "builtin.h"
4 #include "parse-options.h"
5 #include "run-command.h"
6 #include "string-list.h"
8 static const char * const for_each_repo_usage[] = {
9 N_("git for-each-repo --config=<config> <command-args>"),
10 NULL
13 static int run_command_on_repo(const char *path,
14 void *cbdata)
16 int i;
17 struct child_process child = CHILD_PROCESS_INIT;
18 struct strvec *args = (struct strvec *)cbdata;
20 child.git_cmd = 1;
21 strvec_pushl(&child.args, "-C", path, NULL);
23 for (i = 0; i < args->nr; i++)
24 strvec_push(&child.args, args->v[i]);
26 return run_command(&child);
29 int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
31 static const char *config_key = NULL;
32 int i, result = 0;
33 const struct string_list *values;
34 struct strvec args = STRVEC_INIT;
36 const struct option options[] = {
37 OPT_STRING(0, "config", &config_key, N_("config"),
38 N_("config key storing a list of repository paths")),
39 OPT_END()
42 argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
43 PARSE_OPT_STOP_AT_NON_OPTION);
45 if (!config_key)
46 die(_("missing --config=<config>"));
48 for (i = 0; i < argc; i++)
49 strvec_push(&args, argv[i]);
51 values = repo_config_get_value_multi(the_repository,
52 config_key);
55 * Do nothing on an empty list, which is equivalent to the case
56 * where the config variable does not exist at all.
58 if (!values)
59 return 0;
61 for (i = 0; !result && i < values->nr; i++)
62 result = run_command_on_repo(values->items[i].string, &args);
64 return result;