t/unit-tests: fix typos
[git/gitster.git] / builtin / for-each-repo.c
blobfae7f91cf15f7e5b5964d59bbc0f19b4f08146fc
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "parse-options.h"
6 #include "path.h"
7 #include "run-command.h"
8 #include "string-list.h"
10 static const char * const for_each_repo_usage[] = {
11 N_("git for-each-repo --config=<config> [--] <arguments>"),
12 NULL
15 static int run_command_on_repo(const char *path, int argc, const char ** argv)
17 int i;
18 struct child_process child = CHILD_PROCESS_INIT;
19 char *abspath = interpolate_path(path, 0);
21 child.git_cmd = 1;
22 strvec_pushl(&child.args, "-C", abspath, NULL);
24 for (i = 0; i < argc; i++)
25 strvec_push(&child.args, argv[i]);
27 free(abspath);
29 return run_command(&child);
32 int cmd_for_each_repo(int argc,
33 const char **argv,
34 const char *prefix,
35 struct repository *repo UNUSED)
37 static const char *config_key = NULL;
38 int keep_going = 0;
39 int i, result = 0;
40 const struct string_list *values;
41 int err;
43 const struct option options[] = {
44 OPT_STRING(0, "config", &config_key, N_("config"),
45 N_("config key storing a list of repository paths")),
46 OPT_BOOL(0, "keep-going", &keep_going,
47 N_("keep going even if command fails in a repository")),
48 OPT_END()
51 argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
52 PARSE_OPT_STOP_AT_NON_OPTION);
54 if (!config_key)
55 die(_("missing --config=<config>"));
57 err = repo_config_get_string_multi(the_repository, config_key, &values);
58 if (err < 0)
59 usage_msg_optf(_("got bad config --config=%s"),
60 for_each_repo_usage, options, config_key);
61 else if (err)
62 return 0;
64 for (i = 0; i < values->nr; i++) {
65 int ret = run_command_on_repo(values->items[i].string, argc, argv);
66 if (ret) {
67 if (!keep_going)
68 return ret;
69 result = 1;
73 return result;