Bug 450760 - bug linking is too aggressive for numbers in the middle of a word, r=ted
authorBenjamin Smedberg <benjamin@smedbergs.us>
Fri, 15 Aug 2008 16:50:01 -0400
changeset 43 c57e026c9e22ff660a1e253468c8a81a88811ac2
parent 42 1650131bc37ee2996843a0e46a79e77d4192562c
child 44 1f5164ca184af7ccb0c6c06459d176f3c73973b1
push id17
push userbsmedberg@mozilla.com
push dateFri, 15 Aug 2008 20:50:19 +0000
reviewersted
bugs450760
Bug 450760 - bug linking is too aggressive for numbers in the middle of a word, r=ted
buglink.py
--- a/buglink.py
+++ b/buglink.py
@@ -2,14 +2,56 @@
 #
 # [extensions]
 # buglink = /path/to/buglink.py
 
 import re
 from mercurial import templatefilters
 
 bugzilla = r'<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=\2">\1</a>'
-bug_re = re.compile(r'((?:bug|b=|(?=#?\d{4,}))(?:\s*#?)(\d+))', re.I)
+bug_re = re.compile(r'''# bug followed by any sequence of numbers, or
+                        # a standalone sequence of numbers
+                     (
+                        (?:
+                          bug |
+                          b= |
+                          # a sequence of 5+ numbers preceded by whitespace
+                          (?=\b\#?\d{5,}) |
+                          # numbers at the very beginning
+                          ^(?=\d)
+                        )
+                        (?:\s*\#?)(\d+)
+                     )''', re.I | re.X)
 
 def buglink(x):
     return bug_re.sub(bugzilla, x)
 
 templatefilters.filters["buglink"] = buglink
+
+if __name__ == '__main__':
+    import unittest
+    
+    _tests = (
+        ('bug 1', ('', 'bug 1', '1', '')),
+        ('bug 123456', ('', 'bug 123456', '123456', '')),
+        ('testb=1234x', ('test', 'b=1234', '1234', 'x')),
+        ('ab4665521e2f', None),
+        ('Aug 2008', None),
+        ('b=#12345', ('', 'b=#12345', '12345', '')),
+        ('GECKO_191a2_20080815_RELBRANCH', None),
+        ('12345 is a bug', ('', '12345', '12345', ' is a bug')),
+        (' 123456 whitespace!', (' ', '123456', '123456', ' whitespace!')),
+        )
+
+    class TestBugRe(unittest.TestCase):
+        def testreplacements(self):
+            for str, result in _tests:
+                if result is None:
+                    resulttext = str
+                else:
+                    pretext, text, bugid, posttext = result
+                    resulttext = '%s<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=%s">%s</a>%s' % (pretext, bugid, text, posttext)
+
+                self.assertEqual(resulttext, buglink(str))
+
+    
+
+    unittest.main()