# HG changeset patch # User Thomas De Schampheleire # Date 2018-02-16 22:30:51 # Node ID e5a7f8f413703f4328e744d689613a537f9b5b24 # Parent 638ac4e653658173216906114e8fdf3960d80783 issues: backout special whitespace handling This is essentially a backout of commit 32e1e0745d3c. That commit checked for whitespace at the beginning of the matched issue reference, and explicitly retained it in the resulting link text. The way this was handled is not only suboptimal, e.g. a set of 4 spaces would still be reduced to 1, but is also not actually necessary: if whitespace before the issue reference is not required, then it does not need to be specified in the issue pattern, and if it _is_ required, then a positive lookbehind assertion can be used instead. diff --git a/kallithea/lib/helpers.py b/kallithea/lib/helpers.py --- a/kallithea/lib/helpers.py +++ b/kallithea/lib/helpers.py @@ -1156,17 +1156,15 @@ def urlify_issues(newtext, repo_name): def issues_replace(match_obj, issue_server_link=issue_server_link, issue_prefix=issue_prefix): - leadingspace = ' ' if match_obj.group().startswith(' ') else '' issue_id = ''.join(match_obj.groups()) issue_url = issue_server_link.replace('{id}', issue_id) issue_url = issue_url.replace('{repo}', repo_name) issue_url = issue_url.replace('{repo_name}', repo_name.split(URL_SEP)[-1]) return ( - '%(leadingspace)s' + '' '%(issue-prefix)s%(id-repr)s' '' ) % { - 'leadingspace': leadingspace, 'url': issue_url, 'id-repr': issue_id, 'issue-prefix': issue_prefix, diff --git a/kallithea/lib/paster_commands/template.ini.mako b/kallithea/lib/paster_commands/template.ini.mako --- a/kallithea/lib/paster_commands/template.ini.mako +++ b/kallithea/lib/paster_commands/template.ini.mako @@ -267,6 +267,9 @@ default_encoding = utf8 <%text>## pattern to get the issues from commit messages <%text>## default one used here is # with a regex passive group for `#` <%text>## {id} will be all groups matched from this pattern +<%text>## To require mandatory whitespace before the issue pattern, use: +<%text>## (?:^|(?<=\s)) before the actual pattern, and for mandatory whitespace +<%text>## behind the issue pattern, use (?:$|(?=\s)) after the actual pattern issue_pat = #(\d+) diff --git a/kallithea/tests/other/test_libs.py b/kallithea/tests/other/test_libs.py --- a/kallithea/tests/other/test_libs.py +++ b/kallithea/tests/other/test_libs.py @@ -411,22 +411,34 @@ class TestLibs(TestController): 'issue #123 and issue#456', """issue #123 and """ """issue#456"""), + # following test case shows the result of a backward incompatible change that was made: the + # space between 'issue' and '#123' is removed, because the space is part of the pattern. (r'(?:\s*#)(\d+)', 'http://foo/{repo}/issue/{id}', '#', 'issue #123 and issue#456', - """issue #123 and """ + """issue#123 and """ """issue#456"""), + # to require whitespace before the issue reference, one may be tempted to use \b... (r'\bPR(\d+)', 'http://foo/{repo}/issue/{id}', '#', 'issue PR123 and issuePR456', """issue #123 and """ """issuePR456"""), - # following test case shows that \b does not work well in combination with '#': the expectations + # ... but it turns out that \b does not work well in combination with '#': the expectations # are reversed from what is actually happening. (r'\b#(\d+)', 'http://foo/{repo}/issue/{id}', '#', 'issue #123 and issue#456', """issue #123 and """ """issue#456"""), - (r'[ \t]#(\d+)', 'http://foo/{repo}/issue/{id}', '#', - 'issue #123 and issue#456', + # ... so maybe try to be explicit? Unfortunately the whitespace before the issue + # reference is not retained, again, because it is part of the pattern. + (r'(?:^|\s)#(\d+)', 'http://foo/{repo}/issue/{id}', '#', + '#15 and issue #123 and issue#456', + """#15 and """ + """issue#123 and """ + """issue#456"""), + # ... instead, use lookbehind assertions. + (r'(?:^|(?<=\s))#(\d+)', 'http://foo/{repo}/issue/{id}', '#', + '#15 and issue #123 and issue#456', + """#15 and """ """issue #123 and """ """issue#456"""), (r'(?:pullrequest|pull request|PR|pr) ?#?(\d+)', 'http://foo/{repo}/issue/{id}', 'PR#',