diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2015-01-08 09:45:50 +0100 |
---|---|---|
committer | Sebastian Thiel <byronimo@gmail.com> | 2015-01-08 09:45:50 +0100 |
commit | f7b7eb6245e7d7c4535975268a9be936e2c59dc8 (patch) | |
tree | bdc6c5aa710632cc60e2c31ab08882c1e8b0d958 | |
parent | c7887c66483ffa9a839ecf1a53c5ef718dcd1d2d (diff) | |
download | gitpython-f7b7eb6245e7d7c4535975268a9be936e2c59dc8.tar.gz |
Added Remote.exists() method, and test. Fixes #229
-rw-r--r-- | git/remote.py | 13 | ||||
-rw-r--r-- | git/repo/base.py | 5 | ||||
-rw-r--r-- | git/test/test_repo.py | 7 |
3 files changed, 23 insertions, 2 deletions
diff --git a/git/remote.py b/git/remote.py index 4d249004..deddd1f4 100644 --- a/git/remote.py +++ b/git/remote.py @@ -397,6 +397,19 @@ class Remote(LazyMixin, Iterable): def __hash__(self): return hash(self.name) + def exists(self): + """:return: True if this is a valid, existing remote. + Valid remotes have an entry in the repository's configuration""" + try: + self.config_reader.get('url') + return True + except cp.NoOptionError: + # we have the section at least ... + return True + except cp.NoSectionError: + return False + # end + @classmethod def iter_items(cls, repo): """:return: Iterator yielding Remote objects of the given repository""" diff --git a/git/repo/base.py b/git/repo/base.py index 34fe4046..7bd2be48 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -250,7 +250,10 @@ class Repo(object): def remote(self, name='origin'): """:return: Remote with the specified name :raise ValueError: if no remote with such a name exists""" - return Remote(self, name) + r = Remote(self, name) + if not r.exists(): + raise ValueError("Remote named '%s' didn't exist" % name) + return r #{ Submodules diff --git a/git/test/test_repo.py b/git/test/test_repo.py index 260f7d68..e0be2074 100644 --- a/git/test/test_repo.py +++ b/git/test/test_repo.py @@ -25,7 +25,8 @@ from git import ( Reference, GitDB, Submodule, - GitCmdObjectDB + GitCmdObjectDB, + Remote ) from git.util import join_path_native from git.exc import BadObject @@ -673,3 +674,7 @@ class TestRepo(TestBase): last_commit(repo, 'master', 'git/test/test_base.py') # end for each repository type # end for each iteration + + def test_remote_method(self): + self.failUnlessRaises(ValueError, self.rorepo.remote, 'foo-blue') + assert isinstance(self.rorepo.remote(name='origin'), Remote) |