##// END OF EJS Templates
shadow: Use only a single regular expression to generate and match repo/group slugs.
Martin Bornhold -
r901:8555dd6b default
parent child Browse files
Show More
@@ -112,14 +112,13 b' class SimpleVCS(object):'
112 vcs_repo_name and is_shadow_repo on the current instance.
112 vcs_repo_name and is_shadow_repo on the current instance.
113 """
113 """
114 # TODO: martinb: Move to class or module scope.
114 # TODO: martinb: Move to class or module scope.
115 # TODO: martinb: Check if we have to use re.UNICODE.
115 from rhodecode.lib.utils import SLUG_RE
116 # TODO: martinb: Check which chars are allowed for repo/group names.
117 # These chars are excluded: '`?=[]\;\'"<>,/~!@#$%^&*()+{}|: '
118 # Code from: rhodecode/lib/utils.py:repo_name_slug()
119 pr_regex = re.compile(
116 pr_regex = re.compile(
120 '(?P<base_name>(?:[\w-]+)(?:/[\w-]+)*)/' # repo groups
117 '(?P<groups>(?:{slug_pat})(?:/{slug_pat})*)' # repo groups
121 '(?P<repo_name>[\w-]+)' # target repo name
118 '/(?P<target>{slug_pat})' # target repo
122 '/pull-request/(?P<pr_id>\d+)/repository') # pr suffix
119 '/pull-request/(?P<pr_id>\d+)' # pull request
120 '/repository$' # shadow repo
121 .format(slug_pat=SLUG_RE.pattern))
123
122
124 # Get url repo name from environment.
123 # Get url repo name from environment.
125 self.url_repo_name = self._get_repository_name(environ)
124 self.url_repo_name = self._get_repository_name(environ)
@@ -58,26 +58,16 b' log = logging.getLogger(__name__)'
58
58
59 REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*')
59 REMOVED_REPO_PAT = re.compile(r'rm__\d{8}_\d{6}_\d{6}__.*')
60
60
61 _license_cache = None
61 # String of characters which are not allowed in repo/group slugs.
62
62 SLUG_BAD_CHARS = re.escape('`?=[]\;\'"<>,/~!@#$%^&*()+{}|:')
63
63 # Regex that matches forbidden characters in repo/group slugs.
64 def recursive_replace(str_, replace=' '):
64 SLUG_BAD_CHAR_RE = re.compile('[{}]'.format(SLUG_BAD_CHARS))
65 """
65 # Regex that matches allowed characters in repo/group slugs.
66 Recursive replace of given sign to just one instance
66 SLUG_GOOD_CHAR_RE = re.compile('[^{}]'.format(SLUG_BAD_CHARS))
67
67 # Regex that matches whole repo/group slugs.
68 :param str_: given string
68 SLUG_RE = re.compile('[^{}]+'.format(SLUG_BAD_CHARS))
69 :param replace: char to find and replace multiple instances
70
69
71 Examples::
70 _license_cache = None
72 >>> recursive_replace("Mighty---Mighty-Bo--sstones",'-')
73 'Mighty-Mighty-Bo-sstones'
74 """
75
76 if str_.find(replace * 2) == -1:
77 return str_
78 else:
79 str_ = str_.replace(replace * 2, replace)
80 return recursive_replace(str_, replace)
81
71
82
72
83 def repo_name_slug(value):
73 def repo_name_slug(value):
@@ -86,14 +76,11 b' def repo_name_slug(value):'
86 This function is called on each creation/modification
76 This function is called on each creation/modification
87 of repository to prevent bad names in repo
77 of repository to prevent bad names in repo
88 """
78 """
79 replacement_char = '-'
89
80
90 slug = remove_formatting(value)
81 slug = remove_formatting(value)
91 slug = strip_tags(slug)
82 slug = SLUG_BAD_CHAR_RE.sub(replacement_char, slug)
92
83 slug = collapse(slug, replacement_char)
93 for c in """`?=[]\;'"<>,/~!@#$%^&*()+{}|: """:
94 slug = slug.replace(c, '-')
95 slug = recursive_replace(slug, '-')
96 slug = collapse(slug, '-')
97 return slug
84 return slug
98
85
99
86
General Comments 0
You need to be logged in to leave comments. Login now