[PATCH] fancyopts: making config defaults actually override defaults

Rodrigo Damazio Bovendorp rdamazio at google.com
Sun Mar 12 02:03:30 UTC 2017


# HG changeset patch
# User Rodrigo Damazio <rdamazio at google.com>
# Date 1489274373 28800
#      Sat Mar 11 15:19:33 2017 -0800
# Node ID 8c833b81a994e2d3304c3b06793f536355528aab
# Parent  62939e0148f170b67ca8c7374f36c413b67fd387
fancyopts: making config defaults actually override defaults

This introduces the new defaults format "command.option" which directly
overrides the default of the option, instead of prepending the command-line
option.

diff -r 62939e0148f1 -r 8c833b81a994 mercurial/dispatch.py
--- a/mercurial/dispatch.py	Wed Mar 08 18:11:41 2017 -0500
+++ b/mercurial/dispatch.py	Sat Mar 11 15:19:33 2017 -0800
@@ -470,10 +470,29 @@
                                          ui.configbool("ui", "strict"))
         cmd = aliases[0]
         args = aliasargs(entry[0], args)
+
+        # override old-style defaults from config file by prepending command-line
+        # flags
         defaults = ui.config("defaults", cmd)
         if defaults:
-            args = map(util.expandpath, pycompat.shlexsplit(defaults)) + args
+            args = [util.expandpath(x) for x
+                    in pycompat.shlexsplit(defaults)] + args
         c = list(entry[1])
+
+        # override new-style defaults from config file by actually changing the
+        # option defaults.
+        for idx, opt in enumerate(c):
+            optname = opt[1]
+            shortname = opt[0]
+            defaulttype = type(opt[2])
+            rawdefault = ui.config("defaults", "%s.%s" % (cmd, optname)) or (
+                         ui.config("defaults", "%s.%s" % (cmd, shortname)))
+            if rawdefault:
+                # parse the new default using the type of the original default.
+                default = fancyopts.parsevalue(optname, rawdefault, defaulttype,
+                                               util.parsebool(rawdefault))
+                c[idx] = (opt[0], opt[1], default, opt[3])
+
     else:
         cmd = None
         c = []
diff -r 62939e0148f1 -r 8c833b81a994 mercurial/fancyopts.py
--- a/mercurial/fancyopts.py	Wed Mar 08 18:11:41 2017 -0500
+++ b/mercurial/fancyopts.py	Sat Mar 11 15:19:33 2017 -0800
@@ -142,18 +142,27 @@
         t = type(obj)
         if callable(obj):
             state[name] = defmap[name](val)
-        elif t is type(1):
-            try:
-                state[name] = int(val)
-            except ValueError:
-                raise error.Abort(_('invalid value %r for option %s, '
-                                   'expected int') % (val, opt))
-        elif t is type(''):
-            state[name] = val
         elif t is type([]):
             state[name].append(val)
-        elif t is type(None) or t is type(False):
-            state[name] = boolval
+        else:
+            # non-callable single value.
+            state[name] = parsevalue(name, val, t, boolval)
 
     # return unparsed args
     return args
+
+def parsevalue(name, val, typ, boolval=True):
+    if typ is type(1):
+        try:
+            return int(val)
+        except ValueError:
+            raise error.Abort(_('invalid value %r for option %s, '
+                                'expected int') % (val, name))
+    elif typ is type(''):
+        return val
+    elif (typ is type(None) or typ is type(False)) and (
+          type(boolval) is type(False)):
+        return boolval
+    else:
+        raise error.Abort(_('invalid value %r for option %s, '
+                            'unknown type') % (val, name))
diff -r 62939e0148f1 -r 8c833b81a994 tests/test-dispatch.t
--- a/tests/test-dispatch.t	Wed Mar 08 18:11:41 2017 -0500
+++ b/tests/test-dispatch.t	Sat Mar 11 15:19:33 2017 -0800
@@ -8,8 +8,10 @@
   $ hg -v log -v x
 
   $ echo a > a
+  $ echo b > b
   $ hg ci -Ama
   adding a
+  adding b
 
 Missing arg:
 
@@ -34,6 +36,7 @@
 
   $ hg cat a
   a
+  $ cp $HGRCPATH hgrc.bak
   $ cat >> $HGRCPATH <<EOF
   > [defaults]
   > cat = -r null
@@ -42,6 +45,54 @@
   a: no such file in rev 000000000000
   [1]
 
+new-style [defaults] and overrides
+
+  $ cp -f hgrc.bak $HGRCPATH
+  $ hg cat a
+  a
+  $ cat >> $HGRCPATH <<EOF
+  > [defaults]
+  > cat.r = null
+  > EOF
+  $ hg cat a
+  a: no such file in rev 000000000000
+  [1]
+
+  $ cp -f hgrc.bak $HGRCPATH
+  $ cat >> $HGRCPATH <<EOF
+  > [defaults]
+  > cat.rev = null
+  > EOF
+  $ hg cat a
+  a: no such file in rev 000000000000
+  [1]
+
+  $ mv -f hgrc.bak $HGRCPATH
+  $ echo foo >> a
+  $ hg rm b
+  $ echo bar > c
+  $ hg add c
+  $ hg status
+  M a
+  A c
+  R b
+  $ cat >> $HGRCPATH <<EOF
+  > [defaults]
+  > status.removed = 1
+  > EOF
+  $ hg status
+  R b
+  $ hg status --modified
+  M a
+  R b
+  $ hg status --modified --no-removed
+  M a
+  $ hg status --no-removed
+  M a
+  A c
+  R b
+  $ hg revert a b c
+
   $ cd "$TESTTMP"
 
 OSError "No such file or directory" / "The system cannot find the path


More information about the Mercurial-devel mailing list