Skip to content

Commit f7b7eb6

Browse files
committed
Added Remote.exists() method, and test. Fixes #229
1 parent c7887c6 commit f7b7eb6

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

git/remote.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ def __ne__(self, other):
397397
def __hash__(self):
398398
return hash(self.name)
399399

400+
def exists(self):
401+
""":return: True if this is a valid, existing remote.
402+
Valid remotes have an entry in the repository's configuration"""
403+
try:
404+
self.config_reader.get('url')
405+
return True
406+
except cp.NoOptionError:
407+
# we have the section at least ...
408+
return True
409+
except cp.NoSectionError:
410+
return False
411+
# end
412+
400413
@classmethod
401414
def iter_items(cls, repo):
402415
""":return: Iterator yielding Remote objects of the given repository"""

git/repo/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,10 @@ def remotes(self):
250250
def remote(self, name='origin'):
251251
""":return: Remote with the specified name
252252
:raise ValueError: if no remote with such a name exists"""
253-
return Remote(self, name)
253+
r = Remote(self, name)
254+
if not r.exists():
255+
raise ValueError("Remote named '%s' didn't exist" % name)
256+
return r
254257

255258
#{ Submodules
256259

git/test/test_repo.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
Reference,
2626
GitDB,
2727
Submodule,
28-
GitCmdObjectDB
28+
GitCmdObjectDB,
29+
Remote
2930
)
3031
from git.util import join_path_native
3132
from git.exc import BadObject
@@ -673,3 +674,7 @@ def last_commit(repo, rev, path):
673674
last_commit(repo, 'master', 'git/test/test_base.py')
674675
# end for each repository type
675676
# end for each iteration
677+
678+
def test_remote_method(self):
679+
self.failUnlessRaises(ValueError, self.rorepo.remote, 'foo-blue')
680+
assert isinstance(self.rorepo.remote(name='origin'), Remote)

0 commit comments

Comments
 (0)