author | Matt Mackall <mpm@selenic.com> |
Sat, 21 Jul 2007 16:02:09 -0500 | |
changeset 4957 | 46e39935ce33 |
parent 4610 | b43f17691ae6 |
child 5029 | ac97e065cfc7 |
permissions | -rw-r--r-- |
4610
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 |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
60 |
for s in syntaxes.values(): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 |
if line.startswith(s): |
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 |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
64 |
pats[f].append(pat) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
65 |
except IOError, inst: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
66 |
if f != files[0]: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 |
warn(_("skipping unreadable ignore file '%s': %s\n") % |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 |
(f, inst.strerror)) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 |
allpats = [] |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 |
[allpats.extend(patlist) for patlist in pats.values()] |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 |
if not allpats: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 |
return util.never |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
74 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
75 |
try: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
76 |
files, ignorefunc, anypats = ( |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
77 |
util.matcher(root, inc=allpats, src='.hgignore')) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
78 |
except util.Abort: |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
79 |
# 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
|
80 |
for f, patlist in pats.items(): |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
81 |
files, ignorefunc, anypats = ( |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 |
util.matcher(root, inc=patlist, src=f)) |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
83 |
|
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 |
return ignorefunc |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 |
|
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 |
'''default match function used by dirstate and |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 |
localrepository. this honours the repository .hgignore file |
b43f17691ae6
dirstate: move ignore to its own file
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 |
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
|
90 |