for-each-repo: optionally keep going on an error

In https://github.com/microsoft/git/issues/623, it was reported that the regularly scheduled maintenance stops if one repo in the middle of the list was found to be missing. This is undesirable, and points out a gap in the design of `git for-each-repo`: We need a mode where that command does not stop on an error, but continues to try running the specified command with the other repositories. Imitating the `--keep-going` option of GNU make, this commit teaches `for-each-repo` the same trick: to continue with the operation on all the remaining repositories in case there was a problem with one repository, still setting the exit code to indicate an error occurred. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Helped-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 24, 2024 at 16:14 UTC 12c2ee5fbd1ab0f0fcf7ca37c613d438db52821d
3 files changed +36 -2
Documentation/git-for-each-repo.txt
+9
@@ -42,6 +42,15 @@ These config values are loaded from system, global, and local Git config,
42 as available. If `git for-each-repo` is run in a directory that is not a
43 Git repository, then only the system and global config is used.
44
45 +--keep-going::
46 + Continue with the remaining repositories if the command failed
47 + on a repository. The exit code will still indicate that the
48 + overall operation was not successful.
49 ++
50 +Note that the exact exit code of the failing command is not passed
51 +through as the exit code of the `for-each-repo` command: If the command
52 +failed in any of the specified repositories, the overall exit code will
53 +be 1.
54
55 SUBPROCESS BEHAVIOR
56 -------------------
builtin/for-each-repo.c
+11 -2
@@ -32,6 +32,7 @@ static int run_command_on_repo(const char *path, int argc, const char ** argv)
32 int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
33 {
34 static const char *config_key = NULL;
35 + int keep_going = 0;
36 int i, result = 0;
37 const struct string_list *values;
38 int err;
@@ -39,6 +40,8 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
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_BOOL(0, "keep-going", &keep_going,
44 + N_("keep going even if command fails in a repository")),
45 OPT_END()
46 };
47
@@ -55,8 +58,14 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
58 else if (err)
59 return 0;
60
58 - for (i = 0; !result && i < values->nr; i++)
59 - result = run_command_on_repo(values->items[i].string, argc, argv);
61 + for (i = 0; i < values->nr; i++) {
62 + int ret = run_command_on_repo(values->items[i].string, argc, argv);
63 + if (ret) {
64 + if (!keep_going)
65 + return ret;
66 + result = 1;
67 + }
68 + }
69
70 return result;
71 }
t/t0068-for-each-repo.sh
+16
@@ -59,4 +59,20 @@ test_expect_success 'error on NULL value for config keys' '
59 test_cmp expect actual
60 '
61
62 +test_expect_success '--keep-going' '
63 + git config keep.going non-existing &&
64 + git config --add keep.going . &&
65 +
66 + test_must_fail git for-each-repo --config=keep.going \
67 + -- branch >out 2>err &&
68 + test_grep "cannot change to .*non-existing" err &&
69 + test_must_be_empty out &&
70 +
71 + test_must_fail git for-each-repo --config=keep.going --keep-going \
72 + -- branch >out 2>err &&
73 + test_grep "cannot change to .*non-existing" err &&
74 + git branch >expect &&
75 + test_cmp expect out
76 +'
77 +
78 test_done