Skip to content

Commit 6721e3a

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 the 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. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 3c2a3fd commit 6721e3a

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

Documentation/git-for-each-repo.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ 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.
4549

4650
SUBPROCESS BEHAVIOR
4751
-------------------

builtin/for-each-repo.c

Lines changed: 6 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_("stop at the first repository where the operation failed")),
4245
OPT_END()
4346
};
4447

@@ -55,8 +58,9 @@ 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; (keep_going || !result) && i < values->nr; i++)
62+
if (run_command_on_repo(values->items[i].string, argc, argv))
63+
result = 1;
6064

6165
return result;
6266
}

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)