maintenance: swap method locations

The systemd_timer_write_unit_templates() method writes a single template that is then used to start the hourly, daily, and weekly schedules with systemd. However, in order to schedule systemd maintenance on a given minute, these templates need to be replaced with specific schedules for each of these jobs. Before modifying the schedules, move the writing method above the systemd_timer_enable_unit() method, so we can write a specific schedule for each unit. The diff is computed smaller by showing systemd_timer_enable_unit() and systemd_timer_delete_units() move instead of systemd_timer_write_unit_templates() and systemd_timer_delete_unit_templates(). Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Aug 10, 2023 at 20:39 UTC f44d7d00e5f71ca5f232bca3793dfafd98329e0d
1 file changed +48 -48
builtin/gc.c
+48 -48
@@ -2292,46 +2292,6 @@ static char *xdg_config_home_systemd(const char *filename)
2292 return xdg_config_home_for("systemd/user", filename);
2293 }
2294
2295 -static int systemd_timer_enable_unit(int enable,
2296 - enum schedule_priority schedule)
2297 -{
2298 - const char *cmd = "systemctl";
2299 - struct child_process child = CHILD_PROCESS_INIT;
2300 - const char *frequency = get_frequency(schedule);
2301 -
2302 - /*
2303 - * Disabling the systemd unit while it is already disabled makes
2304 - * systemctl print an error.
2305 - * Let's ignore it since it means we already are in the expected state:
2306 - * the unit is disabled.
2307 - *
2308 - * On the other hand, enabling a systemd unit which is already enabled
2309 - * produces no error.
2310 - */
2311 - if (!enable)
2312 - child.no_stderr = 1;
2313 -
2314 - get_schedule_cmd(&cmd, NULL);
2315 - strvec_split(&child.args, cmd);
2316 - strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2317 - "--now", NULL);
2318 - strvec_pushf(&child.args, "git-maintenance@%s.timer", frequency);
2319 -
2320 - if (start_command(&child))
2321 - return error(_("failed to start systemctl"));
2322 - if (finish_command(&child))
2323 - /*
2324 - * Disabling an already disabled systemd unit makes
2325 - * systemctl fail.
2326 - * Let's ignore this failure.
2327 - *
2328 - * Enabling an enabled systemd unit doesn't fail.
2329 - */
2330 - if (enable)
2331 - return error(_("failed to run systemctl"));
2332 - return 0;
2333 -}
2334 -
2295 static int systemd_timer_delete_unit_templates(void)
2296 {
2297 int ret = 0;
@@ -2348,14 +2308,6 @@ static int systemd_timer_delete_unit_templates(void)
2308 return ret;
2309 }
2310
2351 -static int systemd_timer_delete_units(void)
2352 -{
2353 - return systemd_timer_enable_unit(0, SCHEDULE_HOURLY) ||
2354 - systemd_timer_enable_unit(0, SCHEDULE_DAILY) ||
2355 - systemd_timer_enable_unit(0, SCHEDULE_WEEKLY) ||
2356 - systemd_timer_delete_unit_templates();
2357 -}
2358 -
2311 static int systemd_timer_write_unit_templates(const char *exec_path)
2312 {
2313 char *filename;
@@ -2437,6 +2389,54 @@ error:
2389 return -1;
2390 }
2391
2392 +static int systemd_timer_enable_unit(int enable,
2393 + enum schedule_priority schedule)
2394 +{
2395 + const char *cmd = "systemctl";
2396 + struct child_process child = CHILD_PROCESS_INIT;
2397 + const char *frequency = get_frequency(schedule);
2398 +
2399 + /*
2400 + * Disabling the systemd unit while it is already disabled makes
2401 + * systemctl print an error.
2402 + * Let's ignore it since it means we already are in the expected state:
2403 + * the unit is disabled.
2404 + *
2405 + * On the other hand, enabling a systemd unit which is already enabled
2406 + * produces no error.
2407 + */
2408 + if (!enable)
2409 + child.no_stderr = 1;
2410 +
2411 + get_schedule_cmd(&cmd, NULL);
2412 + strvec_split(&child.args, cmd);
2413 + strvec_pushl(&child.args, "--user", enable ? "enable" : "disable",
2414 + "--now", NULL);
2415 + strvec_pushf(&child.args, "git-maintenance@%s.timer", frequency);
2416 +
2417 + if (start_command(&child))
2418 + return error(_("failed to start systemctl"));
2419 + if (finish_command(&child))
2420 + /*
2421 + * Disabling an already disabled systemd unit makes
2422 + * systemctl fail.
2423 + * Let's ignore this failure.
2424 + *
2425 + * Enabling an enabled systemd unit doesn't fail.
2426 + */
2427 + if (enable)
2428 + return error(_("failed to run systemctl"));
2429 + return 0;
2430 +}
2431 +
2432 +static int systemd_timer_delete_units(void)
2433 +{
2434 + return systemd_timer_enable_unit(0, SCHEDULE_HOURLY) ||
2435 + systemd_timer_enable_unit(0, SCHEDULE_DAILY) ||
2436 + systemd_timer_enable_unit(0, SCHEDULE_WEEKLY) ||
2437 + systemd_timer_delete_unit_templates();
2438 +}
2439 +
2440 static int systemd_timer_setup_units(void)
2441 {
2442 const char *exec_path = git_exec_path();