##// END OF EJS Templates
treemanifest: make diff() faster...
Martin von Zweigbergk -
r24404:96cccf1e default
parent child Browse files
Show More
@@ -1,643 +1,651 b''
1 # manifest.py - manifest revision class for mercurial
1 # manifest.py - manifest revision class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-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 mdiff, parsers, error, revlog, util, scmutil
9 import mdiff, parsers, error, revlog, util, scmutil
10 import array, struct
10 import array, struct
11
11
12 propertycache = util.propertycache
12 propertycache = util.propertycache
13
13
14 class _lazymanifest(dict):
14 class _lazymanifest(dict):
15 """This is the pure implementation of lazymanifest.
15 """This is the pure implementation of lazymanifest.
16
16
17 It has not been optimized *at all* and is not lazy.
17 It has not been optimized *at all* and is not lazy.
18 """
18 """
19
19
20 def __init__(self, data):
20 def __init__(self, data):
21 # This init method does a little bit of excessive-looking
21 # This init method does a little bit of excessive-looking
22 # precondition checking. This is so that the behavior of this
22 # precondition checking. This is so that the behavior of this
23 # class exactly matches its C counterpart to try and help
23 # class exactly matches its C counterpart to try and help
24 # prevent surprise breakage for anyone that develops against
24 # prevent surprise breakage for anyone that develops against
25 # the pure version.
25 # the pure version.
26 if data and data[-1] != '\n':
26 if data and data[-1] != '\n':
27 raise ValueError('Manifest did not end in a newline.')
27 raise ValueError('Manifest did not end in a newline.')
28 dict.__init__(self)
28 dict.__init__(self)
29 prev = None
29 prev = None
30 for l in data.splitlines():
30 for l in data.splitlines():
31 if prev is not None and prev > l:
31 if prev is not None and prev > l:
32 raise ValueError('Manifest lines not in sorted order.')
32 raise ValueError('Manifest lines not in sorted order.')
33 prev = l
33 prev = l
34 f, n = l.split('\0')
34 f, n = l.split('\0')
35 if len(n) > 40:
35 if len(n) > 40:
36 self[f] = revlog.bin(n[:40]), n[40:]
36 self[f] = revlog.bin(n[:40]), n[40:]
37 else:
37 else:
38 self[f] = revlog.bin(n), ''
38 self[f] = revlog.bin(n), ''
39
39
40 def __setitem__(self, k, v):
40 def __setitem__(self, k, v):
41 node, flag = v
41 node, flag = v
42 assert node is not None
42 assert node is not None
43 if len(node) > 21:
43 if len(node) > 21:
44 node = node[:21] # match c implementation behavior
44 node = node[:21] # match c implementation behavior
45 dict.__setitem__(self, k, (node, flag))
45 dict.__setitem__(self, k, (node, flag))
46
46
47 def __iter__(self):
47 def __iter__(self):
48 return iter(sorted(dict.keys(self)))
48 return iter(sorted(dict.keys(self)))
49
49
50 def iterkeys(self):
50 def iterkeys(self):
51 return iter(sorted(dict.keys(self)))
51 return iter(sorted(dict.keys(self)))
52
52
53 def iterentries(self):
53 def iterentries(self):
54 return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
54 return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
55
55
56 def copy(self):
56 def copy(self):
57 c = _lazymanifest('')
57 c = _lazymanifest('')
58 c.update(self)
58 c.update(self)
59 return c
59 return c
60
60
61 def diff(self, m2, clean=False):
61 def diff(self, m2, clean=False):
62 '''Finds changes between the current manifest and m2.'''
62 '''Finds changes between the current manifest and m2.'''
63 diff = {}
63 diff = {}
64
64
65 for fn, e1 in self.iteritems():
65 for fn, e1 in self.iteritems():
66 if fn not in m2:
66 if fn not in m2:
67 diff[fn] = e1, (None, '')
67 diff[fn] = e1, (None, '')
68 else:
68 else:
69 e2 = m2[fn]
69 e2 = m2[fn]
70 if e1 != e2:
70 if e1 != e2:
71 diff[fn] = e1, e2
71 diff[fn] = e1, e2
72 elif clean:
72 elif clean:
73 diff[fn] = None
73 diff[fn] = None
74
74
75 for fn, e2 in m2.iteritems():
75 for fn, e2 in m2.iteritems():
76 if fn not in self:
76 if fn not in self:
77 diff[fn] = (None, ''), e2
77 diff[fn] = (None, ''), e2
78
78
79 return diff
79 return diff
80
80
81 def filtercopy(self, filterfn):
81 def filtercopy(self, filterfn):
82 c = _lazymanifest('')
82 c = _lazymanifest('')
83 for f, n, fl in self.iterentries():
83 for f, n, fl in self.iterentries():
84 if filterfn(f):
84 if filterfn(f):
85 c[f] = n, fl
85 c[f] = n, fl
86 return c
86 return c
87
87
88 def text(self):
88 def text(self):
89 """Get the full data of this manifest as a bytestring."""
89 """Get the full data of this manifest as a bytestring."""
90 fl = sorted(self.iterentries())
90 fl = sorted(self.iterentries())
91
91
92 _hex = revlog.hex
92 _hex = revlog.hex
93 # if this is changed to support newlines in filenames,
93 # if this is changed to support newlines in filenames,
94 # be sure to check the templates/ dir again (especially *-raw.tmpl)
94 # be sure to check the templates/ dir again (especially *-raw.tmpl)
95 return ''.join("%s\0%s%s\n" % (
95 return ''.join("%s\0%s%s\n" % (
96 f, _hex(n[:20]), flag) for f, n, flag in fl)
96 f, _hex(n[:20]), flag) for f, n, flag in fl)
97
97
98 try:
98 try:
99 _lazymanifest = parsers.lazymanifest
99 _lazymanifest = parsers.lazymanifest
100 except AttributeError:
100 except AttributeError:
101 pass
101 pass
102
102
103 class manifestdict(object):
103 class manifestdict(object):
104 def __init__(self, data=''):
104 def __init__(self, data=''):
105 self._lm = _lazymanifest(data)
105 self._lm = _lazymanifest(data)
106
106
107 def __getitem__(self, key):
107 def __getitem__(self, key):
108 return self._lm[key][0]
108 return self._lm[key][0]
109
109
110 def find(self, key):
110 def find(self, key):
111 return self._lm[key]
111 return self._lm[key]
112
112
113 def __len__(self):
113 def __len__(self):
114 return len(self._lm)
114 return len(self._lm)
115
115
116 def __setitem__(self, key, node):
116 def __setitem__(self, key, node):
117 self._lm[key] = node, self.flags(key, '')
117 self._lm[key] = node, self.flags(key, '')
118
118
119 def __contains__(self, key):
119 def __contains__(self, key):
120 return key in self._lm
120 return key in self._lm
121
121
122 def __delitem__(self, key):
122 def __delitem__(self, key):
123 del self._lm[key]
123 del self._lm[key]
124
124
125 def __iter__(self):
125 def __iter__(self):
126 return self._lm.__iter__()
126 return self._lm.__iter__()
127
127
128 def iterkeys(self):
128 def iterkeys(self):
129 return self._lm.iterkeys()
129 return self._lm.iterkeys()
130
130
131 def keys(self):
131 def keys(self):
132 return list(self.iterkeys())
132 return list(self.iterkeys())
133
133
134 def intersectfiles(self, files):
134 def intersectfiles(self, files):
135 '''make a new lazymanifest with the intersection of self with files
135 '''make a new lazymanifest with the intersection of self with files
136
136
137 The algorithm assumes that files is much smaller than self.'''
137 The algorithm assumes that files is much smaller than self.'''
138 ret = manifestdict()
138 ret = manifestdict()
139 lm = self._lm
139 lm = self._lm
140 for fn in files:
140 for fn in files:
141 if fn in lm:
141 if fn in lm:
142 ret._lm[fn] = self._lm[fn]
142 ret._lm[fn] = self._lm[fn]
143 return ret
143 return ret
144
144
145 def filesnotin(self, m2):
145 def filesnotin(self, m2):
146 '''Set of files in this manifest that are not in the other'''
146 '''Set of files in this manifest that are not in the other'''
147 files = set(self)
147 files = set(self)
148 files.difference_update(m2)
148 files.difference_update(m2)
149 return files
149 return files
150
150
151 @propertycache
151 @propertycache
152 def _dirs(self):
152 def _dirs(self):
153 return scmutil.dirs(self)
153 return scmutil.dirs(self)
154
154
155 def dirs(self):
155 def dirs(self):
156 return self._dirs
156 return self._dirs
157
157
158 def hasdir(self, dir):
158 def hasdir(self, dir):
159 return dir in self._dirs
159 return dir in self._dirs
160
160
161 def matches(self, match):
161 def matches(self, match):
162 '''generate a new manifest filtered by the match argument'''
162 '''generate a new manifest filtered by the match argument'''
163 if match.always():
163 if match.always():
164 return self.copy()
164 return self.copy()
165
165
166 files = match.files()
166 files = match.files()
167 if (len(files) < 100 and (match.matchfn == match.exact or
167 if (len(files) < 100 and (match.matchfn == match.exact or
168 (not match.anypats() and util.all(fn in self for fn in files)))):
168 (not match.anypats() and util.all(fn in self for fn in files)))):
169 return self.intersectfiles(files)
169 return self.intersectfiles(files)
170
170
171 lm = manifestdict('')
171 lm = manifestdict('')
172 lm._lm = self._lm.filtercopy(match)
172 lm._lm = self._lm.filtercopy(match)
173 return lm
173 return lm
174
174
175 def diff(self, m2, clean=False):
175 def diff(self, m2, clean=False):
176 '''Finds changes between the current manifest and m2.
176 '''Finds changes between the current manifest and m2.
177
177
178 Args:
178 Args:
179 m2: the manifest to which this manifest should be compared.
179 m2: the manifest to which this manifest should be compared.
180 clean: if true, include files unchanged between these manifests
180 clean: if true, include files unchanged between these manifests
181 with a None value in the returned dictionary.
181 with a None value in the returned dictionary.
182
182
183 The result is returned as a dict with filename as key and
183 The result is returned as a dict with filename as key and
184 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
184 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
185 nodeid in the current/other manifest and fl1/fl2 is the flag
185 nodeid in the current/other manifest and fl1/fl2 is the flag
186 in the current/other manifest. Where the file does not exist,
186 in the current/other manifest. Where the file does not exist,
187 the nodeid will be None and the flags will be the empty
187 the nodeid will be None and the flags will be the empty
188 string.
188 string.
189 '''
189 '''
190 return self._lm.diff(m2._lm, clean)
190 return self._lm.diff(m2._lm, clean)
191
191
192 def setflag(self, key, flag):
192 def setflag(self, key, flag):
193 self._lm[key] = self[key], flag
193 self._lm[key] = self[key], flag
194
194
195 def get(self, key, default=None):
195 def get(self, key, default=None):
196 try:
196 try:
197 return self._lm[key][0]
197 return self._lm[key][0]
198 except KeyError:
198 except KeyError:
199 return default
199 return default
200
200
201 def flags(self, key, default=''):
201 def flags(self, key, default=''):
202 try:
202 try:
203 return self._lm[key][1]
203 return self._lm[key][1]
204 except KeyError:
204 except KeyError:
205 return default
205 return default
206
206
207 def copy(self):
207 def copy(self):
208 c = manifestdict('')
208 c = manifestdict('')
209 c._lm = self._lm.copy()
209 c._lm = self._lm.copy()
210 return c
210 return c
211
211
212 def iteritems(self):
212 def iteritems(self):
213 return (x[:2] for x in self._lm.iterentries())
213 return (x[:2] for x in self._lm.iterentries())
214
214
215 def text(self):
215 def text(self):
216 return self._lm.text()
216 return self._lm.text()
217
217
218 def fastdelta(self, base, changes):
218 def fastdelta(self, base, changes):
219 """Given a base manifest text as an array.array and a list of changes
219 """Given a base manifest text as an array.array and a list of changes
220 relative to that text, compute a delta that can be used by revlog.
220 relative to that text, compute a delta that can be used by revlog.
221 """
221 """
222 delta = []
222 delta = []
223 dstart = None
223 dstart = None
224 dend = None
224 dend = None
225 dline = [""]
225 dline = [""]
226 start = 0
226 start = 0
227 # zero copy representation of base as a buffer
227 # zero copy representation of base as a buffer
228 addbuf = util.buffer(base)
228 addbuf = util.buffer(base)
229
229
230 # start with a readonly loop that finds the offset of
230 # start with a readonly loop that finds the offset of
231 # each line and creates the deltas
231 # each line and creates the deltas
232 for f, todelete in changes:
232 for f, todelete in changes:
233 # bs will either be the index of the item or the insert point
233 # bs will either be the index of the item or the insert point
234 start, end = _msearch(addbuf, f, start)
234 start, end = _msearch(addbuf, f, start)
235 if not todelete:
235 if not todelete:
236 h, fl = self._lm[f]
236 h, fl = self._lm[f]
237 l = "%s\0%s%s\n" % (f, revlog.hex(h), fl)
237 l = "%s\0%s%s\n" % (f, revlog.hex(h), fl)
238 else:
238 else:
239 if start == end:
239 if start == end:
240 # item we want to delete was not found, error out
240 # item we want to delete was not found, error out
241 raise AssertionError(
241 raise AssertionError(
242 _("failed to remove %s from manifest") % f)
242 _("failed to remove %s from manifest") % f)
243 l = ""
243 l = ""
244 if dstart is not None and dstart <= start and dend >= start:
244 if dstart is not None and dstart <= start and dend >= start:
245 if dend < end:
245 if dend < end:
246 dend = end
246 dend = end
247 if l:
247 if l:
248 dline.append(l)
248 dline.append(l)
249 else:
249 else:
250 if dstart is not None:
250 if dstart is not None:
251 delta.append([dstart, dend, "".join(dline)])
251 delta.append([dstart, dend, "".join(dline)])
252 dstart = start
252 dstart = start
253 dend = end
253 dend = end
254 dline = [l]
254 dline = [l]
255
255
256 if dstart is not None:
256 if dstart is not None:
257 delta.append([dstart, dend, "".join(dline)])
257 delta.append([dstart, dend, "".join(dline)])
258 # apply the delta to the base, and get a delta for addrevision
258 # apply the delta to the base, and get a delta for addrevision
259 deltatext, arraytext = _addlistdelta(base, delta)
259 deltatext, arraytext = _addlistdelta(base, delta)
260 return arraytext, deltatext
260 return arraytext, deltatext
261
261
262 def _msearch(m, s, lo=0, hi=None):
262 def _msearch(m, s, lo=0, hi=None):
263 '''return a tuple (start, end) that says where to find s within m.
263 '''return a tuple (start, end) that says where to find s within m.
264
264
265 If the string is found m[start:end] are the line containing
265 If the string is found m[start:end] are the line containing
266 that string. If start == end the string was not found and
266 that string. If start == end the string was not found and
267 they indicate the proper sorted insertion point.
267 they indicate the proper sorted insertion point.
268
268
269 m should be a buffer or a string
269 m should be a buffer or a string
270 s is a string'''
270 s is a string'''
271 def advance(i, c):
271 def advance(i, c):
272 while i < lenm and m[i] != c:
272 while i < lenm and m[i] != c:
273 i += 1
273 i += 1
274 return i
274 return i
275 if not s:
275 if not s:
276 return (lo, lo)
276 return (lo, lo)
277 lenm = len(m)
277 lenm = len(m)
278 if not hi:
278 if not hi:
279 hi = lenm
279 hi = lenm
280 while lo < hi:
280 while lo < hi:
281 mid = (lo + hi) // 2
281 mid = (lo + hi) // 2
282 start = mid
282 start = mid
283 while start > 0 and m[start - 1] != '\n':
283 while start > 0 and m[start - 1] != '\n':
284 start -= 1
284 start -= 1
285 end = advance(start, '\0')
285 end = advance(start, '\0')
286 if m[start:end] < s:
286 if m[start:end] < s:
287 # we know that after the null there are 40 bytes of sha1
287 # we know that after the null there are 40 bytes of sha1
288 # this translates to the bisect lo = mid + 1
288 # this translates to the bisect lo = mid + 1
289 lo = advance(end + 40, '\n') + 1
289 lo = advance(end + 40, '\n') + 1
290 else:
290 else:
291 # this translates to the bisect hi = mid
291 # this translates to the bisect hi = mid
292 hi = start
292 hi = start
293 end = advance(lo, '\0')
293 end = advance(lo, '\0')
294 found = m[lo:end]
294 found = m[lo:end]
295 if s == found:
295 if s == found:
296 # we know that after the null there are 40 bytes of sha1
296 # we know that after the null there are 40 bytes of sha1
297 end = advance(end + 40, '\n')
297 end = advance(end + 40, '\n')
298 return (lo, end + 1)
298 return (lo, end + 1)
299 else:
299 else:
300 return (lo, lo)
300 return (lo, lo)
301
301
302 def _checkforbidden(l):
302 def _checkforbidden(l):
303 """Check filenames for illegal characters."""
303 """Check filenames for illegal characters."""
304 for f in l:
304 for f in l:
305 if '\n' in f or '\r' in f:
305 if '\n' in f or '\r' in f:
306 raise error.RevlogError(
306 raise error.RevlogError(
307 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
307 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
308
308
309
309
310 # apply the changes collected during the bisect loop to our addlist
310 # apply the changes collected during the bisect loop to our addlist
311 # return a delta suitable for addrevision
311 # return a delta suitable for addrevision
312 def _addlistdelta(addlist, x):
312 def _addlistdelta(addlist, x):
313 # for large addlist arrays, building a new array is cheaper
313 # for large addlist arrays, building a new array is cheaper
314 # than repeatedly modifying the existing one
314 # than repeatedly modifying the existing one
315 currentposition = 0
315 currentposition = 0
316 newaddlist = array.array('c')
316 newaddlist = array.array('c')
317
317
318 for start, end, content in x:
318 for start, end, content in x:
319 newaddlist += addlist[currentposition:start]
319 newaddlist += addlist[currentposition:start]
320 if content:
320 if content:
321 newaddlist += array.array('c', content)
321 newaddlist += array.array('c', content)
322
322
323 currentposition = end
323 currentposition = end
324
324
325 newaddlist += addlist[currentposition:]
325 newaddlist += addlist[currentposition:]
326
326
327 deltatext = "".join(struct.pack(">lll", start, end, len(content))
327 deltatext = "".join(struct.pack(">lll", start, end, len(content))
328 + content for start, end, content in x)
328 + content for start, end, content in x)
329 return deltatext, newaddlist
329 return deltatext, newaddlist
330
330
331 def _splittopdir(f):
331 def _splittopdir(f):
332 if '/' in f:
332 if '/' in f:
333 dir, subpath = f.split('/', 1)
333 dir, subpath = f.split('/', 1)
334 return dir + '/', subpath
334 return dir + '/', subpath
335 else:
335 else:
336 return '', f
336 return '', f
337
337
338 class treemanifest(object):
338 class treemanifest(object):
339 def __init__(self, dir='', text=''):
339 def __init__(self, dir='', text=''):
340 self._dir = dir
340 self._dir = dir
341 self._dirs = {}
341 self._dirs = {}
342 # Using _lazymanifest here is a little slower than plain old dicts
342 # Using _lazymanifest here is a little slower than plain old dicts
343 self._files = {}
343 self._files = {}
344 self._flags = {}
344 self._flags = {}
345 lm = _lazymanifest(text)
345 lm = _lazymanifest(text)
346 for f, n, fl in lm.iterentries():
346 for f, n, fl in lm.iterentries():
347 self[f] = n
347 self[f] = n
348 if fl:
348 if fl:
349 self.setflag(f, fl)
349 self.setflag(f, fl)
350
350
351 def _subpath(self, path):
351 def _subpath(self, path):
352 return self._dir + path
352 return self._dir + path
353
353
354 def __len__(self):
354 def __len__(self):
355 size = len(self._files)
355 size = len(self._files)
356 for m in self._dirs.values():
356 for m in self._dirs.values():
357 size += m.__len__()
357 size += m.__len__()
358 return size
358 return size
359
359
360 def __str__(self):
360 def __str__(self):
361 return '<treemanifest dir=%s>' % self._dir
361 return '<treemanifest dir=%s>' % self._dir
362
362
363 def iteritems(self):
363 def iteritems(self):
364 for p, n in sorted(self._dirs.items() + self._files.items()):
364 for p, n in sorted(self._dirs.items() + self._files.items()):
365 if p in self._files:
365 if p in self._files:
366 yield self._subpath(p), n
366 yield self._subpath(p), n
367 else:
367 else:
368 for f, sn in n.iteritems():
368 for f, sn in n.iteritems():
369 yield f, sn
369 yield f, sn
370
370
371 def iterkeys(self):
371 def iterkeys(self):
372 for p in sorted(self._dirs.keys() + self._files.keys()):
372 for p in sorted(self._dirs.keys() + self._files.keys()):
373 if p in self._files:
373 if p in self._files:
374 yield self._subpath(p)
374 yield self._subpath(p)
375 else:
375 else:
376 for f in self._dirs[p].iterkeys():
376 for f in self._dirs[p].iterkeys():
377 yield f
377 yield f
378
378
379 def keys(self):
379 def keys(self):
380 return list(self.iterkeys())
380 return list(self.iterkeys())
381
381
382 def __iter__(self):
382 def __iter__(self):
383 return self.iterkeys()
383 return self.iterkeys()
384
384
385 def __contains__(self, f):
385 def __contains__(self, f):
386 if f is None:
386 if f is None:
387 return False
387 return False
388 dir, subpath = _splittopdir(f)
388 dir, subpath = _splittopdir(f)
389 if dir:
389 if dir:
390 if dir not in self._dirs:
390 if dir not in self._dirs:
391 return False
391 return False
392 return self._dirs[dir].__contains__(subpath)
392 return self._dirs[dir].__contains__(subpath)
393 else:
393 else:
394 return f in self._files
394 return f in self._files
395
395
396 def get(self, f, default=None):
396 def get(self, f, default=None):
397 dir, subpath = _splittopdir(f)
397 dir, subpath = _splittopdir(f)
398 if dir:
398 if dir:
399 if dir not in self._dirs:
399 if dir not in self._dirs:
400 return default
400 return default
401 return self._dirs[dir].get(subpath, default)
401 return self._dirs[dir].get(subpath, default)
402 else:
402 else:
403 return self._files.get(f, default)
403 return self._files.get(f, default)
404
404
405 def __getitem__(self, f):
405 def __getitem__(self, f):
406 dir, subpath = _splittopdir(f)
406 dir, subpath = _splittopdir(f)
407 if dir:
407 if dir:
408 return self._dirs[dir].__getitem__(subpath)
408 return self._dirs[dir].__getitem__(subpath)
409 else:
409 else:
410 return self._files[f]
410 return self._files[f]
411
411
412 def flags(self, f):
412 def flags(self, f):
413 dir, subpath = _splittopdir(f)
413 dir, subpath = _splittopdir(f)
414 if dir:
414 if dir:
415 if dir not in self._dirs:
415 if dir not in self._dirs:
416 return ''
416 return ''
417 return self._dirs[dir].flags(subpath)
417 return self._dirs[dir].flags(subpath)
418 else:
418 else:
419 if f in self._dirs:
419 if f in self._dirs:
420 return ''
420 return ''
421 return self._flags.get(f, '')
421 return self._flags.get(f, '')
422
422
423 def find(self, f):
423 def find(self, f):
424 dir, subpath = _splittopdir(f)
424 dir, subpath = _splittopdir(f)
425 if dir:
425 if dir:
426 return self._dirs[dir].find(subpath)
426 return self._dirs[dir].find(subpath)
427 else:
427 else:
428 return self._files[f], self._flags.get(f, '')
428 return self._files[f], self._flags.get(f, '')
429
429
430 def __delitem__(self, f):
430 def __delitem__(self, f):
431 dir, subpath = _splittopdir(f)
431 dir, subpath = _splittopdir(f)
432 if dir:
432 if dir:
433 self._dirs[dir].__delitem__(subpath)
433 self._dirs[dir].__delitem__(subpath)
434 # If the directory is now empty, remove it
434 # If the directory is now empty, remove it
435 if not self._dirs[dir]._dirs and not self._dirs[dir]._files:
435 if not self._dirs[dir]._dirs and not self._dirs[dir]._files:
436 del self._dirs[dir]
436 del self._dirs[dir]
437 else:
437 else:
438 del self._files[f]
438 del self._files[f]
439 if f in self._flags:
439 if f in self._flags:
440 del self._flags[f]
440 del self._flags[f]
441
441
442 def __setitem__(self, f, n):
442 def __setitem__(self, f, n):
443 assert n is not None
443 assert n is not None
444 dir, subpath = _splittopdir(f)
444 dir, subpath = _splittopdir(f)
445 if dir:
445 if dir:
446 if dir not in self._dirs:
446 if dir not in self._dirs:
447 self._dirs[dir] = treemanifest(self._subpath(dir))
447 self._dirs[dir] = treemanifest(self._subpath(dir))
448 self._dirs[dir].__setitem__(subpath, n)
448 self._dirs[dir].__setitem__(subpath, n)
449 else:
449 else:
450 self._files[f] = n
450 self._files[f] = n
451
451
452 def setflag(self, f, flags):
452 def setflag(self, f, flags):
453 """Set the flags (symlink, executable) for path f."""
453 """Set the flags (symlink, executable) for path f."""
454 dir, subpath = _splittopdir(f)
454 dir, subpath = _splittopdir(f)
455 if dir:
455 if dir:
456 if dir not in self._dirs:
456 if dir not in self._dirs:
457 self._dirs[dir] = treemanifest(self._subpath(dir))
457 self._dirs[dir] = treemanifest(self._subpath(dir))
458 self._dirs[dir].setflag(subpath, flags)
458 self._dirs[dir].setflag(subpath, flags)
459 else:
459 else:
460 self._flags[f] = flags
460 self._flags[f] = flags
461
461
462 def copy(self):
462 def copy(self):
463 copy = treemanifest(self._dir)
463 copy = treemanifest(self._dir)
464 for d in self._dirs:
464 for d in self._dirs:
465 copy._dirs[d] = self._dirs[d].copy()
465 copy._dirs[d] = self._dirs[d].copy()
466 copy._files = dict.copy(self._files)
466 copy._files = dict.copy(self._files)
467 copy._flags = dict.copy(self._flags)
467 copy._flags = dict.copy(self._flags)
468 return copy
468 return copy
469
469
470 def intersectfiles(self, files):
470 def intersectfiles(self, files):
471 '''make a new treemanifest with the intersection of self with files
471 '''make a new treemanifest with the intersection of self with files
472
472
473 The algorithm assumes that files is much smaller than self.'''
473 The algorithm assumes that files is much smaller than self.'''
474 ret = treemanifest()
474 ret = treemanifest()
475 for fn in files:
475 for fn in files:
476 if fn in self:
476 if fn in self:
477 ret[fn] = self[fn]
477 ret[fn] = self[fn]
478 flags = self.flags(fn)
478 flags = self.flags(fn)
479 if flags:
479 if flags:
480 ret.setflag(fn, flags)
480 ret.setflag(fn, flags)
481 return ret
481 return ret
482
482
483 def filesnotin(self, m2):
483 def filesnotin(self, m2):
484 '''Set of files in this manifest that are not in the other'''
484 '''Set of files in this manifest that are not in the other'''
485 files = set(self.iterkeys())
485 files = set(self.iterkeys())
486 files.difference_update(m2.iterkeys())
486 files.difference_update(m2.iterkeys())
487 return files
487 return files
488
488
489 @propertycache
489 @propertycache
490 def _alldirs(self):
490 def _alldirs(self):
491 return scmutil.dirs(self)
491 return scmutil.dirs(self)
492
492
493 def dirs(self):
493 def dirs(self):
494 return self._alldirs
494 return self._alldirs
495
495
496 def hasdir(self, dir):
496 def hasdir(self, dir):
497 return dir in self._alldirs
497 return dir in self._alldirs
498
498
499 def matches(self, match):
499 def matches(self, match):
500 '''generate a new manifest filtered by the match argument'''
500 '''generate a new manifest filtered by the match argument'''
501 if match.always():
501 if match.always():
502 return self.copy()
502 return self.copy()
503
503
504 files = match.files()
504 files = match.files()
505 if (match.matchfn == match.exact or
505 if (match.matchfn == match.exact or
506 (not match.anypats() and util.all(fn in self for fn in files))):
506 (not match.anypats() and util.all(fn in self for fn in files))):
507 return self.intersectfiles(files)
507 return self.intersectfiles(files)
508
508
509 m = self.copy()
509 m = self.copy()
510 for fn in m.keys():
510 for fn in m.keys():
511 if not match(fn):
511 if not match(fn):
512 del m[fn]
512 del m[fn]
513 return m
513 return m
514
514
515 def diff(self, m2, clean=False):
515 def diff(self, m2, clean=False):
516 '''Finds changes between the current manifest and m2.
516 '''Finds changes between the current manifest and m2.
517
517
518 Args:
518 Args:
519 m2: the manifest to which this manifest should be compared.
519 m2: the manifest to which this manifest should be compared.
520 clean: if true, include files unchanged between these manifests
520 clean: if true, include files unchanged between these manifests
521 with a None value in the returned dictionary.
521 with a None value in the returned dictionary.
522
522
523 The result is returned as a dict with filename as key and
523 The result is returned as a dict with filename as key and
524 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
524 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
525 nodeid in the current/other manifest and fl1/fl2 is the flag
525 nodeid in the current/other manifest and fl1/fl2 is the flag
526 in the current/other manifest. Where the file does not exist,
526 in the current/other manifest. Where the file does not exist,
527 the nodeid will be None and the flags will be the empty
527 the nodeid will be None and the flags will be the empty
528 string.
528 string.
529 '''
529 '''
530 diff = {}
530 result = {}
531 emptytree = treemanifest()
532 def _diff(t1, t2):
533 for d, m1 in t1._dirs.iteritems():
534 m2 = t2._dirs.get(d, emptytree)
535 _diff(m1, m2)
536
537 for d, m2 in t2._dirs.iteritems():
538 if d not in t1._dirs:
539 _diff(emptytree, m2)
531
540
532 for fn, n1 in self.iteritems():
541 for fn, n1 in t1._files.iteritems():
533 fl1 = self.flags(fn)
542 fl1 = t1._flags.get(fn, '')
534 n2 = m2.get(fn, None)
543 n2 = t2._files.get(fn, None)
535 fl2 = m2.flags(fn)
544 fl2 = t2._flags.get(fn, '')
536 if n2 is None:
545 if n1 != n2 or fl1 != fl2:
537 fl2 = ''
546 result[t1._subpath(fn)] = ((n1, fl1), (n2, fl2))
538 if n1 != n2 or fl1 != fl2:
547 elif clean:
539 diff[fn] = ((n1, fl1), (n2, fl2))
548 result[t1._subpath(fn)] = None
540 elif clean:
541 diff[fn] = None
542
549
543 for fn, n2 in m2.iteritems():
550 for fn, n2 in t2._files.iteritems():
544 if fn not in self:
551 if fn not in t1._files:
545 fl2 = m2.flags(fn)
552 fl2 = t2._flags.get(fn, '')
546 diff[fn] = ((None, ''), (n2, fl2))
553 result[t2._subpath(fn)] = ((None, ''), (n2, fl2))
547
554
548 return diff
555 _diff(self, m2)
556 return result
549
557
550 def text(self):
558 def text(self):
551 """Get the full data of this manifest as a bytestring."""
559 """Get the full data of this manifest as a bytestring."""
552 fl = self.keys()
560 fl = self.keys()
553 _checkforbidden(fl)
561 _checkforbidden(fl)
554
562
555 hex, flags = revlog.hex, self.flags
563 hex, flags = revlog.hex, self.flags
556 # if this is changed to support newlines in filenames,
564 # if this is changed to support newlines in filenames,
557 # be sure to check the templates/ dir again (especially *-raw.tmpl)
565 # be sure to check the templates/ dir again (especially *-raw.tmpl)
558 return ''.join("%s\0%s%s\n" % (f, hex(self[f]), flags(f)) for f in fl)
566 return ''.join("%s\0%s%s\n" % (f, hex(self[f]), flags(f)) for f in fl)
559
567
560 class manifest(revlog.revlog):
568 class manifest(revlog.revlog):
561 def __init__(self, opener):
569 def __init__(self, opener):
562 # During normal operations, we expect to deal with not more than four
570 # During normal operations, we expect to deal with not more than four
563 # revs at a time (such as during commit --amend). When rebasing large
571 # revs at a time (such as during commit --amend). When rebasing large
564 # stacks of commits, the number can go up, hence the config knob below.
572 # stacks of commits, the number can go up, hence the config knob below.
565 cachesize = 4
573 cachesize = 4
566 usetreemanifest = False
574 usetreemanifest = False
567 opts = getattr(opener, 'options', None)
575 opts = getattr(opener, 'options', None)
568 if opts is not None:
576 if opts is not None:
569 cachesize = opts.get('manifestcachesize', cachesize)
577 cachesize = opts.get('manifestcachesize', cachesize)
570 usetreemanifest = opts.get('usetreemanifest', usetreemanifest)
578 usetreemanifest = opts.get('usetreemanifest', usetreemanifest)
571 self._mancache = util.lrucachedict(cachesize)
579 self._mancache = util.lrucachedict(cachesize)
572 revlog.revlog.__init__(self, opener, "00manifest.i")
580 revlog.revlog.__init__(self, opener, "00manifest.i")
573 self._usetreemanifest = usetreemanifest
581 self._usetreemanifest = usetreemanifest
574
582
575 def _newmanifest(self, data=''):
583 def _newmanifest(self, data=''):
576 if self._usetreemanifest:
584 if self._usetreemanifest:
577 return treemanifest('', data)
585 return treemanifest('', data)
578 return manifestdict(data)
586 return manifestdict(data)
579
587
580 def readdelta(self, node):
588 def readdelta(self, node):
581 r = self.rev(node)
589 r = self.rev(node)
582 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
590 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
583 return self._newmanifest(d)
591 return self._newmanifest(d)
584
592
585 def readfast(self, node):
593 def readfast(self, node):
586 '''use the faster of readdelta or read'''
594 '''use the faster of readdelta or read'''
587 r = self.rev(node)
595 r = self.rev(node)
588 deltaparent = self.deltaparent(r)
596 deltaparent = self.deltaparent(r)
589 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
597 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
590 return self.readdelta(node)
598 return self.readdelta(node)
591 return self.read(node)
599 return self.read(node)
592
600
593 def read(self, node):
601 def read(self, node):
594 if node == revlog.nullid:
602 if node == revlog.nullid:
595 return self._newmanifest() # don't upset local cache
603 return self._newmanifest() # don't upset local cache
596 if node in self._mancache:
604 if node in self._mancache:
597 return self._mancache[node][0]
605 return self._mancache[node][0]
598 text = self.revision(node)
606 text = self.revision(node)
599 arraytext = array.array('c', text)
607 arraytext = array.array('c', text)
600 m = self._newmanifest(text)
608 m = self._newmanifest(text)
601 self._mancache[node] = (m, arraytext)
609 self._mancache[node] = (m, arraytext)
602 return m
610 return m
603
611
604 def find(self, node, f):
612 def find(self, node, f):
605 '''look up entry for a single file efficiently.
613 '''look up entry for a single file efficiently.
606 return (node, flags) pair if found, (None, None) if not.'''
614 return (node, flags) pair if found, (None, None) if not.'''
607 m = self.read(node)
615 m = self.read(node)
608 try:
616 try:
609 return m.find(f)
617 return m.find(f)
610 except KeyError:
618 except KeyError:
611 return None, None
619 return None, None
612
620
613 def add(self, m, transaction, link, p1, p2, added, removed):
621 def add(self, m, transaction, link, p1, p2, added, removed):
614 if p1 in self._mancache and not self._usetreemanifest:
622 if p1 in self._mancache and not self._usetreemanifest:
615 # If our first parent is in the manifest cache, we can
623 # If our first parent is in the manifest cache, we can
616 # compute a delta here using properties we know about the
624 # compute a delta here using properties we know about the
617 # manifest up-front, which may save time later for the
625 # manifest up-front, which may save time later for the
618 # revlog layer.
626 # revlog layer.
619
627
620 _checkforbidden(added)
628 _checkforbidden(added)
621 # combine the changed lists into one list for sorting
629 # combine the changed lists into one list for sorting
622 work = [(x, False) for x in added]
630 work = [(x, False) for x in added]
623 work.extend((x, True) for x in removed)
631 work.extend((x, True) for x in removed)
624 # this could use heapq.merge() (from Python 2.6+) or equivalent
632 # this could use heapq.merge() (from Python 2.6+) or equivalent
625 # since the lists are already sorted
633 # since the lists are already sorted
626 work.sort()
634 work.sort()
627
635
628 arraytext, deltatext = m.fastdelta(self._mancache[p1][1], work)
636 arraytext, deltatext = m.fastdelta(self._mancache[p1][1], work)
629 cachedelta = self.rev(p1), deltatext
637 cachedelta = self.rev(p1), deltatext
630 text = util.buffer(arraytext)
638 text = util.buffer(arraytext)
631 else:
639 else:
632 # The first parent manifest isn't already loaded, so we'll
640 # The first parent manifest isn't already loaded, so we'll
633 # just encode a fulltext of the manifest and pass that
641 # just encode a fulltext of the manifest and pass that
634 # through to the revlog layer, and let it handle the delta
642 # through to the revlog layer, and let it handle the delta
635 # process.
643 # process.
636 text = m.text()
644 text = m.text()
637 arraytext = array.array('c', text)
645 arraytext = array.array('c', text)
638 cachedelta = None
646 cachedelta = None
639
647
640 n = self.addrevision(text, transaction, link, p1, p2, cachedelta)
648 n = self.addrevision(text, transaction, link, p1, p2, cachedelta)
641 self._mancache[n] = (m, arraytext)
649 self._mancache[n] = (m, arraytext)
642
650
643 return n
651 return n
General Comments 0
You need to be logged in to leave comments. Login now