Show More
@@ -1,111 +1,113 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 | pat = syntax + line |
|
43 | pat = syntax + line | |
44 | for s, rels in syntaxes.iteritems(): |
|
44 | for s, rels in syntaxes.iteritems(): | |
45 | if line.startswith(rels): |
|
45 | if line.startswith(rels): | |
46 | pat = line |
|
46 | pat = line | |
47 | break |
|
47 | break | |
48 | elif line.startswith(s+':'): |
|
48 | elif line.startswith(s+':'): | |
49 | pat = rels + line[len(s) + 1:] |
|
49 | pat = rels + line[len(s) + 1:] | |
50 | break |
|
50 | break | |
51 | patterns.append(pat) |
|
51 | patterns.append(pat) | |
52 |
|
52 | |||
53 | return patterns, warnings |
|
53 | return patterns, warnings | |
54 |
|
54 | |||
55 | def readpats(root, files, warn): |
|
55 | def readpats(root, files, warn): | |
56 | '''return a dict mapping ignore-file-name to list-of-patterns''' |
|
56 | '''return a dict mapping ignore-file-name to list-of-patterns''' | |
57 |
|
57 | |||
58 | pats = {} |
|
58 | pats = {} | |
59 | for f in files: |
|
59 | for f in files: | |
|
60 | if f in pats: | |||
|
61 | continue | |||
60 | try: |
|
62 | try: | |
61 | pats[f] = [] |
|
63 | pats[f] = [] | |
62 | fp = open(f) |
|
64 | fp = open(f) | |
63 | pats[f], warnings = ignorepats(fp) |
|
65 | pats[f], warnings = ignorepats(fp) | |
64 | fp.close() |
|
66 | fp.close() | |
65 | for warning in warnings: |
|
67 | for warning in warnings: | |
66 | warn("%s: %s\n" % (f, warning)) |
|
68 | warn("%s: %s\n" % (f, warning)) | |
67 | except IOError, inst: |
|
69 | except IOError, inst: | |
68 | if f != files[0]: |
|
70 | if f != files[0]: | |
69 | warn(_("skipping unreadable ignore file '%s': %s\n") % |
|
71 | warn(_("skipping unreadable ignore file '%s': %s\n") % | |
70 | (f, inst.strerror)) |
|
72 | (f, inst.strerror)) | |
71 | return pats |
|
73 | return pats | |
72 |
|
74 | |||
73 | def ignore(root, files, warn): |
|
75 | def ignore(root, files, warn): | |
74 | '''return matcher covering patterns in 'files'. |
|
76 | '''return matcher covering patterns in 'files'. | |
75 |
|
77 | |||
76 | the files parsed for patterns include: |
|
78 | the files parsed for patterns include: | |
77 | .hgignore in the repository root |
|
79 | .hgignore in the repository root | |
78 | any additional files specified in the [ui] section of ~/.hgrc |
|
80 | any additional files specified in the [ui] section of ~/.hgrc | |
79 |
|
81 | |||
80 | trailing white space is dropped. |
|
82 | trailing white space is dropped. | |
81 | the escape character is backslash. |
|
83 | the escape character is backslash. | |
82 | comments start with #. |
|
84 | comments start with #. | |
83 | empty lines are skipped. |
|
85 | empty lines are skipped. | |
84 |
|
86 | |||
85 | lines can be of the following formats: |
|
87 | lines can be of the following formats: | |
86 |
|
88 | |||
87 | syntax: regexp # defaults following lines to non-rooted regexps |
|
89 | syntax: regexp # defaults following lines to non-rooted regexps | |
88 | syntax: glob # defaults following lines to non-rooted globs |
|
90 | syntax: glob # defaults following lines to non-rooted globs | |
89 | re:pattern # non-rooted regular expression |
|
91 | re:pattern # non-rooted regular expression | |
90 | glob:pattern # non-rooted glob |
|
92 | glob:pattern # non-rooted glob | |
91 | pattern # pattern of the current default type''' |
|
93 | pattern # pattern of the current default type''' | |
92 |
|
94 | |||
93 | pats = readpats(root, files, warn) |
|
95 | pats = readpats(root, files, warn) | |
94 |
|
96 | |||
95 | allpats = [] |
|
97 | allpats = [] | |
96 | for patlist in pats.values(): |
|
98 | for patlist in pats.values(): | |
97 | allpats.extend(patlist) |
|
99 | allpats.extend(patlist) | |
98 | if not allpats: |
|
100 | if not allpats: | |
99 | return util.never |
|
101 | return util.never | |
100 |
|
102 | |||
101 | try: |
|
103 | try: | |
102 | ignorefunc = match.match(root, '', [], allpats) |
|
104 | ignorefunc = match.match(root, '', [], allpats) | |
103 | except util.Abort: |
|
105 | except util.Abort: | |
104 | # Re-raise an exception where the src is the right file |
|
106 | # Re-raise an exception where the src is the right file | |
105 | for f, patlist in pats.iteritems(): |
|
107 | for f, patlist in pats.iteritems(): | |
106 | try: |
|
108 | try: | |
107 | match.match(root, '', [], patlist) |
|
109 | match.match(root, '', [], patlist) | |
108 | except util.Abort, inst: |
|
110 | except util.Abort, inst: | |
109 | raise util.Abort('%s: %s' % (f, inst[0])) |
|
111 | raise util.Abort('%s: %s' % (f, inst[0])) | |
110 |
|
112 | |||
111 | return ignorefunc |
|
113 | return ignorefunc |
General Comments 0
You need to be logged in to leave comments.
Login now