histedit: use new function for getting first line of a string
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 24 Mar 2022 16:51:20 -0700
changeset 49022 e160f073c37b
parent 49021 51aed118f9dc
child 49023 7bdf19f892c6
histedit: use new function for getting first line of a string This fixes a crash you can run into if you enter a commit message that's just a "newline-like" byte, like a form feed byte (`hg ci -m \x0f` in Fish). That bug is the motivation for this series. Differential Revision: https://phab.mercurial-scm.org/D12405
hgext/histedit.py
--- a/hgext/histedit.py	Thu Mar 24 16:09:12 2022 -0700
+++ b/hgext/histedit.py	Thu Mar 24 16:51:20 2022 -0700
@@ -552,9 +552,7 @@
             summary = cmdutil.rendertemplate(
                 ctx, ui.config(b'histedit', b'summary-template')
             )
-        # Handle the fact that `''.splitlines() => []`
-        summary = summary.splitlines()[0] if summary else b''
-        line = b'%s %s %s' % (self.verb, ctx, summary)
+        line = b'%s %s %s' % (self.verb, ctx, stringutil.firstline(summary))
         # trim to 75 columns by default so it's not stupidly wide in my editor
         # (the 5 more are left for verb)
         maxlen = self.repo.ui.configint(b'histedit', b'linelen')
@@ -1192,7 +1190,7 @@
         # This is split off from the prefix property so that we can
         # separately make the description for 'roll' red (since it
         # will get discarded).
-        return self.ctx.description().splitlines()[0].strip()
+        return stringutil.firstline(self.ctx.description())
 
     def checkconflicts(self, other):
         if other.pos > self.pos and other.origpos <= self.origpos:
@@ -1291,7 +1289,7 @@
         line = b"bookmark:  %s" % b' '.join(bms)
         win.addstr(3, 1, line[:length])
 
-        line = b"summary:   %s" % (ctx.description().splitlines()[0])
+        line = b"summary:   %s" % stringutil.firstline(ctx.description())
         win.addstr(4, 1, line[:length])
 
         line = b"files:     "
@@ -2324,9 +2322,7 @@
     # a common pattern is to extract the summary but default to the empty
     # string
     summary = ctx.description() or b''
-    if summary:
-        summary = summary.splitlines()[0]
-    return summary
+    return stringutil.firstline(summary)
 
 
 def bootstrapcontinue(ui, state, opts):