# HG changeset patch # User Martin Bornhold # Date 2016-09-22 13:33:11 # Node ID 8555dd6b100e6197ceb9cacb1a964745bc060b12 # Parent 3895c836194393760f8c992ae5ce6cfab5554591 shadow: Use only a single regular expression to generate and match repo/group slugs. diff --git a/rhodecode/lib/middleware/simplevcs.py b/rhodecode/lib/middleware/simplevcs.py --- 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 --- a/rhodecode/lib/utils.py +++ b/rhodecode/lib/utils.py @@ -58,26 +58,16 @@ log = logging.getLogger(__name__) REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*') -_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 +# 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)) - 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) +_license_cache = None def repo_name_slug(value): @@ -86,14 +76,11 @@ def repo_name_slug(value): 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