diff options
author | Edward Thomson <ethomson@github.com> | 2016-06-03 14:18:08 -0500 |
---|---|---|
committer | Edward Thomson <ethomson@github.com> | 2016-06-03 14:18:08 -0500 |
commit | 5afe1873488b43a0658bf3816565a19d075e0182 (patch) | |
tree | 2ea69b0b95c2fe4f642788e30045bb8aa98d3740 | |
parent | 0aaba445933bed0ab046009fe6c4aca1aaf7c6b9 (diff) | |
download | libgit2-ethomson/unborn_head.tar.gz |
repository_head: return HEAD when on unborn branchethomson/unborn_head
When we're on an unborn branch, correctly set the out param so that
callers can determine what the unborn branch is.
-rw-r--r-- | src/repository.c | 12 | ||||
-rw-r--r-- | tests/repo/head.c | 5 |
2 files changed, 14 insertions, 3 deletions
diff --git a/src/repository.c b/src/repository.c index d39a9015d..8723e2fd0 100644 --- a/src/repository.c +++ b/src/repository.c @@ -1747,10 +1747,16 @@ int git_repository_head(git_reference **head_out, git_repository *repo) return 0; } - error = git_reference_lookup_resolved(head_out, repo, git_reference_symbolic_target(head), -1); - git_reference_free(head); + error = git_reference_lookup_resolved(head_out, + repo, git_reference_symbolic_target(head), -1); - return error == GIT_ENOTFOUND ? GIT_EUNBORNBRANCH : error; + if (error == GIT_ENOTFOUND) { + *head_out = head; + return GIT_EUNBORNBRANCH; + } + + git_reference_free(head); + return error; } int git_repository_head_unborn(git_repository *repo) diff --git a/tests/repo/head.c b/tests/repo/head.c index 31c228777..7acdeff70 100644 --- a/tests/repo/head.c +++ b/tests/repo/head.c @@ -79,6 +79,11 @@ void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_ cl_assert_equal_i(false, git_repository_head_detached(repo)); cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head(&head, repo)); + + cl_assert(head); + cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head)); + cl_assert_equal_s("HEAD", git_reference_name(head)); + cl_assert_equal_s("refs/heads/doesnt/exist/yet", git_reference_symbolic_target(head)); } void test_repo_head__set_head_Returns_ENOTFOUND_when_the_reference_doesnt_exist(void) |