diff --git a/builtin/gc.c b/builtin/gc.c index d52735354c..34848626e4 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -2890,8 +2890,17 @@ static int update_background_schedule(const struct maintenance_start_opts *opts, char *lock_path = xstrfmt("%s/schedule", the_repository->objects->odb->path); if (hold_lock_file_for_update(&lk, lock_path, LOCK_NO_DEREF) < 0) { + if (errno == EEXIST) + error(_("unable to create '%s.lock': %s.\n\n" + "Another scheduled git-maintenance(1) process seems to be running in this\n" + "repository. Please make sure no other maintenance processes are running and\n" + "then try again. If it still fails, a git-maintenance(1) process may have\n" + "crashed in this repository earlier: remove the file manually to continue."), + absolute_path(lock_path), strerror(errno)); + else + error_errno(_("cannot acquire lock for scheduled background maintenance")); free(lock_path); - return error(_("another process is scheduling background maintenance")); + return -1; } for (i = 1; i < ARRAY_SIZE(scheduler_fn); i++) { diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh index c224c8450c..4bbc171958 100755 --- a/t/t7900-maintenance.sh +++ b/t/t7900-maintenance.sh @@ -1011,4 +1011,17 @@ test_expect_success 'repacking loose objects is quiet' ' ) ' +test_expect_success 'maintenance aborts with existing lock file' ' + test_when_finished "rm -rf repo script" && + mkdir script && + write_script script/systemctl <<-\EOF && + true + EOF + + git init repo && + : >repo/.git/objects/schedule.lock && + test_must_fail env PATH="$PWD/script:$PATH" git -C repo maintenance start --scheduler=systemd 2>err && + test_grep "Another scheduled git-maintenance(1) process seems to be running" err +' + test_done