Patchwork D2959: stringutil: add isauthorwellformed function

login
register
mail settings
Submitter phabricator
Date March 30, 2018, 12:56 p.m.
Message ID <e1e802a5891e0af42427fe3894e542ec@localhost.localdomain>
Download mbox | patch
Permalink /patch/29956/
State Not Applicable
Headers show

Comments

phabricator - March 30, 2018, 12:56 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGf8e1f48de118: stringutil: add isauthorwellformed function (authored by sheehan, committed by ).

CHANGED PRIOR TO COMMIT
  https://phab.mercurial-scm.org/D2959?vs=7359&id=7379#toc

REPOSITORY
  rHG Mercurial

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

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
@@ -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='...')