Commit 075910b4 authored by Michal Čihař's avatar Michal Čihař

Implement remote repository configuration

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent d50c3df7
......@@ -152,3 +152,21 @@ class VCSGitTest(RepoTestCase):
len(obj_hash),
40
)
def test_configure_remote(self):
self.repo.configure_remote('pullurl', 'pushurl', 'branch')
self.assertEquals(
self.repo.get_config('remote.origin.url'),
'pullurl',
)
self.assertEquals(
self.repo.get_config('remote.origin.pushURL'),
'pushurl',
)
# Test that we handle not set fetching
self.repo._execute(['config', '--unset', 'remote.origin.fetch'])
self.repo.configure_remote('pullurl', 'pushurl', 'branch')
self.assertEquals(
self.repo.get_config('remote.origin.fetch'),
'+refs/heads/branch:refs/remotes/origin/branch',
)
......@@ -37,7 +37,6 @@ class Repository(object):
Currently missing methods:
- repository configuration (SubProject.configure_repo)
- branch configuration (SubProject.configure_branch)
"""
_last_revision = None
......@@ -187,6 +186,12 @@ class Repository(object):
"""
raise NotImplementedError()
def configure_remote(self, pull_url, push_url, branch):
"""
Configure remote repository.
"""
raise NotImplementedError()
class GitRepository(Repository):
"""
......@@ -335,3 +340,39 @@ class GitRepository(Repository):
Returns hash of object in the VCS.
"""
return self._execute(['ls-tree', 'HEAD', path]).split()[2]
def configure_remote(self, pull_url, push_url, branch):
"""
Configure remote repository.
"""
old_pull = None
old_push = None
# Parse existing remotes
for remote in self._execute(['remote', '-v']).splitlines():
name, url, kind = remote.split()
if name != 'origin':
continue
if kind == '(fetch)':
old_pull = url
elif kind == '(push)':
old_push = url
if old_pull is None:
# No origin existing
self._execute(['remote', 'add', 'origin', pull_url])
elif old_pull != pull_url:
# URL changed?
self._execute(['remote', 'set-url', 'origin', pull_url])
if old_push != push_url:
self._execute(['remote', 'set-url', 'origin', '--push', push_url])
# Set branch to track
try:
self._execute(
['remote', 'set-branches', 'origin', branch]
)
except RepositoryException:
self._execute(
['remote', 'set-branches', '--add', 'origin', branch]
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment