Merge pull request #5000 from augfab/branch_lookup_all Have git_branch_lookup accept GIT_BRANCH_ALL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
diff --git a/src/branch.c b/src/branch.c
index 7c6d747..a93db7e 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -22,7 +22,7 @@ static int retrieve_branch_reference(
git_reference **branch_reference_out,
git_repository *repo,
const char *branch_name,
- int is_remote)
+ bool is_remote)
{
git_reference *branch = NULL;
int error = 0;
@@ -334,9 +334,23 @@ int git_branch_lookup(
const char *branch_name,
git_branch_t branch_type)
{
+ int error = -1;
assert(ref_out && repo && branch_name);
- return retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE);
+ switch (branch_type) {
+ case GIT_BRANCH_LOCAL:
+ case GIT_BRANCH_REMOTE:
+ error = retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE);
+ break;
+ case GIT_BRANCH_ALL:
+ error = retrieve_branch_reference(ref_out, repo, branch_name, false);
+ if (error == GIT_ENOTFOUND)
+ error = retrieve_branch_reference(ref_out, repo, branch_name, true);
+ break;
+ default:
+ assert(false);
+ }
+ return error;
}
int git_branch_name(
diff --git a/tests/refs/branches/lookup.c b/tests/refs/branches/lookup.c
index 95d49a4..ef0c1f9 100644
--- a/tests/refs/branches/lookup.c
+++ b/tests/refs/branches/lookup.c
@@ -20,20 +20,41 @@ void test_refs_branches_lookup__cleanup(void)
repo = NULL;
}
-void test_refs_branches_lookup__can_retrieve_a_local_branch(void)
+void test_refs_branches_lookup__can_retrieve_a_local_branch_local(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
}
-void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch(void)
+void test_refs_branches_lookup__can_retrieve_a_local_branch_all(void)
+{
+ cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_ALL));
+}
+
+void test_refs_branches_lookup__trying_to_retrieve_a_local_branch_remote(void)
+{
+ cl_git_fail(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_REMOTE));
+}
+
+void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_remote(void)
{
cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_REMOTE));
}
+void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_all(void)
+{
+ cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_ALL));
+}
+
+void test_refs_branches_lookup__trying_to_retrieve_a_remote_tracking_branch_local(void)
+{
+ cl_git_fail(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_LOCAL));
+}
+
void test_refs_branches_lookup__trying_to_retrieve_an_unknown_branch_returns_ENOTFOUND(void)
{
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "where/are/you", GIT_BRANCH_LOCAL));
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "over/here", GIT_BRANCH_REMOTE));
+ cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "maybe/here", GIT_BRANCH_ALL));
}
void test_refs_branches_lookup__trying_to_retrieve_a_branch_with_an_invalid_name_returns_EINVALIDSPEC(void)
@@ -42,4 +63,6 @@ void test_refs_branches_lookup__trying_to_retrieve_a_branch_with_an_invalid_name
git_branch_lookup(&branch, repo, "are/you/inv@{id", GIT_BRANCH_LOCAL));
cl_assert_equal_i(GIT_EINVALIDSPEC,
git_branch_lookup(&branch, repo, "yes/i am", GIT_BRANCH_REMOTE));
+ cl_assert_equal_i(GIT_EINVALIDSPEC,
+ git_branch_lookup(&branch, repo, "inv al/id", GIT_BRANCH_ALL));
}