Patchwork [2,of,2,V3] commitextras: check the format of the arguments and no internal key is used

login
register
mail settings
Submitter Pulkit Goyal
Date July 14, 2017, 8:01 p.m.
Message ID <2cbccf36af1bd0d9ae9d.1500062484@workspace>
Download mbox | patch
Permalink /patch/22374/
State Accepted
Headers show

Comments

Pulkit Goyal - July 14, 2017, 8:01 p.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1499856010 -19800
#      Wed Jul 12 16:10:10 2017 +0530
# Node ID 2cbccf36af1bd0d9ae9df1ad5fd4a7f8d870ae6c
# Parent  e51d188da49636884ae6c0df94f501e84436b857
# EXP-Topic fbext
commitextras: check the format of the arguments and no internal key is used

This patch adds check to make the arguments are passed as KEY=VALUE and no key
which is used internally is passed.

This patch also adds test for the extension.
Yuya Nishihara - July 17, 2017, 2:56 p.m.
On Sat, 15 Jul 2017 01:31:24 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1499856010 -19800
> #      Wed Jul 12 16:10:10 2017 +0530
> # Node ID 2cbccf36af1bd0d9ae9df1ad5fd4a7f8d870ae6c
> # Parent  e51d188da49636884ae6c0df94f501e84436b857
> # EXP-Topic fbext
> commitextras: check the format of the arguments and no internal key is used

> +usedinternally = set(['amend_source', 'branch', 'histedit_source', 'topic',
> +                    'rebase_source', 'intermediate-source', '__touch-noise__',
> +                    'source', 'transplant_source'])

Updated to a set literal. I found one more internal key, 'close', so added it
in flight.

>  def extsetup(ui):
>      entry = extensions.wrapcommand(commands.table, 'commit', _commit)
>      options = entry[1]
> @@ -33,7 +38,15 @@
>              extras = opts.get('extra')
>              if extras:
>                  for raw in extras:
> +                    if '=' not in raw:
> +                        msg = _("unable to parse '%s', should follow "
> +                                "KEY=VALUE format")
> +                        raise error.Abort(msg % raw)
>                      k, v = raw.split('=', 1)
> +                    if k in usedinternally:
> +                        msg = _("key '%s' is used internally, can't be set "
> +                                "manually")
> +                        raise error.Abort(msg % k)

Perhaps it's better to restrict k to non-empty ASCII word.

Patch

diff --git a/hgext/commitextras.py b/hgext/commitextras.py
--- a/hgext/commitextras.py
+++ b/hgext/commitextras.py
@@ -12,6 +12,7 @@ 
 from mercurial.i18n import _
 from mercurial import (
     commands,
+    error,
     extensions,
     registrar,
 )
@@ -20,6 +21,10 @@ 
 command = registrar.command(cmdtable)
 testedwith = 'ships-with-hg-core'
 
+usedinternally = set(['amend_source', 'branch', 'histedit_source', 'topic',
+                    'rebase_source', 'intermediate-source', '__touch-noise__',
+                    'source', 'transplant_source'])
+
 def extsetup(ui):
     entry = extensions.wrapcommand(commands.table, 'commit', _commit)
     options = entry[1]
@@ -33,7 +38,15 @@ 
             extras = opts.get('extra')
             if extras:
                 for raw in extras:
+                    if '=' not in raw:
+                        msg = _("unable to parse '%s', should follow "
+                                "KEY=VALUE format")
+                        raise error.Abort(msg % raw)
                     k, v = raw.split('=', 1)
+                    if k in usedinternally:
+                        msg = _("key '%s' is used internally, can't be set "
+                                "manually")
+                        raise error.Abort(msg % k)
                     inneropts['extra'][k] = v
             return origcommit(*innerpats, **inneropts)
 
diff --git a/tests/test-commit.t b/tests/test-commit.t
--- a/tests/test-commit.t
+++ b/tests/test-commit.t
@@ -124,6 +124,24 @@ 
   $ hg tip --template '{date|isodate}\n' | grep '1970'
   [1]
 
+Using the advanced --extra flag
+
+  $ echo "[extensions]" >> $HGRCPATH
+  $ echo "commitextras=" >> $HGRCPATH
+  $ hg status
+  ? baz
+  ? quux
+  $ hg add baz
+  $ hg commit -m "adding extras" --extra sourcehash=foo --extra oldhash=bar
+  $ hg log -r . -T '{extras % "{extra}\n"}'
+  branch=default
+  oldhash=bar
+  sourcehash=foo
+  $ hg add quux
+  $ hg commit -m "adding internal used extras" --extra amend_source=hash
+  abort: key 'amend_source' is used internally, can't be set manually
+  [255]
+
 Make sure we do not obscure unknown requires file entries (issue2649)
 
   $ echo foo >> foo