Skip to content

Better msg when gitpython fails #5903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion readthedocs/projects/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ class RepositoryError(BuildEnvironmentError):
'Private repositories are not supported.',
)

INVALID_SUBMODULES = _('One or more submodule URLs are not valid: {}.',)
INVALID_SUBMODULES = _('One or more submodule URLs are not valid: {}.')
INVALID_SUBMODULES_PATH = _(
'One or more submodule paths are not valid. '
'Check that all your submodules in .gitmodules are used.'
)

DUPLICATED_RESERVED_VERSIONS = _(
'You can not have two versions with the name latest or stable.',
Expand Down
18 changes: 18 additions & 0 deletions readthedocs/rtd_tests/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from os.path import exists
from tempfile import mkdtemp
import textwrap

import django_dynamic_fixture as fixture
from django.contrib.auth.models import User
Expand Down Expand Up @@ -189,6 +190,23 @@ def test_check_invalid_submodule_urls(self):
RepositoryError.INVALID_SUBMODULES.format(['invalid']),
)

def test_invalid_submodule_path(self):
repo_path = self.project.repo
gitmodules_path = os.path.join(repo_path, '.gitmodules')

with open(gitmodules_path, 'w+') as f:
content = textwrap.dedent("""
[submodule "not-valid-path"]
path = not-valid-path
url = https://github.com/readthedocs/readthedocs.org
""")
f.write(content)

repo = self.project.vcs_repo()
repo.working_dir = repo_path
with self.assertRaises(RepositoryError, msg=RepositoryError.INVALID_SUBMODULES_PATH):
repo.update_submodules(self.dummy_conf)

@patch('readthedocs.projects.models.Project.checkout_path')
def test_fetch_clean_tags_and_branches(self, checkout_path):
upstream_repo = self.project.repo
Expand Down
16 changes: 12 additions & 4 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def are_submodules_available(self, config):
return False

# Keep compatibility with previous projects
repo = git.Repo(self.working_dir)
return bool(repo.submodules)
return bool(self.submodules)

def validate_submodules(self, config):
"""
Expand All @@ -104,8 +103,7 @@ def validate_submodules(self, config):
Returns `False` if at least one submodule is invalid.
Returns the list of invalid submodules.
"""
repo = git.Repo(self.working_dir)
submodules = {sub.path: sub for sub in repo.submodules}
submodules = {sub.path: sub for sub in self.submodules}

for sub_path in config.submodules.exclude:
path = sub_path.rstrip('/')
Expand Down Expand Up @@ -222,6 +220,16 @@ def commit(self):
return stdout.strip()
return None

@property
def submodules(self):
try:
repo = git.Repo(self.working_dir)
return list(repo.submodules)
except InvalidGitRepositoryError:
raise RepositoryError(
RepositoryError.INVALID_SUBMODULES_PATH,
)

def checkout(self, identifier=None):
"""Checkout to identifier or latest."""
super().checkout()
Expand Down