changeset 1394:c4055968f030

maps: use regex for better comment handling This is copied straight from mercurial's hgignore parsing.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:21:00 -0500
parents f53a27e4e0d3
children 53184be1b1fd
files hgsubversion/maps.py
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -2,6 +2,7 @@
 
 import errno
 import os
+import re
 from mercurial import util as hgutil
 from mercurial.node import bin, hex, nullid
 
@@ -16,6 +17,8 @@ class BaseMap(dict):
         self.meta = meta
         super(BaseMap, self).__init__()
 
+        self._commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
+
         # trickery: all subclasses have the same name as their file and config
         # names, e.g. AuthorMap is meta.authormap_file for the filename and
         # 'authormap' for the config option
@@ -46,8 +49,14 @@ class BaseMap(dict):
             if writing:
                 writing.write(line)
 
-            line = line.split('#')[0]
-            if not line.strip():
+            # strip out comments
+            if "#" in line:
+                # remove comments prefixed by an even number of escapes
+                line = self._commentre.sub(r'\1', line)
+                # fixup properly escaped comments that survived the above
+                line = line.replace("\\#", "#")
+            line = line.rstrip()
+            if not line:
                 continue
 
             try: