Skip to content

Commit 7415ca0

Browse files
committed
Remove remote for re-creating it if url has changed
We're adding the possibility to remove the remote. This, with the addition of names_url_dict, makes the posibility of checking the remote url, and if it's updated, then remove it for re-creating.
1 parent f3c9fc3 commit 7415ca0

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

git_wrapper/remote.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ def names(self):
2424
"""
2525
return [x.name for x in self.git_repo.repo.remotes]
2626

27+
def names_url_dict(self):
28+
"""Returns a dict of remotes for a given repo with its url
29+
30+
:return dict: A dict of utf-8 encoded remote names with
31+
url as its value
32+
"""
33+
return {x.name: x.url for x in self.git_repo.repo.remotes}
34+
2735
def add(self, name, url):
2836
"""Adds a remote to the given repo
2937
@@ -111,3 +119,28 @@ def fetch_all(self, prune=False, prune_tags=False):
111119
if errors:
112120
msg = f"Error fetching these remotes: {', '.join(errors)}"
113121
raise exceptions.RemoteException(msg)
122+
123+
def remove(self, name):
124+
"""Remove the specified remote to the given repo
125+
126+
:param str name: Remote name to remove
127+
:return bool: True if the remote was removed, False otherwise
128+
"""
129+
working_dir = self.git_repo.repo.working_dir
130+
self.logger.debug(f"Removing remote {name} to repo {working_dir}")
131+
ret_status = False
132+
133+
try:
134+
remote = self.git_repo.repo.remote(name)
135+
except ValueError:
136+
repo = self.git_repo.repo.working_dir
137+
msg = f"Remote {name} does not exist on repo {repo}"
138+
raise exceptions.ReferenceNotFoundException(msg)
139+
140+
try:
141+
self.git_repo.repo.delete_remote(remote)
142+
ret_status = True
143+
except git.CommandError:
144+
return ret_status
145+
146+
return ret_status

tests/test_remote.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ def remote_generator(names):
2121
return ret_data
2222

2323

24+
def remote_generator_url(remotes):
25+
"""Generates objects to be used with git.Repo.remotes call"""
26+
ret_data = []
27+
for name, url in remotes.items():
28+
obj = type('', (), {})()
29+
obj.name = name
30+
obj.url = url
31+
ret_data.append(obj)
32+
return ret_data
33+
34+
2435
def test_get_remotes_returns_list(mock_repo):
2536
"""
2637
GIVEN GitRepo is initialized with a path and repo
@@ -36,6 +47,21 @@ def test_get_remotes_returns_list(mock_repo):
3647
assert expected == git_util.remote.names()
3748

3849

50+
def test_get_remotes_returns_dict(mock_repo):
51+
"""
52+
GIVEN GitRepo is initialized with a path and repo
53+
WHEN remote.names_url_dict is called
54+
THEN a dict of remote names with its url is returned
55+
"""
56+
expected = {'a': 1, 'b': 2, 'c': 3}
57+
attrs = {'remotes': remote_generator_url(expected)}
58+
mock_repo.configure_mock(**attrs)
59+
60+
git_util = GitRepo('./', mock_repo)
61+
62+
assert expected == git_util.remote.names_url_dict()
63+
64+
3965
def test_add_remote_adds(mock_repo):
4066
"""
4167
GIVEN GitRepo initialized with a path and repo
@@ -89,6 +115,47 @@ def test_add_remote_update_fails(mock_repo):
89115
delete_mock.assert_called_once_with(remote_mock)
90116

91117

118+
def test_remove_remote_removes(mock_repo):
119+
"""
120+
GIVEN GitRepo initialized with a path and repo
121+
WHEN remote.add is called with a name and url
122+
THEN a TRUE status is returned
123+
WITH update called
124+
"""
125+
git_util = GitRepo('./', mock_repo)
126+
127+
assert git_util.remote.remove('origin') is True
128+
129+
130+
def test_remove_remote_remote_fails(mock_repo):
131+
"""
132+
GIVEN GitRepo initialized with a path and repo
133+
WHEN remote.add is called with a name and url
134+
AND the remote create fails with an exception
135+
THEN a False status is returned
136+
"""
137+
mock_repo.remote.side_effect = ValueError
138+
139+
repo = GitRepo(repo=mock_repo)
140+
with pytest.raises(exceptions.ReferenceNotFoundException):
141+
repo.remote.remove("doesntExist")
142+
143+
mock_repo.remote.assert_called_with("doesntExist")
144+
145+
146+
def test_remove_remote_remove_fails(mock_repo):
147+
"""
148+
GIVEN GitRepo initialized with a path and repo
149+
WHEN remote.add is called with a name and url
150+
AND the remote create fails with an exception
151+
THEN a False status is returned
152+
"""
153+
mock_repo.delete_remote.side_effect = git.CommandError('remove')
154+
git_util = GitRepo('./', mock_repo)
155+
156+
assert git_util.remote.remove('rdo') is False
157+
158+
92159
def test_fetch(mock_repo):
93160
"""
94161
GIVEN GitRepo is initialized with a path and repo

0 commit comments

Comments
 (0)