refs/reftable: don't fail empty transactions in repo without HEAD

Under normal circumstances, it shouldn't ever happen that a repository has no HEAD reference. In fact, git-update-ref(1) would fail any request to delete the HEAD reference, and a newly initialized repository always pre-creates it, too. We have however changed git-clone(1) to partially initialize the refdb just up to the point where remote helpers can find the repository. With that change, we are going to run into a situation where repositories have no refs at all. Now there is a very particular edge case in this situation: when preparing an empty ref transacton, we end up returning whatever value `read_ref_without_reload()` returned to the caller. Under normal conditions this would be fine: "HEAD" should usually exist, and thus the function would return `0`. But if "HEAD" doesn't exist, the function returns a positive value which we end up returning to the caller. Fix this bug by resetting the return code to `0` and add a test. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 27, 2024 at 15:27 UTC b0f6b6b523c58cb204292455ed7121896f0aa6ed
2 files changed +14
refs/reftable-backend.c
+1
@@ -821,6 +821,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store,
821 &head_referent, &head_type);
822 if (ret < 0)
823 goto done;
824 + ret = 0;
825
826 for (i = 0; i < transaction->nr; i++) {
827 struct ref_update *u = transaction->updates[i];
t/t0610-reftable-basics.sh
+13
@@ -328,6 +328,19 @@ test_expect_success 'ref transaction: writes are synced' '
328 EOF
329 '
330
331 +test_expect_success 'ref transaction: empty transaction in empty repo' '
332 + test_when_finished "rm -rf repo" &&
333 + git init repo &&
334 + test_commit -C repo --no-tag A &&
335 + COMMIT=$(git -C repo rev-parse HEAD) &&
336 + git -C repo update-ref -d refs/heads/main &&
337 + test-tool -C repo ref-store main delete-refs REF_NO_DEREF msg HEAD &&
338 + git -C repo update-ref --stdin <<-EOF
339 + prepare
340 + commit
341 + EOF
342 +'
343 +
344 test_expect_success 'pack-refs: compacts tables' '
345 test_when_finished "rm -rf repo" &&
346 git init repo &&