Skip to content

Commit 98a4f27

Browse files
committed
refdb_fs: use GIT_ASSERT
1 parent 8d66d57 commit 98a4f27

File tree

1 file changed

+55
-31
lines changed

1 file changed

+55
-31
lines changed

src/refdb_fs.c

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static int refdb_fs_backend__exists(
331331
git_buf ref_path = GIT_BUF_INIT;
332332
int error;
333333

334-
assert(backend);
334+
GIT_ASSERT_ARG(backend);
335335

336336
*exists = 0;
337337

@@ -472,7 +472,7 @@ static int refdb_fs_backend__lookup(
472472
refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
473473
int error;
474474

475-
assert(backend);
475+
GIT_ASSERT_ARG(backend);
476476

477477
if (!(error = loose_lookup(out, backend, ref_name)))
478478
return 0;
@@ -678,7 +678,7 @@ static int refdb_fs_backend__iterator(
678678
refdb_fs_iter *iter = NULL;
679679
int error;
680680

681-
assert(backend);
681+
GIT_ASSERT_ARG(backend);
682682

683683
iter = git__calloc(1, sizeof(refdb_fs_iter));
684684
GIT_ERROR_CHECK_ALLOC(iter);
@@ -783,7 +783,9 @@ static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *
783783
git_buf ref_path = GIT_BUF_INIT;
784784
const char *basedir;
785785

786-
assert(file && backend && name);
786+
GIT_ASSERT_ARG(file);
787+
GIT_ASSERT_ARG(backend);
788+
GIT_ASSERT_ARG(name);
787789

788790
if (!git_path_isvalid(backend->repo, name, 0, GIT_PATH_REJECT_FILESYSTEM_DEFAULTS)) {
789791
git_error_set(GIT_ERROR_INVALID, "invalid reference name '%s'", name);
@@ -819,7 +821,8 @@ static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *
819821

820822
static int loose_commit(git_filebuf *file, const git_reference *ref)
821823
{
822-
assert(file && ref);
824+
GIT_ASSERT_ARG(file);
825+
GIT_ASSERT_ARG(ref);
823826

824827
if (ref->type == GIT_REFERENCE_DIRECT) {
825828
char oid[GIT_OID_HEXSZ + 1];
@@ -829,7 +832,7 @@ static int loose_commit(git_filebuf *file, const git_reference *ref)
829832
} else if (ref->type == GIT_REFERENCE_SYMBOLIC) {
830833
git_filebuf_printf(file, GIT_SYMREF "%s\n", ref->target.symbolic);
831834
} else {
832-
assert(0); /* don't let this happen */
835+
GIT_ASSERT(0);
833836
}
834837

835838
return git_filebuf_commit(file);
@@ -1066,7 +1069,7 @@ static int packed_write(refdb_fs_backend *backend)
10661069

10671070
for (i = 0; i < git_sortedcache_entrycount(refcache); ++i) {
10681071
struct packref *ref = git_sortedcache_entry(refcache, i);
1069-
assert(ref);
1072+
GIT_ASSERT(ref);
10701073

10711074
if ((error = packed_find_peel(backend, ref)) < 0)
10721075
goto fail;
@@ -1219,7 +1222,7 @@ static int refdb_fs_backend__write(
12191222
git_filebuf file = GIT_FILEBUF_INIT;
12201223
int error = 0;
12211224

1222-
assert(backend);
1225+
GIT_ASSERT_ARG(backend);
12231226

12241227
if ((error = reference_path_available(backend, ref->name, NULL, force)) < 0)
12251228
return error;
@@ -1292,18 +1295,20 @@ static int refdb_fs_backend__write_tail(
12921295
return error;
12931296
}
12941297

1295-
static void refdb_fs_backend__prune_refs(
1298+
static int refdb_fs_backend__prune_refs(
12961299
refdb_fs_backend *backend,
12971300
const char *ref_name,
12981301
const char *prefix)
12991302
{
13001303
git_buf relative_path = GIT_BUF_INIT;
13011304
git_buf base_path = GIT_BUF_INIT;
13021305
size_t commonlen;
1306+
int error;
13031307

1304-
assert(backend && ref_name);
1308+
GIT_ASSERT_ARG(backend);
1309+
GIT_ASSERT_ARG(ref_name);
13051310

1306-
if (git_buf_sets(&relative_path, ref_name) < 0)
1311+
if ((error = git_buf_sets(&relative_path, ref_name)) < 0)
13071312
goto cleanup;
13081313

13091314
git_path_squash_slashes(&relative_path);
@@ -1313,20 +1318,30 @@ static void refdb_fs_backend__prune_refs(
13131318

13141319
git_buf_truncate(&relative_path, commonlen);
13151320

1316-
if (prefix) {
1317-
if (git_buf_join3(&base_path, '/', backend->commonpath, prefix, git_buf_cstr(&relative_path)) < 0)
1318-
goto cleanup;
1319-
} else {
1320-
if (git_buf_joinpath(&base_path, backend->commonpath, git_buf_cstr(&relative_path)) < 0)
1321-
goto cleanup;
1322-
}
1321+
if (prefix)
1322+
error = git_buf_join3(&base_path, '/',
1323+
backend->commonpath, prefix,
1324+
git_buf_cstr(&relative_path));
1325+
else
1326+
error = git_buf_joinpath(&base_path,
1327+
backend->commonpath,
1328+
git_buf_cstr(&relative_path));
1329+
1330+
if (error < 0)
1331+
goto cleanup;
1332+
1333+
error = git_futils_rmdir_r(ref_name + commonlen,
1334+
git_buf_cstr(&base_path),
1335+
GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT);
13231336

1324-
git_futils_rmdir_r(ref_name + commonlen, git_buf_cstr(&base_path), GIT_RMDIR_EMPTY_PARENTS | GIT_RMDIR_SKIP_ROOT);
1337+
if (error == GIT_ENOTFOUND)
1338+
error = 0;
13251339
}
13261340

13271341
cleanup:
13281342
git_buf_dispose(&relative_path);
13291343
git_buf_dispose(&base_path);
1344+
return error;
13301345
}
13311346

13321347
static int refdb_fs_backend__delete(
@@ -1338,7 +1353,8 @@ static int refdb_fs_backend__delete(
13381353
git_filebuf file = GIT_FILEBUF_INIT;
13391354
int error = 0;
13401355

1341-
assert(backend && ref_name);
1356+
GIT_ASSERT_ARG(backend);
1357+
GIT_ASSERT_ARG(ref_name);
13421358

13431359
if ((error = loose_lock(&file, backend, ref_name)) < 0)
13441360
return error;
@@ -1424,7 +1440,7 @@ static int refdb_fs_backend__delete_tail(
14241440
cleanup:
14251441
git_filebuf_cleanup(file);
14261442
if (error == 0)
1427-
refdb_fs_backend__prune_refs(backend, ref_name, "");
1443+
error = refdb_fs_backend__prune_refs(backend, ref_name, "");
14281444
return error;
14291445
}
14301446

@@ -1444,7 +1460,7 @@ static int refdb_fs_backend__rename(
14441460
git_filebuf file = GIT_FILEBUF_INIT;
14451461
int error;
14461462

1447-
assert(backend);
1463+
GIT_ASSERT_ARG(backend);
14481464

14491465
if ((error = reference_path_available(
14501466
backend, new_name, old_name, force)) < 0 ||
@@ -1497,7 +1513,7 @@ static int refdb_fs_backend__compress(git_refdb_backend *_backend)
14971513
int error;
14981514
refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
14991515

1500-
assert(backend);
1516+
GIT_ASSERT_ARG(backend);
15011517

15021518
if ((error = packed_reload(backend)) < 0 || /* load the existing packfile */
15031519
(error = packed_loadloose(backend)) < 0 || /* add all the loose refs */
@@ -1511,7 +1527,8 @@ static void refdb_fs_backend__free(git_refdb_backend *_backend)
15111527
{
15121528
refdb_fs_backend *backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
15131529

1514-
assert(backend);
1530+
if (!backend)
1531+
return;
15151532

15161533
git_sortedcache_free(backend->refcache);
15171534
git__free(backend->gitpath);
@@ -1672,7 +1689,7 @@ static int refdb_reflog_fs__ensure_log(git_refdb_backend *_backend, const char *
16721689
git_buf path = GIT_BUF_INIT;
16731690
int error;
16741691

1675-
assert(_backend && name);
1692+
GIT_ASSERT_ARG(_backend && name);
16761693

16771694
backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
16781695
repo = backend->repo;
@@ -1705,7 +1722,8 @@ static int refdb_reflog_fs__has_log(git_refdb_backend *_backend, const char *nam
17051722
{
17061723
refdb_fs_backend *backend;
17071724

1708-
assert(_backend && name);
1725+
GIT_ASSERT_ARG(_backend);
1726+
GIT_ASSERT_ARG(name);
17091727

17101728
backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
17111729

@@ -1721,7 +1739,9 @@ static int refdb_reflog_fs__read(git_reflog **out, git_refdb_backend *_backend,
17211739
git_repository *repo;
17221740
refdb_fs_backend *backend;
17231741

1724-
assert(out && _backend && name);
1742+
GIT_ASSERT_ARG(out);
1743+
GIT_ASSERT_ARG(_backend);
1744+
GIT_ASSERT_ARG(name);
17251745

17261746
backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
17271747
repo = backend->repo;
@@ -1838,7 +1858,8 @@ static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflo
18381858
git_buf log = GIT_BUF_INIT;
18391859
git_filebuf fbuf = GIT_FILEBUF_INIT;
18401860

1841-
assert(_backend && reflog);
1861+
GIT_ASSERT_ARG(_backend);
1862+
GIT_ASSERT_ARG(reflog);
18421863

18431864
backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
18441865

@@ -1960,7 +1981,9 @@ static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_
19601981
git_repository *repo;
19611982
refdb_fs_backend *backend;
19621983

1963-
assert(_backend && old_name && new_name);
1984+
GIT_ASSERT_ARG(_backend);
1985+
GIT_ASSERT_ARG(old_name);
1986+
GIT_ASSERT_ARG(new_name);
19641987

19651988
backend = GIT_CONTAINER_OF(_backend, refdb_fs_backend, parent);
19661989
repo = backend->repo;
@@ -2037,7 +2060,8 @@ static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name
20372060
git_buf path = GIT_BUF_INIT;
20382061
int error;
20392062

2040-
assert(_backend && name);
2063+
GIT_ASSERT_ARG(_backend);
2064+
GIT_ASSERT_ARG(name);
20412065

20422066
if ((error = retrieve_reflog_path(&path, backend->repo, name)) < 0)
20432067
goto out;
@@ -2048,7 +2072,7 @@ static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name
20482072
if ((error = p_unlink(path.ptr)) < 0)
20492073
goto out;
20502074

2051-
refdb_fs_backend__prune_refs(backend, name, GIT_REFLOG_DIR);
2075+
error = refdb_fs_backend__prune_refs(backend, name, GIT_REFLOG_DIR);
20522076

20532077
out:
20542078
git_buf_dispose(&path);

0 commit comments

Comments
 (0)