HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT
authorBrodie Rao <brodie@bitheap.org>
Wed, 05 Jan 2011 00:18:36 +1100
changeset 13849 9f97de157aad
parent 13848 b2798c1defff
child 13850 1209e1d52b68
HGPLAIN: allow exceptions to plain mode, like i18n, via HGPLAINEXCEPT This makes it possible to gain the benefits of HGPLAIN for scripting while preserving different behaviors like internationalization.
mercurial/help/environment.txt
mercurial/i18n.py
mercurial/ui.py
tests/test-hgrc.t
--- a/mercurial/help/environment.txt	Thu Mar 31 17:37:33 2011 -0700
+++ b/mercurial/help/environment.txt	Wed Jan 05 00:18:36 2011 +1100
@@ -59,6 +59,14 @@
     Equivalent options set via command line flags or environment
     variables are not overridden.
 
+HGPLAINEXCEPT
+    This is a comma-separated list of features to preserve when
+    HGPLAIN is enabled. Currently the only value supported is "i18n",
+    which preserves internationalization in plain mode.
+
+    Setting HGPLAINEXCEPT to anything (even an empty string) will
+    enable plain mode.
+
 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	Thu Mar 31 17:37:33 2011 -0700
+++ b/mercurial/i18n.py	Wed Jan 05 00:18:36 2011 +1100
@@ -51,7 +51,13 @@
         # An unknown encoding results in a LookupError.
         return message
 
-if 'HGPLAIN' in os.environ:
+def _plain():
+    if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
+        return False
+    exceptions = os.environ.get('HGPLAINEXCEPT', '').strip().split(',')
+    return 'i18n' not in exceptions
+
+if _plain():
     _ = lambda message: message
 else:
     _ = gettext
--- a/mercurial/ui.py	Thu Mar 31 17:37:33 2011 -0700
+++ b/mercurial/ui.py	Wed Jan 05 00:18:36 2011 +1100
@@ -278,15 +278,22 @@
     def plain(self):
         '''is plain mode active?
 
-        Plain mode means that all configuration variables which affect the
-        behavior and output of Mercurial should be ignored. Additionally, the
-        output should be stable, reproducible and suitable for use in scripts or
-        applications.
+        Plain mode means that all configuration variables which affect
+        the behavior and output of Mercurial should be
+        ignored. Additionally, the output should be stable,
+        reproducible and suitable for use in scripts or applications.
+
+        The only way to trigger plain mode is by setting either the
+        `HGPLAIN' or `HGPLAINEXCEPT' environment variables.
 
-        The only way to trigger plain mode is by setting the `HGPLAIN'
-        environment variable.
+        The return value can either be False, True, or a list of
+        features that plain mode should not apply to (e.g., i18n,
+        progress, etc).
         '''
-        return 'HGPLAIN' in os.environ
+        if 'HGPLAIN' not in os.environ and 'HGPLAINEXCEPT' not in os.environ:
+            return False
+        exceptions = os.environ.get('HGPLAINEXCEPT', '').strip().split(',')
+        return exceptions or True
 
     def username(self):
         """Return default username to be used in commits.
--- a/tests/test-hgrc.t	Thu Mar 31 17:37:33 2011 -0700
+++ b/tests/test-hgrc.t	Wed Jan 05 00:18:36 2011 +1100
@@ -133,3 +133,39 @@
   none: ui.verbose=False
   none: ui.debug=True
   none: ui.quiet=False
+
+plain mode with exceptions
+
+  $ cat > plain.py <<EOF
+  > def uisetup(ui):
+  >     ui.write('plain: %r\n' % ui.plain())
+  > EOF
+  $ echo "[extensions]" >> $HGRCPATH
+  $ echo "plain=./plain.py" >> $HGRCPATH
+  $ HGPLAINEXCEPT=; export HGPLAINEXCEPT
+  $ hg showconfig --config ui.traceback=True --debug
+  plain: ['']
+  read config from: $TESTTMP/hgrc
+  $TESTTMP/hgrc:15: extensions.plain=./plain.py
+  none: ui.traceback=True
+  none: ui.verbose=False
+  none: ui.debug=True
+  none: ui.quiet=False
+  $ unset HGPLAIN
+  $ hg showconfig --config ui.traceback=True --debug
+  plain: ['']
+  read config from: $TESTTMP/hgrc
+  $TESTTMP/hgrc:15: extensions.plain=./plain.py
+  none: ui.traceback=True
+  none: ui.verbose=False
+  none: ui.debug=True
+  none: ui.quiet=False
+  $ HGPLAINEXCEPT=i18n; export HGPLAINEXCEPT
+  $ hg showconfig --config ui.traceback=True --debug
+  plain: ['i18n']
+  read config from: $TESTTMP/hgrc
+  $TESTTMP/hgrc:15: extensions.plain=./plain.py
+  none: ui.traceback=True
+  none: ui.verbose=False
+  none: ui.debug=True
+  none: ui.quiet=False