builtin/maintenance: add a `--detach` flag

Same as the preceding commit, add a `--[no-]detach` flag to the git-maintenance(1) command. This will be used in a subsequent commit to fix backgrounding of that command when configured with a non-standard set of tasks. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 16, 2024 at 12:45 UTC a6affd3343f143e5983036150bb8e95eb336cbc7
2 files changed +45
builtin/gc.c
+6
@@ -1426,6 +1426,10 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts,
1426 }
1427 free(lock_path);
1428
1429 + /* Failure to daemonize is ok, we'll continue in foreground. */
1430 + if (opts->detach > 0)
1431 + daemonize();
1432 +
1433 for (i = 0; !found_selected && i < TASK__COUNT; i++)
1434 found_selected = tasks[i].selected_order >= 0;
1435
@@ -1552,6 +1556,8 @@ static int maintenance_run(int argc, const char **argv, const char *prefix)
1556 struct option builtin_maintenance_run_options[] = {
1557 OPT_BOOL(0, "auto", &opts.auto_flag,
1558 N_("run tasks based on the state of the repository")),
1559 + OPT_BOOL(0, "detach", &opts.detach,
1560 + N_("perform maintenance in the background")),
1561 OPT_CALLBACK(0, "schedule", &opts.schedule, N_("frequency"),
1562 N_("run tasks based on frequency"),
1563 maintenance_opt_schedule),
t/t7900-maintenance.sh
+39
@@ -908,4 +908,43 @@ test_expect_success 'failed schedule prevents config change' '
908 done
909 '
910
911 +test_expect_success '--no-detach causes maintenance to not run in background' '
912 + test_when_finished "rm -rf repo" &&
913 + git init repo &&
914 + (
915 + cd repo &&
916 +
917 + # Prepare the repository such that git-maintenance(1) ends up
918 + # outputting something.
919 + test_commit something &&
920 + git config set maintenance.gc.enabled false &&
921 + git config set maintenance.loose-objects.enabled true &&
922 + git config set maintenance.loose-objects.auto 1 &&
923 + git config set maintenance.incremental-repack.enabled true &&
924 +
925 + # We have no better way to check whether or not the task ran in
926 + # the background than to verify whether it output anything. The
927 + # next testcase checks the reverse, making this somewhat safer.
928 + git maintenance run --no-detach >out 2>&1 &&
929 + test_line_count = 1 out
930 + )
931 +'
932 +
933 +test_expect_success '--detach causes maintenance to run in background' '
934 + test_when_finished "rm -rf repo" &&
935 + git init repo &&
936 + (
937 + cd repo &&
938 +
939 + test_commit something &&
940 + git config set maintenance.gc.enabled false &&
941 + git config set maintenance.loose-objects.enabled true &&
942 + git config set maintenance.loose-objects.auto 1 &&
943 + git config set maintenance.incremental-repack.enabled true &&
944 +
945 + git maintenance run --detach >out 2>&1 &&
946 + test_must_be_empty out
947 + )
948 +'
949 +
950 test_done