aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-07-08 12:19:54 +0200
committerJunio C Hamano <gitster@pobox.com>2025-07-08 14:52:56 -0700
commita3a7f2051686e087cba80f3af1557107406205c9 (patch)
treec46b4262dbe4605cf39b7df386fbef8dce6776b9
parent16bd9f20a403117f2e0d9bcda6c6e621d3763e77 (diff)
downloadgit-a3a7f2051686e087cba80f3af1557107406205c9.tar.gz
refs/files: remove empty parent dirs when ref creation fails
When creating a new reference in the "files" backend we first create the directory hierarchy for that reference, then create the lockfile for that reference, and finally rename the lockfile into place. When the transaction gets aborted we prune the lockfile, but we don't clean up the directory hierarchy that we may have created for the lockfile. In some egde cases this can lead to lots of empty directories being cluttered in the ".git/refs" directory that really serve no purpose at all. We know to prune such empty directories when packing refs, but that only patches over the issue. Improve this by removing empty parents when cleaning up still-locked references in `files_transaction_cleanup()`. This function is also called when preparing or committing the transaction, so this change also helps when not explicitly aborting the transaction. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--refs/files-backend.c2
-rwxr-xr-xt/t1400-update-ref.sh19
2 files changed, 21 insertions, 0 deletions
diff --git a/refs/files-backend.c b/refs/files-backend.c
index bf6f89b1d1..00128f2183 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -2760,6 +2760,8 @@ static void files_transaction_cleanup(struct files_ref_store *refs,
if (lock) {
unlock_ref(lock);
+ try_remove_empty_parents(refs, update->refname,
+ REMOVE_EMPTY_PARENTS_REF);
update->backend_data = NULL;
}
}
diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
index d29d23cb89..42bbe22c85 100755
--- a/t/t1400-update-ref.sh
+++ b/t/t1400-update-ref.sh
@@ -2310,4 +2310,23 @@ test_expect_success 'update-ref should also create reflog for HEAD' '
test_cmp expect actual
'
+test_expect_success REFFILES 'empty directories are pruned when aborting a transaction' '
+ test_path_is_missing .git/refs/heads/nested &&
+ git update-ref --stdin <<-EOF &&
+ create refs/heads/nested/something HEAD
+ prepare
+ abort
+ EOF
+ test_path_is_missing .git/refs/heads/nested
+'
+
+test_expect_success REFFILES 'empty directories are pruned when not committing' '
+ test_path_is_missing .git/refs/heads/nested &&
+ git update-ref --stdin <<-EOF &&
+ create refs/heads/nested/something HEAD
+ prepare
+ EOF
+ test_path_is_missing .git/refs/heads/nested
+'
+
test_done