diff mercurial/utils/stringutil.py @ 49086:51aed118f9dc

templates: extract function to `stringutil` for getting first line of text It's surprisingly hard to get the first line from a string, so let's have our own function in `stringutil` for it. Differential Revision: https://phab.mercurial-scm.org/D12404
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 24 Mar 2022 16:09:12 -0700
parents 642e31cb55f0
children 75794847ef62
line wrap: on
line diff
--- a/mercurial/utils/stringutil.py	Thu Mar 24 15:41:29 2022 -0700
+++ b/mercurial/utils/stringutil.py	Thu Mar 24 16:09:12 2022 -0700
@@ -685,6 +685,14 @@
     return _correctauthorformat.match(author) is not None
 
 
+def firstline(text):
+    """Return the first line of the input"""
+    try:
+        return text.splitlines()[0]
+    except IndexError:
+        return b''
+
+
 def ellipsis(text, maxlength=400):
     """Trim string to at most maxlength (default: 400) columns in display."""
     return encoding.trim(text, maxlength, ellipsis=b'...')