Patchwork cmdutil: handle multiline translations of HG: messages safely

login
register
mail settings
Submitter timeless@mozdev.org
Date Sept. 30, 2015, 4:47 p.m.
Message ID <feeea04e2d0f6afc8f83.1443631644@waste.org>
Download mbox | patch
Permalink /patch/10706/
State Accepted
Headers show

Comments

timeless@mozdev.org - Sept. 30, 2015, 4:47 p.m.
# HG changeset patch
# User timeless@mozdev.org
# Date 1443630990 14400
#      Wed Sep 30 12:36:30 2015 -0400
# Node ID feeea04e2d0f6afc8f83655eb72df951ca2945b4
# Parent  46af0adb5c375cc51ce0d29cbdcd8ba843a33425
cmdutil: handle multiline translations of HG: messages safely

Before this, if a localizer/localization included a multiline
message, and didn't prefix the intermediate lines with 'HG: ',
then the line would be a candidate for inclusion in the commit
message -- which isn't ideal.
Matt Mackall - Sept. 30, 2015, 10:57 p.m.
On Wed, 2015-09-30 at 11:47 -0500, timeless@mozdev.org wrote:
> # HG changeset patch
> # User timeless@mozdev.org
> # Date 1443630990 14400
> #      Wed Sep 30 12:36:30 2015 -0400
> # Node ID feeea04e2d0f6afc8f83655eb72df951ca2945b4
> # Parent  46af0adb5c375cc51ce0d29cbdcd8ba843a33425
> cmdutil: handle multiline translations of HG: messages safely

Queued for default, thanks.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -2735,6 +2735,9 @@ 
     t.show(ctx, extramsg=extramsg)
     return ui.popbuffer()
 
+def hgprefix(msg):
+    return "\n".join(["HG: %s" % a for a in msg.split("\n") if a])
+
 def buildcommittext(repo, ctx, subs, extramsg):
     edittext = []
     modified, added, removed = ctx.modified(), ctx.added(), ctx.removed()
@@ -2742,23 +2745,23 @@ 
         edittext.append(ctx.description())
     edittext.append("")
     edittext.append("") # Empty line between message and comments.
-    edittext.append("HG: %s" % _("Enter commit message."
-                      "  Lines beginning with 'HG:' are removed."))
-    edittext.append("HG: %s" % extramsg)
+    edittext.append(hgprefix(_("Enter commit message."
+                      "  Lines beginning with 'HG:' are removed.")))
+    edittext.append(hgprefix(extramsg))
     edittext.append("HG: --")
-    edittext.append("HG: %s" % (_("user: %s") % ctx.user()))
+    edittext.append(hgprefix(_("user: %s") % ctx.user()))
     if ctx.p2():
-        edittext.append("HG: %s" % _("branch merge"))
+        edittext.append(hgprefix(_("branch merge")))
     if ctx.branch():
-        edittext.append("HG: %s" % (_("branch '%s'") % ctx.branch()))
+        edittext.append(hgprefix(_("branch '%s'") % ctx.branch()))
     if bookmarks.isactivewdirparent(repo):
-        edittext.append("HG: %s" % (_("bookmark '%s'") % repo._activebookmark))
-    edittext.extend(["HG: %s" % (_("subrepo %s") % s) for s in subs])
-    edittext.extend(["HG: %s" % (_("added %s") % f) for f in added])
-    edittext.extend(["HG: %s" % (_("changed %s") % f) for f in modified])
-    edittext.extend(["HG: %s" % (_("removed %s") % f) for f in removed])
+        edittext.append(hgprefix(_("bookmark '%s'") % repo._activebookmark))
+    edittext.extend([hgprefix(_("subrepo %s") % s) for s in subs])
+    edittext.extend([hgprefix(_("added %s") % f) for f in added])
+    edittext.extend([hgprefix(_("changed %s") % f) for f in modified])
+    edittext.extend([hgprefix(_("removed %s") % f) for f in removed])
     if not added and not modified and not removed:
-        edittext.append("HG: %s" % _("no files changed"))
+        edittext.append(hgprefix(_("no files changed")))
     edittext.append("")
 
     return "\n".join(edittext)