mercurial/utils/stringutil.py
changeset 37154 f8e1f48de118
parent 37083 f99d64e8a4e4
child 37155 fb7140f1d09d
--- a/mercurial/utils/stringutil.py	Sat Mar 17 02:37:46 2018 -0400
+++ b/mercurial/utils/stringutil.py	Thu Mar 22 09:48:22 2018 -0400
@@ -131,6 +131,29 @@
         r = None
     return author[author.find('<') + 1:r]
 
+_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
+
 def ellipsis(text, maxlength=400):
     """Trim string to at most maxlength (default: 400) columns in display."""
     return encoding.trim(text, maxlength, ellipsis='...')