summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>2013-09-05 09:53:57 +0300
committerGuido Günther <agx@sigxcpu.org>2013-09-10 09:19:26 +0200
commit3eb401db658a5143e06f2d29a292e9be5b8a623a (patch)
tree7af6db9130c9099809af29c518af82ba26b636a6
parentdb79c5db34498bc353f7d1e10bb1f4b7140d876b (diff)
git: new class and method for remote repositories
Add a new GitRemote class for representing git remote repositories. The initial, very limited, version only contains information about the fetch and push URLs of the remote repository. Also, add a new GitRepository.get_remotes() method for getting remote repositories as instances of the new GitRemote class. Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
-rw-r--r--gbp/git/repository.py61
-rw-r--r--tests/test_GitRepository.py22
2 files changed, 83 insertions, 0 deletions
diff --git a/gbp/git/repository.py b/gbp/git/repository.py
index fc7857b..17c1aa4 100644
--- a/gbp/git/repository.py
+++ b/gbp/git/repository.py
@@ -34,6 +34,35 @@ class GitRepositoryError(GitError):
pass
+class GitRemote(object):
+ """Class representing a remote repository"""
+ def __init__(self, name, fetch_url, push_urls):
+ self._name = name
+ self._fetch_url = fetch_url
+ if isinstance(push_urls, basestring):
+ self._push_urls = [push_urls]
+ else:
+ self._push_urls = [url for url in push_urls]
+
+ def __str__(self):
+ return self.name
+
+ @property
+ def name(self):
+ """Name of the remote"""
+ return self._name
+
+ @property
+ def fetch_url(self):
+ """Fetch URL"""
+ return self._fetch_url
+
+ @property
+ def push_urls(self):
+ """List of push URLs"""
+ return self._push_urls
+
+
class GitRepository(object):
"""
Represents a git repository at I{path}. It's currently assumed that the git
@@ -989,6 +1018,38 @@ class GitRepository(object):
#{ Remote Repositories
+ def get_remotes(self):
+ """
+ Get a list of remote repositories
+
+ @return: remote repositories
+ @rtype: C{dict} of C{GitRemote}
+ """
+ out, err, ret = self._git_inout('remote', [], capture_stderr=True)
+ if ret:
+ raise GitRepositoryError('Failed to get list of remotes: %s' % err)
+
+ # Get information about all remotes
+ remotes = {}
+ for remote in out.splitlines():
+ out, err, _ret = self._git_inout('remote', ['show', '-n', remote],
+ capture_stderr=True)
+ if ret:
+ raise GitRepositoryError('Failed to get information for remote '
+ '%s: %s' % (remote, err))
+ fetch_url = None
+ push_urls = []
+ for line in out.splitlines():
+ match = re.match('\s*Fetch\s+URL:\s*(\S.*)', line)
+ if match:
+ fetch_url = match.group(1)
+ match = re.match('\s*Push\s+URL:\s*(\S.*)', line)
+ if match:
+ push_urls.append(match.group(1))
+ remotes[remote] = GitRemote(remote, fetch_url, push_urls)
+
+ return remotes
+
def get_remote_repos(self):
"""
Get all remote repositories
diff --git a/tests/test_GitRepository.py b/tests/test_GitRepository.py
index f48db0a..427370d 100644
--- a/tests/test_GitRepository.py
+++ b/tests/test_GitRepository.py
@@ -568,6 +568,28 @@ def test_clone():
False
"""
+def test_get_remotes():
+ """
+ Merge a branch
+
+ Methods tested:
+ - L{gbp.git.GitRepository.get_remotes}
+
+ >>> import os
+ >>> import gbp.git.repository
+ >>> repo = gbp.git.repository.GitRepository(os.path.join(clone_dir, 'repo'))
+ >>> remotes = repo.get_remotes()
+ >>> len(remotes)
+ 1
+ >>> origin = remotes['origin']
+ >>> origin.name
+ 'origin'
+ >>> origin.fetch_url == repo_dir
+ True
+ >>> origin.push_urls == [repo_dir]
+ True
+ """
+
def test_merge():
"""
Merge a branch