Patchwork [6,of,6] ignore: move readpatternfile to match.py

login
register
mail settings
Submitter Durham Goode
Date May 18, 2015, 6:29 p.m.
Message ID <eee4e004d9dfc208eeb6.1431973742@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/9142/
State Accepted
Headers show

Comments

Durham Goode - May 18, 2015, 6:29 p.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1431816414 25200
#      Sat May 16 15:46:54 2015 -0700
# Node ID eee4e004d9dfc208eeb6cc5df3cd17f380cbd9e0
# Parent  533b865c77371c6dcbd8283e65df8ac8df5e6fc2
ignore: move readpatternfile to match.py

In preparation for adding 'include:' rule support to match.py, let's move the
pattern file reader function to match.py
Durham Goode - May 18, 2015, 6:35 p.m.
On 5/18/15 11:29 AM, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham@fb.com>
> # Date 1431816414 25200
> #      Sat May 16 15:46:54 2015 -0700
> # Node ID eee4e004d9dfc208eeb6cc5df3cd17f380cbd9e0
> # Parent  533b865c77371c6dcbd8283e65df8ac8df5e6fc2
> ignore: move readpatternfile to match.py
>
> In preparation for adding 'include:' rule support to match.py, let's move the
> pattern file reader function to match.py
>
This is part #1 of a series to add 'include:' and 'relinclude:' pattern 
support to matchers.  This will allow recursively included subdirectory 
.hgignore files.  See the full series here: 
https://bitbucket.org/DurhamG/hg/commits/branch/subinclude

Patch

diff --git a/mercurial/ignore.py b/mercurial/ignore.py
--- a/mercurial/ignore.py
+++ b/mercurial/ignore.py
@@ -7,53 +7,7 @@ 
 
 from i18n import _
 import util, match
-import re
 
-_commentre = None
-
-def readpatternfile(filepath):
-    '''parse a pattern file, returning a list of
-    patterns. These patterns should be given to compile()
-    to be validated and converted into a match function.'''
-    syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
-    syntax = 'relre:'
-    patterns = []
-
-    fp = open(filepath)
-    for line in fp:
-        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 not line:
-            continue
-
-        if line.startswith('syntax:'):
-            s = line[7:].strip()
-            try:
-                syntax = syntaxes[s]
-            except KeyError:
-                raise util.Abort(_("ignoring invalid syntax '%s'") % s)
-            continue
-
-        linesyntax = syntax
-        for s, rels in syntaxes.iteritems():
-            if line.startswith(rels):
-                linesyntax = rels
-                line = line[len(rels):]
-                break
-            elif line.startswith(s+':'):
-                linesyntax = rels
-                line = line[len(s) + 1:]
-                break
-        patterns.append(linesyntax + line)
-    fp.close()
-    return patterns
 
 def readpats(root, files, warn):
     '''return a dict mapping ignore-file-name to list-of-patterns'''
@@ -63,7 +17,7 @@  def readpats(root, files, warn):
         if f in pats:
             continue
         try:
-            pats[f] = readpatternfile(f)
+            pats[f] = match.readpatternfile(f)
         except IOError, inst:
             warn(_("skipping unreadable ignore file '%s': %s\n") %
                  (f, inst.strerror))
diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -498,3 +498,49 @@  def _anypats(kindpats):
     for kind, pat in kindpats:
         if kind in ('glob', 're', 'relglob', 'relre', 'set'):
             return True
+
+_commentre = None
+
+def readpatternfile(filepath):
+    '''parse a pattern file, returning a list of
+    patterns. These patterns should be given to compile()
+    to be validated and converted into a match function.'''
+    syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
+    syntax = 'relre:'
+    patterns = []
+
+    fp = open(filepath)
+    for line in fp:
+        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 not line:
+            continue
+
+        if line.startswith('syntax:'):
+            s = line[7:].strip()
+            try:
+                syntax = syntaxes[s]
+            except KeyError:
+                raise util.Abort(_("ignoring invalid syntax '%s'") % s)
+            continue
+
+        linesyntax = syntax
+        for s, rels in syntaxes.iteritems():
+            if line.startswith(rels):
+                linesyntax = rels
+                line = line[len(rels):]
+                break
+            elif line.startswith(s+':'):
+                linesyntax = rels
+                line = line[len(s) + 1:]
+                break
+        patterns.append(linesyntax + line)
+    fp.close()
+    return patterns