git: new class and method for remote repositories
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Thu, 5 Sep 2013 06:53:57 +0000 (09:53 +0300)
committerGuido Günther <agx@sigxcpu.org>
Tue, 10 Sep 2013 07:19:26 +0000 (09:19 +0200)
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>
gbp/git/repository.py
tests/test_GitRepository.py

index fc7857b97226359ee2e340cddeb44c0be2b66d66..17c1aa47dae423b32601c6c864ca51fa38a6b730 100644 (file)
@@ -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
index f48db0a9caa857d14f931bbbd0045903a4764f4c..427370da84d5a4595c1e8d17cff91c2324215439 100644 (file)
@@ -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