Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 1 | #include "builtin.h" |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 2 | #include "config.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 3 | #include "gettext.h" |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 4 | #include "parse-options.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 5 | #include "path.h" |
| 6 | #include "repository.h" |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 7 | #include "run-command.h" |
| 8 | #include "string-list.h" |
| 9 | |
| 10 | static const char * const for_each_repo_usage[] = { |
Ævar Arnfjörð Bjarmason | c08cfc3 | 2022-10-13 17:39:11 +0200 | [diff] [blame] | 11 | N_("git for-each-repo --config=<config> [--] <arguments>"), |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 12 | NULL |
| 13 | }; |
| 14 | |
Andrzej Hunt | 2b29994 | 2021-07-25 15:08:22 +0200 | [diff] [blame] | 15 | static int run_command_on_repo(const char *path, int argc, const char ** argv) |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 16 | { |
| 17 | int i; |
| 18 | struct child_process child = CHILD_PROCESS_INIT; |
Ronan Pigott | 13d5bbd | 2022-11-09 12:07:07 -0700 | [diff] [blame] | 19 | char *abspath = interpolate_path(path, 0); |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 20 | |
| 21 | child.git_cmd = 1; |
Ronan Pigott | 13d5bbd | 2022-11-09 12:07:07 -0700 | [diff] [blame] | 22 | strvec_pushl(&child.args, "-C", abspath, NULL); |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 23 | |
Andrzej Hunt | 2b29994 | 2021-07-25 15:08:22 +0200 | [diff] [blame] | 24 | for (i = 0; i < argc; i++) |
| 25 | strvec_push(&child.args, argv[i]); |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 26 | |
Ronan Pigott | 13d5bbd | 2022-11-09 12:07:07 -0700 | [diff] [blame] | 27 | free(abspath); |
| 28 | |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 29 | return run_command(&child); |
| 30 | } |
| 31 | |
| 32 | int 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ð Bjarmason | f7b2ff9 | 2023-03-28 16:04:25 +0200 | [diff] [blame] | 37 | int err; |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 38 | |
| 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ð Bjarmason | 3611f74 | 2023-03-28 16:04:28 +0200 | [diff] [blame] | 51 | err = repo_config_get_string_multi(the_repository, config_key, &values); |
Ævar Arnfjörð Bjarmason | f7b2ff9 | 2023-03-28 16:04:25 +0200 | [diff] [blame] | 52 | 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 Stolee | 6c62f01 | 2021-01-08 02:30:46 +0000 | [diff] [blame] | 56 | return 0; |
| 57 | |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 58 | for (i = 0; !result && i < values->nr; i++) |
Andrzej Hunt | 2b29994 | 2021-07-25 15:08:22 +0200 | [diff] [blame] | 59 | result = run_command_on_repo(values->items[i].string, argc, argv); |
Derrick Stolee | 4950b2a | 2020-09-11 17:49:16 +0000 | [diff] [blame] | 60 | |
| 61 | return result; |
| 62 | } |