summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-04-08 12:42:18 -0700
committerVicent Marti <tanoku@gmail.com>2011-04-08 12:42:18 -0700
commit41233c40c0e9cb8556a8ba131aa97a0ffbe46cb1 (patch)
tree4cc53045010f488f675b9a2bb99b6f55f7893231
parentcef75d743006401121cfe9a9da63933cfc7ecec1 (diff)
downloadlibgit2-41233c40c0e9cb8556a8ba131aa97a0ffbe46cb1.tar.gz
Add new method `git_repository_is_empty`
-rw-r--r--include/git2/repository.h12
-rw-r--r--src/repository.c15
-rw-r--r--tests/t12-repo.c14
3 files changed, 41 insertions, 0 deletions
diff --git a/include/git2/repository.h b/include/git2/repository.h
index 00c1f20d0..200f350b7 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -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
diff --git a/src/repository.c b/src/repository.c
index abbbd126a..471c02444 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -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;
+}
+
diff --git a/tests/t12-repo.c b/tests/t12-repo.c
index adf20cfd7..876f0c5ea 100644
--- a/tests/t12-repo.c
+++ b/tests/t12-repo.c
@@ -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