[PATCH] commands: add editconfig command to edit repo/user hgrc

Brodie Rao brodie at bitheap.org
Sun Aug 22 15:33:12 CDT 2010


# HG changeset patch
# User Brodie Rao <brodie at bitheap.org>
# Date 1282508457 14400
# Node ID ca81962217fac8ff2cf9d2e2264a37450f02235b
# Parent  6b04f1e1aa8a81a87ac75e35377e3266f75eaf0c
commands: add editconfig command to edit repo/user hgrc

"hg editconfig" provides a happy medium between making the user edit
their hgrc 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. Otherwise, it loads the user's hgrc.
A -u/--user option is provided if the user wishes to edit the user hgrc
while inside a repo.

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 -r 6b04f1e1aa8a -r ca81962217fa mercurial/commands.py
--- a/mercurial/commands.py	Sun Aug 22 19:41:04 2010 +0200
+++ b/mercurial/commands.py	Sun Aug 22 16:20:57 2010 -0400
@@ -1082,6 +1082,33 @@ def showconfig(ui, repo, *values, **opts
                      ui.configsource(section, name, untrusted))
             ui.write('%s=%s\n' % (sectname, value))
 
+def editconfig(ui, repo, **opts):
+    """load hgrc in configured editor
+
+    With no arguments, the current repository's ``.hg/hgrc`` is
+    loaded in your configured editor. If no repository is found,
+    ``$HOME/.hgrc`` is loaded instead.
+
+    With --user, ``$HOME/.hgrc`` is loaded, even when inside
+    a repository.
+    """
+
+    if opts['user'] or not repo:
+        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]
+    else:
+        path = repo.join('hgrc')
+    editor = ui.geteditor()
+    util.system('%s "%s"' % (editor, path),
+                onerr=util.Abort, errprefix=_('edit failed'))
+
 def debugpushkey(ui, repopath, namespace, *keyinfo):
     '''access the pushkey key/value protocol
 
@@ -4154,6 +4181,10 @@ table = {
            _('change made by revision'), _('REV'))
          ] + diffopts + diffopts2 + walkopts,
          _('[OPTION]... ([-c REV] | [-r REV1 [-r REV2]]) [FILE]...')),
+    "editconfig":
+        (editconfig,
+         [('u', 'user', None, _('edit user hgrc'))],
+         _('[-u]')),
     "^export":
         (export,
          [('o', 'output', '',
@@ -4472,4 +4503,5 @@ table = {
 norepo = ("clone init version help debugcommands debugcomplete debugdata"
           " debugindex debugindexdot debugdate debuginstall debugfsinfo"
           " debugpushkey")
-optionalrepo = ("identify paths serve showconfig debugancestor debugdag")
+optionalrepo = ("identify paths serve showconfig debugancestor debugdag"
+                " editconfig")
diff -r 6b04f1e1aa8a -r ca81962217fa tests/test-hgrc.t
--- a/tests/test-hgrc.t	Sun Aug 22 19:41:04 2010 +0200
+++ b/tests/test-hgrc.t	Sun Aug 22 16:20:57 2010 -0400
@@ -108,3 +108,24 @@ 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
+  [foo]
+  $ hg editconfig -u
+  [foo]
+
+editconfig inside repo
+
+  $ hg init editconfig
+  $ cd editconfig
+  $ echo "[bar]" > .hg/hgrc
+  $ hg editconfig
+  [bar]
+  $ hg editconfig -u
+  [foo]
+  $ cd ..


More information about the Mercurial-devel mailing list