Skip to content

Commit 0a88c83

Browse files
committed
refdb: make low-level deletion helpers explicit
1 parent baf411e commit 0a88c83

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

src/refdb_fs.c

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,30 @@ static int packed_write(refdb_fs_backend *backend)
10971097
return error;
10981098
}
10991099

1100+
static int packed_delete(refdb_fs_backend *backend, const char *ref_name)
1101+
{
1102+
size_t pack_pos;
1103+
int error;
1104+
1105+
if ((error = packed_reload(backend)) < 0)
1106+
goto cleanup;
1107+
1108+
/* If a packed reference exists, remove it from the packfile and repack */
1109+
if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
1110+
goto cleanup;
1111+
1112+
if (!(error = git_sortedcache_lookup_index(
1113+
&pack_pos, backend->refcache, ref_name)))
1114+
error = git_sortedcache_remove(backend->refcache, pack_pos);
1115+
1116+
git_sortedcache_wunlock(backend->refcache);
1117+
1118+
error = packed_write(backend);
1119+
1120+
cleanup:
1121+
return error;
1122+
}
1123+
11001124
static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *author, const char *message);
11011125
static int has_reflog(git_repository *repo, const char *name);
11021126

@@ -1382,15 +1406,32 @@ static int refdb_fs_backend__delete(
13821406
return refdb_fs_backend__delete_tail(_backend, &file, ref_name, old_id, old_target);
13831407
}
13841408

1409+
static int loose_delete(refdb_fs_backend *backend, const char *ref_name)
1410+
{
1411+
git_buf loose_path = GIT_BUF_INIT;
1412+
int error = 0;
1413+
1414+
if (git_buf_joinpath(&loose_path, backend->commonpath, ref_name) < 0)
1415+
return -1;
1416+
1417+
error = p_unlink(loose_path.ptr);
1418+
if (error < 0 && errno == ENOENT)
1419+
error = GIT_ENOTFOUND;
1420+
else if (error != 0)
1421+
error = -1;
1422+
1423+
git_buf_dispose(&loose_path);
1424+
1425+
return error;
1426+
}
1427+
13851428
static int refdb_fs_backend__delete_tail(
13861429
git_refdb_backend *_backend,
13871430
git_filebuf *file,
13881431
const char *ref_name,
13891432
const git_oid *old_id, const char *old_target)
13901433
{
13911434
refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
1392-
git_buf loose_path = GIT_BUF_INIT;
1393-
size_t pack_pos;
13941435
int error = 0, cmp = 0;
13951436
bool loose_deleted = 0;
13961437

@@ -1405,40 +1446,23 @@ static int refdb_fs_backend__delete_tail(
14051446
}
14061447

14071448
/* If a loose reference exists, remove it from the filesystem */
1408-
if (git_buf_joinpath(&loose_path, backend->commonpath, ref_name) < 0)
1409-
return -1;
1410-
1449+
if ((error = loose_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
1450+
goto cleanup;
14111451

1412-
error = p_unlink(loose_path.ptr);
1413-
if (error < 0 && errno == ENOENT)
1452+
if (error == GIT_ENOTFOUND)
14141453
error = 0;
1415-
else if (error < 0)
1416-
goto cleanup;
14171454
else if (error == 0)
14181455
loose_deleted = 1;
14191456

1420-
if ((error = packed_reload(backend)) < 0)
1421-
goto cleanup;
1422-
1423-
/* If a packed reference exists, remove it from the packfile and repack */
1424-
if ((error = git_sortedcache_wlock(backend->refcache)) < 0)
1457+
if ((error = packed_delete(backend, ref_name)) < 0 && error != GIT_ENOTFOUND)
14251458
goto cleanup;
14261459

1427-
if (!(error = git_sortedcache_lookup_index(
1428-
&pack_pos, backend->refcache, ref_name)))
1429-
error = git_sortedcache_remove(backend->refcache, pack_pos);
1430-
1431-
git_sortedcache_wunlock(backend->refcache);
1432-
14331460
if (error == GIT_ENOTFOUND) {
14341461
error = loose_deleted ? 0 : ref_error_notfound(ref_name);
14351462
goto cleanup;
14361463
}
14371464

1438-
error = packed_write(backend);
1439-
14401465
cleanup:
1441-
git_buf_dispose(&loose_path);
14421466
git_filebuf_cleanup(file);
14431467
if (loose_deleted)
14441468
refdb_fs_backend__try_delete_empty_ref_hierarchie(backend, ref_name, false);

0 commit comments

Comments
 (0)