changeset 10455:40dfd46d098f

ui: add HGPLAIN environment variable for easier scripting If HGPLAIN is set, the following settings are ignored when read from hgrc files: - ui.debug - ui.fallbackencoding - ui.quiet - ui.traceback - ui.verbose - defaults.* Localization is also disabled. Equivalent options set via command line are honored.
author Brodie Rao <me+hg@dackz.net>
date Sun, 07 Feb 2010 14:56:18 +0100
parents 37b735d27347
children 64a6a896e5fb 7fbba0c04f92
files mercurial/help/environment.txt mercurial/i18n.py mercurial/ui.py tests/test-hgrc tests/test-hgrc.out
diffstat 5 files changed, 56 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help/environment.txt	Sat Feb 13 23:24:27 2010 -0600
+++ b/mercurial/help/environment.txt	Sun Feb 07 14:56:18 2010 +0100
@@ -42,6 +42,16 @@
     - if it's a directory, all files ending with .rc are added
     - otherwise, the file itself will be added
 
+HGPLAIN
+    When set, this disables any options in .hgrc that might change
+    Mercurial's default output. This includes encoding, defaults,
+    verbose mode, debug mode, quiet mode, tracebacks, and
+    localization. This can be useful when scripting against Mercurial
+    in the face of existing user configuration.
+
+    Equivalent options set via command line flags or environment
+    variables are not overridden.
+
 HGUSER
     This is the string used as the author of a commit. If not set,
     available values will be considered in this order:
--- a/mercurial/i18n.py	Sat Feb 13 23:24:27 2010 -0600
+++ b/mercurial/i18n.py	Sun Feb 07 14:56:18 2010 +0100
@@ -48,5 +48,8 @@
         # An unknown encoding results in a LookupError.
         return message
 
-_ = gettext
+if 'HGPLAIN' in os.environ:
+    _ = lambda message: message
+else:
+    _ = gettext
 
--- a/mercurial/ui.py	Sat Feb 13 23:24:27 2010 -0600
+++ b/mercurial/ui.py	Sun Feb 07 14:56:18 2010 +0100
@@ -79,6 +79,14 @@
                 raise
             self.warn(_("Ignored: %s\n") % str(inst))
 
+        if self.plain():
+            for k in ('debug', 'fallbackencoding', 'quiet', 'traceback',
+                      'verbose'):
+                if k in cfg['ui']:
+                    del cfg['ui'][k]
+            for k, v in cfg.items('defaults'):
+                del cfg['defaults'][k]
+
         if trusted:
             self._tcfg.update(cfg)
             self._tcfg.update(self._ocfg)
@@ -169,6 +177,9 @@
             for name, value in self.configitems(section, untrusted):
                 yield section, name, str(value).replace('\n', '\\n')
 
+    def plain(self):
+        return 'HGPLAIN' in os.environ
+
     def username(self):
         """Return default username to be used in commits.
 
--- a/tests/test-hgrc	Sat Feb 13 23:24:27 2010 -0600
+++ b/tests/test-hgrc	Sun Feb 07 14:56:18 2010 +0100
@@ -25,3 +25,22 @@
 
 echo '%include /no-such-file' > $HGRCPATH
 hg version 2>&1 | sed -e "s|$HGRCPATH|\$HGRCPATH|"
+
+# HGPLAIN
+cd ..
+p=`pwd`
+echo "[ui]" > $HGRCPATH
+echo "debug=true" >> $HGRCPATH
+echo "fallbackencoding=ASCII" >> $HGRCPATH
+echo "quiet=true" >> $HGRCPATH
+echo "traceback=true" >> $HGRCPATH
+echo "verbose=true" >> $HGRCPATH
+echo "[defaults]" >> $HGRCPATH
+echo "identify=-n" >> $HGRCPATH
+
+echo '% customized hgrc'
+hg showconfig | sed -e "s:$p:...:"
+
+echo '% plain hgrc'
+HGPLAIN=; export HGPLAIN
+hg showconfig --config ui.traceback=True --debug | sed -e "s:$p:...:"
--- a/tests/test-hgrc.out	Sat Feb 13 23:24:27 2010 -0600
+++ b/tests/test-hgrc.out	Sun Feb 07 14:56:18 2010 +0100
@@ -10,3 +10,15 @@
 foo.bar=a\nb\nc\nde\nfg
 foo.baz=bif cb
 hg: config error at $HGRCPATH:1: cannot include /no-such-file (No such file or directory)
+% customized hgrc
+.../.hgrc:8: defaults.identify=-n
+.../.hgrc:2: ui.debug=true
+.../.hgrc:3: ui.fallbackencoding=ASCII
+.../.hgrc:4: ui.quiet=true
+.../.hgrc:5: ui.traceback=true
+.../.hgrc:6: ui.verbose=true
+% plain hgrc
+none: ui.traceback=True
+none: ui.verbose=False
+none: ui.debug=True
+none: ui.quiet=False