From 3eb401db658a5143e06f2d29a292e9be5b8a623a Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Thu, 5 Sep 2013 09:53:57 +0300 Subject: 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 --- gbp/git/repository.py | 61 +++++++++++++++++++++++++++++++++++++++++++++ tests/test_GitRepository.py | 22 ++++++++++++++++ 2 files changed, 83 insertions(+) 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 -- cgit v1.2.3