From 9dca5e16376ab8a1115feffec843f06451f9b372 Mon Sep 17 00:00:00 2001 From: Martin Bornhold Date: Thu, 22 Sep 2016 15:33:11 +0200 Subject: [PATCH] shadow: Use only a single regular expression to generate and match repo/group slugs. --- rhodecode/lib/middleware/simplevcs.py | 13 +++++----- rhodecode/lib/utils.py | 37 +++++++++------------------ 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/rhodecode/lib/middleware/simplevcs.py b/rhodecode/lib/middleware/simplevcs.py index b0e3f6b0..1a438ca0 100644 --- a/rhodecode/lib/middleware/simplevcs.py +++ b/rhodecode/lib/middleware/simplevcs.py @@ -112,14 +112,13 @@ class SimpleVCS(object): vcs_repo_name and is_shadow_repo on the current instance. """ # TODO: martinb: Move to class or module scope. - # TODO: martinb: Check if we have to use re.UNICODE. - # TODO: martinb: Check which chars are allowed for repo/group names. - # These chars are excluded: '`?=[]\;\'"<>,/~!@#$%^&*()+{}|: ' - # Code from: rhodecode/lib/utils.py:repo_name_slug() + from rhodecode.lib.utils import SLUG_RE pr_regex = re.compile( - '(?P(?:[\w-]+)(?:/[\w-]+)*)/' # repo groups - '(?P[\w-]+)' # target repo name - '/pull-request/(?P\d+)/repository') # pr suffix + '(?P(?:{slug_pat})(?:/{slug_pat})*)' # repo groups + '/(?P{slug_pat})' # target repo + '/pull-request/(?P\d+)' # pull request + '/repository$' # shadow repo + .format(slug_pat=SLUG_RE.pattern)) # Get url repo name from environment. self.url_repo_name = self._get_repository_name(environ) diff --git a/rhodecode/lib/utils.py b/rhodecode/lib/utils.py index 54dae19e..48e47f23 100644 --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -58,42 +58,29 @@ log = logging.getLogger(__name__) REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*') +# String of characters which are not allowed in repo/group slugs. +SLUG_BAD_CHARS = re.escape('`?=[]\;\'"<>,/~!@#$%^&*()+{}|:') +# Regex that matches forbidden characters in repo/group slugs. +SLUG_BAD_CHAR_RE = re.compile('[{}]'.format(SLUG_BAD_CHARS)) +# Regex that matches allowed characters in repo/group slugs. +SLUG_GOOD_CHAR_RE = re.compile('[^{}]'.format(SLUG_BAD_CHARS)) +# Regex that matches whole repo/group slugs. +SLUG_RE = re.compile('[^{}]+'.format(SLUG_BAD_CHARS)) + _license_cache = None -def recursive_replace(str_, replace=' '): - """ - Recursive replace of given sign to just one instance - - :param str_: given string - :param replace: char to find and replace multiple instances - - Examples:: - >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-') - 'Mighty-Mighty-Bo-sstones' - """ - - if str_.find(replace * 2) == -1: - return str_ - else: - str_ = str_.replace(replace * 2, replace) - return recursive_replace(str_, replace) - - def repo_name_slug(value): """ Return slug of name of repository This function is called on each creation/modification of repository to prevent bad names in repo """ + replacement_char = '-' slug = remove_formatting(value) - slug = strip_tags(slug) - - for c in """`?=[]\;'"<>,/~!@#$%^&*()+{}|: """: - slug = slug.replace(c, '-') - slug = recursive_replace(slug, '-') - slug = collapse(slug, '-') + slug = SLUG_BAD_CHAR_RE.sub(replacement_char, slug) + slug = collapse(slug, replacement_char) return slug