From 4ab6a759f63caa75ea716154887fc0a94de01b6e Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Wed, 28 Aug 2013 22:51:44 -0700 Subject: [PATCH 1/3] Fix incorrect precedence within git_repository_is_empty() Reverts part of 9146f1e57ec4f2b6fa293c78d54f1383464ff5be. --- src/repository.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/repository.c b/src/repository.c index 99ac56ef9..e5f23e4e4 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1500,7 +1500,7 @@ int git_repository_is_empty(git_repository *repo) if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) return -1; - if (!((error = git_reference_type(head)) == GIT_REF_SYMBOLIC)) + if (!(error = (git_reference_type(head) == GIT_REF_SYMBOLIC))) goto cleanup; if (!(error = (strcmp( From 8b2f230cd519010d202c57185dc4dc39ca3d53f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicent=20Mart=C3=AD?= Date: Thu, 29 Aug 2013 13:27:37 +0200 Subject: [PATCH 2/3] repository: Make the is_empty check more explicit --- src/repository.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/repository.c b/src/repository.c index e5f23e4e4..80904d5fa 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1500,8 +1500,10 @@ int git_repository_is_empty(git_repository *repo) if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) return -1; - if (!(error = (git_reference_type(head) == GIT_REF_SYMBOLIC))) + if (git_reference_type(head) != GIT_REF_SYMBOLIC) { + error = -1; goto cleanup; + } if (!(error = (strcmp( git_reference_symbolic_target(head), From 4218183631faa48f97e76a23e928d1a98983be46 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Thu, 29 Aug 2013 10:27:01 -0700 Subject: [PATCH 3/3] Treat detached HEAD as non-empty repo This simplifies the git_repository_is_empty a bit so that a detached HEAD is just taken to mean the repo is not empty, since a newly initialized repo will not have a detached HEAD. --- src/repository.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/repository.c b/src/repository.c index 80904d5fa..eae22ce51 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1495,26 +1495,20 @@ static int repo_contains_no_reference(git_repository *repo) int git_repository_is_empty(git_repository *repo) { git_reference *head = NULL; - int error; + int is_empty = 0; if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0) return -1; - if (git_reference_type(head) != GIT_REF_SYMBOLIC) { - error = -1; - goto cleanup; - } + if (git_reference_type(head) == GIT_REF_SYMBOLIC) + is_empty = + (strcmp(git_reference_symbolic_target(head), + GIT_REFS_HEADS_DIR "master") == 0) && + repo_contains_no_reference(repo); - if (!(error = (strcmp( - git_reference_symbolic_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; + + return is_empty; } const char *git_repository_path(git_repository *repo)