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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 17d025e..20cc08f 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -901,6 +901,7 @@ static int packed_write_ref(struct packref *ref, git_filebuf *file)
static int packed_remove_loose(refdb_fs_backend *backend)
{
size_t i;
+ git_filebuf lock = GIT_FILEBUF_INIT;
git_buf ref_content = GIT_BUF_INIT;
int error = 0;
@@ -908,12 +909,13 @@ static int packed_remove_loose(refdb_fs_backend *backend)
for (i = 0; i < git_sortedcache_entrycount(backend->refcache); ++i) {
struct packref *ref = git_sortedcache_entry(backend->refcache, i);
- git_filebuf lock = GIT_FILEBUF_INIT;
git_oid current_id;
if (!ref || !(ref->flags & PACKREF_WAS_LOOSE))
continue;
+ git_filebuf_cleanup(&lock);
+
/* We need to stop anybody from updating the ref while we try to do a safe delete */
error = loose_lock(&lock, backend, ref->name);
/* If someone else is updating it, let them do it */
@@ -927,28 +929,20 @@ static int packed_remove_loose(refdb_fs_backend *backend)
error = git_futils_readbuffer(&ref_content, lock.path_original);
/* Someone else beat us to cleaning up the ref, let's simply continue */
- if (error == GIT_ENOTFOUND) {
- git_filebuf_cleanup(&lock);
+ if (error == GIT_ENOTFOUND)
continue;
- }
/* This became a symref between us packing and trying to delete it, so ignore it */
- if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF)) {
- git_filebuf_cleanup(&lock);
+ if (!git__prefixcmp(ref_content.ptr, GIT_SYMREF))
continue;
- }
- /* Figure out the current id; if we fail record it but don't fail the whole operation */
- if ((error = loose_parse_oid(¤t_id, lock.path_original, &ref_content)) < 0) {
- git_filebuf_cleanup(&lock);
+ /* Figure out the current id; if we find a bad ref file, skip it so we can do the rest */
+ if (loose_parse_oid(¤t_id, lock.path_original, &ref_content) < 0)
continue;
- }
/* If the ref moved since we packed it, we must not delete it */
- if (!git_oid_equal(¤t_id, &ref->oid)) {
- git_filebuf_cleanup(&lock);
+ if (!git_oid_equal(¤t_id, &ref->oid))
continue;
- }
/*
* 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)
* we haven't lost information.
*/
p_unlink(lock.path_original);
- git_filebuf_cleanup(&lock);
}
+ git_filebuf_cleanup(&lock);
return 0;
}