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