Skip to content

Commit

Permalink
Support git unicode branches
Browse files Browse the repository at this point in the history
Use `gitpython` to get repository branches instead of our custom/hacky
csv parser.

With this change, we loose the output from `git branch -r` command
shown to the user under the build output.
  • Loading branch information
humitos committed Nov 19, 2018
1 parent 4031b90 commit b7ab9d3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 69 deletions.
52 changes: 28 additions & 24 deletions readthedocs/rtd_tests/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,34 @@ def setUp(self):
self.dummy_conf.submodules.include = ALL
self.dummy_conf.submodules.exclude = []

def test_parse_branches(self):
data = """
develop
master
release/2.0.0
origin/2.0.X
origin/HEAD -> origin/master
origin/master
origin/release/2.0.0
origin/release/foo/bar
"""

expected_ids = [
('develop', 'develop'),
('master', 'master'),
('release/2.0.0', 'release/2.0.0'),
('origin/2.0.X', '2.0.X'),
('origin/master', 'master'),
('origin/release/2.0.0', 'release/2.0.0'),
('origin/release/foo/bar', 'release/foo/bar'),
def test_git_branches(self):
repo_path = self.project.repo
default_branches = [
# comes from ``make_test_git`` function
'submodule',
'relativesubmodule',
'invalidsubmodule',
]
given_ids = [(x.identifier, x.verbose_name) for x in
self.project.vcs_repo().parse_branches(data)]
self.assertEqual(expected_ids, given_ids)
branches = [
'develop',
'master',
'2.0.X',
'release/2.0.0',
'release/foo/bar',
'release-ünîø∂é',
]
for branch in branches:
create_git_branch(repo_path, branch)

repo = self.project.vcs_repo()
# We aren't cloning the repo,
# so we need to hack the repo path
repo.working_dir = repo_path

self.assertEqual(
set(branches + default_branches),
{branch.verbose_name for branch in repo.branches},
)

def test_git_checkout(self):
repo = self.project.vcs_repo()
Expand All @@ -90,7 +94,7 @@ def test_git_tags(self):
repo.working_dir = repo_path
self.assertEqual(
set(['v01', 'v02', 'release-ünîø∂é']),
set(vcs.verbose_name for vcs in repo.tags)
{vcs.verbose_name for vcs in repo.tags},
)

def test_check_for_submodules(self):
Expand Down
53 changes: 8 additions & 45 deletions readthedocs/vcs_support/backends/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,51 +174,14 @@ def tags(self):

@property
def branches(self):
# Only show remote branches
retcode, stdout, _ = self.run(
'git',
'branch',
'-r',
record_as_success=True,
)
# error (or no branches found)
if retcode != 0:
return []
return self.parse_branches(stdout)

def parse_branches(self, data):
"""
Parse output of git branch -r.
e.g.:
origin/2.0.X
origin/HEAD -> origin/master
origin/develop
origin/master
origin/release/2.0.0
origin/release/2.1.0
"""
clean_branches = []
# StringIO below is expecting Unicode data, so ensure that it gets it.
if not isinstance(data, str):
data = str(data)
delimiter = str(' ').encode('utf-8') if PY2 else str(' ')
raw_branches = csv.reader(StringIO(data), delimiter=delimiter)
for branch in raw_branches:
branch = [f for f in branch if f not in ('', '*')]
# Handle empty branches
if branch:
branch = branch[0]
if branch.startswith('origin/'):
verbose_name = branch.replace('origin/', '')
if verbose_name in ['HEAD']:
continue
clean_branches.append(
VCSVersion(self, branch, verbose_name))
else:
clean_branches.append(VCSVersion(self, branch, branch))
return clean_branches
repo = git.Repo(self.working_dir)
versions = []
for branch in repo.branches:
verbose_name = branch.name
if verbose_name.startswith('origin/'):
verbose_name = verbose_name.replace('origin/', '')
versions.append(VCSVersion(self, str(branch), verbose_name))
return versions

@property
def commit(self):
Expand Down

0 comments on commit b7ab9d3

Please sign in to comment.