Skip to content

Commit dd1ca6f

Browse files
committed
refdb: refactor the lockfile cleanup
We can reduce the duplication by cleaning up at the beginning of the loop, since it's something we want to do every time we continue.
1 parent 7ea4710 commit dd1ca6f

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/refdb_fs.c

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -901,19 +901,21 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file)
901901
static int packed_remove_loose(refdb_fs_backend *backend)
902902
{
903903
size_t i;
904+
git_filebuf lock = GIT_FILEBUF_INIT;
904905
git_buf ref_content = GIT_BUF_INIT;
905906
int error = 0;
906907

907908
/* backend->refcache is already locked when this is called */
908909

909910
for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
910911
struct packref *ref = git_sortedcache_entry(backend->refcache, i);
911-
git_filebuf lock = GIT_FILEBUF_INIT;
912912
git_oid current_id;
913913

914914
if (!ref || !(ref->flags & PACKREF_WAS_LOOSE))
915915
continue;
916916

917+
git_filebuf_cleanup(&lock);
918+
917919
/* We need to stop anybody from updating the ref while we try to do a safe delete */
918920
error = loose_lock(&lock, backend, ref->name);
919921
/* If someone else is updating it, let them do it */
@@ -927,28 +929,20 @@ static int packed_remove_loose(refdb_fs_backend *backend)
927929

928930
error = git_futils_readbuffer(&ref_content, lock.path_original);
929931
/* Someone else beat us to cleaning up the ref, let's simply continue */
930-
if (error == GIT_ENOTFOUND) {
931-
git_filebuf_cleanup(&lock);
932+
if (error == GIT_ENOTFOUND)
932933
continue;
933-
}
934934

935935
/* This became a symref between us packing and trying to delete it, so ignore it */
936-
if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF)) {
937-
git_filebuf_cleanup(&lock);
936+
if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF))
938937
continue;
939-
}
940938

941-
/* Figure out the current id; if we fail record it but don't fail the whole operation */
942-
if ((error = loose_parse_oid(&current_id, lock.path_original, &ref_content)) < 0) {
943-
git_filebuf_cleanup(&lock);
939+
/* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */
940+
if (loose_parse_oid(&current_id, lock.path_original, &ref_content) < 0)
944941
continue;
945-
}
946942

947943
/* If the ref moved since we packed it, we must not delete it */
948-
if (!git_oid_equal(&current_id, &ref->oid)) {
949-
git_filebuf_cleanup(&lock);
944+
if (!git_oid_equal(&current_id, &ref->oid))
950945
continue;
951-
}
952946

953947
/*
954948
* if we fail to remove a single file, this is *not* good,
@@ -957,9 +951,9 @@ static int packed_remove_loose(refdb_fs_backend *backend)
957951
* we haven't lost information.
958952
*/
959953
p_unlink(lock.path_original);
960-
git_filebuf_cleanup(&lock);
961954
}
962955

956+
git_filebuf_cleanup(&lock);
963957
return 0;
964958
}
965959

0 commit comments

Comments
 (0)