* [PATCH] refs/files: remove empty parent dirs when ref creation fails
@ 2025-07-08 10:19 Patrick Steinhardt
2025-07-08 21:52 ` Junio C Hamano
0 siblings, 1 reply; 2+ messages in thread
From: Patrick Steinhardt @ 2025-07-08 10:19 UTC (permalink / raw)
To: git
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>
---
Hi,
this issue is something we recently discovered in Gitaly. It's nothing
world breaking, but I think it makes sense to try and keep the refdb in
a as-clean-as-possible state anyway.
Thanks!
Patrick
---
refs/files-backend.c | 2 ++
t/t1400-update-ref.sh | 19 +++++++++++++++++++
2 files changed, 21 insertions(+)
diff --git a/refs/files-backend.c b/refs/files-backend.c
index bf6f89b1d19..00128f21832 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 e373d9108b6..c9893f65464 100755
--- a/t/t1400-update-ref.sh
+++ b/t/t1400-update-ref.sh
@@ -2304,4 +2304,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
---
base-commit: 41905d60226a0346b22f0d0d99428c746a5a3b14
change-id: 20250708-b4-pks-reffiles-prune-empty-dirs-on-abort-15c7a7f279bd
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] refs/files: remove empty parent dirs when ref creation fails
2025-07-08 10:19 [PATCH] refs/files: remove empty parent dirs when ref creation fails Patrick Steinhardt
@ 2025-07-08 21:52 ` Junio C Hamano
0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2025-07-08 21:52 UTC (permalink / raw)
To: Patrick Steinhardt; +Cc: git
Patrick Steinhardt <ps@pks•im> writes:
> 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>
> ---
> Hi,
>
> this issue is something we recently discovered in Gitaly. It's nothing
> world breaking, but I think it makes sense to try and keep the refdb in
> a as-clean-as-possible state anyway.
Would we lose an empty ".git/refs/tags/" directory if we fail to
create the first tag?
... goes and looks at the try_remove function ...
OK, we protect the leading two levels when doing so, so we are safe.
Will queue. Thanks.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-07-08 21:52 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-08 10:19 [PATCH] refs/files: remove empty parent dirs when ref creation fails Patrick Steinhardt
2025-07-08 21:52 ` Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox