Skip to content

Commit 1b1b4d4

Browse files
committed
Merge branch 'js/for-each-repo-keep-going' into maint-2.45
A scheduled "git maintenance" job is expected to work on all repositories it knows about, but it stopped at the first one that errored out. Now it keeps going. * js/for-each-repo-keep-going: maintenance: running maintenance should not stop on errors for-each-repo: optionally keep going on an error
2 parents 2a78de0 + c75662b commit 1b1b4d4

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
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
}

builtin/gc.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,7 @@ static int launchctl_schedule_plist(const char *exec_path, enum schedule_priorit
18701870
"<string>%s/git</string>\n"
18711871
"<string>--exec-path=%s</string>\n"
18721872
"<string>for-each-repo</string>\n"
1873+
"<string>--keep-going</string>\n"
18731874
"<string>--config=maintenance.repo</string>\n"
18741875
"<string>maintenance</string>\n"
18751876
"<string>run</string>\n"
@@ -2112,7 +2113,7 @@ static int schtasks_schedule_task(const char *exec_path, enum schedule_priority
21122113
"<Actions Context=\"Author\">\n"
21132114
"<Exec>\n"
21142115
"<Command>\"%s\\headless-git.exe\"</Command>\n"
2115-
"<Arguments>--exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
2116+
"<Arguments>--exec-path=\"%s\" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%s</Arguments>\n"
21162117
"</Exec>\n"
21172118
"</Actions>\n"
21182119
"</Task>\n";
@@ -2257,7 +2258,7 @@ static int crontab_update_schedule(int run_maintenance, int fd)
22572258
"# replaced in the future by a Git command.\n\n");
22582259

22592260
strbuf_addf(&line_format,
2260-
"%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%s\n",
2261+
"%%d %%s * * %%s \"%s/git\" --exec-path=\"%s\" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%%s\n",
22612262
exec_path, exec_path);
22622263
fprintf(cron_in, line_format.buf, minute, "1-23", "*", "hourly");
22632264
fprintf(cron_in, line_format.buf, minute, "0", "1-6", "daily");
@@ -2458,7 +2459,7 @@ static int systemd_timer_write_service_template(const char *exec_path)
24582459
"\n"
24592460
"[Service]\n"
24602461
"Type=oneshot\n"
2461-
"ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --config=maintenance.repo maintenance run --schedule=%%i\n"
2462+
"ExecStart=\"%s/git\" --exec-path=\"%s\" for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=%%i\n"
24622463
"LockPersonality=yes\n"
24632464
"MemoryDenyWriteExecute=yes\n"
24642465
"NoNewPrivileges=yes\n"

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

t/t7900-maintenance.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,9 @@ test_expect_success 'start from empty cron table' '
639639
# start registers the repo
640640
git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
641641
642-
grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
643-
grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
644-
grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
642+
grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
643+
grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
644+
grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
645645
'
646646

647647
test_expect_success 'stop from existing schedule' '

0 commit comments

Comments
 (0)