Skip to content

Commit 6872928

Browse files
authored
Merge pull request libgit2#5000 from augfab/branch_lookup_all
Have git_branch_lookup accept GIT_BRANCH_ALL
2 parents 5a6a3c0 + c5d8e30 commit 6872928

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/branch.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static int retrieve_branch_reference(
2222
git_reference **branch_reference_out,
2323
git_repository *repo,
2424
const char *branch_name,
25-
int is_remote)
25+
bool is_remote)
2626
{
2727
git_reference *branch = NULL;
2828
int error = 0;
@@ -334,9 +334,23 @@ int git_branch_lookup(
334334
const char *branch_name,
335335
git_branch_t branch_type)
336336
{
337+
int error = -1;
337338
assert(ref_out && repo && branch_name);
338339

339-
return retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE);
340+
switch (branch_type) {
341+
case GIT_BRANCH_LOCAL:
342+
case GIT_BRANCH_REMOTE:
343+
error = retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE);
344+
break;
345+
case GIT_BRANCH_ALL:
346+
error = retrieve_branch_reference(ref_out, repo, branch_name, false);
347+
if (error == GIT_ENOTFOUND)
348+
error = retrieve_branch_reference(ref_out, repo, branch_name, true);
349+
break;
350+
default:
351+
assert(false);
352+
}
353+
return error;
340354
}
341355

342356
int git_branch_name(

tests/refs/branches/lookup.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,41 @@ void test_refs_branches_lookup__cleanup(void)
2020
repo = NULL;
2121
}
2222

23-
void test_refs_branches_lookup__can_retrieve_a_local_branch(void)
23+
void test_refs_branches_lookup__can_retrieve_a_local_branch_local(void)
2424
{
2525
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
2626
}
2727

28-
void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch(void)
28+
void test_refs_branches_lookup__can_retrieve_a_local_branch_all(void)
29+
{
30+
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_ALL));
31+
}
32+
33+
void test_refs_branches_lookup__trying_to_retrieve_a_local_branch_remote(void)
34+
{
35+
cl_git_fail(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_REMOTE));
36+
}
37+
38+
void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_remote(void)
2939
{
3040
cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_REMOTE));
3141
}
3242

43+
void test_refs_branches_lookup__can_retrieve_a_remote_tracking_branch_all(void)
44+
{
45+
cl_git_pass(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_ALL));
46+
}
47+
48+
void test_refs_branches_lookup__trying_to_retrieve_a_remote_tracking_branch_local(void)
49+
{
50+
cl_git_fail(git_branch_lookup(&branch, repo, "test/master", GIT_BRANCH_LOCAL));
51+
}
52+
3353
void test_refs_branches_lookup__trying_to_retrieve_an_unknown_branch_returns_ENOTFOUND(void)
3454
{
3555
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "where/are/you", GIT_BRANCH_LOCAL));
3656
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "over/here", GIT_BRANCH_REMOTE));
57+
cl_assert_equal_i(GIT_ENOTFOUND, git_branch_lookup(&branch, repo, "maybe/here", GIT_BRANCH_ALL));
3758
}
3859

3960
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
4263
git_branch_lookup(&branch, repo, "are/you/inv@{id", GIT_BRANCH_LOCAL));
4364
cl_assert_equal_i(GIT_EINVALIDSPEC,
4465
git_branch_lookup(&branch, repo, "yes/i am", GIT_BRANCH_REMOTE));
66+
cl_assert_equal_i(GIT_EINVALIDSPEC,
67+
git_branch_lookup(&branch, repo, "inv al/id", GIT_BRANCH_ALL));
4568
}

0 commit comments

Comments
 (0)