Skip to content

Commit 39ee638

Browse files
committed
for-each-repo: optionally keep going on an error
In microsoft#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>
1 parent 3c2a3fd commit 39ee638

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Documentation/git-for-each-repo.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ These config values are loaded from system, global, and local Git config,
4242
as available. If `git for-each-repo` is run in a directory that is not a
4343
Git repository, then only the system and global config is used.
4444

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.
4554

4655
SUBPROCESS BEHAVIOR
4756
-------------------

builtin/for-each-repo.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ static int run_command_on_repo(const char *path, int argc, const char ** argv)
3232
int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
3333
{
3434
static const char *config_key = NULL;
35+
int keep_going = 0;
3536
int i, result = 0;
3637
const struct string_list *values;
3738
int err;
3839

3940
const struct option options[] = {
4041
OPT_STRING(0, "config", &config_key, N_("config"),
4142
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")),
4245
OPT_END()
4346
};
4447

@@ -55,8 +58,14 @@ int cmd_for_each_repo(int argc, const char **argv, const char *prefix)
5558
else if (err)
5659
return 0;
5760

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+
}
6069

6170
return result;
6271
}

t/t0068-for-each-repo.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,20 @@ test_expect_success 'error on NULL value for config keys' '
5959
test_cmp expect actual
6060
'
6161

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+
6278
test_done

0 commit comments

Comments
 (0)