[PATCH v3 1/2] maintenance: report lock acquisition failures
- From
Pushkar Singh <pushkarkumarsingh1970@gmail.com>
- Date
- Nov 4, 2026, 17:34 UTC
- Message-ID
- <20261104173422.20725-2-pushkarkumarsingh1970@gmail.com>
- In-Reply-To
- <20261104173422.20725-1-pushkarkumarsingh1970@gmail.com>
When git maintenance run fails to acquire the maintenance lock, report the failure instead of silently succeeding.
For automatic invocations, print a warning while preserving the successful exit status. For manual invocations, report the failure as an error and return a non-zero exit status.
Respect the --quiet option when reporting the failure.
Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com> --- builtin/gc.c | 15 +++++++++++---- t/t7900-maintenance.sh | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/builtin/gc.c b/builtin/gc.c index 81cecf6b6c..9ca9a3584b 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -1402,6 +1402,7 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts, enum auto_gc_hook_result auto_gc_hook_result = AUTO_GC_HOOK_UNDECIDED; if (repo_hold_lock_file_for_update(r, &lk, lock_path, LOCK_NO_DEREF) < 0) { + int err = errno; /* * Another maintenance command is running. * @@ -1409,11 +1410,17 @@ static int maintenance_run_tasks(struct maintenance_run_opts *opts, * recursive process stack. Do not report an error in * that case. */ - if (!opts->auto_flag && !opts->quiet) - warning(_("lock file '%s' exists, skipping maintenance"), - lock_path); + if (!opts->quiet) { + struct strbuf buf = STRBUF_INIT; + unable_to_lock_message(lock_path, err, &buf); + if (opts->auto_flag) + warning("%s", buf.buf); + else + error("%s", buf.buf); + strbuf_release(&buf); + } free(lock_path); - return 0; + return opts->auto_flag ? 0 : -1; } free(lock_path); diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh index 9d610a29fe..3288efb391 100755 --- a/t/t7900-maintenance.sh +++ b/t/t7900-maintenance.sh @@ -387,6 +387,30 @@ test_expect_success 'loose-objects task' ' test_cmp packs-between packs-after ' +test_expect_success 'auto maintenance warns when lock exists' ' + test_when_finished "rm -f err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + git maintenance run --auto --no-quiet 2>err && + test_grep "Unable to create.*maintenance\.lock" err && + test_path_is_file .git/objects/maintenance.lock +' + +test_expect_success 'manual maintenance fails with existing lock' ' + test_when_finished "rm -f err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + test_must_fail git maintenance run --no-quiet 2>err && + test_grep "maintenance\.lock" err && + test_path_is_file .git/objects/maintenance.lock +' + +test_expect_success 'manual maintenance with --quiet suppresses diagnostic' ' + test_when_finished "rm -f err .git/objects/maintenance.lock" && + : >.git/objects/maintenance.lock && + test_must_fail git maintenance run --quiet 2>err && + test_must_be_empty err && + test_path_is_file .git/objects/maintenance.lock +' + test_expect_success 'maintenance.loose-objects.auto' ' git repack -adk && GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
-- 2.53.0