Patchwork D2959: stringutil: add isauthorwellformed function

login
register
mail settings
Submitter phabricator
Date March 29, 2018, 2:10 p.m.
Message ID <72d94dee7067c441ca315fdbdea746b2@localhost.localdomain>
Download mbox | patch
Permalink /patch/29935/
State Not Applicable
Headers show

Comments

phabricator - March 29, 2018, 2:10 p.m.
sheehan updated this revision to Diff 7359.
sheehan marked 3 inline comments as done.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D2959?vs=7343&id=7359

REVISION DETAIL
  https://phab.mercurial-scm.org/D2959

AFFECTED FILES
  mercurial/utils/stringutil.py

CHANGE DETAILS




To: sheehan, #hg-reviewers, yuja
Cc: yuja, av6, mercurial-devel

Patch

diff --git a/mercurial/utils/stringutil.py b/mercurial/utils/stringutil.py
--- a/mercurial/utils/stringutil.py
+++ b/mercurial/utils/stringutil.py
@@ -286,3 +286,25 @@ 
     If s is not a valid boolean, returns None.
     """
     return _booleans.get(s.lower(), None)
+
+_correctauthorformat = remod.compile(br'^[^<]+\s\<[^<>]+@[^<>]+\>$')
+def isauthorwellformed(author):
+    '''Return True if the author field is well formed
+    (ie "Contributor Name <contrib@email.dom>")
+
+    >>> isauthorwellformed(b'Good Author <good@author.com>')
+    True
+    >>> isauthorwellformed(b'Author <good@author.com>')
+    True
+    >>> isauthorwellformed(b'Bad Author')
+    False
+    >>> isauthorwellformed(b'Bad Author <author@author.com')
+    False
+    >>> isauthorwellformed(b'Bad Author author@author.com')
+    False
+    >>> isauthorwellformed(b'<author@author.com>')
+    False
+    >>> isauthorwellformed(b'Bad Author <author>')
+    False
+    '''
+    return _correctauthorformat.match(author) is not None