summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-11-17 07:19:14 -0800
committernulltoken <emeric.fermas@gmail.com>2012-11-17 07:20:08 -0800
commit6091457e76638503fec46de269bc158f63e4721a (patch)
tree2b21848ef5a83207ad5137f84b004d957b788d66 /src
parentf5a0e734bc440ca6c89d15a894fea0ea3bf38f36 (diff)
downloadlibgit2-6091457e76638503fec46de269bc158f63e4721a.tar.gz
repo: ensure is_empty() checks there are no refs
Diffstat (limited to 'src')
-rw-r--r--src/repository.c51
1 files changed, 31 insertions, 20 deletions
diff --git a/src/repository.c b/src/repository.c
index 101497c4d..2d6ce4dbb 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1241,36 +1241,47 @@ int git_repository_head_orphan(git_repository *repo)
return 0;
}
-int git_repository_is_empty(git_repository *repo)
+int at_least_one_cb(const char *refname, void *payload)
{
- git_reference *head = NULL, *branch = NULL;
- int error;
+ GIT_UNUSED(refname);
+ GIT_UNUSED(payload);
- if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
- return -1;
+ return GIT_EUSER;
+}
- if (git_reference_type(head) != GIT_REF_SYMBOLIC) {
- git_reference_free(head);
- return 0;
- }
+static int repo_contains_no_reference(git_repository *repo)
+{
+ int error;
+
+ error = git_reference_foreach(repo, GIT_REF_LISTALL, at_least_one_cb, NULL);
- if (strcmp(git_reference_target(head), GIT_REFS_HEADS_DIR "master") != 0) {
- git_reference_free(head);
+ if (error == GIT_EUSER)
return 0;
- }
- error = git_reference_resolve(&branch, head);
-
- git_reference_free(head);
- git_reference_free(branch);
+ return error == 0 ? 1 : error;
+}
- if (error == GIT_ENOTFOUND)
- return 1;
+int git_repository_is_empty(git_repository *repo)
+{
+ git_reference *head = NULL;
+ int error, ref_count = 0;
- if (error < 0)
+ if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
return -1;
- return 0;
+ if (!(error = git_reference_type(head) == GIT_REF_SYMBOLIC))
+ goto cleanup;
+
+ if (!(error = strcmp(
+ git_reference_target(head),
+ GIT_REFS_HEADS_DIR "master") == 0))
+ goto cleanup;
+
+ error = repo_contains_no_reference(repo);
+
+cleanup:
+ git_reference_free(head);
+ return error < 0 ? -1 : error;
}
const char *git_repository_path(git_repository *repo)