Before commit 976640edbb (maintenance: use packaged systemd units, 2024-03-21), we we're putting systemd unit files in $XDG_CONFIG_HOME ; these could mask those we are now distributing as part of git. Remove all the systemd units possibly created by previous version of git when running `git maintenance start/stop`. Avoid overwriting units we didn't write, by comparing the first line with the start of the comment we added to our unit files previously. Signed-off-by: Max Gautier <mg@xxxxxxxxxxxxxxxx> --- Notes: How should I refer to a commit which is part of the same patch series ? The commit id will change so the message won't be correct anymore, right ? builtin/gc.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/builtin/gc.c b/builtin/gc.c index aaee91451a..99b158e481 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -2329,8 +2329,50 @@ static int systemd_set_units_state(int enable) return 0; } +/* + * TODO: in the future (~2026 ?) remove this cleanup code + */ +static void systemd_delete_user_unit(char const *unit) +{ + char const file_start_stale[] = "# This file was created and is" + " maintained by Git."; + char file_start_user[sizeof(file_start_stale)] = {'\0'}; + + char *filename = xdg_config_home_for("systemd/user", unit); + int handle = open(filename, O_RDONLY); + + /* + * Check this is actually our file and we're not removing a legitimate + * user override. + */ + if (handle == -1 && !is_missing_file_error(errno)) + warning(_("failed to delete '%s'"), filename); + else { + read(handle, file_start_user, sizeof(file_start_stale) - 1); + close(handle); + if (strcmp(file_start_stale, file_start_user) == 0) { + if (unlink(filename) == 0) + warning(_("deleted stale unit file '%s'"), filename); + else if (!is_missing_file_error(errno)) + warning(_("failed to delete '%s'"), filename); + } + } + + free(filename); +} + static int systemd_timer_update_schedule(int run_maintenance, int fd UNUSED) { + /* + * A previous version of Git wrote the units in the user configuration + * directory. Clean these up, if they exist. + * TODO: in the future (~2026 ?) remove this cleanup code + */ + systemd_delete_user_unit("git-maintenance@hourly.timer"); + systemd_delete_user_unit("git-maintenance@daily.timer"); + systemd_delete_user_unit("git-maintenance@weekly.timer"); + systemd_delete_user_unit("git-maintenance@.timer"); + systemd_delete_user_unit("git-maintenance@.service"); return systemd_set_units_state(run_maintenance); } -- 2.44.0