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.
This commit is contained in:
Vladyslav Yeremeichuk
2024-10-21 22:42:56 +03:00
parent 229181bf9b
commit d1be60bbe8
3 changed files with 30 additions and 4 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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));