object: add object_array initializer helper function
[git/gitster.git] / builtin / for-each-repo.c
blob598ca16c46e5455d0c810a8a043c4a49a818404a
1 #include "cache.h"
2 #include "config.h"
3 #include "builtin.h"
4 #include "gettext.h"
5 #include "parse-options.h"
6 #include "run-command.h"
7 #include "string-list.h"
9 static const char * const for_each_repo_usage[] = {
10 N_("git for-each-repo --config=<config> [--] <arguments>"),
11 NULL
14 static int run_command_on_repo(const char *path, int argc, const char ** argv)
16 int i;
17 struct child_process child = CHILD_PROCESS_INIT;
18 char *abspath = interpolate_path(path, 0);
20 child.git_cmd = 1;
21 strvec_pushl(&child.args, "-C", abspath, NULL);
23 for (i = 0; i < argc; i++)
24 strvec_push(&child.args, argv[i]);
26 free(abspath);
28 return run_command(&child);
31 int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
33 static const char *config_key = NULL;
34 int i, result = 0;
35 const struct string_list *values;
36 int err;
38 const struct option options[] = {
39 OPT_STRING(0, "config", &config_key, N_("config"),
40 N_("config key storing a list of repository paths")),
41 OPT_END()
44 argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
45 PARSE_OPT_STOP_AT_NON_OPTION);
47 if (!config_key)
48 die(_("missing --config=<config>"));
50 err = repo_config_get_string_multi(the_repository, config_key, &values);
51 if (err < 0)
52 usage_msg_optf(_("got bad config --config=%s"),
53 for_each_repo_usage, options, config_key);
54 else if (err)
55 return 0;
57 for (i = 0; !result && i < values->nr; i++)
58 result = run_command_on_repo(values->items[i].string, argc, argv);
60 return result;