# HG changeset patch # User Connor Sheehan # Date 2018-03-22 13:48:22 # Node ID f8e1f48de118e71c4be26d7f9e8953f65979b60c # Parent f51c2780db3ae7df4a899f97b9816f85f675a527 stringutil: add isauthorwellformed function The regular expression for this function formerly lived at https://hg.mozilla.org/hgcustom/version-control-tools/file/tip/hghooks/mozhghooks/author_format.py#l13 Differential Revision: https://phab.mercurial-scm.org/D2959 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 @@ def email(author): 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 ") + + >>> isauthorwellformed(b'Good Author ') + True + >>> isauthorwellformed(b'Author ') + True + >>> isauthorwellformed(b'Bad Author') + False + >>> isauthorwellformed(b'Bad Author >> isauthorwellformed(b'Bad Author author@author.com') + False + >>> isauthorwellformed(b'') + False + >>> isauthorwellformed(b'Bad 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='...')