Patchwork D9241: config: move message about leading spaces in config to config.py

login
register
mail settings
Submitter phabricator
Date Oct. 22, 2020, 6:18 p.m.
Message ID <differential-rev-PHID-DREV-pz6nydg56rdxchsa2lqg-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47508/
State Superseded
Headers show

Comments

phabricator - Oct. 22, 2020, 6:18 p.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  When the config parser raises a ParseError, it uses the line that
  failed to parse as the error message. It doesn't currently tell the
  user anything about why it failed to parse. b13b99d39a46 <https://phab.mercurial-scm.org/rHGb13b99d39a465df4843048484e9bd0bab0433a0c> (config:
  highlight parse error caused by leading spaces (issue3214),
  2014-03-16) added a checked based on the error *message* having
  leading spaces. That has worked fine because only the config parser
  uses the line itself as error message (I think the revset and fileset
  parsers use more user-friendly proper messages). It still feels like a
  hack. Let's make the config parser give a useful message about leading
  whitespace instead. We should ideally follow up with more useful
  messages for other parse errors in config files.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9241

AFFECTED FILES
  mercurial/config.py
  mercurial/dispatch.py
  tests/test-config.t
  tests/test-hgrc.t

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-hgrc.t b/tests/test-hgrc.t
--- a/tests/test-hgrc.t
+++ b/tests/test-hgrc.t
@@ -68,8 +68,7 @@ 
   $ echo '[foo]' > $HGRC
   $ echo '  x = y' >> $HGRC
   $ hg version
-  hg: parse error at $TESTTMP/hgrc:2:   x = y
-  unexpected leading whitespace
+  hg: parse error at $TESTTMP/hgrc:2: unexpected leading whitespace:   x = y
   [255]
 
   $ "$PYTHON" -c "from __future__ import print_function; print('[foo]\nbar = a\n b\n c \n  de\n fg \nbaz = bif cb \n')" \
diff --git a/tests/test-config.t b/tests/test-config.t
--- a/tests/test-config.t
+++ b/tests/test-config.t
@@ -25,8 +25,7 @@ 
   >  key=value
   > EOF
   $ hg showconfig
-  hg: parse error at $TESTTMP/.hg/hgrc:1:  key=value
-  unexpected leading whitespace
+  hg: parse error at $TESTTMP/.hg/hgrc:1: unexpected leading whitespace:  key=value
   [255]
 
   $ cat > .hg/hgrc << EOF
@@ -34,8 +33,7 @@ 
   > key=value
   > EOF
   $ hg showconfig
-  hg: parse error at $TESTTMP/.hg/hgrc:1:  [section]
-  unexpected leading whitespace
+  hg: parse error at $TESTTMP/.hg/hgrc:1: unexpected leading whitespace:  [section]
   [255]
 
 Reset hgrc
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -248,8 +248,6 @@ 
             _(b"hg: parse error at %s: %s\n")
             % (pycompat.bytestr(inst.location), inst.message)
         )
-        if inst.message.startswith(b' '):
-            write(_(b"unexpected leading whitespace\n"))
     else:
         write(_(b"hg: parse error: %s\n") % inst.message)
         _reportsimilar(write, similar)
diff --git a/mercurial/config.py b/mercurial/config.py
--- a/mercurial/config.py
+++ b/mercurial/config.py
@@ -200,7 +200,10 @@ 
                 self._unset.append((section, name))
                 continue
 
-            raise error.ParseError(l.rstrip(), (b"%s:%d" % (src, line)))
+            message = l.rstrip()
+            if l.startswith(b' '):
+                message = b"unexpected leading whitespace: %s" % message
+            raise error.ParseError(message, (b"%s:%d" % (src, line)))
 
     def read(self, path, fp=None, sections=None, remap=None):
         if not fp: