Skip to content

Commit

Permalink
Merge pull request #6548 from stsewd/dont-use-instance-when-isnot-needed
Browse files Browse the repository at this point in the history
Don't use an instance of VCS when isn't needed
  • Loading branch information
stsewd authored Jan 23, 2020
2 parents adc224f + 22fe275 commit cd7265f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 2 additions & 4 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def vcs_url(self):
if self.slug == STABLE:
slug_url = self.ref
elif self.slug == LATEST:
slug_url = self.project.default_branch or self.project.vcs_repo().fallback_branch
slug_url = self.project.get_default_branch()
else:
slug_url = self.slug

Expand Down Expand Up @@ -250,9 +250,7 @@ def commit_name(self):
# LATEST is special as it is usually a branch but does not contain the
# name in verbose_name.
if self.slug == LATEST:
if self.project.default_branch:
return self.project.default_branch
return self.project.vcs_repo().fallback_branch
return self.project.get_default_branch()

if self.slug == STABLE:
if self.type == BRANCH:
Expand Down
14 changes: 11 additions & 3 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def save(self, *args, **kwargs): # pylint: disable=arguments-differ
except Exception:
log.exception('failed to update static metadata')
try:
branch = self.default_branch or self.vcs_repo().fallback_branch
branch = self.get_default_branch()
if not self.versions.filter(slug=LATEST).exists():
self.versions.create_latest(identifier=branch)
except Exception:
Expand Down Expand Up @@ -840,7 +840,7 @@ def vcs_repo(
# ``version.slug`` instead of a ``Version`` instance (I prefer an
# instance here)

backend = backend_cls.get(self.repo_type)
backend = self.vcs_class()
if not backend:
repo = None
else:
Expand All @@ -850,6 +850,14 @@ def vcs_repo(
)
return repo

def vcs_class(self):
"""
Get the class used for VCS operations.
This is useful when doing operations that don't need to have the repository on disk.
"""
return backend_cls.get(self.repo_type)

def git_service_class(self):
"""Get the service class for project. e.g: GitHubService, GitLabService."""
from readthedocs.oauth.services import registry
Expand Down Expand Up @@ -1080,7 +1088,7 @@ def get_default_branch(self):
"""Get the version representing 'latest'."""
if self.default_branch:
return self.default_branch
return self.vcs_repo().fallback_branch
return self.vcs_class().fallback_branch

def add_subproject(self, child, alias=None):
subproject, __ = ProjectRelationship.objects.get_or_create(
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def sync_repo(self, environment):
if not os.path.exists(self.project.doc_path):
os.makedirs(self.project.doc_path)

if not self.project.vcs_repo():
if not self.project.vcs_class():
raise RepositoryError(
_('Repository type "{repo_type}" unknown').format(
repo_type=self.project.repo_type,
Expand Down

0 comments on commit cd7265f

Please sign in to comment.