Skip to content

Commit a1ee8f9

Browse files
jpichonmergify[bot]
authored andcommitted
Add convenience method for checking if a branch exists
1 parent 0c7c78c commit a1ee8f9

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

git_wrapper/branch.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ def _run_cherry(self, upstream, head, regex):
4848
ret_data[match.group(1)] = match.group(2)
4949
return ret_data
5050

51+
def exists(self, name, remote=None):
52+
"""Checks if a branch exists locally or on the specified remote.
53+
54+
:param str name: Name of the branch to find
55+
:param str remote: Remote name to check for the branch, or None
56+
if local
57+
"""
58+
if not remote:
59+
if name in self.git_repo.repo.branches:
60+
return True
61+
else:
62+
return False
63+
else:
64+
if remote not in self.git_repo.remote.names():
65+
raise exceptions.RemoteException(
66+
"Remote {0} does not exist.".format(remote)
67+
)
68+
if name in self.git_repo.repo.remotes[remote].refs:
69+
return True
70+
else:
71+
return False
72+
5173
def cherry_on_head_only(self, upstream, head):
5274
"""Get new patches between upstream and head.
5375

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from mock import Mock
77

88
import git
9+
from git.util import IterableList
910
import pytest
1011

1112

@@ -18,7 +19,9 @@ def mock_repo():
1819

1920
remote = Mock(spec=git.Remote)
2021
remote.configure_mock(name="origin", url="http://example.com")
21-
repo_mock.remotes = [remote]
22+
remote_list = IterableList("name")
23+
remote_list.extend([remote])
24+
repo_mock.remotes = remote_list
2225

2326
return repo_mock
2427

tests/test_branch.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,69 @@ def test_reset_reset_failure(mock_repo):
677677
mock_repo.head.reset.side_effect = git.GitCommandError('reset', '')
678678
with pytest.raises(exceptions.ResetException):
679679
repo.branch.hard_reset(refresh=False)
680+
681+
682+
def test_local_branch_exists(mock_repo):
683+
"""
684+
GIVEN GitRepo is initialized with a path and repo
685+
WHEN branch.exists is called with a valid branch and None remote
686+
THEN True is returned
687+
"""
688+
repo = GitRepo(repo=mock_repo)
689+
mock_repo.branches = ["master", "test"]
690+
691+
assert repo.branch.exists("test") is True
692+
693+
694+
def test_local_branch_doesnt_exist(mock_repo):
695+
"""
696+
GIVEN GitRepo is initialized with a path and repo
697+
WHEN branch.exists is called with an invalid branch and None remote
698+
THEN False is returned
699+
"""
700+
repo = GitRepo(repo=mock_repo)
701+
mock_repo.branches = ["master", "test"]
702+
703+
assert repo.branch.exists("another-test") is False
704+
705+
706+
def test_branch_exists_with_invalid_remote(mock_repo):
707+
"""
708+
GIVEN GitRepo is initialized with a path and repo
709+
WHEN branch.exists is called with a valid branch and invalid remote
710+
THEN a RemoteException is raised
711+
"""
712+
repo = GitRepo(repo=mock_repo)
713+
714+
with pytest.raises(exceptions.RemoteException):
715+
assert repo.branch.exists("another", "doesntexist")
716+
717+
718+
def test_remote_branch_exists(mock_repo):
719+
"""
720+
GIVEN GitRepo is initialized with a path and repo
721+
WHEN branch.exists is called with a valid branch and valid remote
722+
THEN True is returned
723+
"""
724+
repo = GitRepo(repo=mock_repo)
725+
726+
remote = Mock(spec=git.Remote)
727+
remote.configure_mock(name="testremote", refs=["testbranch"])
728+
mock_repo.remotes.extend([remote])
729+
730+
assert repo.branch.exists("testbranch", "testremote") is True
731+
732+
733+
def test_remote_branch_doesnt_exists(mock_repo):
734+
"""
735+
GIVEN GitRepo is initialized with a path and repo
736+
WHEN branch.exists is called with an invalid branch and valid remote
737+
THEN True is returned
738+
"""
739+
repo = GitRepo(repo=mock_repo)
740+
741+
remote = Mock(spec=git.Remote)
742+
remote.configure_mock(name="testremote", refs=[])
743+
mock_repo.remotes.extend([remote])
744+
745+
assert repo.branch.exists("testbranch", "testremote") is False

0 commit comments

Comments
 (0)