##// END OF EJS Templates
ignore: fix up comment parsing...
Matt Mackall -
r5640:04c76f29 default
parent child Browse files
Show More
@@ -1,87 +1,90 b''
1 1 # ignore.py - ignored file handling for mercurial
2 2 #
3 3 # Copyright 2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from i18n import _
9 import util
9 import util, re
10
11 _commentre = None
10 12
11 13 def _parselines(fp):
12 14 for line in fp:
13 if not line.endswith('\n'):
14 line += '\n'
15 escape = False
16 for i in xrange(len(line)):
17 if escape: escape = False
18 elif line[i] == '\\': escape = True
19 elif line[i] == '#': break
20 line = line[:i].rstrip()
15 if "#" in line:
16 global _commentre
17 if not _commentre:
18 _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*')
19 # remove comments prefixed by an even number of escapes
20 line = _commentre.sub(r'\1', line)
21 # fixup properly escaped comments that survived the above
22 line = line.replace("\\#", "#")
23 line = line.rstrip()
21 24 if line:
22 25 yield line
23 26
24 27 def ignore(root, files, warn):
25 28 '''return the contents of .hgignore files as a list of patterns.
26 29
27 30 the files parsed for patterns include:
28 31 .hgignore in the repository root
29 32 any additional files specified in the [ui] section of ~/.hgrc
30 33
31 34 trailing white space is dropped.
32 35 the escape character is backslash.
33 36 comments start with #.
34 37 empty lines are skipped.
35 38
36 39 lines can be of the following formats:
37 40
38 41 syntax: regexp # defaults following lines to non-rooted regexps
39 42 syntax: glob # defaults following lines to non-rooted globs
40 43 re:pattern # non-rooted regular expression
41 44 glob:pattern # non-rooted glob
42 45 pattern # pattern of the current default type'''
43 46
44 47 syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'}
45 48 pats = {}
46 49 for f in files:
47 50 try:
48 51 pats[f] = []
49 52 fp = open(f)
50 53 syntax = 'relre:'
51 54 for line in _parselines(fp):
52 55 if line.startswith('syntax:'):
53 56 s = line[7:].strip()
54 57 try:
55 58 syntax = syntaxes[s]
56 59 except KeyError:
57 60 warn(_("%s: ignoring invalid syntax '%s'\n") % (f, s))
58 61 continue
59 62 pat = syntax + line
60 63 for s, rels in syntaxes.items():
61 64 if line.startswith(rels):
62 65 pat = line
63 66 break
64 67 elif line.startswith(s+':'):
65 68 pat = rels + line[len(s)+1:]
66 69 break
67 70 pats[f].append(pat)
68 71 except IOError, inst:
69 72 if f != files[0]:
70 73 warn(_("skipping unreadable ignore file '%s': %s\n") %
71 74 (f, inst.strerror))
72 75
73 76 allpats = []
74 77 [allpats.extend(patlist) for patlist in pats.values()]
75 78 if not allpats:
76 79 return util.never
77 80
78 81 try:
79 82 files, ignorefunc, anypats = (
80 83 util.matcher(root, inc=allpats, src='.hgignore'))
81 84 except util.Abort:
82 85 # Re-raise an exception where the src is the right file
83 86 for f, patlist in pats.items():
84 87 files, ignorefunc, anypats = (
85 88 util.matcher(root, inc=patlist, src=f))
86 89
87 90 return ignorefunc
General Comments 0
You need to be logged in to leave comments. Login now