##// END OF EJS Templates
ignore: separate pattern extraction from match compilation...
Brendan Cully -
r9091:79a886bc default
parent child Browse files
Show More
@@ -11,8 +11,16 b' import re'
11
11
12 _commentre = None
12 _commentre = None
13
13
14 def _parselines(fp):
14 def ignorepats(lines):
15 for line in fp:
15 '''parse lines (iterable) of .hgignore text, returning a tuple of
16 (patterns, parse errors). 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 warnings = []
22
23 for line in lines:
16 if "#" in line:
24 if "#" in line:
17 global _commentre
25 global _commentre
18 if not _commentre:
26 if not _commentre:
@@ -22,11 +30,30 b' def _parselines(fp):'
22 # fixup properly escaped comments that survived the above
30 # fixup properly escaped comments that survived the above
23 line = line.replace("\\#", "#")
31 line = line.replace("\\#", "#")
24 line = line.rstrip()
32 line = line.rstrip()
25 if line:
33 if not line:
26 yield line
34 continue
35
36 if line.startswith('syntax:'):
37 s = line[7:].strip()
38 try:
39 syntax = syntaxes[s]
40 except KeyError:
41 warnings.append(_("ignoring invalid syntax '%s'") % s)
42 continue
43 pat = syntax + line
44 for s, rels in syntaxes.iteritems():
45 if line.startswith(rels):
46 pat = line
47 break
48 elif line.startswith(s+':'):
49 pat = rels + line[len(s)+1:]
50 break
51 patterns.append(pat)
52
53 return patterns, warnings
27
54
28 def ignore(root, files, warn):
55 def ignore(root, files, warn):
29 '''return the contents of .hgignore files as a list of patterns.
56 '''return matcher covering patterns in 'files'.
30
57
31 the files parsed for patterns include:
58 the files parsed for patterns include:
32 .hgignore in the repository root
59 .hgignore in the repository root
@@ -45,30 +72,14 b' def ignore(root, files, warn):'
45 glob:pattern # non-rooted glob
72 glob:pattern # non-rooted glob
46 pattern # pattern of the current default type'''
73 pattern # pattern of the current default type'''
47
74
48 syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
49 pats = {}
75 pats = {}
50 for f in files:
76 for f in files:
51 try:
77 try:
52 pats[f] = []
78 pats[f] = []
53 fp = open(f)
79 fp = open(f)
54 syntax = 'relre:'
80 pats[f], warnings = ignorepats(fp)
55 for line in _parselines(fp):
81 for warning in warnings:
56 if line.startswith('syntax:'):
82 warn("%s: %s\n" % (f, warning))
57 s = line[7:].strip()
58 try:
59 syntax = syntaxes[s]
60 except KeyError:
61 warn(_("%s: ignoring invalid syntax '%s'\n") % (f, s))
62 continue
63 pat = syntax + line
64 for s, rels in syntaxes.iteritems():
65 if line.startswith(rels):
66 pat = line
67 break
68 elif line.startswith(s+':'):
69 pat = rels + line[len(s)+1:]
70 break
71 pats[f].append(pat)
72 except IOError, inst:
83 except IOError, inst:
73 if f != files[0]:
84 if f != files[0]:
74 warn(_("skipping unreadable ignore file '%s': %s\n") %
85 warn(_("skipping unreadable ignore file '%s': %s\n") %
General Comments 0
You need to be logged in to leave comments. Login now