comparison mercurial/ignore.py @ 25167:6f7048cc2419

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
author Durham Goode <durham@fb.com>
date Sat, 16 May 2015 15:46:54 -0700
parents 7f53f305d4a6
children
comparison
equal deleted inserted replaced
25166:7f53f305d4a6 25167:6f7048cc2419
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from i18n import _ 8 from i18n import _
9 import util, match 9 import util, match
10 import re
11
12 _commentre = None
13
14 def readpatternfile(filepath, warn):
15 '''parse a pattern file, returning a list of
16 patterns. These patterns should be given to compile()
17 to be validated and converted into a match function.'''
18 syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
19 syntax = 'relre:'
20 patterns = []
21
22 fp = open(filepath)
23 for line in fp:
24 if "#" in line:
25 global _commentre
26 if not _commentre:
27 _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
28 # remove comments prefixed by an even number of escapes
29 line = _commentre.sub(r'\1', line)
30 # fixup properly escaped comments that survived the above
31 line = line.replace("\\#", "#")
32 line = line.rstrip()
33 if not line:
34 continue
35
36 if line.startswith('syntax:'):
37 s = line[7:].strip()
38 try:
39 syntax = syntaxes[s]
40 except KeyError:
41 warn(_("%s: ignoring invalid syntax '%s'\n") % (filepath, s))
42 continue
43
44 linesyntax = syntax
45 for s, rels in syntaxes.iteritems():
46 if line.startswith(rels):
47 linesyntax = rels
48 line = line[len(rels):]
49 break
50 elif line.startswith(s+':'):
51 linesyntax = rels
52 line = line[len(s) + 1:]
53 break
54 patterns.append(linesyntax + line)
55
56 fp.close()
57 return patterns
58 10
59 def readpats(root, files, warn): 11 def readpats(root, files, warn):
60 '''return a dict mapping ignore-file-name to list-of-patterns''' 12 '''return a dict mapping ignore-file-name to list-of-patterns'''
61 13
62 pats = {} 14 pats = {}
63 for f in files: 15 for f in files:
64 if f in pats: 16 if f in pats:
65 continue 17 continue
66 try: 18 try:
67 pats[f] = readpatternfile(f, warn) 19 pats[f] = match.readpatternfile(f, warn)
68 except IOError, inst: 20 except IOError, inst:
69 warn(_("skipping unreadable ignore file '%s': %s\n") % 21 warn(_("skipping unreadable ignore file '%s': %s\n") %
70 (f, inst.strerror)) 22 (f, inst.strerror))
71 23
72 return [(f, pats[f]) for f in files if f in pats] 24 return [(f, pats[f]) for f in files if f in pats]