author | Brendan Cully <brendan@kublai.com> |
Wed, 01 Aug 2007 14:26:30 -0700 | |
changeset 5047 | d9689c6474bd |
parent 5029 | ac97e065cfc7 |
child 5271 | fae670ee6c6d |
permissions | -rw-r--r-- |
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 _ |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 |
import util |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
10 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 |
def _parselines(fp): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 |
for line in fp: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 |
if not line.endswith('\n'): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 |
line += '\n' |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 |
escape = False |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 |
for i in xrange(len(line)): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 |
if escape: escape = False |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
18 |
elif line[i] == '\\': escape = True |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
19 |
elif line[i] == '#': break |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
20 |
line = line[:i].rstrip() |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
21 |
if line: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
22 |
yield line |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
23 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 |
def ignore(root, files, warn): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 |
'''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
|
26 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
27 |
the files parsed for patterns include: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 |
.hgignore in the repository root |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 |
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
|
30 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
31 |
trailing white space is dropped. |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
32 |
the escape character is backslash. |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
33 |
comments start with #. |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
34 |
empty lines are skipped. |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
35 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
36 |
lines can be of the following formats: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
re:pattern # non-rooted regular expression |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 |
glob:pattern # non-rooted glob |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 |
pattern # pattern of the current default type''' |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
44 |
syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'} |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 |
pats = {} |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 |
for f in files: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 |
try: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 |
pats[f] = [] |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 |
fp = open(f) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 |
syntax = 'relre:' |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 |
for line in _parselines(fp): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 |
if line.startswith('syntax:'): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 |
s = line[7:].strip() |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
try: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 |
syntax = syntaxes[s] |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 |
except KeyError: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
57 |
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
|
58 |
continue |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
59 |
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
|
60 |
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
|
61 |
if line.startswith(rels): |
4609
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 |
pat = line |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 |
break |
5029
ac97e065cfc7
Fix re: and glob: patterns in .hgignore (reported by Brad Schick)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4609
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
break |
4609
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 |
pats[f].append(pat) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 |
except IOError, inst: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
if f != files[0]: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 |
warn(_("skipping unreadable ignore file '%s': %s\n") % |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 |
(f, inst.strerror)) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 |
allpats = [] |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
74 |
[allpats.extend(patlist) for patlist in pats.values()] |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
75 |
if not allpats: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
76 |
return util.never |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
77 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
78 |
try: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
79 |
files, ignorefunc, anypats = ( |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
80 |
util.matcher(root, inc=allpats, src='.hgignore')) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
81 |
except util.Abort: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 |
# 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
|
83 |
for f, patlist in pats.items(): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 |
files, ignorefunc, anypats = ( |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 |
util.matcher(root, inc=patlist, src=f)) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
86 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
87 |
return ignorefunc |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 |
|
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 |
'''default match function used by dirstate and |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
91 |
localrepository. this honours the repository .hgignore file |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
92 |
and any other files specified in the [ui] section of .hgrc.''' |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 |