ignore: fix up comment parsing
authorMatt Mackall <mpm@selenic.com>
Mon, 10 Dec 2007 10:26:42 -0600
changeset 5640 04c76f296ad6
parent 5639 7dd5cf9d1e09
child 5648 2079faccb408
ignore: fix up comment parsing - remove redundant newline bits - change loop to use regex - eliminate escaping of # in internal representation
mercurial/ignore.py
--- a/mercurial/ignore.py	Mon Dec 10 10:24:47 2007 -0600
+++ b/mercurial/ignore.py	Mon Dec 10 10:26:42 2007 -0600
@@ -6,18 +6,21 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from i18n import _
-import util
+import util, re
+
+_commentre = None
 
 def _parselines(fp):
     for line in fp:
-        if not line.endswith('\n'):
-            line += '\n'
-        escape = False
-        for i in xrange(len(line)):
-            if escape: escape = False
-            elif line[i] == '\\': escape = True
-            elif line[i] == '#': break
-        line = line[:i].rstrip()
+        if "#" in line:
+            global _commentre
+            if not _commentre:
+                _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
+            # remove comments prefixed by an even number of escapes
+            line = _commentre.sub(r'\1', line)
+            # fixup properly escaped comments that survived the above
+            line = line.replace("\\#", "#")
+        line = line.rstrip()
         if line:
             yield line