From d1be60bbe8e23aa8a00c67bd73d2209421d65c13 Mon Sep 17 00:00:00 2001 From: Vladyslav Yeremeichuk Date: Mon, 21 Oct 2024 22:42:56 +0300 Subject: [PATCH] Add the ability to check if a mempack is empty Implement git_mempack_empty, which returns 1 if the mempack is empty and 0 otherwise. --- include/git2/sys/mempack.h | 8 ++++++++ src/libgit2/odb_mempack.c | 11 +++++++++++ tests/libgit2/odb/backend/mempack.c | 15 +++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/include/git2/sys/mempack.h b/include/git2/sys/mempack.h index 96bd8a7e8..f8dedee82 100644 --- a/include/git2/sys/mempack.h +++ b/include/git2/sys/mempack.h @@ -101,6 +101,14 @@ GIT_EXTERN(int) git_mempack_dump(git_buf *pack, git_repository *repo, git_odb_ba */ GIT_EXTERN(int) git_mempack_reset(git_odb_backend *backend); +/** + * Checks if mempack is empty + * + * @param backend The mempack backend + * @return 1 if the repository is empty, 0 if it isn't + */ +GIT_EXTERN(int) git_mempack_empty(git_odb_backend *backend); + GIT_END_DECL #endif diff --git a/src/libgit2/odb_mempack.c b/src/libgit2/odb_mempack.c index c6210cec6..732a30573 100644 --- a/src/libgit2/odb_mempack.c +++ b/src/libgit2/odb_mempack.c @@ -209,3 +209,14 @@ int git_mempack_new(git_odb_backend **out) *out = (git_odb_backend *)db; return 0; } + +int git_mempack_empty(git_odb_backend *_backend) +{ + struct memory_packer_db *db = (struct memory_packer_db *)_backend; + GIT_ASSERT_ARG(_backend); + + if (git_odb_mempack_oidmap_size(&db->objects) || db->commits.size) + return 0; + + return 1; +} diff --git a/tests/libgit2/odb/backend/mempack.c b/tests/libgit2/odb/backend/mempack.c index c8a86a2ae..66450a6c5 100644 --- a/tests/libgit2/odb/backend/mempack.c +++ b/tests/libgit2/odb/backend/mempack.c @@ -8,14 +8,13 @@ static git_odb *_odb; static git_oid _oid; static git_odb_object *_obj; static git_repository *_repo; +static git_odb_backend *_backend; void test_odb_backend_mempack__initialize(void) { - git_odb_backend *backend; - - cl_git_pass(git_mempack_new(&backend)); + cl_git_pass(git_mempack_new(&_backend)); cl_git_pass(git_odb__new(&_odb, NULL)); - cl_git_pass(git_odb_add_backend(_odb, backend, 10)); + cl_git_pass(git_odb_add_backend(_odb, _backend, 10)); cl_git_pass(git_repository__wrap_odb(&_repo, _odb, GIT_OID_SHA1)); } @@ -33,6 +32,14 @@ void test_odb_backend_mempack__write_succeeds(void) cl_git_pass(git_odb_read(&_obj, _odb, &_oid)); } +void test_odb_backend_mempack_empty(void) +{ + const char *data = "data"; + cl_assert_equal_sz(1, git_mempack_empty(_backend)); + cl_git_pass(git_odb_write(&_oid, _odb, data, strlen(data) + 1, GIT_OBJECT_BLOB)); + cl_assert_equal_sz(0, git_mempack_empty(_backend)); +} + void test_odb_backend_mempack__read_of_missing_object_fails(void) { cl_git_pass(git_oid__fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633", GIT_OID_SHA1));