Commit d20e6260 authored by Michal Čihař's avatar Michal Čihař

Move branch information to the repository object

It usually doesn't change over it's lifetime so it's not needed to pass
it as parameter to many calls.
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent cf87b089
...@@ -583,7 +583,9 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -583,7 +583,9 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
return self.linked_subproject.repository return self.linked_subproject.repository
if self._repository is None: if self._repository is None:
self._repository = VCS_REGISTRY[self.vcs](self.get_path()) self._repository = VCS_REGISTRY[self.vcs](
self.get_path(), self.branch
)
cache_key = '{0}-config-check'.format(self.get_full_slug()) cache_key = '{0}-config-check'.format(self.get_full_slug())
if cache.get(cache_key) is None: if cache.get(cache_key) is None:
self._repository.check_config() self._repository.check_config()
...@@ -799,7 +801,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -799,7 +801,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
try: try:
self.log_info('pushing to remote repo') self.log_info('pushing to remote repo')
with self.repository_lock: with self.repository_lock:
self.repository.push(self.branch) self.repository.push()
Change.objects.create( Change.objects.create(
action=Change.ACTION_PUSH, action=Change.ACTION_PUSH,
...@@ -843,7 +845,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -843,7 +845,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
try: try:
self.log_info('reseting to remote repo') self.log_info('reseting to remote repo')
with self.repository_lock: with self.repository_lock:
self.repository.reset(self.branch) self.repository.reset()
Change.objects.create( Change.objects.create(
action=Change.ACTION_RESET, action=Change.ACTION_RESET,
...@@ -941,7 +943,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -941,7 +943,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
with self.repository_lock: with self.repository_lock:
try: try:
# Try to merge it # Try to merge it
method(self.branch) method()
self.log_info( self.log_info(
'%s remote into repo', '%s remote into repo',
self.merge_style, self.merge_style,
...@@ -1441,7 +1443,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -1441,7 +1443,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
''' '''
if self.is_repo_link: if self.is_repo_link:
return self.linked_subproject.repo_needs_merge() return self.linked_subproject.repo_needs_merge()
return self.repository.needs_merge(self.branch) return self.repository.needs_merge()
def repo_needs_push(self): def repo_needs_push(self):
''' '''
...@@ -1449,7 +1451,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin): ...@@ -1449,7 +1451,7 @@ class SubProject(models.Model, PercentMixin, URLMixin, PathMixin):
''' '''
if self.is_repo_link: if self.is_repo_link:
return self.linked_subproject.repo_needs_push() return self.linked_subproject.repo_needs_push()
return self.repository.needs_push(self.branch) return self.repository.needs_push()
@property @property
def file_format_cls(self): def file_format_cls(self):
......
...@@ -85,7 +85,6 @@ class VCSGitTest(RepoTestCase): ...@@ -85,7 +85,6 @@ class VCSGitTest(RepoTestCase):
_tempdir = None _tempdir = None
_class = GitRepository _class = GitRepository
_vcs = 'git' _vcs = 'git'
_branch = 'master'
_can_push = True _can_push = True
def setUp(self): def setUp(self):
...@@ -128,7 +127,7 @@ class VCSGitTest(RepoTestCase): ...@@ -128,7 +127,7 @@ class VCSGitTest(RepoTestCase):
) )
# Push it # Push it
repo.push(self._branch) repo.push()
finally: finally:
shutil.rmtree(tempdir) shutil.rmtree(tempdir)
...@@ -147,7 +146,7 @@ class VCSGitTest(RepoTestCase): ...@@ -147,7 +146,7 @@ class VCSGitTest(RepoTestCase):
self.repo.update_remote() self.repo.update_remote()
def test_push(self): def test_push(self):
self.repo.push(self._branch) self.repo.push()
def test_push_commit(self): def test_push_commit(self):
self.test_commit() self.test_commit()
...@@ -155,11 +154,11 @@ class VCSGitTest(RepoTestCase): ...@@ -155,11 +154,11 @@ class VCSGitTest(RepoTestCase):
def test_reset(self): def test_reset(self):
original = self.repo.last_revision original = self.repo.last_revision
self.repo.reset(self._branch) self.repo.reset()
self.assertEqual(original, self.repo.last_revision) self.assertEqual(original, self.repo.last_revision)
self.test_commit() self.test_commit()
self.assertNotEqual(original, self.repo.last_revision) self.assertNotEqual(original, self.repo.last_revision)
self.repo.reset(self._branch) self.repo.reset()
self.assertEqual(original, self.repo.last_revision) self.assertEqual(original, self.repo.last_revision)
def test_merge_commit(self): def test_merge_commit(self):
...@@ -206,11 +205,11 @@ class VCSGitTest(RepoTestCase): ...@@ -206,11 +205,11 @@ class VCSGitTest(RepoTestCase):
def test_merge(self): def test_merge(self):
self.test_update_remote() self.test_update_remote()
self.repo.merge(self._branch) self.repo.merge()
def test_rebase(self): def test_rebase(self):
self.test_update_remote() self.test_update_remote()
self.repo.rebase(self._branch) self.repo.rebase()
def test_status(self): def test_status(self):
status = self.repo.status() status = self.repo.status()
...@@ -264,12 +263,12 @@ class VCSGitTest(RepoTestCase): ...@@ -264,12 +263,12 @@ class VCSGitTest(RepoTestCase):
self.check_valid_info(info) self.check_valid_info(info)
def test_needs_merge(self): def test_needs_merge(self):
self.assertFalse(self.repo.needs_merge(self._branch)) self.assertFalse(self.repo.needs_merge())
self.assertFalse(self.repo.needs_push(self._branch)) self.assertFalse(self.repo.needs_push())
def test_needs_push(self): def test_needs_push(self):
self.test_commit() self.test_commit()
self.assertTrue(self.repo.needs_push(self._branch)) self.assertTrue(self.repo.needs_push())
def test_is_supported(self): def test_is_supported(self):
self.assertTrue(self._class.is_supported()) self.assertTrue(self._class.is_supported())
...@@ -364,7 +363,7 @@ class VCSGitTest(RepoTestCase): ...@@ -364,7 +363,7 @@ class VCSGitTest(RepoTestCase):
def test_configure_branch(self): def test_configure_branch(self):
# Existing branch # Existing branch
self.repo.configure_branch(self._branch) self.repo.configure_branch(self._class.default_branch)
self.assertRaises( self.assertRaises(
RepositoryException, RepositoryException,
...@@ -376,14 +375,12 @@ class VCSGitTest(RepoTestCase): ...@@ -376,14 +375,12 @@ class VCSGitTest(RepoTestCase):
class VCSGerritTest(VCSGitTest): class VCSGerritTest(VCSGitTest):
_class = GitWithGerritRepository _class = GitWithGerritRepository
_vcs = 'git' _vcs = 'git'
_branch = 'master'
_can_push = False _can_push = False
class VCSGithubTest(VCSGitTest): class VCSGithubTest(VCSGitTest):
_class = GithubFakeRepository _class = GithubFakeRepository
_vcs = 'git' _vcs = 'git'
_branch = 'master'
_can_push = False _can_push = False
...@@ -393,17 +390,6 @@ class VCSHgTest(VCSGitTest): ...@@ -393,17 +390,6 @@ class VCSHgTest(VCSGitTest):
""" """
_class = HgRepository _class = HgRepository
_vcs = 'hg' _vcs = 'hg'
_branch = 'default'
def test_configure_branch(self):
# Existing branch
self.repo.configure_branch('default')
self.assertRaises(
RepositoryException,
self.repo.configure_branch,
'branch'
)
def test_configure_remote(self): def test_configure_remote(self):
self.repo.configure_remote('/pullurl', '/pushurl', 'branch') self.repo.configure_remote('/pullurl', '/pushurl', 'branch')
......
...@@ -97,8 +97,12 @@ class Repository(object): ...@@ -97,8 +97,12 @@ class Repository(object):
_is_supported = None _is_supported = None
_version = None _version = None
def __init__(self, path): def __init__(self, path, branch=None):
self.path = path self.path = path
if branch is None:
self.branch = self.default_branch
else:
self.branch = branch
self.last_output = '' self.last_output = ''
if not self.is_valid(): if not self.is_valid():
self.init() self.init()
...@@ -189,7 +193,7 @@ class Repository(object): ...@@ -189,7 +193,7 @@ class Repository(object):
return self._last_remote_revision return self._last_remote_revision
@classmethod @classmethod
def clone(cls, source, target, bare=False): def clone(cls, source, target, branch=None, bare=False):
""" """
Clones repository and returns Repository object for cloned Clones repository and returns Repository object for cloned
repository. repository.
...@@ -209,25 +213,25 @@ class Repository(object): ...@@ -209,25 +213,25 @@ class Repository(object):
""" """
return self.execute(self._cmd_status) return self.execute(self._cmd_status)
def push(self, branch): def push(self):
""" """
Pushes given branch to remote repository. Pushes given branch to remote repository.
""" """
self.execute(self._cmd_push + [branch]) self.execute(self._cmd_push + [self.branch])
def reset(self, branch): def reset(self):
""" """
Resets working copy to match remote branch. Resets working copy to match remote branch.
""" """
raise NotImplementedError() raise NotImplementedError()
def merge(self, branch=None, abort=False): def merge(self, abort=False):
""" """
Merges remote branch or reverts the merge. Merges remote branch or reverts the merge.
""" """
raise NotImplementedError() raise NotImplementedError()
def rebase(self, branch=None, abort=False): def rebase(self, abort=False):
""" """
Rebases working copy on top of remote branch. Rebases working copy on top of remote branch.
""" """
...@@ -239,14 +243,14 @@ class Repository(object): ...@@ -239,14 +243,14 @@ class Repository(object):
""" """
raise NotImplementedError() raise NotImplementedError()
def needs_merge(self, branch): def needs_merge(self):
""" """
Checks whether repository needs merge with upstream Checks whether repository needs merge with upstream
(is missing some revisions). (is missing some revisions).
""" """
raise NotImplementedError() raise NotImplementedError()
def needs_push(self, branch): def needs_push(self):
""" """
Checks whether repository needs push to upstream Checks whether repository needs push to upstream
(has additional revisions). (has additional revisions).
...@@ -391,7 +395,7 @@ class GitRepository(Repository): ...@@ -391,7 +395,7 @@ class GitRepository(Repository):
self.set_config('push.default', 'current') self.set_config('push.default', 'current')
@classmethod @classmethod
def clone(cls, source, target, bare=False): def clone(cls, source, target, branch=None, bare=False):
""" """
Clones repository and returns Repository object for cloned Clones repository and returns Repository object for cloned
repository. repository.
...@@ -400,7 +404,7 @@ class GitRepository(Repository): ...@@ -400,7 +404,7 @@ class GitRepository(Repository):
cls._popen(['clone', '--bare', source, target]) cls._popen(['clone', '--bare', source, target])
else: else:
cls._popen(['clone', source, target]) cls._popen(['clone', source, target])
return cls(target) return cls(target, branch)
def get_config(self, path): def get_config(self, path):
""" """
...@@ -421,30 +425,30 @@ class GitRepository(Repository): ...@@ -421,30 +425,30 @@ class GitRepository(Repository):
self.set_config('user.name', name) self.set_config('user.name', name)
self.set_config('user.email', mail) self.set_config('user.email', mail)
def reset(self, branch): def reset(self):
""" """
Resets working copy to match remote branch. Resets working copy to match remote branch.
""" """
self.execute(['reset', '--hard', 'origin/{0}'.format(branch)]) self.execute(['reset', '--hard', 'origin/{0}'.format(self.branch)])
self._last_revision = None self._last_revision = None
def rebase(self, branch=None, abort=False): def rebase(self, abort=False):
""" """
Rebases working copy on top of remote branch. Rebases working copy on top of remote branch.
""" """
if abort: if abort:
self.execute(['rebase', '--abort']) self.execute(['rebase', '--abort'])
else: else:
self.execute(['rebase', 'origin/{0}'.format(branch)]) self.execute(['rebase', 'origin/{0}'.format(self.branch)])
def merge(self, branch=None, abort=False): def merge(self, abort=False):
""" """
Merges remote branch or reverts the merge. Merges remote branch or reverts the merge.
""" """
if abort: if abort:
self.execute(['merge', '--abort']) self.execute(['merge', '--abort'])
else: else:
self.execute(['merge', 'origin/{0}'.format(branch)]) self.execute(['merge', 'origin/{0}'.format(self.branch)])
def needs_commit(self, filename=None): def needs_commit(self, filename=None):
""" """
...@@ -511,19 +515,19 @@ class GitRepository(Repository): ...@@ -511,19 +515,19 @@ class GitRepository(Repository):
['log', '--oneline', refspec, '--'] ['log', '--oneline', refspec, '--']
) )
def needs_merge(self, branch): def needs_merge(self):
""" """
Checks whether repository needs merge with upstream Checks whether repository needs merge with upstream
(is missing some revisions). (is missing some revisions).
""" """
return self._log_revisions('..origin/{0}'.format(branch)) != '' return self._log_revisions('..origin/{0}'.format(self.branch)) != ''
def needs_push(self, branch): def needs_push(self):
""" """
Checks whether repository needs push to upstream Checks whether repository needs push to upstream
(has additional revisions). (has additional revisions).
""" """
return self._log_revisions('origin/{0}..'.format(branch)) != '' return self._log_revisions('origin/{0}..'.format(self.branch)) != ''
@classmethod @classmethod
def _get_version(cls): def _get_version(cls):
...@@ -600,6 +604,7 @@ class GitRepository(Repository): ...@@ -600,6 +604,7 @@ class GitRepository(Repository):
'remote.origin.fetch', 'remote.origin.fetch',
'+refs/heads/{0}:refs/remotes/origin/{0}'.format(branch) '+refs/heads/{0}:refs/remotes/origin/{0}'.format(branch)
) )
self.branch = branch
def configure_branch(self, branch): def configure_branch(self, branch):
""" """
...@@ -625,6 +630,7 @@ class GitRepository(Repository): ...@@ -625,6 +630,7 @@ class GitRepository(Repository):
# Checkout # Checkout
self.execute(['checkout', branch]) self.execute(['checkout', branch])
self.branch = branch
def describe(self): def describe(self):
""" """
...@@ -638,7 +644,7 @@ class GitWithGerritRepository(GitRepository): ...@@ -638,7 +644,7 @@ class GitWithGerritRepository(GitRepository):
name = 'Gerrit' name = 'Gerrit'
def push(self, branch): def push(self):
try: try:
self.execute(['review']) self.execute(['review'])
except RepositoryException as error: except RepositoryException as error:
...@@ -689,16 +695,16 @@ class GithubRepository(GitRepository): ...@@ -689,16 +695,16 @@ class GithubRepository(GitRepository):
if self._hub_user not in self.execute(['remote']).splitlines(): if self._hub_user not in self.execute(['remote']).splitlines():
self.execute(['fork']) self.execute(['fork'])
def push(self, branch): def push(self):
""" """
Forks repository on Github, pushes changes to *-weblate branch Forks repository on Github, pushes changes to *-weblate branch
on fork and creates pull request against original repository. on fork and creates pull request against original repository.
""" """
self.fork() self.fork()
fork_branch = '{0}-weblate'.format(branch) fork_branch = '{0}-weblate'.format(self.branch)
self.push_to_fork(branch, fork_branch) self.push_to_fork(self.branch, fork_branch)
try: try:
self.create_pull_request(branch, fork_branch) self.create_pull_request(self.branch, fork_branch)
except RepositoryException as error: except RepositoryException as error:
if error.retcode == 1: if error.retcode == 1:
# Pull request already exists. # Pull request already exists.
...@@ -745,7 +751,7 @@ class HgRepository(Repository): ...@@ -745,7 +751,7 @@ class HgRepository(Repository):
self.set_config('ui.ssh', ssh_file(SSH_WRAPPER)) self.set_config('ui.ssh', ssh_file(SSH_WRAPPER))
@classmethod @classmethod
def clone(cls, source, target, bare=False): def clone(cls, source, target, branch=None, bare=False):
""" """
Clones repository and returns Repository object for cloned Clones repository and returns Repository object for cloned
repository. repository.
...@@ -754,7 +760,7 @@ class HgRepository(Repository): ...@@ -754,7 +760,7 @@ class HgRepository(Repository):
cls._popen(['clone', '--updaterev', 'null', source, target]) cls._popen(['clone', '--updaterev', 'null', source, target])
else: else:
cls._popen(['clone', source, target]) cls._popen(['clone', source, target])
return cls(target) return cls(target, branch)
def get_config(self, path): def get_config(self, path):
""" """
...@@ -801,17 +807,17 @@ class HgRepository(Repository): ...@@ -801,17 +807,17 @@ class HgRepository(Repository):
'{0} <{1}>'.format(name, mail) '{0} <{1}>'.format(name, mail)
) )
def reset(self, branch): def reset(self):
""" """
Resets working copy to match remote branch. Resets working copy to match remote branch.
""" """
self.set_config('extensions.strip', '') self.set_config('extensions.strip', '')
self.execute(['revert', '-a', '--no-backup']) self.execute(['revert', '-a', '--no-backup'])
if self.needs_push(branch): if self.needs_push():
self.execute(['strip', 'roots(outgoing())']) self.execute(['strip', 'roots(outgoing())'])
self._last_revision = None self._last_revision = None
def rebase(self, branch=None, abort=False): def rebase(self, abort=False):
""" """
Rebases working copy on top of remote branch. Rebases working copy on top of remote branch.
""" """
...@@ -827,7 +833,7 @@ class HgRepository(Repository): ...@@ -827,7 +833,7 @@ class HgRepository(Repository):
return return
raise raise
def merge(self, branch=None, abort=False): def merge(self, abort=False):
""" """
Merges remote branch or reverts the merge. Merges remote branch or reverts the merge.
""" """
...@@ -919,14 +925,14 @@ class HgRepository(Repository): ...@@ -919,14 +925,14 @@ class HgRepository(Repository):
return result return result
def needs_merge(self, branch): def needs_merge(self):
""" """
Checks whether repository needs merge with upstream Checks whether repository needs merge with upstream
(is missing some revisions). (is missing some revisions).
""" """
return self.execute(['log', '-r', 'only(tip,.)']) != '' return self.execute(['log', '-r', 'only(tip,.)']) != ''
def needs_push(self, branch): def needs_push(self):
""" """
Checks whether repository needs push to upstream Checks whether repository needs push to upstream
(has additional revisions). (has additional revisions).
...@@ -991,11 +997,14 @@ class HgRepository(Repository): ...@@ -991,11 +997,14 @@ class HgRepository(Repository):
self.set_config('experimental.evolution', 'all') self.set_config('experimental.evolution', 'all')
self.set_config('phases.publish', 'False') self.set_config('phases.publish', 'False')
self.branch = branch
def configure_branch(self, branch): def configure_branch(self, branch):
""" """
Configure repository branch. Configure repository branch.
""" """
self.execute(['update', branch]) self.execute(['update', branch])
self.branch = branch
def describe(self): def describe(self):
""" """
...@@ -1007,7 +1016,7 @@ class HgRepository(Repository): ...@@ -1007,7 +1016,7 @@ class HgRepository(Repository):
'--template', '{latesttag}-{latesttagdistance}-{node|short}' '--template', '{latesttag}-{latesttagdistance}-{node|short}'
]).strip() ]).strip()
def push(self, branch): def push(self):
""" """
Pushes given branch to remote repository. Pushes given branch to remote repository.
""" """
......
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