[PATCH stable] config: allow single-character continuation lines and %include filenames
Greg Ward
greg-hg at gerg.ca
Thu Jan 28 03:15:32 UTC 2010
# HG changeset patch
# User Greg Ward <greg-hg at gerg.ca>
# Date 1264648479 18000
# Branch stable
# Node ID 70551c4f2f90a58280177a8d9eceac72e42131a3
# Parent 1c4ab236ebcbb2602a3e77c32796a29db2a227c4
config: allow single-character continuation lines and %include filenames
(issue1999)
- consume all characters to EOL, then rstrip() off the whitespace
- add new test script: test-config-parse.py
diff --git a/mercurial/config.py b/mercurial/config.py
--- a/mercurial/config.py
+++ b/mercurial/config.py
@@ -73,10 +73,10 @@
def parse(self, src, data, sections=None, remap=None, include=None):
sectionre = re.compile(r'\[([^\[]+)\]')
itemre = re.compile(r'([^=\s][^=]*?)\s*=\s*(.*\S|)')
- contre = re.compile(r'\s+(\S.*\S)')
+ contre = re.compile(r'\s+(\S.*)')
emptyre = re.compile(r'(;|#|\s*$)')
unsetre = re.compile(r'%unset\s+(\S+)')
- includere = re.compile(r'%include\s+(\S.*\S)')
+ includere = re.compile(r'%include\s+(\S.*)')
section = ""
item = None
line = 0
@@ -89,14 +89,14 @@
if m:
if sections and section not in sections:
continue
- v = self.get(section, item) + "\n" + m.group(1)
+ v = self.get(section, item) + "\n" + m.group(1).rstrip()
self.set(section, item, v, "%s:%d" % (src, line))
continue
item = None
cont = False
m = includere.match(l)
if m:
- inc = m.group(1)
+ inc = m.group(1).rstrip()
base = os.path.dirname(src)
inc = os.path.normpath(os.path.join(base, inc))
if include:
diff --git a/tests/test-config-parse.py b/tests/test-config-parse.py
new file mode 100755
--- /dev/null
+++ b/tests/test-config-parse.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+
+import sys
+import os
+import tempfile
+import shutil
+from mercurial import config
+
+def testparse(tempdir):
+
+ fn1 = os.path.join(tempdir, 'file1')
+ file1 = open(fn1, 'w')
+ file1.write('[section1]\n'
+ 'val1 = hello\n'
+ 'val2 = hello \n'
+ 'val3 = foo\n'
+ ' bar \n'
+ ' baz\n'
+ 'val4 =\n'
+ ' a\n' # 1-char continuation line (issue1999)
+ ' b\n'
+ '\n'
+ '%include a\n')
+ file1.close()
+
+ # must be a 1-character filename to expose issue1999
+ file2 = open(os.path.join(tempdir, 'a'), 'w')
+ file2.write('[section2]\n'
+ 'val1 = ooga \n')
+ file2.close()
+
+ write = sys.stdout.write
+ write('parsing...\n')
+ cfg = config.config()
+ #cfg.parse('inline', data)
+ cfg.read(fn1)
+
+ write('values:\n')
+ for item in ('val1', 'val2', 'val3', 'val4'):
+ value = cfg.get('section1', item)
+ assert ' ' not in value
+ write('section1.%s = >%s<\n' % (item, value))
+
+ write('section2.val1 = >%s<\n' % cfg.get('section2', 'val1'))
+
+def main():
+ tempdir = tempfile.mkdtemp()
+ try:
+ testparse(tempdir)
+ finally:
+ shutil.rmtree(tempdir)
+
+main()
+
diff --git a/tests/test-config-parse.py.out b/tests/test-config-parse.py.out
new file mode 100644
--- /dev/null
+++ b/tests/test-config-parse.py.out
@@ -0,0 +1,11 @@
+parsing...
+values:
+section1.val1 = >hello<
+section1.val2 = >hello<
+section1.val3 = >foo
+bar
+baz<
+section1.val4 = >
+a
+b<
+section2.val1 = >ooga<
More information about the Mercurial-devel
mailing list