Commit 41233c40 by Vicent Marti

Add new method `git_repository_is_empty`

parent cef75d74
......@@ -182,6 +182,18 @@ GIT_EXTERN(void) git_repository_free(git_repository *repo);
*/
GIT_EXTERN(int) git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare);
/**
* Check if a repository is empty
*
* An empty repository has just been initialized and contains
* no commits.
*
* @param repo Repo to test
* @return 1 if the repository is empty, 0 if it isn't, error code
* if the repository is corrupted
*/
GIT_EXTERN(int) git_repository_is_empty(git_repository *repo);
/** @} */
GIT_END_DECL
#endif
......@@ -473,3 +473,18 @@ cleanup:
return error;
}
int git_repository_is_empty(git_repository *repo)
{
git_reference *head, *branch;
int error;
error = git_reference_lookup(&head, repo, "HEAD");
if (error < GIT_SUCCESS)
return error;
if (git_reference_type(head) != GIT_REF_SYMBOLIC)
return GIT_EOBJCORRUPTED;
return git_reference_resolve(&branch, head) == GIT_SUCCESS ? 0 : 1;
}
......@@ -244,6 +244,19 @@ BEGIN_TEST(open2, "Open a bare repository with a relative path escaping out of t
rmdir_recurs(TEMP_REPO_FOLDER);
END_TEST
BEGIN_TEST(empty0, "test if a repository is empty or not")
git_repository *repo_empty, *repo_normal;
must_pass(git_repository_open(&repo_normal, REPOSITORY_FOLDER));
must_be_true(git_repository_is_empty(repo_normal) == 0);
git_repository_free(repo_normal);
must_pass(git_repository_open(&repo_empty, EMPTY_BARE_REPOSITORY_FOLDER));
must_be_true(git_repository_is_empty(repo_empty) == 1);
git_repository_free(repo_empty);
END_TEST
BEGIN_SUITE(repository)
ADD_TEST(odb0);
ADD_TEST(odb1);
......@@ -253,5 +266,6 @@ BEGIN_SUITE(repository)
ADD_TEST(open0);
ADD_TEST(open1);
ADD_TEST(open2);
ADD_TEST(empty0);
END_SUITE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment