blob: 28186b30f54818cdbb42a4876358b7ceb20d20e2 [file] [log] [blame]
Derrick Stolee4950b2a2020-09-11 17:49:16 +00001#include "builtin.h"
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00002#include "config.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Derrick Stolee4950b2a2020-09-11 17:49:16 +00004#include "parse-options.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00005#include "path.h"
6#include "repository.h"
Derrick Stolee4950b2a2020-09-11 17:49:16 +00007#include "run-command.h"
8#include "string-list.h"
9
10static const char * const for_each_repo_usage[] = {
Ævar Arnfjörð Bjarmasonc08cfc32022-10-13 17:39:11 +020011 N_("git for-each-repo --config=<config> [--] <arguments>"),
Derrick Stolee4950b2a2020-09-11 17:49:16 +000012 NULL
13};
14
Andrzej Hunt2b299942021-07-25 15:08:22 +020015static int run_command_on_repo(const char *path, int argc, const char ** argv)
Derrick Stolee4950b2a2020-09-11 17:49:16 +000016{
17 int i;
18 struct child_process child = CHILD_PROCESS_INIT;
Ronan Pigott13d5bbd2022-11-09 12:07:07 -070019 char *abspath = interpolate_path(path, 0);
Derrick Stolee4950b2a2020-09-11 17:49:16 +000020
21 child.git_cmd = 1;
Ronan Pigott13d5bbd2022-11-09 12:07:07 -070022 strvec_pushl(&child.args, "-C", abspath, NULL);
Derrick Stolee4950b2a2020-09-11 17:49:16 +000023
Andrzej Hunt2b299942021-07-25 15:08:22 +020024 for (i = 0; i < argc; i++)
25 strvec_push(&child.args, argv[i]);
Derrick Stolee4950b2a2020-09-11 17:49:16 +000026
Ronan Pigott13d5bbd2022-11-09 12:07:07 -070027 free(abspath);
28
Derrick Stolee4950b2a2020-09-11 17:49:16 +000029 return run_command(&child);
30}
31
32int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
33{
34 static const char *config_key = NULL;
35 int i, result = 0;
36 const struct string_list *values;
Ævar Arnfjörð Bjarmasonf7b2ff92023-03-28 16:04:25 +020037 int err;
Derrick Stolee4950b2a2020-09-11 17:49:16 +000038
39 const struct option options[] = {
40 OPT_STRING(0, "config", &config_key, N_("config"),
41 N_("config key storing a list of repository paths")),
42 OPT_END()
43 };
44
45 argc = parse_options(argc, argv, prefix, options, for_each_repo_usage,
46 PARSE_OPT_STOP_AT_NON_OPTION);
47
48 if (!config_key)
49 die(_("missing --config=<config>"));
50
Ævar Arnfjörð Bjarmason3611f742023-03-28 16:04:28 +020051 err = repo_config_get_string_multi(the_repository, config_key, &values);
Ævar Arnfjörð Bjarmasonf7b2ff92023-03-28 16:04:25 +020052 if (err < 0)
53 usage_msg_optf(_("got bad config --config=%s"),
54 for_each_repo_usage, options, config_key);
55 else if (err)
Derrick Stolee6c62f012021-01-08 02:30:46 +000056 return 0;
57
Derrick Stolee4950b2a2020-09-11 17:49:16 +000058 for (i = 0; !result && i < values->nr; i++)
Andrzej Hunt2b299942021-07-25 15:08:22 +020059 result = run_command_on_repo(values->items[i].string, argc, argv);
Derrick Stolee4950b2a2020-09-11 17:49:16 +000060
61 return result;
62}