author | Matt Mackall <mpm@selenic.com> |
Tue, 18 Oct 2005 12:11:23 -0700 | |
changeset 1397 | 66fd3bc1cfcf |
parent 1396 | 8c3e2a254257 |
child 1400 | cf9a1233738a |
permissions | -rw-r--r-- |
1089 | 1 |
""" |
2 |
dirstate.py - working directory tracking for mercurial |
|
3 |
||
4 |
Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
5 |
||
6 |
This software may be used and distributed according to the terms |
|
7 |
of the GNU General Public License, incorporated herein by reference. |
|
8 |
""" |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
9 |
|
1094 | 10 |
import struct, os |
11 |
from node import * |
|
262 | 12 |
from demandload import * |
1104 | 13 |
demandload(globals(), "time bisect stat util re") |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
14 |
|
220 | 15 |
class dirstate: |
244 | 16 |
def __init__(self, opener, ui, root): |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
17 |
self.opener = opener |
244 | 18 |
self.root = root |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
19 |
self.dirty = 0 |
20 | 20 |
self.ui = ui |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
21 |
self.map = None |
227 | 22 |
self.pl = None |
363 | 23 |
self.copies = {} |
723 | 24 |
self.ignorefunc = None |
1183 | 25 |
self.blockignore = False |
723 | 26 |
|
27 |
def wjoin(self, f): |
|
28 |
return os.path.join(self.root, f) |
|
29 |
||
870
a82eae840447
Teach walk code about absolute paths.
Bryan O'Sullivan <bos@serpentine.com>
parents:
839
diff
changeset
|
30 |
def getcwd(self): |
a82eae840447
Teach walk code about absolute paths.
Bryan O'Sullivan <bos@serpentine.com>
parents:
839
diff
changeset
|
31 |
cwd = os.getcwd() |
a82eae840447
Teach walk code about absolute paths.
Bryan O'Sullivan <bos@serpentine.com>
parents:
839
diff
changeset
|
32 |
if cwd == self.root: return '' |
a82eae840447
Teach walk code about absolute paths.
Bryan O'Sullivan <bos@serpentine.com>
parents:
839
diff
changeset
|
33 |
return cwd[len(self.root) + 1:] |
a82eae840447
Teach walk code about absolute paths.
Bryan O'Sullivan <bos@serpentine.com>
parents:
839
diff
changeset
|
34 |
|
1270
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
35 |
def hgignore(self): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
36 |
'''return the contents of .hgignore as a list of patterns. |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
37 |
|
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
38 |
trailing white space is dropped. |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
39 |
the escape character is backslash. |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
40 |
comments start with #. |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
41 |
empty lines are skipped. |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
42 |
|
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
43 |
lines can be of the following formats: |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
44 |
|
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
45 |
syntax: regexp # defaults following lines to non-rooted regexps |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
46 |
syntax: glob # defaults following lines to non-rooted globs |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
47 |
re:pattern # non-rooted regular expression |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
48 |
glob:pattern # non-rooted glob |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
49 |
pattern # pattern of the current default type''' |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
50 |
syntaxes = {'re': 'relre:', 'regexp': 'relre:', 'glob': 'relglob:'} |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
51 |
def parselines(fp): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
52 |
for line in fp: |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
53 |
escape = False |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
54 |
for i in xrange(len(line)): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
55 |
if escape: escape = False |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
56 |
elif line[i] == '\\': escape = True |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
57 |
elif line[i] == '#': break |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
58 |
line = line[:i].rstrip() |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
59 |
if line: yield line |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
60 |
pats = [] |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
61 |
try: |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
62 |
fp = open(self.wjoin('.hgignore')) |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
63 |
syntax = 'relre:' |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
64 |
for line in parselines(fp): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
65 |
if line.startswith('syntax:'): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
66 |
s = line[7:].strip() |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
67 |
try: |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
68 |
syntax = syntaxes[s] |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
69 |
except KeyError: |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
70 |
self.ui.warn("ignoring invalid syntax '%s'\n" % s) |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
71 |
continue |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
72 |
pat = syntax + line |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
73 |
for s in syntaxes.values(): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
74 |
if line.startswith(s): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
75 |
pat = line |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
76 |
break |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
77 |
pats.append(pat) |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
78 |
except IOError: pass |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
79 |
return pats |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
80 |
|
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
81 |
def ignore(self, fn): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
82 |
'''default match function used by dirstate and localrepository. |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
83 |
this honours the .hgignore file, and nothing more.''' |
1183 | 84 |
if self.blockignore: |
85 |
return False |
|
723 | 86 |
if not self.ignorefunc: |
1271
9ab14ca22e37
Fix ignore regression.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1270
diff
changeset
|
87 |
ignore = self.hgignore() |
9ab14ca22e37
Fix ignore regression.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1270
diff
changeset
|
88 |
if ignore: |
9ab14ca22e37
Fix ignore regression.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1270
diff
changeset
|
89 |
files, self.ignorefunc, anypats = util.matcher(self.root, |
9ab14ca22e37
Fix ignore regression.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1270
diff
changeset
|
90 |
inc=ignore) |
9ab14ca22e37
Fix ignore regression.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1270
diff
changeset
|
91 |
else: |
9ab14ca22e37
Fix ignore regression.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1270
diff
changeset
|
92 |
self.ignorefunc = util.never |
1270
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
93 |
return self.ignorefunc(fn) |
220 | 94 |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
95 |
def __del__(self): |
220 | 96 |
if self.dirty: |
97 |
self.write() |
|
98 |
||
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
99 |
def __getitem__(self, key): |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
100 |
try: |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
101 |
return self.map[key] |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
102 |
except TypeError: |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
103 |
self.read() |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
104 |
return self[key] |
220 | 105 |
|
106 |
def __contains__(self, key): |
|
107 |
if not self.map: self.read() |
|
108 |
return key in self.map |
|
109 |
||
227 | 110 |
def parents(self): |
111 |
if not self.pl: |
|
112 |
self.read() |
|
113 |
return self.pl |
|
114 |
||
723 | 115 |
def markdirty(self): |
116 |
if not self.dirty: |
|
117 |
self.dirty = 1 |
|
118 |
||
1062 | 119 |
def setparents(self, p1, p2=nullid): |
1394
b20b683e8d95
dirstate: make sure we read the dirstate before setting parents
Matt Mackall <mpm@selenic.com>
parents:
1392
diff
changeset
|
120 |
if not self.pl: |
b20b683e8d95
dirstate: make sure we read the dirstate before setting parents
Matt Mackall <mpm@selenic.com>
parents:
1392
diff
changeset
|
121 |
self.read() |
723 | 122 |
self.markdirty() |
227 | 123 |
self.pl = p1, p2 |
124 |
||
220 | 125 |
def state(self, key): |
126 |
try: |
|
127 |
return self[key][0] |
|
128 |
except KeyError: |
|
129 |
return "?" |
|
130 |
||
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
131 |
def read(self): |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
132 |
if self.map is not None: return self.map |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
133 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
134 |
self.map = {} |
227 | 135 |
self.pl = [nullid, nullid] |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
136 |
try: |
220 | 137 |
st = self.opener("dirstate").read() |
311 | 138 |
if not st: return |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
139 |
except: return |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
140 |
|
227 | 141 |
self.pl = [st[:20], st[20: 40]] |
142 |
||
143 |
pos = 40 |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
144 |
while pos < len(st): |
220 | 145 |
e = struct.unpack(">cllll", st[pos:pos+17]) |
146 |
l = e[4] |
|
147 |
pos += 17 |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
148 |
f = st[pos:pos + l] |
515 | 149 |
if '\0' in f: |
363 | 150 |
f, c = f.split('\0') |
151 |
self.copies[f] = c |
|
220 | 152 |
self.map[f] = e[:4] |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
153 |
pos += l |
363 | 154 |
|
155 |
def copy(self, source, dest): |
|
156 |
self.read() |
|
723 | 157 |
self.markdirty() |
363 | 158 |
self.copies[dest] = source |
159 |
||
160 |
def copied(self, file): |
|
161 |
return self.copies.get(file, None) |
|
515 | 162 |
|
862
d70c1c31fd45
Fix 3-way-merge of original parent, workdir and new parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
861
diff
changeset
|
163 |
def update(self, files, state, **kw): |
220 | 164 |
''' current states: |
165 |
n normal |
|
231 | 166 |
m needs merging |
220 | 167 |
r marked for removal |
168 |
a marked for addition''' |
|
169 |
||
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
170 |
if not files: return |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
171 |
self.read() |
723 | 172 |
self.markdirty() |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
173 |
for f in files: |
220 | 174 |
if state == "r": |
175 |
self.map[f] = ('r', 0, 0, 0) |
|
176 |
else: |
|
1230 | 177 |
s = os.lstat(os.path.join(self.root, f)) |
862
d70c1c31fd45
Fix 3-way-merge of original parent, workdir and new parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
861
diff
changeset
|
178 |
st_size = kw.get('st_size', s.st_size) |
d70c1c31fd45
Fix 3-way-merge of original parent, workdir and new parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
861
diff
changeset
|
179 |
st_mtime = kw.get('st_mtime', s.st_mtime) |
865
2d2fee33ec68
Cleanup after previous changes:
Thomas Arendsen Hein <thomas@intevation.de>
parents:
863
diff
changeset
|
180 |
self.map[f] = (state, s.st_mode, st_size, st_mtime) |
1117 | 181 |
if self.copies.has_key(f): |
182 |
del self.copies[f] |
|
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
183 |
|
220 | 184 |
def forget(self, files): |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
185 |
if not files: return |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
186 |
self.read() |
723 | 187 |
self.markdirty() |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
188 |
for f in files: |
20 | 189 |
try: |
190 |
del self.map[f] |
|
191 |
except KeyError: |
|
220 | 192 |
self.ui.warn("not in dirstate: %s!\n" % f) |
20 | 193 |
pass |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
194 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
195 |
def clear(self): |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
196 |
self.map = {} |
723 | 197 |
self.markdirty() |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
198 |
|
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
199 |
def write(self): |
220 | 200 |
st = self.opener("dirstate", "w") |
227 | 201 |
st.write("".join(self.pl)) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
202 |
for f, e in self.map.items(): |
363 | 203 |
c = self.copied(f) |
204 |
if c: |
|
205 |
f = f + "\0" + c |
|
220 | 206 |
e = struct.pack(">cllll", e[0], e[1], e[2], e[3], len(f)) |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
207 |
st.write(e + f) |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
208 |
self.dirty = 0 |
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
209 |
|
879 | 210 |
def filterfiles(self, files): |
211 |
ret = {} |
|
212 |
unknown = [] |
|
213 |
||
214 |
for x in files: |
|
215 |
if x is '.': |
|
216 |
return self.map.copy() |
|
217 |
if x not in self.map: |
|
218 |
unknown.append(x) |
|
219 |
else: |
|
220 |
ret[x] = self.map[x] |
|
919 | 221 |
|
879 | 222 |
if not unknown: |
223 |
return ret |
|
224 |
||
225 |
b = self.map.keys() |
|
226 |
b.sort() |
|
227 |
blen = len(b) |
|
228 |
||
229 |
for x in unknown: |
|
230 |
bs = bisect.bisect(b, x) |
|
919 | 231 |
if bs != 0 and b[bs-1] == x: |
879 | 232 |
ret[x] = self.map[x] |
233 |
continue |
|
234 |
while bs < blen: |
|
235 |
s = b[bs] |
|
236 |
if len(s) > len(x) and s.startswith(x) and s[len(x)] == '/': |
|
237 |
ret[s] = self.map[s] |
|
238 |
else: |
|
239 |
break |
|
240 |
bs += 1 |
|
241 |
return ret |
|
242 |
||
1062 | 243 |
def walk(self, files=None, match=util.always, dc=None): |
0
9117c6561b0b
Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff
changeset
|
244 |
self.read() |
879 | 245 |
|
723 | 246 |
# walk all files by default |
879 | 247 |
if not files: |
248 |
files = [self.root] |
|
249 |
if not dc: |
|
250 |
dc = self.map.copy() |
|
251 |
elif not dc: |
|
252 |
dc = self.filterfiles(files) |
|
919 | 253 |
|
1183 | 254 |
def statmatch(file, stat): |
1224
cc61d366bc3b
Fix Windows status problem from new dirstate walk code
mpm@selenic.com
parents:
1183
diff
changeset
|
255 |
file = util.pconvert(file) |
1183 | 256 |
if file not in dc and self.ignore(file): |
257 |
return False |
|
258 |
return match(file) |
|
1224
cc61d366bc3b
Fix Windows status problem from new dirstate walk code
mpm@selenic.com
parents:
1183
diff
changeset
|
259 |
|
1183 | 260 |
return self.walkhelper(files=files, statmatch=statmatch, dc=dc) |
261 |
||
262 |
# walk recursively through the directory tree, finding all files |
|
263 |
# matched by the statmatch function |
|
1224
cc61d366bc3b
Fix Windows status problem from new dirstate walk code
mpm@selenic.com
parents:
1183
diff
changeset
|
264 |
# |
1183 | 265 |
# results are yielded in a tuple (src, filename), where src is one of: |
266 |
# 'f' the file was found in the directory tree |
|
267 |
# 'm' the file was only in the dirstate and not in the tree |
|
268 |
# |
|
269 |
# dc is an optional arg for the current dirstate. dc is not modified |
|
270 |
# directly by this function, but might be modified by your statmatch call. |
|
271 |
# |
|
272 |
def walkhelper(self, files, statmatch, dc): |
|
1392
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
273 |
def supported_type(f, st): |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
274 |
if stat.S_ISREG(st.st_mode): |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
275 |
return True |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
276 |
else: |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
277 |
kind = 'unknown' |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
278 |
if stat.S_ISCHR(st.st_mode): kind = 'character device' |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
279 |
elif stat.S_ISBLK(st.st_mode): kind = 'block device' |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
280 |
elif stat.S_ISFIFO(st.st_mode): kind = 'fifo' |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
281 |
elif stat.S_ISLNK(st.st_mode): kind = 'symbolic link' |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
282 |
elif stat.S_ISSOCK(st.st_mode): kind = 'socket' |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
283 |
elif stat.S_ISDIR(st.st_mode): kind = 'directory' |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
284 |
self.ui.warn('%s: unsupported file type (type is %s)\n' % ( |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
285 |
util.pathto(self.getcwd(), f), |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
286 |
kind)) |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
287 |
return False |
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
288 |
|
1183 | 289 |
# recursion free walker, faster than os.walk. |
290 |
def findfiles(s): |
|
291 |
retfiles = [] |
|
292 |
work = [s] |
|
293 |
while work: |
|
294 |
top = work.pop() |
|
295 |
names = os.listdir(top) |
|
296 |
names.sort() |
|
297 |
# nd is the top of the repository dir tree |
|
298 |
nd = util.normpath(top[len(self.root) + 1:]) |
|
299 |
if nd == '.': nd = '' |
|
300 |
for f in names: |
|
301 |
np = os.path.join(nd, f) |
|
302 |
if seen(np): |
|
303 |
continue |
|
304 |
p = os.path.join(top, f) |
|
1228
db950da49539
Fix dangling symlink bug in dirstate walk code
mpm@selenic.com
parents:
1224
diff
changeset
|
305 |
# don't trip over symlinks |
db950da49539
Fix dangling symlink bug in dirstate walk code
mpm@selenic.com
parents:
1224
diff
changeset
|
306 |
st = os.lstat(p) |
1183 | 307 |
if stat.S_ISDIR(st.st_mode): |
308 |
ds = os.path.join(nd, f +'/') |
|
309 |
if statmatch(ds, st): |
|
310 |
work.append(p) |
|
1396
8c3e2a254257
check if a file is ignored before complaining if it is an unsupported type
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1394
diff
changeset
|
311 |
elif statmatch(np, st) and supported_type(np, st): |
8c3e2a254257
check if a file is ignored before complaining if it is an unsupported type
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1394
diff
changeset
|
312 |
yield util.pconvert(np) |
1183 | 313 |
|
1392
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
314 |
|
821
72d9bd4841f3
Ensure that dirstate.walk only yields names once.
Bryan O'Sullivan <bos@serpentine.com>
parents:
820
diff
changeset
|
315 |
known = {'.hg': 1} |
72d9bd4841f3
Ensure that dirstate.walk only yields names once.
Bryan O'Sullivan <bos@serpentine.com>
parents:
820
diff
changeset
|
316 |
def seen(fn): |
72d9bd4841f3
Ensure that dirstate.walk only yields names once.
Bryan O'Sullivan <bos@serpentine.com>
parents:
820
diff
changeset
|
317 |
if fn in known: return True |
72d9bd4841f3
Ensure that dirstate.walk only yields names once.
Bryan O'Sullivan <bos@serpentine.com>
parents:
820
diff
changeset
|
318 |
known[fn] = 1 |
1183 | 319 |
|
320 |
# step one, find all files that match our criteria |
|
321 |
files.sort() |
|
322 |
for ff in util.unique(files): |
|
323 |
f = os.path.join(self.root, ff) |
|
324 |
try: |
|
1230 | 325 |
st = os.lstat(f) |
1183 | 326 |
except OSError, inst: |
327 |
if ff not in dc: self.ui.warn('%s: %s\n' % ( |
|
328 |
util.pathto(self.getcwd(), ff), |
|
329 |
inst.strerror)) |
|
330 |
continue |
|
331 |
if stat.S_ISDIR(st.st_mode): |
|
332 |
sorted = [ x for x in findfiles(f) ] |
|
333 |
sorted.sort() |
|
334 |
for fl in sorted: |
|
335 |
yield 'f', fl |
|
1392
32d8068b3e36
add a check for filetype when walking
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1276
diff
changeset
|
336 |
else: |
1183 | 337 |
ff = util.normpath(ff) |
338 |
if seen(ff): |
|
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
883
diff
changeset
|
339 |
continue |
1183 | 340 |
found = False |
341 |
self.blockignore = True |
|
1396
8c3e2a254257
check if a file is ignored before complaining if it is an unsupported type
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
1394
diff
changeset
|
342 |
if statmatch(ff, st) and supported_type(ff, st): |
1183 | 343 |
found = True |
344 |
self.blockignore = False |
|
345 |
if found: |
|
884
087771ebe2e6
Fix walk code for files that do not exist anywhere, and unhandled types.
Bryan O'Sullivan <bos@serpentine.com>
parents:
883
diff
changeset
|
346 |
yield 'f', ff |
536 | 347 |
|
1183 | 348 |
# step two run through anything left in the dc hash and yield |
349 |
# if we haven't already seen it |
|
350 |
ks = dc.keys() |
|
351 |
ks.sort() |
|
352 |
for k in ks: |
|
353 |
if not seen(k) and (statmatch(k, None)): |
|
726
809a870a0e73
Add a source designator to the walk methods.
Bryan O'Sullivan <bos@serpentine.com>
parents:
725
diff
changeset
|
354 |
yield 'm', k |
669
8aa2a282eda4
.hgignore speedups patch incorporating Matt's feedback.
mwilli2@localhost.localdomain
parents:
667
diff
changeset
|
355 |
|
861
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
356 |
def changes(self, files=None, match=util.always): |
723 | 357 |
self.read() |
879 | 358 |
if not files: |
1183 | 359 |
files = [self.root] |
879 | 360 |
dc = self.map.copy() |
361 |
else: |
|
362 |
dc = self.filterfiles(files) |
|
861
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
363 |
lookup, modified, added, unknown = [], [], [], [] |
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
364 |
removed, deleted = [], [] |
723 | 365 |
|
1183 | 366 |
# statmatch function to eliminate entries from the dirstate copy |
367 |
# and put files into the appropriate array. This gets passed |
|
368 |
# to the walking code |
|
369 |
def statmatch(fn, s): |
|
1224
cc61d366bc3b
Fix Windows status problem from new dirstate walk code
mpm@selenic.com
parents:
1183
diff
changeset
|
370 |
fn = util.pconvert(fn) |
1183 | 371 |
def checkappend(l, fn): |
372 |
if match is util.always or match(fn): |
|
373 |
l.append(fn) |
|
1224
cc61d366bc3b
Fix Windows status problem from new dirstate walk code
mpm@selenic.com
parents:
1183
diff
changeset
|
374 |
|
1183 | 375 |
if not s or stat.S_ISDIR(s.st_mode): |
1268
c631f26346ca
Fix performance of dirstate.changes with ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1245
diff
changeset
|
376 |
if self.ignore(fn): return False |
c631f26346ca
Fix performance of dirstate.changes with ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1245
diff
changeset
|
377 |
return match(fn) |
1183 | 378 |
|
379 |
c = dc.pop(fn, None) |
|
861
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
380 |
if c: |
1183 | 381 |
type, mode, size, time = c |
382 |
# check the common case first |
|
383 |
if type == 'n': |
|
384 |
if size != s.st_size or (mode ^ s.st_mode) & 0100: |
|
385 |
checkappend(modified, fn) |
|
386 |
elif time != s.st_mtime: |
|
387 |
checkappend(lookup, fn) |
|
388 |
elif type == 'm': |
|
389 |
checkappend(modified, fn) |
|
390 |
elif type == 'a': |
|
391 |
checkappend(added, fn) |
|
392 |
elif type == 'r': |
|
393 |
checkappend(unknown, fn) |
|
1270
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
394 |
elif not self.ignore(fn) and match(fn): |
fc3b41570082
Switch to new syntax for .hgignore files.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1268
diff
changeset
|
395 |
unknown.append(fn) |
1183 | 396 |
# return false because we've already handled all cases above. |
397 |
# there's no need for the walking code to process the file |
|
398 |
# any further. |
|
399 |
return False |
|
536 | 400 |
|
1183 | 401 |
# because our statmatch always returns false, self.walk will only |
402 |
# return files in the dirstate map that are not present in the FS. |
|
403 |
# But, we still need to iterate through the results to force the |
|
404 |
# walk to complete |
|
405 |
for src, fn in self.walkhelper(files, statmatch, dc): |
|
406 |
pass |
|
407 |
||
1276
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
408 |
# there may be patterns in the .hgignore file that prevent us |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
409 |
# from examining entire directories in the dirstate map, so we |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
410 |
# go back and explicitly examine any matching files we've |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
411 |
# ignored |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
412 |
unexamined = [fn for fn in dc.iterkeys() |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
413 |
if self.ignore(fn) and match(fn)] |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
414 |
|
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
415 |
for src, fn in self.walkhelper(unexamined, statmatch, dc): |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
416 |
pass |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
417 |
|
1183 | 418 |
# anything left in dc didn't exist in the filesystem |
1276
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
419 |
for fn, c in dc.iteritems(): |
25e5b1086624
Fix dirstate.changes for ignored directories.
Bryan O'Sullivan <bos@serpentine.com>
parents:
1271
diff
changeset
|
420 |
if not match(fn): continue |
861
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
421 |
if c[0] == 'r': |
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
422 |
removed.append(fn) |
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
423 |
else: |
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
424 |
deleted.append(fn) |
cbe5c4d016b7
dirstate.changes() now distinguishes 'hg remove'd or just deleted files.
Thomas Arendsen Hein <thomas@intevation.de>
parents:
856
diff
changeset
|
425 |
return (lookup, modified, added, removed + deleted, unknown) |