[PATCH 2 of 2 v3] commands: add editconfig command to edit repo/user config

Brodie Rao brodie at bitheap.org
Wed Sep 22 20:31:34 CDT 2010


# HG changeset patch
# User Brodie Rao <brodie at bitheap.org>
# Date 1285205392 18000
# Node ID 8a156b68be591a89654729c64bc5eb87e7caae4d
# Parent  0bf68f8654746f151617e6179e3b273b08f5f211
commands: add editconfig command to edit repo/user config

"hg editconfig" provides a happy medium between making the user edit
their config manually and providing a command to edit specific
settings.

Without any arguments, the command will load the repo's hgrc in an
editor if the user is inside a repo. When run outside a repo, it
aborts. To edit the user config, a -u/--user option is provided.

If there's no user or repo config, editconfig will provide configs
with commented out examples.

This also has the added benefit of working the same across platforms,
instead of requiring the user to hunt down their user hgrc in
different places depending on whether they're on win32 or posix.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1059,6 +1059,8 @@ def showconfig(ui, repo, *values, **opts
     With --debug, the source (filename and line number) is printed
     for each config item.
 
+    To change config settings, see :hg:`help editconfig`.
+
     Returns 0 on success.
     """
 
@@ -1085,6 +1087,81 @@ def showconfig(ui, repo, *values, **opts
                      ui.configsource(section, name, untrusted))
             ui.write('%s=%s\n' % (sectname, value))
 
+def editconfig(ui, repo, **opts):
+    """load configuration file in configured editor
+
+    With no arguments, the current repository's ``.hg/hgrc`` is
+    loaded in your configured editor.
+
+    With --user, your user configuration file is loaded.
+    """
+
+    def writeconfig(path, example):
+        if example == 'user':
+            s = _(
+"""# This is an example config file. Uncomment settings to enable them.
+# See "hg help config" or hgrc(5) for more information.
+
+#[ui]
+#username = Firstname Lastname <firstname.lastname at example.net>
+
+#[extensions]
+# colorizes console output
+#color =
+
+# provides glog command to display a graph with log output
+#graphlog =
+
+# enables progress bars
+#progress =
+""")
+        elif example == 'repo':
+            s = _(
+"""# This is an example config file. Uncomment settings to enable them.
+# See "hg help config" or hgrc(5) for more information.
+
+#[paths]
+# default directory or URL to pull from/push to
+#default = http://...
+
+# "hg push/pull another-repo" will use this directory/URL
+#another-repo = ../another-repo
+""")
+        try:
+            f = open(path, 'w')
+            f.write(s)
+            f.close()
+        except IOError:
+            ui.warn(_('failed to write example config to %s') % path)
+
+    # Extensions adding new options can pass path=... for their own
+    # configs
+    path = opts.get('path')
+    if not path:
+        if opts['user']:
+            paths = util.user_rcpath()
+            # There can be multiple user hgrcs on Windows, so we look
+            # for the first one that exists. Otherwise, we choose the
+            # first in the list.
+            for p in paths:
+                if os.path.exists(p):
+                    path = p
+                    break
+            else:
+                path = paths[0]
+
+            if not os.path.exists(path):
+                writeconfig(path, 'user')
+        elif repo:
+            path = repo.join('hgrc')
+            if not os.path.exists(path):
+                writeconfig(path, 'repo')
+        else:
+            raise util.Abort(_("There is no Mercurial repository here "
+                               "(.hg not found)"))
+
+    ui.editfile(path)
+
 def debugpushkey(ui, repopath, namespace, *keyinfo):
     '''access the pushkey key/value protocol
 
@@ -4126,6 +4203,10 @@ table = {
            _('change made by revision'), _('REV'))
          ] + diffopts + diffopts2 + walkopts + subrepoopts,
          _('[OPTION]... ([-c REV] | [-r REV1 [-r REV2]]) [FILE]...')),
+    "editconfig":
+        (editconfig,
+         [('u', 'user', None, _('edit user hgrc'))],
+         _('[-u]')),
     "^export":
         (export,
          [('o', 'output', '',
@@ -4443,5 +4524,5 @@ table = {
 
 norepo = ("clone init version help debugcommands debugcomplete"
           " debugdate debuginstall debugfsinfo debugpushkey")
-optionalrepo = ("identify paths serve showconfig debugancestor debugdag"
-                " debugdata debugindex debugindexdot")
+optionalrepo = ("identify paths serve showconfig editconfig debugancestor"
+                " debugdag debugdata debugindex debugindexdot")
diff --git a/tests/test-debugcomplete.t b/tests/test-debugcomplete.t
--- a/tests/test-debugcomplete.t
+++ b/tests/test-debugcomplete.t
@@ -14,6 +14,7 @@ Show all commands except debug commands
   commit
   copy
   diff
+  editconfig
   export
   forget
   grep
@@ -223,6 +224,7 @@ Show all commands + options
   debugstate: nodates
   debugsub: rev
   debugwalk: include, exclude
+  editconfig: user
   grep: print0, all, follow, ignore-case, files-with-matches, line-number, rev, user, date, include, exclude
   heads: rev, topo, active, closed, style, template
   help: 
diff --git a/tests/test-globalopts.t b/tests/test-globalopts.t
--- a/tests/test-globalopts.t
+++ b/tests/test-globalopts.t
@@ -268,6 +268,7 @@ Testing -h/--help:
    commit       commit the specified files or all outstanding changes
    copy         mark files as copied for the next commit
    diff         diff repository (or selected files)
+   editconfig   load configuration file in configured editor
    export       dump the header and diffs for one or more changesets
    forget       forget the specified files on the next commit
    grep         search for a pattern in specified files and revisions
@@ -342,6 +343,7 @@ Testing -h/--help:
    commit       commit the specified files or all outstanding changes
    copy         mark files as copied for the next commit
    diff         diff repository (or selected files)
+   editconfig   load configuration file in configured editor
    export       dump the header and diffs for one or more changesets
    forget       forget the specified files on the next commit
    grep         search for a pattern in specified files and revisions
diff --git a/tests/test-help.t b/tests/test-help.t
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -63,6 +63,7 @@ Short help:
    commit       commit the specified files or all outstanding changes
    copy         mark files as copied for the next commit
    diff         diff repository (or selected files)
+   editconfig   load configuration file in configured editor
    export       dump the header and diffs for one or more changesets
    forget       forget the specified files on the next commit
    grep         search for a pattern in specified files and revisions
@@ -133,6 +134,7 @@ Short help:
    commit       commit the specified files or all outstanding changes
    copy         mark files as copied for the next commit
    diff         diff repository (or selected files)
+   editconfig   load configuration file in configured editor
    export       dump the header and diffs for one or more changesets
    forget       forget the specified files on the next commit
    grep         search for a pattern in specified files and revisions
@@ -649,6 +651,7 @@ Test that default list of commands omits
    commit       commit the specified files or all outstanding changes
    copy         mark files as copied for the next commit
    diff         diff repository (or selected files)
+   editconfig   load configuration file in configured editor
    export       dump the header and diffs for one or more changesets
    forget       forget the specified files on the next commit
    grep         search for a pattern in specified files and revisions
diff --git a/tests/test-hgrc.t b/tests/test-hgrc.t
--- a/tests/test-hgrc.t
+++ b/tests/test-hgrc.t
@@ -111,3 +111,66 @@ plain hgrc
   none: ui.verbose=False
   none: ui.debug=True
   none: ui.quiet=False
+
+editconfig outside repo
+
+  $ HGEDITOR=cat; export HGEDITOR
+  $ HOME=`pwd`; export HOME
+  $ echo "[foo]" > $HGRCPATH
+  $ hg editconfig
+  abort: There is no Mercurial repository here (.hg not found)
+  [255]
+  $ hg editconfig -u
+  [foo]
+
+editconfig inside repo
+
+  $ hg init editconfig
+  $ cd editconfig
+  $ echo "[bar]" > .hg/hgrc
+  $ hg editconfig
+  [bar]
+  $ hg editconfig -u
+  [foo]
+
+editconfig with -R
+
+  $ cd ..
+  $ hg -R editconfig editconfig
+  [bar]
+  $ hg -R invalidrepo editconfig
+  abort: There is no Mercurial repository here (.hg not found)
+  [255]
+
+editconfig with example configs
+
+  $ cd editconfig
+  $ rm .hg/hgrc
+  $ hg editconfig
+  # This is an example config file. Uncomment settings to enable them.
+  # See "hg help config" or hgrc(5) for more information.
+  
+  #[paths]
+  # default directory or URL to pull from/push to
+  #default = http://...
+  
+  # "hg push/pull another-repo" will use this directory/URL
+  #another-repo = ../another-repo
+  $ rm "$HGRCPATH"
+  $ hg editconfig -u
+  # This is an example config file. Uncomment settings to enable them.
+  # See "hg help config" or hgrc(5) for more information.
+  
+  #[ui]
+  #username = Firstname Lastname <firstname.lastname at example.net>
+  
+  #[extensions]
+  # colorizes console output
+  #color =
+  
+  # provides glog command to display a graph with log output
+  #graphlog =
+  
+  # enables progress bars
+  #progress =
+  $ cd ..


More information about the Mercurial-devel mailing list