[PATCH v2] commands: add editconfig command to edit repo/user hgrc
Brodie Rao
brodie at bitheap.org
Mon Aug 23 20:56:58 UTC 2010
# HG changeset patch
# User Brodie Rao <brodie at bitheap.org>
# Date 1282591581 14400
# Node ID 6d8b574052369e3a88c37a37a51b0e940bd6ac45
# Parent 2315a95ee88759ad2ed250dcbf2fc93be4073688
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.
If there's no user or repo hgrc, editconfig will provided 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 -r 2315a95ee887 -r 6d8b57405236 mercurial/commands.py
--- a/mercurial/commands.py Mon Aug 23 13:28:04 2010 +0200
+++ b/mercurial/commands.py Mon Aug 23 15:26:21 2010 -0400
@@ -1082,6 +1082,65 @@ 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.
+ """
+
+ def writeconfig(path, s):
+ try:
+ f = open(path, 'w')
+ f.write(s)
+ f.close()
+ except IOError:
+ ui.warn(_('failed to write example config to %s') % path)
+
+ 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]
+
+ if not os.path.exists(path):
+ writeconfig(path, _(
+"""; This is an example ~/.hgrc 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]
+;color = ; colorizes console output
+;graphlog = ; provides glog command to display a graph with log output
+;progress = ; enables progress bars
+"""))
+ else:
+ path = repo.join('hgrc')
+ if not os.path.exists(path):
+ writeconfig(path, _(
+"""; This is an example .hg/hgrc file. Uncomment settings to enable them.
+; See "hg help config" or hgrc(5) for more information.
+;
+;[paths]
+;default = http://... ; default directory or URL to pull from/push to
+;foo = ../foo ; "hg push/pull foo" will use this directory/URL
+"""))
+
+ 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 +4213,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 +4535,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 2315a95ee887 -r 6d8b57405236 tests/test-hgrc.t
--- a/tests/test-hgrc.t Mon Aug 23 13:28:04 2010 +0200
+++ b/tests/test-hgrc.t Mon Aug 23 15:26:21 2010 -0400
@@ -108,3 +108,47 @@ 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]
+
+editconfig with example configs
+
+ $ rm .hg/hgrc
+ $ hg editconfig
+ ; This is an example .hg/hgrc file. Uncomment settings to enable them.
+ ; See "hg help config" or hgrc(5) for more information.
+ ;
+ ;[paths]
+ ;default = http://... ; default directory or URL to pull from/push to
+ ;foo = ../foo ; "hg push/pull foo" will use this directory/URL
+ $ rm "$HGRCPATH"
+ $ hg editconfig -u
+ ; This is an example ~/.hgrc 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]
+ ;color = ; colorizes console output
+ ;graphlog = ; provides glog command to display a graph with log output
+ ;progress = ; enables progress bars
+ $ cd ..
More information about the Mercurial-devel
mailing list