changeset 7229:e5a7f8f41370

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.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Fri, 16 Feb 2018 22:30:51 +0100
parents 638ac4e65365
children d24051ce961c
files kallithea/lib/helpers.py kallithea/lib/paster_commands/template.ini.mako kallithea/tests/other/test_libs.py
diffstat 3 files changed, 20 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/helpers.py	Sat Feb 24 21:18:24 2018 +0100
+++ b/kallithea/lib/helpers.py	Fri Feb 16 22:30:51 2018 +0100
@@ -1156,17 +1156,15 @@
 
             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<a class="issue-tracker-link" href="%(url)s">'
+                    '<a class="issue-tracker-link" href="%(url)s">'
                     '%(issue-prefix)s%(id-repr)s'
                     '</a>'
                     ) % {
-                     'leadingspace': leadingspace,
                      'url': issue_url,
                      'id-repr': issue_id,
                      'issue-prefix': issue_prefix,
--- a/kallithea/lib/paster_commands/template.ini.mako	Sat Feb 24 21:18:24 2018 +0100
+++ b/kallithea/lib/paster_commands/template.ini.mako	Fri Feb 16 22:30:51 2018 +0100
@@ -267,6 +267,9 @@
 <%text>## pattern to get the issues from commit messages</%text>
 <%text>## default one used here is #<numbers> with a regex passive group for `#`</%text>
 <%text>## {id} will be all groups matched from this pattern</%text>
+<%text>## To require mandatory whitespace before the issue pattern, use:</%text>
+<%text>## (?:^|(?<=\s)) before the actual pattern, and for mandatory whitespace</%text>
+<%text>## behind the issue pattern, use (?:$|(?=\s)) after the actual pattern</%text>
 
 issue_pat = #(\d+)
 
--- a/kallithea/tests/other/test_libs.py	Sat Feb 24 21:18:24 2018 +0100
+++ b/kallithea/tests/other/test_libs.py	Fri Feb 16 22:30:51 2018 +0100
@@ -411,22 +411,34 @@
             'issue #123 and issue#456',
             """issue <a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a> and """
             """issue<a class="issue-tracker-link" href="http://foo/repo_name/issue/456">#456</a>"""),
+        # 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 <a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a> and """
+            """issue<a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a> and """
             """issue<a class="issue-tracker-link" href="http://foo/repo_name/issue/456">#456</a>"""),
+        # 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 <a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a> 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<a class="issue-tracker-link" href="http://foo/repo_name/issue/456">#456</a>"""),
-        (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',
+            """<a class="issue-tracker-link" href="http://foo/repo_name/issue/15">#15</a> and """
+            """issue<a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a> and """
+            """issue#456"""),
+        # ... instead, use lookbehind assertions.
+        (r'(?:^|(?<=\s))#(\d+)', 'http://foo/{repo}/issue/{id}', '#',
+            '#15 and issue #123 and issue#456',
+            """<a class="issue-tracker-link" href="http://foo/repo_name/issue/15">#15</a> and """
             """issue <a class="issue-tracker-link" href="http://foo/repo_name/issue/123">#123</a> and """
             """issue#456"""),
         (r'(?:pullrequest|pull request|PR|pr) ?#?(\d+)', 'http://foo/{repo}/issue/{id}', 'PR#',