read-cache: move shared commit and ls-files code
[alt-git.git] / builtin / for-each-repo.c
blob37daf7bec143e8e72f1a6b91fd2c8d9381840c1c
1 #include "cache.h"
2 #include "config.h"
3 #include "builtin.h"
4 #include "gettext.h"
5 #include "parse-options.h"
6 #include "path.h"
7 #include "repository.h"
8 #include "run-command.h"
9 #include "string-list.h"
11 static const char * const for_each_repo_usage[] = {
12 N_("git for-each-repo --config=<config> [--] <arguments>"),
13 NULL
16 static int run_command_on_repo(const char *path, int argc, const char ** argv)
18 int i;
19 struct child_process child = CHILD_PROCESS_INIT;
20 char *abspath = interpolate_path(path, 0);
22 child.git_cmd = 1;
23 strvec_pushl(&child.args, "-C", abspath, NULL);
25 for (i = 0; i < argc; i++)
26 strvec_push(&child.args, argv[i]);
28 free(abspath);
30 return run_command(&child);
33 int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
35 static const char *config_key = NULL;
36 int i, result = 0;
37 const struct string_list *values;
38 int err;
40 const struct option options[] = {
41 OPT_STRING(0, "config", &config_key, N_("config"),
42 N_("config key storing a list of repository paths")),
43 OPT_END()
46 argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
47 PARSE_OPT_STOP_AT_NON_OPTION);
49 if (!config_key)
50 die(_("missing --config=<config>"));
52 err = repo_config_get_string_multi(the_repository, config_key, &values);
53 if (err < 0)
54 usage_msg_optf(_("got bad config --config=%s"),
55 for_each_repo_usage, options, config_key);
56 else if (err)
57 return 0;
59 for (i = 0; !result && i < values->nr; i++)
60 result = run_command_on_repo(values->items[i].string, argc, argv);
62 return result;