diff mercurial/ignore.py @ 5640:04c76f296ad6

ignore: fix up comment parsing - remove redundant newline bits - change loop to use regex - eliminate escaping of # in internal representation
author Matt Mackall <mpm@selenic.com>
date Mon, 10 Dec 2007 10:26:42 -0600
parents fae670ee6c6d
children 4dd7b28003d2
line wrap: on
line diff
--- 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