Show More
@@ -1,120 +1,119 b'' | |||||
1 | # ignore.py - ignored file handling for mercurial |
|
1 | # ignore.py - ignored file handling for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2007 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2007 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
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 |
|
10 | import re | |
11 |
|
11 | |||
12 | _commentre = None |
|
12 | _commentre = None | |
13 |
|
13 | |||
14 | def ignorepats(lines): |
|
14 | def ignorepats(lines): | |
15 | '''parse lines (iterable) of .hgignore text, returning a tuple of |
|
15 | '''parse lines (iterable) of .hgignore text, returning a tuple of | |
16 | (patterns, parse errors). These patterns should be given to compile() |
|
16 | (patterns, parse errors). These patterns should be given to compile() | |
17 | to be validated and converted into a match function.''' |
|
17 | to be validated and converted into a match function.''' | |
18 | syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'} |
|
18 | syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'} | |
19 | syntax = 'relre:' |
|
19 | syntax = 'relre:' | |
20 | patterns = [] |
|
20 | patterns = [] | |
21 | warnings = [] |
|
21 | warnings = [] | |
22 |
|
22 | |||
23 | for line in lines: |
|
23 | for line in lines: | |
24 | if "#" in line: |
|
24 | if "#" in line: | |
25 | global _commentre |
|
25 | global _commentre | |
26 | if not _commentre: |
|
26 | if not _commentre: | |
27 | _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*') |
|
27 | _commentre = re.compile(r'((^|[^\\])(\\\\)*)#.*') | |
28 | # remove comments prefixed by an even number of escapes |
|
28 | # remove comments prefixed by an even number of escapes | |
29 | line = _commentre.sub(r'\1', line) |
|
29 | line = _commentre.sub(r'\1', line) | |
30 | # fixup properly escaped comments that survived the above |
|
30 | # fixup properly escaped comments that survived the above | |
31 | line = line.replace("\\#", "#") |
|
31 | line = line.replace("\\#", "#") | |
32 | line = line.rstrip() |
|
32 | line = line.rstrip() | |
33 | if not line: |
|
33 | if not line: | |
34 | continue |
|
34 | continue | |
35 |
|
35 | |||
36 | if line.startswith('syntax:'): |
|
36 | if line.startswith('syntax:'): | |
37 | s = line[7:].strip() |
|
37 | s = line[7:].strip() | |
38 | try: |
|
38 | try: | |
39 | syntax = syntaxes[s] |
|
39 | syntax = syntaxes[s] | |
40 | except KeyError: |
|
40 | except KeyError: | |
41 | warnings.append(_("ignoring invalid syntax '%s'") % s) |
|
41 | warnings.append(_("ignoring invalid syntax '%s'") % s) | |
42 | continue |
|
42 | continue | |
43 |
|
43 | |||
44 | linesyntax = syntax |
|
44 | linesyntax = syntax | |
45 | for s, rels in syntaxes.iteritems(): |
|
45 | for s, rels in syntaxes.iteritems(): | |
46 | if line.startswith(rels): |
|
46 | if line.startswith(rels): | |
47 | linesyntax = rels |
|
47 | linesyntax = rels | |
48 | line = line[len(rels):] |
|
48 | line = line[len(rels):] | |
49 | break |
|
49 | break | |
50 | elif line.startswith(s+':'): |
|
50 | elif line.startswith(s+':'): | |
51 | linesyntax = rels |
|
51 | linesyntax = rels | |
52 | line = line[len(s) + 1:] |
|
52 | line = line[len(s) + 1:] | |
53 | break |
|
53 | break | |
54 | patterns.append(linesyntax + line) |
|
54 | patterns.append(linesyntax + line) | |
55 |
|
55 | |||
56 | return patterns, warnings |
|
56 | return patterns, warnings | |
57 |
|
57 | |||
58 | def readignorefile(filepath, warn): |
|
58 | def readignorefile(filepath, warn): | |
59 | try: |
|
|||
60 | pats = [] |
|
|||
61 |
|
|
59 | fp = open(filepath) | |
62 |
|
|
60 | pats, warnings = ignorepats(fp) | |
63 |
|
|
61 | fp.close() | |
64 |
|
|
62 | for warning in warnings: | |
65 |
|
|
63 | warn("%s: %s\n" % (filepath, warning)) | |
66 | except IOError, inst: |
|
|||
67 | warn(_("skipping unreadable ignore file '%s': %s\n") % |
|
|||
68 | (filepath, inst.strerror)) |
|
|||
69 | return pats |
|
64 | return pats | |
70 |
|
65 | |||
71 | def readpats(root, files, warn): |
|
66 | def readpats(root, files, warn): | |
72 | '''return a dict mapping ignore-file-name to list-of-patterns''' |
|
67 | '''return a dict mapping ignore-file-name to list-of-patterns''' | |
73 |
|
68 | |||
74 | pats = {} |
|
69 | pats = {} | |
75 | for f in files: |
|
70 | for f in files: | |
76 | if f in pats: |
|
71 | if f in pats: | |
77 | continue |
|
72 | continue | |
|
73 | try: | |||
78 | pats[f] = readignorefile(f, warn) |
|
74 | pats[f] = readignorefile(f, warn) | |
|
75 | except IOError, inst: | |||
|
76 | warn(_("skipping unreadable ignore file '%s': %s\n") % | |||
|
77 | (f, inst.strerror)) | |||
79 |
|
78 | |||
80 | return [(f, pats[f]) for f in files if f in pats] |
|
79 | return [(f, pats[f]) for f in files if f in pats] | |
81 |
|
80 | |||
82 | def ignore(root, files, warn): |
|
81 | def ignore(root, files, warn): | |
83 | '''return matcher covering patterns in 'files'. |
|
82 | '''return matcher covering patterns in 'files'. | |
84 |
|
83 | |||
85 | the files parsed for patterns include: |
|
84 | the files parsed for patterns include: | |
86 | .hgignore in the repository root |
|
85 | .hgignore in the repository root | |
87 | any additional files specified in the [ui] section of ~/.hgrc |
|
86 | any additional files specified in the [ui] section of ~/.hgrc | |
88 |
|
87 | |||
89 | trailing white space is dropped. |
|
88 | trailing white space is dropped. | |
90 | the escape character is backslash. |
|
89 | the escape character is backslash. | |
91 | comments start with #. |
|
90 | comments start with #. | |
92 | empty lines are skipped. |
|
91 | empty lines are skipped. | |
93 |
|
92 | |||
94 | lines can be of the following formats: |
|
93 | lines can be of the following formats: | |
95 |
|
94 | |||
96 | syntax: regexp # defaults following lines to non-rooted regexps |
|
95 | syntax: regexp # defaults following lines to non-rooted regexps | |
97 | syntax: glob # defaults following lines to non-rooted globs |
|
96 | syntax: glob # defaults following lines to non-rooted globs | |
98 | re:pattern # non-rooted regular expression |
|
97 | re:pattern # non-rooted regular expression | |
99 | glob:pattern # non-rooted glob |
|
98 | glob:pattern # non-rooted glob | |
100 | pattern # pattern of the current default type''' |
|
99 | pattern # pattern of the current default type''' | |
101 |
|
100 | |||
102 | pats = readpats(root, files, warn) |
|
101 | pats = readpats(root, files, warn) | |
103 |
|
102 | |||
104 | allpats = [] |
|
103 | allpats = [] | |
105 | for f, patlist in pats: |
|
104 | for f, patlist in pats: | |
106 | allpats.extend(patlist) |
|
105 | allpats.extend(patlist) | |
107 | if not allpats: |
|
106 | if not allpats: | |
108 | return util.never |
|
107 | return util.never | |
109 |
|
108 | |||
110 | try: |
|
109 | try: | |
111 | ignorefunc = match.match(root, '', [], allpats) |
|
110 | ignorefunc = match.match(root, '', [], allpats) | |
112 | except util.Abort: |
|
111 | except util.Abort: | |
113 | # Re-raise an exception where the src is the right file |
|
112 | # Re-raise an exception where the src is the right file | |
114 | for f, patlist in pats: |
|
113 | for f, patlist in pats: | |
115 | try: |
|
114 | try: | |
116 | match.match(root, '', [], patlist) |
|
115 | match.match(root, '', [], patlist) | |
117 | except util.Abort, inst: |
|
116 | except util.Abort, inst: | |
118 | raise util.Abort('%s: %s' % (f, inst[0])) |
|
117 | raise util.Abort('%s: %s' % (f, inst[0])) | |
119 |
|
118 | |||
120 | return ignorefunc |
|
119 | return ignorefunc |
General Comments 0
You need to be logged in to leave comments.
Login now