diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-11-12 16:50:27 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-11-22 13:49:26 +0000 |
commit | 0f757009e17ed38f6aa8012691747f465872a994 (patch) | |
tree | bc736492a351ab706336b431490d7d66b37531f2 /morphlib/gitdir.py | |
parent | 7f4662c38ac529faaed94734c3e7fab25d9bcc8b (diff) | |
download | morph-0f757009e17ed38f6aa8012691747f465872a994.tar.gz |
GitDir: Split out Remote object
Operations on remotes are now accessed through this proxy object.
Diffstat (limited to 'morphlib/gitdir.py')
-rw-r--r-- | morphlib/gitdir.py | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/morphlib/gitdir.py b/morphlib/gitdir.py index df2fde61..2b7bba2a 100644 --- a/morphlib/gitdir.py +++ b/morphlib/gitdir.py @@ -90,6 +90,39 @@ class RefDeleteError(RefChangeError): 'located at %(dirname)s: %(original_exception)r' % locals()) +class Remote(object): + '''Represent a remote git repository. + + This can either be nascent or concrete, depending on whether the + name is given. + + Changes to a concrete remote's config are written-through to git's + config files, while a nascent remote keeps changes in-memory. + + ''' + + def __init__(self, gd, name=None): + self.gd = gd + self.name = name + self.fetch_url = None + + def set_fetch_url(self, url): + self.fetch_url = url + if self.name is not None: + self.gd._runcmd(['git', 'remote', 'set-url', self.name, url]) + + def get_fetch_url(self): + if self.name is None: + return self.fetch_url + try: + # git-ls-remote is used, rather than git-config, since + # url.*.{push,}insteadof is processed after config is loaded + return self.gd._runcmd( + ['git', 'ls-remote', '--get-url', self.name]).rstrip('\n') + except cliapp.AppException: + return None + + class GitDirectory(object): '''Represent a git working tree + .git directory. @@ -196,20 +229,14 @@ class GitDirectory(object): value = self._runcmd(['git', 'config', '-z', key]) return value.rstrip('\0') - def set_remote_fetch_url(self, remote_name, url): - '''Set the fetch URL for a remote.''' - self._runcmd(['git', 'remote', 'set-url', remote_name, url]) - - def get_remote_fetch_url(self, remote_name): - '''Return the fetch URL for a given remote.''' - output = self._runcmd(['git', 'remote', '-v']) - for line in output.splitlines(): - words = line.split() - if (len(words) == 3 and - words[0] == remote_name and - words[2] == '(fetch)'): - return words[1] - return None + def get_remote(self, *args, **kwargs): + '''Get a remote for this Repository. + + Gets a previously configured remote if a remote name is given. + Otherwise a nascent one is created. + + ''' + return Remote(self, *args, **kwargs) def update_remotes(self): # pragma: no cover '''Run "git remote update --prune".''' |