-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add support for unicode Git tags/branches #2997
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,13 @@ | |
|
||
from __future__ import absolute_import | ||
|
||
import re | ||
import logging | ||
import csv | ||
import os | ||
import re | ||
|
||
from builtins import bytes, str # pylint: disable=redefined-builtin | ||
from readthedocs.projects.exceptions import ProjectImportError | ||
from readthedocs.vcs_support.base import BaseVCS, VCSVersion | ||
|
||
from future import standard_library | ||
standard_library.install_aliases() | ||
from io import StringIO # noqa | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
@@ -27,6 +21,25 @@ class Backend(BaseVCS): | |
supports_branches = True | ||
fallback_branch = 'master' # default branch | ||
|
||
TAG_REGEX = re.compile( | ||
r''' | ||
^\s* | ||
(?P<hash>[0-9a-f]+) | ||
\s+ | ||
(?P<tag>.*) | ||
(?:\n|$) | ||
''', | ||
(re.VERBOSE | re.MULTILINE) | ||
) | ||
BRANCH_REGEX = re.compile( | ||
r''' | ||
^\s* | ||
(?P<branch>\w.+) | ||
(?:\n|$) | ||
''', | ||
(re.VERBOSE | re.MULTILINE) | ||
) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(Backend, self).__init__(*args, **kwargs) | ||
self.token = kwargs.get('token', None) | ||
|
@@ -115,19 +128,11 @@ def parse_tags(self, data): | |
hash as identifier. | ||
""" | ||
# parse the lines into a list of tuples (commit-hash, tag ref name) | ||
# StringIO below is expecting Unicode data, so ensure that it gets it. | ||
if not isinstance(data, str): | ||
data = str(data) | ||
raw_tags = csv.reader(StringIO(data), delimiter=' ') | ||
vcs_tags = [] | ||
for row in raw_tags: | ||
row = [f for f in row if f != ''] | ||
if row == []: | ||
continue | ||
commit_hash, name = row | ||
clean_name = name.split('/')[-1] | ||
vcs_tags.append(VCSVersion(self, commit_hash, clean_name)) | ||
return vcs_tags | ||
tags = [] | ||
for match in self.TAG_REGEX.finditer(data): | ||
tag = match.group('tag').split('/')[-1] | ||
tags.append(VCSVersion(self, match.group('hash'), tag)) | ||
return tags | ||
|
||
@property | ||
def branches(self): | ||
|
@@ -151,27 +156,20 @@ def parse_branches(self, data): | |
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) | ||
raw_branches = csv.reader(StringIO(data), delimiter=' ') | ||
for branch in raw_branches: | ||
branch = [f for f in branch if f != '' and f != '*'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# Handle empty branches | ||
if branch: | ||
branch = branch[0] | ||
if branch.startswith('origin/'): | ||
cut_len = len('origin/') | ||
slug = branch[cut_len:].replace('/', '-') | ||
if slug in ['HEAD']: | ||
continue | ||
clean_branches.append(VCSVersion(self, branch, slug)) | ||
else: | ||
# Believe this is dead code. | ||
slug = branch.replace('/', '-') | ||
clean_branches.append(VCSVersion(self, branch, slug)) | ||
return clean_branches | ||
branches = [] | ||
for match in self.BRANCH_REGEX.finditer(data): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we're depending on some regex magic here that isn't easy to understand. It would be good to document exactly how this is working. Are we still handling empty branches, and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what is meant by empty branches or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous code was doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add some test cases for this. I missed the '*', but empty is likely not being caught anyways. |
||
branch = match.group('branch') | ||
if branch.startswith('origin/HEAD'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
continue | ||
elif branch.startswith('origin/'): | ||
cut_len = len('origin/') | ||
slug = branch[cut_len:].replace('/', '-') | ||
branches.append(VCSVersion(self, branch, slug)) | ||
else: | ||
# Believe this is dead code. | ||
slug = branch.replace('/', '-') | ||
branches.append(VCSVersion(self, branch, slug)) | ||
return branches | ||
|
||
@property | ||
def commit(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could use some comments that explain them.