##// END OF EJS Templates
revlog: return lazy set from findcommonmissing...
Durham Goode -
r20073:eeba4eaf default
parent child Browse files
Show More
@@ -1,1348 +1,1370 b''
1 # revlog.py - storage back-end for mercurial
1 # revlog.py - storage back-end 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 """Storage back-end for Mercurial.
8 """Storage back-end for Mercurial.
9
9
10 This provides efficient delta storage with O(1) retrieve and append
10 This provides efficient delta storage with O(1) retrieve and append
11 and O(changes) merge between branches.
11 and O(changes) merge between branches.
12 """
12 """
13
13
14 # import stuff from node for others to import from revlog
14 # import stuff from node for others to import from revlog
15 from node import bin, hex, nullid, nullrev
15 from node import bin, hex, nullid, nullrev
16 from i18n import _
16 from i18n import _
17 import ancestor, mdiff, parsers, error, util, templatefilters
17 import ancestor, mdiff, parsers, error, util, templatefilters
18 import struct, zlib, errno
18 import struct, zlib, errno
19
19
20 _pack = struct.pack
20 _pack = struct.pack
21 _unpack = struct.unpack
21 _unpack = struct.unpack
22 _compress = zlib.compress
22 _compress = zlib.compress
23 _decompress = zlib.decompress
23 _decompress = zlib.decompress
24 _sha = util.sha1
24 _sha = util.sha1
25
25
26 # revlog header flags
26 # revlog header flags
27 REVLOGV0 = 0
27 REVLOGV0 = 0
28 REVLOGNG = 1
28 REVLOGNG = 1
29 REVLOGNGINLINEDATA = (1 << 16)
29 REVLOGNGINLINEDATA = (1 << 16)
30 REVLOGGENERALDELTA = (1 << 17)
30 REVLOGGENERALDELTA = (1 << 17)
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
32 REVLOG_DEFAULT_FORMAT = REVLOGNG
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGGENERALDELTA
34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGGENERALDELTA
35
35
36 # revlog index flags
36 # revlog index flags
37 REVIDX_KNOWN_FLAGS = 0
37 REVIDX_KNOWN_FLAGS = 0
38
38
39 # max size of revlog with inline data
39 # max size of revlog with inline data
40 _maxinline = 131072
40 _maxinline = 131072
41 _chunksize = 1048576
41 _chunksize = 1048576
42
42
43 RevlogError = error.RevlogError
43 RevlogError = error.RevlogError
44 LookupError = error.LookupError
44 LookupError = error.LookupError
45
45
46 def getoffset(q):
46 def getoffset(q):
47 return int(q >> 16)
47 return int(q >> 16)
48
48
49 def gettype(q):
49 def gettype(q):
50 return int(q & 0xFFFF)
50 return int(q & 0xFFFF)
51
51
52 def offset_type(offset, type):
52 def offset_type(offset, type):
53 return long(long(offset) << 16 | type)
53 return long(long(offset) << 16 | type)
54
54
55 nullhash = _sha(nullid)
55 nullhash = _sha(nullid)
56
56
57 def hash(text, p1, p2):
57 def hash(text, p1, p2):
58 """generate a hash from the given text and its parent hashes
58 """generate a hash from the given text and its parent hashes
59
59
60 This hash combines both the current file contents and its history
60 This hash combines both the current file contents and its history
61 in a manner that makes it easy to distinguish nodes with the same
61 in a manner that makes it easy to distinguish nodes with the same
62 content in the revision graph.
62 content in the revision graph.
63 """
63 """
64 # As of now, if one of the parent node is null, p2 is null
64 # As of now, if one of the parent node is null, p2 is null
65 if p2 == nullid:
65 if p2 == nullid:
66 # deep copy of a hash is faster than creating one
66 # deep copy of a hash is faster than creating one
67 s = nullhash.copy()
67 s = nullhash.copy()
68 s.update(p1)
68 s.update(p1)
69 else:
69 else:
70 # none of the parent nodes are nullid
70 # none of the parent nodes are nullid
71 l = [p1, p2]
71 l = [p1, p2]
72 l.sort()
72 l.sort()
73 s = _sha(l[0])
73 s = _sha(l[0])
74 s.update(l[1])
74 s.update(l[1])
75 s.update(text)
75 s.update(text)
76 return s.digest()
76 return s.digest()
77
77
78 def decompress(bin):
78 def decompress(bin):
79 """ decompress the given input """
79 """ decompress the given input """
80 if not bin:
80 if not bin:
81 return bin
81 return bin
82 t = bin[0]
82 t = bin[0]
83 if t == '\0':
83 if t == '\0':
84 return bin
84 return bin
85 if t == 'x':
85 if t == 'x':
86 try:
86 try:
87 return _decompress(bin)
87 return _decompress(bin)
88 except zlib.error, e:
88 except zlib.error, e:
89 raise RevlogError(_("revlog decompress error: %s") % str(e))
89 raise RevlogError(_("revlog decompress error: %s") % str(e))
90 if t == 'u':
90 if t == 'u':
91 return bin[1:]
91 return bin[1:]
92 raise RevlogError(_("unknown compression type %r") % t)
92 raise RevlogError(_("unknown compression type %r") % t)
93
93
94 # index v0:
94 # index v0:
95 # 4 bytes: offset
95 # 4 bytes: offset
96 # 4 bytes: compressed length
96 # 4 bytes: compressed length
97 # 4 bytes: base rev
97 # 4 bytes: base rev
98 # 4 bytes: link rev
98 # 4 bytes: link rev
99 # 32 bytes: parent 1 nodeid
99 # 32 bytes: parent 1 nodeid
100 # 32 bytes: parent 2 nodeid
100 # 32 bytes: parent 2 nodeid
101 # 32 bytes: nodeid
101 # 32 bytes: nodeid
102 indexformatv0 = ">4l20s20s20s"
102 indexformatv0 = ">4l20s20s20s"
103 v0shaoffset = 56
103 v0shaoffset = 56
104
104
105 class revlogoldio(object):
105 class revlogoldio(object):
106 def __init__(self):
106 def __init__(self):
107 self.size = struct.calcsize(indexformatv0)
107 self.size = struct.calcsize(indexformatv0)
108
108
109 def parseindex(self, data, inline):
109 def parseindex(self, data, inline):
110 s = self.size
110 s = self.size
111 index = []
111 index = []
112 nodemap = {nullid: nullrev}
112 nodemap = {nullid: nullrev}
113 n = off = 0
113 n = off = 0
114 l = len(data)
114 l = len(data)
115 while off + s <= l:
115 while off + s <= l:
116 cur = data[off:off + s]
116 cur = data[off:off + s]
117 off += s
117 off += s
118 e = _unpack(indexformatv0, cur)
118 e = _unpack(indexformatv0, cur)
119 # transform to revlogv1 format
119 # transform to revlogv1 format
120 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
120 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
121 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
121 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
122 index.append(e2)
122 index.append(e2)
123 nodemap[e[6]] = n
123 nodemap[e[6]] = n
124 n += 1
124 n += 1
125
125
126 # add the magic null revision at -1
126 # add the magic null revision at -1
127 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
127 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
128
128
129 return index, nodemap, None
129 return index, nodemap, None
130
130
131 def packentry(self, entry, node, version, rev):
131 def packentry(self, entry, node, version, rev):
132 if gettype(entry[0]):
132 if gettype(entry[0]):
133 raise RevlogError(_("index entry flags need RevlogNG"))
133 raise RevlogError(_("index entry flags need RevlogNG"))
134 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
134 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
135 node(entry[5]), node(entry[6]), entry[7])
135 node(entry[5]), node(entry[6]), entry[7])
136 return _pack(indexformatv0, *e2)
136 return _pack(indexformatv0, *e2)
137
137
138 # index ng:
138 # index ng:
139 # 6 bytes: offset
139 # 6 bytes: offset
140 # 2 bytes: flags
140 # 2 bytes: flags
141 # 4 bytes: compressed length
141 # 4 bytes: compressed length
142 # 4 bytes: uncompressed length
142 # 4 bytes: uncompressed length
143 # 4 bytes: base rev
143 # 4 bytes: base rev
144 # 4 bytes: link rev
144 # 4 bytes: link rev
145 # 4 bytes: parent 1 rev
145 # 4 bytes: parent 1 rev
146 # 4 bytes: parent 2 rev
146 # 4 bytes: parent 2 rev
147 # 32 bytes: nodeid
147 # 32 bytes: nodeid
148 indexformatng = ">Qiiiiii20s12x"
148 indexformatng = ">Qiiiiii20s12x"
149 ngshaoffset = 32
149 ngshaoffset = 32
150 versionformat = ">I"
150 versionformat = ">I"
151
151
152 class revlogio(object):
152 class revlogio(object):
153 def __init__(self):
153 def __init__(self):
154 self.size = struct.calcsize(indexformatng)
154 self.size = struct.calcsize(indexformatng)
155
155
156 def parseindex(self, data, inline):
156 def parseindex(self, data, inline):
157 # call the C implementation to parse the index data
157 # call the C implementation to parse the index data
158 index, cache = parsers.parse_index2(data, inline)
158 index, cache = parsers.parse_index2(data, inline)
159 return index, getattr(index, 'nodemap', None), cache
159 return index, getattr(index, 'nodemap', None), cache
160
160
161 def packentry(self, entry, node, version, rev):
161 def packentry(self, entry, node, version, rev):
162 p = _pack(indexformatng, *entry)
162 p = _pack(indexformatng, *entry)
163 if rev == 0:
163 if rev == 0:
164 p = _pack(versionformat, version) + p[4:]
164 p = _pack(versionformat, version) + p[4:]
165 return p
165 return p
166
166
167 class revlog(object):
167 class revlog(object):
168 """
168 """
169 the underlying revision storage object
169 the underlying revision storage object
170
170
171 A revlog consists of two parts, an index and the revision data.
171 A revlog consists of two parts, an index and the revision data.
172
172
173 The index is a file with a fixed record size containing
173 The index is a file with a fixed record size containing
174 information on each revision, including its nodeid (hash), the
174 information on each revision, including its nodeid (hash), the
175 nodeids of its parents, the position and offset of its data within
175 nodeids of its parents, the position and offset of its data within
176 the data file, and the revision it's based on. Finally, each entry
176 the data file, and the revision it's based on. Finally, each entry
177 contains a linkrev entry that can serve as a pointer to external
177 contains a linkrev entry that can serve as a pointer to external
178 data.
178 data.
179
179
180 The revision data itself is a linear collection of data chunks.
180 The revision data itself is a linear collection of data chunks.
181 Each chunk represents a revision and is usually represented as a
181 Each chunk represents a revision and is usually represented as a
182 delta against the previous chunk. To bound lookup time, runs of
182 delta against the previous chunk. To bound lookup time, runs of
183 deltas are limited to about 2 times the length of the original
183 deltas are limited to about 2 times the length of the original
184 version data. This makes retrieval of a version proportional to
184 version data. This makes retrieval of a version proportional to
185 its size, or O(1) relative to the number of revisions.
185 its size, or O(1) relative to the number of revisions.
186
186
187 Both pieces of the revlog are written to in an append-only
187 Both pieces of the revlog are written to in an append-only
188 fashion, which means we never need to rewrite a file to insert or
188 fashion, which means we never need to rewrite a file to insert or
189 remove data, and can use some simple techniques to avoid the need
189 remove data, and can use some simple techniques to avoid the need
190 for locking while reading.
190 for locking while reading.
191 """
191 """
192 def __init__(self, opener, indexfile):
192 def __init__(self, opener, indexfile):
193 """
193 """
194 create a revlog object
194 create a revlog object
195
195
196 opener is a function that abstracts the file opening operation
196 opener is a function that abstracts the file opening operation
197 and can be used to implement COW semantics or the like.
197 and can be used to implement COW semantics or the like.
198 """
198 """
199 self.indexfile = indexfile
199 self.indexfile = indexfile
200 self.datafile = indexfile[:-2] + ".d"
200 self.datafile = indexfile[:-2] + ".d"
201 self.opener = opener
201 self.opener = opener
202 self._cache = None
202 self._cache = None
203 self._basecache = None
203 self._basecache = None
204 self._chunkcache = (0, '')
204 self._chunkcache = (0, '')
205 self.index = []
205 self.index = []
206 self._pcache = {}
206 self._pcache = {}
207 self._nodecache = {nullid: nullrev}
207 self._nodecache = {nullid: nullrev}
208 self._nodepos = None
208 self._nodepos = None
209
209
210 v = REVLOG_DEFAULT_VERSION
210 v = REVLOG_DEFAULT_VERSION
211 opts = getattr(opener, 'options', None)
211 opts = getattr(opener, 'options', None)
212 if opts is not None:
212 if opts is not None:
213 if 'revlogv1' in opts:
213 if 'revlogv1' in opts:
214 if 'generaldelta' in opts:
214 if 'generaldelta' in opts:
215 v |= REVLOGGENERALDELTA
215 v |= REVLOGGENERALDELTA
216 else:
216 else:
217 v = 0
217 v = 0
218
218
219 i = ''
219 i = ''
220 self._initempty = True
220 self._initempty = True
221 try:
221 try:
222 f = self.opener(self.indexfile)
222 f = self.opener(self.indexfile)
223 i = f.read()
223 i = f.read()
224 f.close()
224 f.close()
225 if len(i) > 0:
225 if len(i) > 0:
226 v = struct.unpack(versionformat, i[:4])[0]
226 v = struct.unpack(versionformat, i[:4])[0]
227 self._initempty = False
227 self._initempty = False
228 except IOError, inst:
228 except IOError, inst:
229 if inst.errno != errno.ENOENT:
229 if inst.errno != errno.ENOENT:
230 raise
230 raise
231
231
232 self.version = v
232 self.version = v
233 self._inline = v & REVLOGNGINLINEDATA
233 self._inline = v & REVLOGNGINLINEDATA
234 self._generaldelta = v & REVLOGGENERALDELTA
234 self._generaldelta = v & REVLOGGENERALDELTA
235 flags = v & ~0xFFFF
235 flags = v & ~0xFFFF
236 fmt = v & 0xFFFF
236 fmt = v & 0xFFFF
237 if fmt == REVLOGV0 and flags:
237 if fmt == REVLOGV0 and flags:
238 raise RevlogError(_("index %s unknown flags %#04x for format v0")
238 raise RevlogError(_("index %s unknown flags %#04x for format v0")
239 % (self.indexfile, flags >> 16))
239 % (self.indexfile, flags >> 16))
240 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
240 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
241 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
241 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
242 % (self.indexfile, flags >> 16))
242 % (self.indexfile, flags >> 16))
243 elif fmt > REVLOGNG:
243 elif fmt > REVLOGNG:
244 raise RevlogError(_("index %s unknown format %d")
244 raise RevlogError(_("index %s unknown format %d")
245 % (self.indexfile, fmt))
245 % (self.indexfile, fmt))
246
246
247 self._io = revlogio()
247 self._io = revlogio()
248 if self.version == REVLOGV0:
248 if self.version == REVLOGV0:
249 self._io = revlogoldio()
249 self._io = revlogoldio()
250 try:
250 try:
251 d = self._io.parseindex(i, self._inline)
251 d = self._io.parseindex(i, self._inline)
252 except (ValueError, IndexError):
252 except (ValueError, IndexError):
253 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
253 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
254 self.index, nodemap, self._chunkcache = d
254 self.index, nodemap, self._chunkcache = d
255 if nodemap is not None:
255 if nodemap is not None:
256 self.nodemap = self._nodecache = nodemap
256 self.nodemap = self._nodecache = nodemap
257 if not self._chunkcache:
257 if not self._chunkcache:
258 self._chunkclear()
258 self._chunkclear()
259
259
260 def tip(self):
260 def tip(self):
261 return self.node(len(self.index) - 2)
261 return self.node(len(self.index) - 2)
262 def __len__(self):
262 def __len__(self):
263 return len(self.index) - 1
263 return len(self.index) - 1
264 def __iter__(self):
264 def __iter__(self):
265 return iter(xrange(len(self)))
265 return iter(xrange(len(self)))
266 def revs(self, start=0, stop=None):
266 def revs(self, start=0, stop=None):
267 """iterate over all rev in this revlog (from start to stop)"""
267 """iterate over all rev in this revlog (from start to stop)"""
268 step = 1
268 step = 1
269 if stop is not None:
269 if stop is not None:
270 if start > stop:
270 if start > stop:
271 step = -1
271 step = -1
272 stop += step
272 stop += step
273 else:
273 else:
274 stop = len(self)
274 stop = len(self)
275 return xrange(start, stop, step)
275 return xrange(start, stop, step)
276
276
277 @util.propertycache
277 @util.propertycache
278 def nodemap(self):
278 def nodemap(self):
279 self.rev(self.node(0))
279 self.rev(self.node(0))
280 return self._nodecache
280 return self._nodecache
281
281
282 def hasnode(self, node):
282 def hasnode(self, node):
283 try:
283 try:
284 self.rev(node)
284 self.rev(node)
285 return True
285 return True
286 except KeyError:
286 except KeyError:
287 return False
287 return False
288
288
289 def clearcaches(self):
289 def clearcaches(self):
290 try:
290 try:
291 self._nodecache.clearcaches()
291 self._nodecache.clearcaches()
292 except AttributeError:
292 except AttributeError:
293 self._nodecache = {nullid: nullrev}
293 self._nodecache = {nullid: nullrev}
294 self._nodepos = None
294 self._nodepos = None
295
295
296 def rev(self, node):
296 def rev(self, node):
297 try:
297 try:
298 return self._nodecache[node]
298 return self._nodecache[node]
299 except RevlogError:
299 except RevlogError:
300 # parsers.c radix tree lookup failed
300 # parsers.c radix tree lookup failed
301 raise LookupError(node, self.indexfile, _('no node'))
301 raise LookupError(node, self.indexfile, _('no node'))
302 except KeyError:
302 except KeyError:
303 # pure python cache lookup failed
303 # pure python cache lookup failed
304 n = self._nodecache
304 n = self._nodecache
305 i = self.index
305 i = self.index
306 p = self._nodepos
306 p = self._nodepos
307 if p is None:
307 if p is None:
308 p = len(i) - 2
308 p = len(i) - 2
309 for r in xrange(p, -1, -1):
309 for r in xrange(p, -1, -1):
310 v = i[r][7]
310 v = i[r][7]
311 n[v] = r
311 n[v] = r
312 if v == node:
312 if v == node:
313 self._nodepos = r - 1
313 self._nodepos = r - 1
314 return r
314 return r
315 raise LookupError(node, self.indexfile, _('no node'))
315 raise LookupError(node, self.indexfile, _('no node'))
316
316
317 def node(self, rev):
317 def node(self, rev):
318 return self.index[rev][7]
318 return self.index[rev][7]
319 def linkrev(self, rev):
319 def linkrev(self, rev):
320 return self.index[rev][4]
320 return self.index[rev][4]
321 def parents(self, node):
321 def parents(self, node):
322 i = self.index
322 i = self.index
323 d = i[self.rev(node)]
323 d = i[self.rev(node)]
324 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
324 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
325 def parentrevs(self, rev):
325 def parentrevs(self, rev):
326 return self.index[rev][5:7]
326 return self.index[rev][5:7]
327 def start(self, rev):
327 def start(self, rev):
328 return int(self.index[rev][0] >> 16)
328 return int(self.index[rev][0] >> 16)
329 def end(self, rev):
329 def end(self, rev):
330 return self.start(rev) + self.length(rev)
330 return self.start(rev) + self.length(rev)
331 def length(self, rev):
331 def length(self, rev):
332 return self.index[rev][1]
332 return self.index[rev][1]
333 def chainbase(self, rev):
333 def chainbase(self, rev):
334 index = self.index
334 index = self.index
335 base = index[rev][3]
335 base = index[rev][3]
336 while base != rev:
336 while base != rev:
337 rev = base
337 rev = base
338 base = index[rev][3]
338 base = index[rev][3]
339 return base
339 return base
340 def flags(self, rev):
340 def flags(self, rev):
341 return self.index[rev][0] & 0xFFFF
341 return self.index[rev][0] & 0xFFFF
342 def rawsize(self, rev):
342 def rawsize(self, rev):
343 """return the length of the uncompressed text for a given revision"""
343 """return the length of the uncompressed text for a given revision"""
344 l = self.index[rev][2]
344 l = self.index[rev][2]
345 if l >= 0:
345 if l >= 0:
346 return l
346 return l
347
347
348 t = self.revision(self.node(rev))
348 t = self.revision(self.node(rev))
349 return len(t)
349 return len(t)
350 size = rawsize
350 size = rawsize
351
351
352 def ancestors(self, revs, stoprev=0, inclusive=False):
352 def ancestors(self, revs, stoprev=0, inclusive=False):
353 """Generate the ancestors of 'revs' in reverse topological order.
353 """Generate the ancestors of 'revs' in reverse topological order.
354 Does not generate revs lower than stoprev.
354 Does not generate revs lower than stoprev.
355
355
356 See the documentation for ancestor.lazyancestors for more details."""
356 See the documentation for ancestor.lazyancestors for more details."""
357
357
358 return ancestor.lazyancestors(self, revs, stoprev=stoprev,
358 return ancestor.lazyancestors(self, revs, stoprev=stoprev,
359 inclusive=inclusive)
359 inclusive=inclusive)
360
360
361 def descendants(self, revs):
361 def descendants(self, revs):
362 """Generate the descendants of 'revs' in revision order.
362 """Generate the descendants of 'revs' in revision order.
363
363
364 Yield a sequence of revision numbers starting with a child of
364 Yield a sequence of revision numbers starting with a child of
365 some rev in revs, i.e., each revision is *not* considered a
365 some rev in revs, i.e., each revision is *not* considered a
366 descendant of itself. Results are ordered by revision number (a
366 descendant of itself. Results are ordered by revision number (a
367 topological sort)."""
367 topological sort)."""
368 first = min(revs)
368 first = min(revs)
369 if first == nullrev:
369 if first == nullrev:
370 for i in self:
370 for i in self:
371 yield i
371 yield i
372 return
372 return
373
373
374 seen = set(revs)
374 seen = set(revs)
375 for i in self.revs(start=first + 1):
375 for i in self.revs(start=first + 1):
376 for x in self.parentrevs(i):
376 for x in self.parentrevs(i):
377 if x != nullrev and x in seen:
377 if x != nullrev and x in seen:
378 seen.add(i)
378 seen.add(i)
379 yield i
379 yield i
380 break
380 break
381
381
382 def findcommonmissing(self, common=None, heads=None):
382 def findcommonmissing(self, common=None, heads=None):
383 """Return a tuple of the ancestors of common and the ancestors of heads
383 """Return a tuple of the ancestors of common and the ancestors of heads
384 that are not ancestors of common. In revset terminology, we return the
384 that are not ancestors of common. In revset terminology, we return the
385 tuple:
385 tuple:
386
386
387 ::common, (::heads) - (::common)
387 ::common, (::heads) - (::common)
388
388
389 The list is sorted by revision number, meaning it is
389 The list is sorted by revision number, meaning it is
390 topologically sorted.
390 topologically sorted.
391
391
392 'heads' and 'common' are both lists of node IDs. If heads is
392 'heads' and 'common' are both lists of node IDs. If heads is
393 not supplied, uses all of the revlog's heads. If common is not
393 not supplied, uses all of the revlog's heads. If common is not
394 supplied, uses nullid."""
394 supplied, uses nullid."""
395 if common is None:
395 if common is None:
396 common = [nullid]
396 common = [nullid]
397 if heads is None:
397 if heads is None:
398 heads = self.heads()
398 heads = self.heads()
399
399
400 common = [self.rev(n) for n in common]
400 common = [self.rev(n) for n in common]
401 heads = [self.rev(n) for n in heads]
401 heads = [self.rev(n) for n in heads]
402
402
403 # we want the ancestors, but inclusive
403 # we want the ancestors, but inclusive
404 has = set(self.ancestors(common))
404 class lazyset(object):
405 def __init__(self, lazyvalues):
406 self.addedvalues = set()
407 self.lazyvalues = lazyvalues
408
409 def __contains__(self, value):
410 return value in self.addedvalues or value in self.lazyvalues
411
412 def __iter__(self):
413 added = self.addedvalues
414 for r in added:
415 yield r
416 for r in self.lazyvalues:
417 if not r in added:
418 yield r
419
420 def add(self, value):
421 self.addedvalues.add(value)
422
423 def update(self, values):
424 self.addedvalues.update(values)
425
426 has = lazyset(self.ancestors(common))
405 has.add(nullrev)
427 has.add(nullrev)
406 has.update(common)
428 has.update(common)
407
429
408 # take all ancestors from heads that aren't in has
430 # take all ancestors from heads that aren't in has
409 missing = set()
431 missing = set()
410 visit = util.deque(r for r in heads if r not in has)
432 visit = util.deque(r for r in heads if r not in has)
411 while visit:
433 while visit:
412 r = visit.popleft()
434 r = visit.popleft()
413 if r in missing:
435 if r in missing:
414 continue
436 continue
415 else:
437 else:
416 missing.add(r)
438 missing.add(r)
417 for p in self.parentrevs(r):
439 for p in self.parentrevs(r):
418 if p not in has:
440 if p not in has:
419 visit.append(p)
441 visit.append(p)
420 missing = list(missing)
442 missing = list(missing)
421 missing.sort()
443 missing.sort()
422 return has, [self.node(r) for r in missing]
444 return has, [self.node(r) for r in missing]
423
445
424 def findmissingrevs(self, common=None, heads=None):
446 def findmissingrevs(self, common=None, heads=None):
425 """Return the revision numbers of the ancestors of heads that
447 """Return the revision numbers of the ancestors of heads that
426 are not ancestors of common.
448 are not ancestors of common.
427
449
428 More specifically, return a list of revision numbers corresponding to
450 More specifically, return a list of revision numbers corresponding to
429 nodes N such that every N satisfies the following constraints:
451 nodes N such that every N satisfies the following constraints:
430
452
431 1. N is an ancestor of some node in 'heads'
453 1. N is an ancestor of some node in 'heads'
432 2. N is not an ancestor of any node in 'common'
454 2. N is not an ancestor of any node in 'common'
433
455
434 The list is sorted by revision number, meaning it is
456 The list is sorted by revision number, meaning it is
435 topologically sorted.
457 topologically sorted.
436
458
437 'heads' and 'common' are both lists of revision numbers. If heads is
459 'heads' and 'common' are both lists of revision numbers. If heads is
438 not supplied, uses all of the revlog's heads. If common is not
460 not supplied, uses all of the revlog's heads. If common is not
439 supplied, uses nullid."""
461 supplied, uses nullid."""
440 if common is None:
462 if common is None:
441 common = [nullrev]
463 common = [nullrev]
442 if heads is None:
464 if heads is None:
443 heads = self.headrevs()
465 heads = self.headrevs()
444
466
445 return ancestor.missingancestors(heads, common, self.parentrevs)
467 return ancestor.missingancestors(heads, common, self.parentrevs)
446
468
447 def findmissing(self, common=None, heads=None):
469 def findmissing(self, common=None, heads=None):
448 """Return the ancestors of heads that are not ancestors of common.
470 """Return the ancestors of heads that are not ancestors of common.
449
471
450 More specifically, return a list of nodes N such that every N
472 More specifically, return a list of nodes N such that every N
451 satisfies the following constraints:
473 satisfies the following constraints:
452
474
453 1. N is an ancestor of some node in 'heads'
475 1. N is an ancestor of some node in 'heads'
454 2. N is not an ancestor of any node in 'common'
476 2. N is not an ancestor of any node in 'common'
455
477
456 The list is sorted by revision number, meaning it is
478 The list is sorted by revision number, meaning it is
457 topologically sorted.
479 topologically sorted.
458
480
459 'heads' and 'common' are both lists of node IDs. If heads is
481 'heads' and 'common' are both lists of node IDs. If heads is
460 not supplied, uses all of the revlog's heads. If common is not
482 not supplied, uses all of the revlog's heads. If common is not
461 supplied, uses nullid."""
483 supplied, uses nullid."""
462 if common is None:
484 if common is None:
463 common = [nullid]
485 common = [nullid]
464 if heads is None:
486 if heads is None:
465 heads = self.heads()
487 heads = self.heads()
466
488
467 common = [self.rev(n) for n in common]
489 common = [self.rev(n) for n in common]
468 heads = [self.rev(n) for n in heads]
490 heads = [self.rev(n) for n in heads]
469
491
470 return [self.node(r) for r in
492 return [self.node(r) for r in
471 ancestor.missingancestors(heads, common, self.parentrevs)]
493 ancestor.missingancestors(heads, common, self.parentrevs)]
472
494
473 def nodesbetween(self, roots=None, heads=None):
495 def nodesbetween(self, roots=None, heads=None):
474 """Return a topological path from 'roots' to 'heads'.
496 """Return a topological path from 'roots' to 'heads'.
475
497
476 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
498 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
477 topologically sorted list of all nodes N that satisfy both of
499 topologically sorted list of all nodes N that satisfy both of
478 these constraints:
500 these constraints:
479
501
480 1. N is a descendant of some node in 'roots'
502 1. N is a descendant of some node in 'roots'
481 2. N is an ancestor of some node in 'heads'
503 2. N is an ancestor of some node in 'heads'
482
504
483 Every node is considered to be both a descendant and an ancestor
505 Every node is considered to be both a descendant and an ancestor
484 of itself, so every reachable node in 'roots' and 'heads' will be
506 of itself, so every reachable node in 'roots' and 'heads' will be
485 included in 'nodes'.
507 included in 'nodes'.
486
508
487 'outroots' is the list of reachable nodes in 'roots', i.e., the
509 'outroots' is the list of reachable nodes in 'roots', i.e., the
488 subset of 'roots' that is returned in 'nodes'. Likewise,
510 subset of 'roots' that is returned in 'nodes'. Likewise,
489 'outheads' is the subset of 'heads' that is also in 'nodes'.
511 'outheads' is the subset of 'heads' that is also in 'nodes'.
490
512
491 'roots' and 'heads' are both lists of node IDs. If 'roots' is
513 'roots' and 'heads' are both lists of node IDs. If 'roots' is
492 unspecified, uses nullid as the only root. If 'heads' is
514 unspecified, uses nullid as the only root. If 'heads' is
493 unspecified, uses list of all of the revlog's heads."""
515 unspecified, uses list of all of the revlog's heads."""
494 nonodes = ([], [], [])
516 nonodes = ([], [], [])
495 if roots is not None:
517 if roots is not None:
496 roots = list(roots)
518 roots = list(roots)
497 if not roots:
519 if not roots:
498 return nonodes
520 return nonodes
499 lowestrev = min([self.rev(n) for n in roots])
521 lowestrev = min([self.rev(n) for n in roots])
500 else:
522 else:
501 roots = [nullid] # Everybody's a descendant of nullid
523 roots = [nullid] # Everybody's a descendant of nullid
502 lowestrev = nullrev
524 lowestrev = nullrev
503 if (lowestrev == nullrev) and (heads is None):
525 if (lowestrev == nullrev) and (heads is None):
504 # We want _all_ the nodes!
526 # We want _all_ the nodes!
505 return ([self.node(r) for r in self], [nullid], list(self.heads()))
527 return ([self.node(r) for r in self], [nullid], list(self.heads()))
506 if heads is None:
528 if heads is None:
507 # All nodes are ancestors, so the latest ancestor is the last
529 # All nodes are ancestors, so the latest ancestor is the last
508 # node.
530 # node.
509 highestrev = len(self) - 1
531 highestrev = len(self) - 1
510 # Set ancestors to None to signal that every node is an ancestor.
532 # Set ancestors to None to signal that every node is an ancestor.
511 ancestors = None
533 ancestors = None
512 # Set heads to an empty dictionary for later discovery of heads
534 # Set heads to an empty dictionary for later discovery of heads
513 heads = {}
535 heads = {}
514 else:
536 else:
515 heads = list(heads)
537 heads = list(heads)
516 if not heads:
538 if not heads:
517 return nonodes
539 return nonodes
518 ancestors = set()
540 ancestors = set()
519 # Turn heads into a dictionary so we can remove 'fake' heads.
541 # Turn heads into a dictionary so we can remove 'fake' heads.
520 # Also, later we will be using it to filter out the heads we can't
542 # Also, later we will be using it to filter out the heads we can't
521 # find from roots.
543 # find from roots.
522 heads = dict.fromkeys(heads, False)
544 heads = dict.fromkeys(heads, False)
523 # Start at the top and keep marking parents until we're done.
545 # Start at the top and keep marking parents until we're done.
524 nodestotag = set(heads)
546 nodestotag = set(heads)
525 # Remember where the top was so we can use it as a limit later.
547 # Remember where the top was so we can use it as a limit later.
526 highestrev = max([self.rev(n) for n in nodestotag])
548 highestrev = max([self.rev(n) for n in nodestotag])
527 while nodestotag:
549 while nodestotag:
528 # grab a node to tag
550 # grab a node to tag
529 n = nodestotag.pop()
551 n = nodestotag.pop()
530 # Never tag nullid
552 # Never tag nullid
531 if n == nullid:
553 if n == nullid:
532 continue
554 continue
533 # A node's revision number represents its place in a
555 # A node's revision number represents its place in a
534 # topologically sorted list of nodes.
556 # topologically sorted list of nodes.
535 r = self.rev(n)
557 r = self.rev(n)
536 if r >= lowestrev:
558 if r >= lowestrev:
537 if n not in ancestors:
559 if n not in ancestors:
538 # If we are possibly a descendant of one of the roots
560 # If we are possibly a descendant of one of the roots
539 # and we haven't already been marked as an ancestor
561 # and we haven't already been marked as an ancestor
540 ancestors.add(n) # Mark as ancestor
562 ancestors.add(n) # Mark as ancestor
541 # Add non-nullid parents to list of nodes to tag.
563 # Add non-nullid parents to list of nodes to tag.
542 nodestotag.update([p for p in self.parents(n) if
564 nodestotag.update([p for p in self.parents(n) if
543 p != nullid])
565 p != nullid])
544 elif n in heads: # We've seen it before, is it a fake head?
566 elif n in heads: # We've seen it before, is it a fake head?
545 # So it is, real heads should not be the ancestors of
567 # So it is, real heads should not be the ancestors of
546 # any other heads.
568 # any other heads.
547 heads.pop(n)
569 heads.pop(n)
548 if not ancestors:
570 if not ancestors:
549 return nonodes
571 return nonodes
550 # Now that we have our set of ancestors, we want to remove any
572 # Now that we have our set of ancestors, we want to remove any
551 # roots that are not ancestors.
573 # roots that are not ancestors.
552
574
553 # If one of the roots was nullid, everything is included anyway.
575 # If one of the roots was nullid, everything is included anyway.
554 if lowestrev > nullrev:
576 if lowestrev > nullrev:
555 # But, since we weren't, let's recompute the lowest rev to not
577 # But, since we weren't, let's recompute the lowest rev to not
556 # include roots that aren't ancestors.
578 # include roots that aren't ancestors.
557
579
558 # Filter out roots that aren't ancestors of heads
580 # Filter out roots that aren't ancestors of heads
559 roots = [n for n in roots if n in ancestors]
581 roots = [n for n in roots if n in ancestors]
560 # Recompute the lowest revision
582 # Recompute the lowest revision
561 if roots:
583 if roots:
562 lowestrev = min([self.rev(n) for n in roots])
584 lowestrev = min([self.rev(n) for n in roots])
563 else:
585 else:
564 # No more roots? Return empty list
586 # No more roots? Return empty list
565 return nonodes
587 return nonodes
566 else:
588 else:
567 # We are descending from nullid, and don't need to care about
589 # We are descending from nullid, and don't need to care about
568 # any other roots.
590 # any other roots.
569 lowestrev = nullrev
591 lowestrev = nullrev
570 roots = [nullid]
592 roots = [nullid]
571 # Transform our roots list into a set.
593 # Transform our roots list into a set.
572 descendants = set(roots)
594 descendants = set(roots)
573 # Also, keep the original roots so we can filter out roots that aren't
595 # Also, keep the original roots so we can filter out roots that aren't
574 # 'real' roots (i.e. are descended from other roots).
596 # 'real' roots (i.e. are descended from other roots).
575 roots = descendants.copy()
597 roots = descendants.copy()
576 # Our topologically sorted list of output nodes.
598 # Our topologically sorted list of output nodes.
577 orderedout = []
599 orderedout = []
578 # Don't start at nullid since we don't want nullid in our output list,
600 # Don't start at nullid since we don't want nullid in our output list,
579 # and if nullid shows up in descendants, empty parents will look like
601 # and if nullid shows up in descendants, empty parents will look like
580 # they're descendants.
602 # they're descendants.
581 for r in self.revs(start=max(lowestrev, 0), stop=highestrev + 1):
603 for r in self.revs(start=max(lowestrev, 0), stop=highestrev + 1):
582 n = self.node(r)
604 n = self.node(r)
583 isdescendant = False
605 isdescendant = False
584 if lowestrev == nullrev: # Everybody is a descendant of nullid
606 if lowestrev == nullrev: # Everybody is a descendant of nullid
585 isdescendant = True
607 isdescendant = True
586 elif n in descendants:
608 elif n in descendants:
587 # n is already a descendant
609 # n is already a descendant
588 isdescendant = True
610 isdescendant = True
589 # This check only needs to be done here because all the roots
611 # This check only needs to be done here because all the roots
590 # will start being marked is descendants before the loop.
612 # will start being marked is descendants before the loop.
591 if n in roots:
613 if n in roots:
592 # If n was a root, check if it's a 'real' root.
614 # If n was a root, check if it's a 'real' root.
593 p = tuple(self.parents(n))
615 p = tuple(self.parents(n))
594 # If any of its parents are descendants, it's not a root.
616 # If any of its parents are descendants, it's not a root.
595 if (p[0] in descendants) or (p[1] in descendants):
617 if (p[0] in descendants) or (p[1] in descendants):
596 roots.remove(n)
618 roots.remove(n)
597 else:
619 else:
598 p = tuple(self.parents(n))
620 p = tuple(self.parents(n))
599 # A node is a descendant if either of its parents are
621 # A node is a descendant if either of its parents are
600 # descendants. (We seeded the dependents list with the roots
622 # descendants. (We seeded the dependents list with the roots
601 # up there, remember?)
623 # up there, remember?)
602 if (p[0] in descendants) or (p[1] in descendants):
624 if (p[0] in descendants) or (p[1] in descendants):
603 descendants.add(n)
625 descendants.add(n)
604 isdescendant = True
626 isdescendant = True
605 if isdescendant and ((ancestors is None) or (n in ancestors)):
627 if isdescendant and ((ancestors is None) or (n in ancestors)):
606 # Only include nodes that are both descendants and ancestors.
628 # Only include nodes that are both descendants and ancestors.
607 orderedout.append(n)
629 orderedout.append(n)
608 if (ancestors is not None) and (n in heads):
630 if (ancestors is not None) and (n in heads):
609 # We're trying to figure out which heads are reachable
631 # We're trying to figure out which heads are reachable
610 # from roots.
632 # from roots.
611 # Mark this head as having been reached
633 # Mark this head as having been reached
612 heads[n] = True
634 heads[n] = True
613 elif ancestors is None:
635 elif ancestors is None:
614 # Otherwise, we're trying to discover the heads.
636 # Otherwise, we're trying to discover the heads.
615 # Assume this is a head because if it isn't, the next step
637 # Assume this is a head because if it isn't, the next step
616 # will eventually remove it.
638 # will eventually remove it.
617 heads[n] = True
639 heads[n] = True
618 # But, obviously its parents aren't.
640 # But, obviously its parents aren't.
619 for p in self.parents(n):
641 for p in self.parents(n):
620 heads.pop(p, None)
642 heads.pop(p, None)
621 heads = [n for n, flag in heads.iteritems() if flag]
643 heads = [n for n, flag in heads.iteritems() if flag]
622 roots = list(roots)
644 roots = list(roots)
623 assert orderedout
645 assert orderedout
624 assert roots
646 assert roots
625 assert heads
647 assert heads
626 return (orderedout, roots, heads)
648 return (orderedout, roots, heads)
627
649
628 def headrevs(self):
650 def headrevs(self):
629 try:
651 try:
630 return self.index.headrevs()
652 return self.index.headrevs()
631 except AttributeError:
653 except AttributeError:
632 return self._headrevs()
654 return self._headrevs()
633
655
634 def _headrevs(self):
656 def _headrevs(self):
635 count = len(self)
657 count = len(self)
636 if not count:
658 if not count:
637 return [nullrev]
659 return [nullrev]
638 # we won't iter over filtered rev so nobody is a head at start
660 # we won't iter over filtered rev so nobody is a head at start
639 ishead = [0] * (count + 1)
661 ishead = [0] * (count + 1)
640 index = self.index
662 index = self.index
641 for r in self:
663 for r in self:
642 ishead[r] = 1 # I may be an head
664 ishead[r] = 1 # I may be an head
643 e = index[r]
665 e = index[r]
644 ishead[e[5]] = ishead[e[6]] = 0 # my parent are not
666 ishead[e[5]] = ishead[e[6]] = 0 # my parent are not
645 return [r for r, val in enumerate(ishead) if val]
667 return [r for r, val in enumerate(ishead) if val]
646
668
647 def heads(self, start=None, stop=None):
669 def heads(self, start=None, stop=None):
648 """return the list of all nodes that have no children
670 """return the list of all nodes that have no children
649
671
650 if start is specified, only heads that are descendants of
672 if start is specified, only heads that are descendants of
651 start will be returned
673 start will be returned
652 if stop is specified, it will consider all the revs from stop
674 if stop is specified, it will consider all the revs from stop
653 as if they had no children
675 as if they had no children
654 """
676 """
655 if start is None and stop is None:
677 if start is None and stop is None:
656 if not len(self):
678 if not len(self):
657 return [nullid]
679 return [nullid]
658 return [self.node(r) for r in self.headrevs()]
680 return [self.node(r) for r in self.headrevs()]
659
681
660 if start is None:
682 if start is None:
661 start = nullid
683 start = nullid
662 if stop is None:
684 if stop is None:
663 stop = []
685 stop = []
664 stoprevs = set([self.rev(n) for n in stop])
686 stoprevs = set([self.rev(n) for n in stop])
665 startrev = self.rev(start)
687 startrev = self.rev(start)
666 reachable = set((startrev,))
688 reachable = set((startrev,))
667 heads = set((startrev,))
689 heads = set((startrev,))
668
690
669 parentrevs = self.parentrevs
691 parentrevs = self.parentrevs
670 for r in self.revs(start=startrev + 1):
692 for r in self.revs(start=startrev + 1):
671 for p in parentrevs(r):
693 for p in parentrevs(r):
672 if p in reachable:
694 if p in reachable:
673 if r not in stoprevs:
695 if r not in stoprevs:
674 reachable.add(r)
696 reachable.add(r)
675 heads.add(r)
697 heads.add(r)
676 if p in heads and p not in stoprevs:
698 if p in heads and p not in stoprevs:
677 heads.remove(p)
699 heads.remove(p)
678
700
679 return [self.node(r) for r in heads]
701 return [self.node(r) for r in heads]
680
702
681 def children(self, node):
703 def children(self, node):
682 """find the children of a given node"""
704 """find the children of a given node"""
683 c = []
705 c = []
684 p = self.rev(node)
706 p = self.rev(node)
685 for r in self.revs(start=p + 1):
707 for r in self.revs(start=p + 1):
686 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
708 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
687 if prevs:
709 if prevs:
688 for pr in prevs:
710 for pr in prevs:
689 if pr == p:
711 if pr == p:
690 c.append(self.node(r))
712 c.append(self.node(r))
691 elif p == nullrev:
713 elif p == nullrev:
692 c.append(self.node(r))
714 c.append(self.node(r))
693 return c
715 return c
694
716
695 def descendant(self, start, end):
717 def descendant(self, start, end):
696 if start == nullrev:
718 if start == nullrev:
697 return True
719 return True
698 for i in self.descendants([start]):
720 for i in self.descendants([start]):
699 if i == end:
721 if i == end:
700 return True
722 return True
701 elif i > end:
723 elif i > end:
702 break
724 break
703 return False
725 return False
704
726
705 def ancestor(self, a, b):
727 def ancestor(self, a, b):
706 """calculate the least common ancestor of nodes a and b"""
728 """calculate the least common ancestor of nodes a and b"""
707
729
708 a, b = self.rev(a), self.rev(b)
730 a, b = self.rev(a), self.rev(b)
709 try:
731 try:
710 ancs = self.index.ancestors(a, b)
732 ancs = self.index.ancestors(a, b)
711 except (AttributeError, OverflowError):
733 except (AttributeError, OverflowError):
712 ancs = ancestor.ancestors(self.parentrevs, a, b)
734 ancs = ancestor.ancestors(self.parentrevs, a, b)
713 if ancs:
735 if ancs:
714 # choose a consistent winner when there's a tie
736 # choose a consistent winner when there's a tie
715 return min(map(self.node, ancs))
737 return min(map(self.node, ancs))
716 return nullid
738 return nullid
717
739
718 def _match(self, id):
740 def _match(self, id):
719 if isinstance(id, int):
741 if isinstance(id, int):
720 # rev
742 # rev
721 return self.node(id)
743 return self.node(id)
722 if len(id) == 20:
744 if len(id) == 20:
723 # possibly a binary node
745 # possibly a binary node
724 # odds of a binary node being all hex in ASCII are 1 in 10**25
746 # odds of a binary node being all hex in ASCII are 1 in 10**25
725 try:
747 try:
726 node = id
748 node = id
727 self.rev(node) # quick search the index
749 self.rev(node) # quick search the index
728 return node
750 return node
729 except LookupError:
751 except LookupError:
730 pass # may be partial hex id
752 pass # may be partial hex id
731 try:
753 try:
732 # str(rev)
754 # str(rev)
733 rev = int(id)
755 rev = int(id)
734 if str(rev) != id:
756 if str(rev) != id:
735 raise ValueError
757 raise ValueError
736 if rev < 0:
758 if rev < 0:
737 rev = len(self) + rev
759 rev = len(self) + rev
738 if rev < 0 or rev >= len(self):
760 if rev < 0 or rev >= len(self):
739 raise ValueError
761 raise ValueError
740 return self.node(rev)
762 return self.node(rev)
741 except (ValueError, OverflowError):
763 except (ValueError, OverflowError):
742 pass
764 pass
743 if len(id) == 40:
765 if len(id) == 40:
744 try:
766 try:
745 # a full hex nodeid?
767 # a full hex nodeid?
746 node = bin(id)
768 node = bin(id)
747 self.rev(node)
769 self.rev(node)
748 return node
770 return node
749 except (TypeError, LookupError):
771 except (TypeError, LookupError):
750 pass
772 pass
751
773
752 def _partialmatch(self, id):
774 def _partialmatch(self, id):
753 try:
775 try:
754 n = self.index.partialmatch(id)
776 n = self.index.partialmatch(id)
755 if n and self.hasnode(n):
777 if n and self.hasnode(n):
756 return n
778 return n
757 return None
779 return None
758 except RevlogError:
780 except RevlogError:
759 # parsers.c radix tree lookup gave multiple matches
781 # parsers.c radix tree lookup gave multiple matches
760 # fall through to slow path that filters hidden revisions
782 # fall through to slow path that filters hidden revisions
761 pass
783 pass
762 except (AttributeError, ValueError):
784 except (AttributeError, ValueError):
763 # we are pure python, or key was too short to search radix tree
785 # we are pure python, or key was too short to search radix tree
764 pass
786 pass
765
787
766 if id in self._pcache:
788 if id in self._pcache:
767 return self._pcache[id]
789 return self._pcache[id]
768
790
769 if len(id) < 40:
791 if len(id) < 40:
770 try:
792 try:
771 # hex(node)[:...]
793 # hex(node)[:...]
772 l = len(id) // 2 # grab an even number of digits
794 l = len(id) // 2 # grab an even number of digits
773 prefix = bin(id[:l * 2])
795 prefix = bin(id[:l * 2])
774 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
796 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
775 nl = [n for n in nl if hex(n).startswith(id) and
797 nl = [n for n in nl if hex(n).startswith(id) and
776 self.hasnode(n)]
798 self.hasnode(n)]
777 if len(nl) > 0:
799 if len(nl) > 0:
778 if len(nl) == 1:
800 if len(nl) == 1:
779 self._pcache[id] = nl[0]
801 self._pcache[id] = nl[0]
780 return nl[0]
802 return nl[0]
781 raise LookupError(id, self.indexfile,
803 raise LookupError(id, self.indexfile,
782 _('ambiguous identifier'))
804 _('ambiguous identifier'))
783 return None
805 return None
784 except TypeError:
806 except TypeError:
785 pass
807 pass
786
808
787 def lookup(self, id):
809 def lookup(self, id):
788 """locate a node based on:
810 """locate a node based on:
789 - revision number or str(revision number)
811 - revision number or str(revision number)
790 - nodeid or subset of hex nodeid
812 - nodeid or subset of hex nodeid
791 """
813 """
792 n = self._match(id)
814 n = self._match(id)
793 if n is not None:
815 if n is not None:
794 return n
816 return n
795 n = self._partialmatch(id)
817 n = self._partialmatch(id)
796 if n:
818 if n:
797 return n
819 return n
798
820
799 raise LookupError(id, self.indexfile, _('no match found'))
821 raise LookupError(id, self.indexfile, _('no match found'))
800
822
801 def cmp(self, node, text):
823 def cmp(self, node, text):
802 """compare text with a given file revision
824 """compare text with a given file revision
803
825
804 returns True if text is different than what is stored.
826 returns True if text is different than what is stored.
805 """
827 """
806 p1, p2 = self.parents(node)
828 p1, p2 = self.parents(node)
807 return hash(text, p1, p2) != node
829 return hash(text, p1, p2) != node
808
830
809 def _addchunk(self, offset, data):
831 def _addchunk(self, offset, data):
810 o, d = self._chunkcache
832 o, d = self._chunkcache
811 # try to add to existing cache
833 # try to add to existing cache
812 if o + len(d) == offset and len(d) + len(data) < _chunksize:
834 if o + len(d) == offset and len(d) + len(data) < _chunksize:
813 self._chunkcache = o, d + data
835 self._chunkcache = o, d + data
814 else:
836 else:
815 self._chunkcache = offset, data
837 self._chunkcache = offset, data
816
838
817 def _loadchunk(self, offset, length):
839 def _loadchunk(self, offset, length):
818 if self._inline:
840 if self._inline:
819 df = self.opener(self.indexfile)
841 df = self.opener(self.indexfile)
820 else:
842 else:
821 df = self.opener(self.datafile)
843 df = self.opener(self.datafile)
822
844
823 readahead = max(65536, length)
845 readahead = max(65536, length)
824 df.seek(offset)
846 df.seek(offset)
825 d = df.read(readahead)
847 d = df.read(readahead)
826 df.close()
848 df.close()
827 self._addchunk(offset, d)
849 self._addchunk(offset, d)
828 if readahead > length:
850 if readahead > length:
829 return util.buffer(d, 0, length)
851 return util.buffer(d, 0, length)
830 return d
852 return d
831
853
832 def _getchunk(self, offset, length):
854 def _getchunk(self, offset, length):
833 o, d = self._chunkcache
855 o, d = self._chunkcache
834 l = len(d)
856 l = len(d)
835
857
836 # is it in the cache?
858 # is it in the cache?
837 cachestart = offset - o
859 cachestart = offset - o
838 cacheend = cachestart + length
860 cacheend = cachestart + length
839 if cachestart >= 0 and cacheend <= l:
861 if cachestart >= 0 and cacheend <= l:
840 if cachestart == 0 and cacheend == l:
862 if cachestart == 0 and cacheend == l:
841 return d # avoid a copy
863 return d # avoid a copy
842 return util.buffer(d, cachestart, cacheend - cachestart)
864 return util.buffer(d, cachestart, cacheend - cachestart)
843
865
844 return self._loadchunk(offset, length)
866 return self._loadchunk(offset, length)
845
867
846 def _chunkraw(self, startrev, endrev):
868 def _chunkraw(self, startrev, endrev):
847 start = self.start(startrev)
869 start = self.start(startrev)
848 end = self.end(endrev)
870 end = self.end(endrev)
849 if self._inline:
871 if self._inline:
850 start += (startrev + 1) * self._io.size
872 start += (startrev + 1) * self._io.size
851 end += (endrev + 1) * self._io.size
873 end += (endrev + 1) * self._io.size
852 length = end - start
874 length = end - start
853 return self._getchunk(start, length)
875 return self._getchunk(start, length)
854
876
855 def _chunk(self, rev):
877 def _chunk(self, rev):
856 return decompress(self._chunkraw(rev, rev))
878 return decompress(self._chunkraw(rev, rev))
857
879
858 def _chunks(self, revs):
880 def _chunks(self, revs):
859 '''faster version of [self._chunk(rev) for rev in revs]
881 '''faster version of [self._chunk(rev) for rev in revs]
860
882
861 Assumes that revs is in ascending order.'''
883 Assumes that revs is in ascending order.'''
862 if not revs:
884 if not revs:
863 return []
885 return []
864 start = self.start
886 start = self.start
865 length = self.length
887 length = self.length
866 inline = self._inline
888 inline = self._inline
867 iosize = self._io.size
889 iosize = self._io.size
868 buffer = util.buffer
890 buffer = util.buffer
869
891
870 l = []
892 l = []
871 ladd = l.append
893 ladd = l.append
872
894
873 # preload the cache
895 # preload the cache
874 self._chunkraw(revs[0], revs[-1])
896 self._chunkraw(revs[0], revs[-1])
875 offset, data = self._chunkcache
897 offset, data = self._chunkcache
876
898
877 for rev in revs:
899 for rev in revs:
878 chunkstart = start(rev)
900 chunkstart = start(rev)
879 if inline:
901 if inline:
880 chunkstart += (rev + 1) * iosize
902 chunkstart += (rev + 1) * iosize
881 chunklength = length(rev)
903 chunklength = length(rev)
882 ladd(decompress(buffer(data, chunkstart - offset, chunklength)))
904 ladd(decompress(buffer(data, chunkstart - offset, chunklength)))
883
905
884 return l
906 return l
885
907
886 def _chunkclear(self):
908 def _chunkclear(self):
887 self._chunkcache = (0, '')
909 self._chunkcache = (0, '')
888
910
889 def deltaparent(self, rev):
911 def deltaparent(self, rev):
890 """return deltaparent of the given revision"""
912 """return deltaparent of the given revision"""
891 base = self.index[rev][3]
913 base = self.index[rev][3]
892 if base == rev:
914 if base == rev:
893 return nullrev
915 return nullrev
894 elif self._generaldelta:
916 elif self._generaldelta:
895 return base
917 return base
896 else:
918 else:
897 return rev - 1
919 return rev - 1
898
920
899 def revdiff(self, rev1, rev2):
921 def revdiff(self, rev1, rev2):
900 """return or calculate a delta between two revisions"""
922 """return or calculate a delta between two revisions"""
901 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
923 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
902 return str(self._chunk(rev2))
924 return str(self._chunk(rev2))
903
925
904 return mdiff.textdiff(self.revision(rev1),
926 return mdiff.textdiff(self.revision(rev1),
905 self.revision(rev2))
927 self.revision(rev2))
906
928
907 def revision(self, nodeorrev):
929 def revision(self, nodeorrev):
908 """return an uncompressed revision of a given node or revision
930 """return an uncompressed revision of a given node or revision
909 number.
931 number.
910 """
932 """
911 if isinstance(nodeorrev, int):
933 if isinstance(nodeorrev, int):
912 rev = nodeorrev
934 rev = nodeorrev
913 node = self.node(rev)
935 node = self.node(rev)
914 else:
936 else:
915 node = nodeorrev
937 node = nodeorrev
916 rev = None
938 rev = None
917
939
918 cachedrev = None
940 cachedrev = None
919 if node == nullid:
941 if node == nullid:
920 return ""
942 return ""
921 if self._cache:
943 if self._cache:
922 if self._cache[0] == node:
944 if self._cache[0] == node:
923 return self._cache[2]
945 return self._cache[2]
924 cachedrev = self._cache[1]
946 cachedrev = self._cache[1]
925
947
926 # look up what we need to read
948 # look up what we need to read
927 text = None
949 text = None
928 if rev is None:
950 if rev is None:
929 rev = self.rev(node)
951 rev = self.rev(node)
930
952
931 # check rev flags
953 # check rev flags
932 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
954 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
933 raise RevlogError(_('incompatible revision flag %x') %
955 raise RevlogError(_('incompatible revision flag %x') %
934 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
956 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
935
957
936 # build delta chain
958 # build delta chain
937 chain = []
959 chain = []
938 index = self.index # for performance
960 index = self.index # for performance
939 generaldelta = self._generaldelta
961 generaldelta = self._generaldelta
940 iterrev = rev
962 iterrev = rev
941 e = index[iterrev]
963 e = index[iterrev]
942 while iterrev != e[3] and iterrev != cachedrev:
964 while iterrev != e[3] and iterrev != cachedrev:
943 chain.append(iterrev)
965 chain.append(iterrev)
944 if generaldelta:
966 if generaldelta:
945 iterrev = e[3]
967 iterrev = e[3]
946 else:
968 else:
947 iterrev -= 1
969 iterrev -= 1
948 e = index[iterrev]
970 e = index[iterrev]
949
971
950 if iterrev == cachedrev:
972 if iterrev == cachedrev:
951 # cache hit
973 # cache hit
952 text = self._cache[2]
974 text = self._cache[2]
953 else:
975 else:
954 chain.append(iterrev)
976 chain.append(iterrev)
955 chain.reverse()
977 chain.reverse()
956
978
957 # drop cache to save memory
979 # drop cache to save memory
958 self._cache = None
980 self._cache = None
959
981
960 bins = self._chunks(chain)
982 bins = self._chunks(chain)
961 if text is None:
983 if text is None:
962 text = str(bins[0])
984 text = str(bins[0])
963 bins = bins[1:]
985 bins = bins[1:]
964
986
965 text = mdiff.patches(text, bins)
987 text = mdiff.patches(text, bins)
966
988
967 text = self._checkhash(text, node, rev)
989 text = self._checkhash(text, node, rev)
968
990
969 self._cache = (node, rev, text)
991 self._cache = (node, rev, text)
970 return text
992 return text
971
993
972 def _checkhash(self, text, node, rev):
994 def _checkhash(self, text, node, rev):
973 p1, p2 = self.parents(node)
995 p1, p2 = self.parents(node)
974 self.checkhash(text, p1, p2, node, rev)
996 self.checkhash(text, p1, p2, node, rev)
975 return text
997 return text
976
998
977 def checkhash(self, text, p1, p2, node, rev=None):
999 def checkhash(self, text, p1, p2, node, rev=None):
978 if node != hash(text, p1, p2):
1000 if node != hash(text, p1, p2):
979 revornode = rev
1001 revornode = rev
980 if revornode is None:
1002 if revornode is None:
981 revornode = templatefilters.short(hex(node))
1003 revornode = templatefilters.short(hex(node))
982 raise RevlogError(_("integrity check failed on %s:%s")
1004 raise RevlogError(_("integrity check failed on %s:%s")
983 % (self.indexfile, revornode))
1005 % (self.indexfile, revornode))
984
1006
985 def checkinlinesize(self, tr, fp=None):
1007 def checkinlinesize(self, tr, fp=None):
986 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
1008 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
987 return
1009 return
988
1010
989 trinfo = tr.find(self.indexfile)
1011 trinfo = tr.find(self.indexfile)
990 if trinfo is None:
1012 if trinfo is None:
991 raise RevlogError(_("%s not found in the transaction")
1013 raise RevlogError(_("%s not found in the transaction")
992 % self.indexfile)
1014 % self.indexfile)
993
1015
994 trindex = trinfo[2]
1016 trindex = trinfo[2]
995 dataoff = self.start(trindex)
1017 dataoff = self.start(trindex)
996
1018
997 tr.add(self.datafile, dataoff)
1019 tr.add(self.datafile, dataoff)
998
1020
999 if fp:
1021 if fp:
1000 fp.flush()
1022 fp.flush()
1001 fp.close()
1023 fp.close()
1002
1024
1003 df = self.opener(self.datafile, 'w')
1025 df = self.opener(self.datafile, 'w')
1004 try:
1026 try:
1005 for r in self:
1027 for r in self:
1006 df.write(self._chunkraw(r, r))
1028 df.write(self._chunkraw(r, r))
1007 finally:
1029 finally:
1008 df.close()
1030 df.close()
1009
1031
1010 fp = self.opener(self.indexfile, 'w', atomictemp=True)
1032 fp = self.opener(self.indexfile, 'w', atomictemp=True)
1011 self.version &= ~(REVLOGNGINLINEDATA)
1033 self.version &= ~(REVLOGNGINLINEDATA)
1012 self._inline = False
1034 self._inline = False
1013 for i in self:
1035 for i in self:
1014 e = self._io.packentry(self.index[i], self.node, self.version, i)
1036 e = self._io.packentry(self.index[i], self.node, self.version, i)
1015 fp.write(e)
1037 fp.write(e)
1016
1038
1017 # if we don't call close, the temp file will never replace the
1039 # if we don't call close, the temp file will never replace the
1018 # real index
1040 # real index
1019 fp.close()
1041 fp.close()
1020
1042
1021 tr.replace(self.indexfile, trindex * self._io.size)
1043 tr.replace(self.indexfile, trindex * self._io.size)
1022 self._chunkclear()
1044 self._chunkclear()
1023
1045
1024 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None,
1046 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None,
1025 node=None):
1047 node=None):
1026 """add a revision to the log
1048 """add a revision to the log
1027
1049
1028 text - the revision data to add
1050 text - the revision data to add
1029 transaction - the transaction object used for rollback
1051 transaction - the transaction object used for rollback
1030 link - the linkrev data to add
1052 link - the linkrev data to add
1031 p1, p2 - the parent nodeids of the revision
1053 p1, p2 - the parent nodeids of the revision
1032 cachedelta - an optional precomputed delta
1054 cachedelta - an optional precomputed delta
1033 node - nodeid of revision; typically node is not specified, and it is
1055 node - nodeid of revision; typically node is not specified, and it is
1034 computed by default as hash(text, p1, p2), however subclasses might
1056 computed by default as hash(text, p1, p2), however subclasses might
1035 use different hashing method (and override checkhash() in such case)
1057 use different hashing method (and override checkhash() in such case)
1036 """
1058 """
1037 if link == nullrev:
1059 if link == nullrev:
1038 raise RevlogError(_("attempted to add linkrev -1 to %s")
1060 raise RevlogError(_("attempted to add linkrev -1 to %s")
1039 % self.indexfile)
1061 % self.indexfile)
1040 node = node or hash(text, p1, p2)
1062 node = node or hash(text, p1, p2)
1041 if node in self.nodemap:
1063 if node in self.nodemap:
1042 return node
1064 return node
1043
1065
1044 dfh = None
1066 dfh = None
1045 if not self._inline:
1067 if not self._inline:
1046 dfh = self.opener(self.datafile, "a")
1068 dfh = self.opener(self.datafile, "a")
1047 ifh = self.opener(self.indexfile, "a+")
1069 ifh = self.opener(self.indexfile, "a+")
1048 try:
1070 try:
1049 return self._addrevision(node, text, transaction, link, p1, p2,
1071 return self._addrevision(node, text, transaction, link, p1, p2,
1050 cachedelta, ifh, dfh)
1072 cachedelta, ifh, dfh)
1051 finally:
1073 finally:
1052 if dfh:
1074 if dfh:
1053 dfh.close()
1075 dfh.close()
1054 ifh.close()
1076 ifh.close()
1055
1077
1056 def compress(self, text):
1078 def compress(self, text):
1057 """ generate a possibly-compressed representation of text """
1079 """ generate a possibly-compressed representation of text """
1058 if not text:
1080 if not text:
1059 return ("", text)
1081 return ("", text)
1060 l = len(text)
1082 l = len(text)
1061 bin = None
1083 bin = None
1062 if l < 44:
1084 if l < 44:
1063 pass
1085 pass
1064 elif l > 1000000:
1086 elif l > 1000000:
1065 # zlib makes an internal copy, thus doubling memory usage for
1087 # zlib makes an internal copy, thus doubling memory usage for
1066 # large files, so lets do this in pieces
1088 # large files, so lets do this in pieces
1067 z = zlib.compressobj()
1089 z = zlib.compressobj()
1068 p = []
1090 p = []
1069 pos = 0
1091 pos = 0
1070 while pos < l:
1092 while pos < l:
1071 pos2 = pos + 2**20
1093 pos2 = pos + 2**20
1072 p.append(z.compress(text[pos:pos2]))
1094 p.append(z.compress(text[pos:pos2]))
1073 pos = pos2
1095 pos = pos2
1074 p.append(z.flush())
1096 p.append(z.flush())
1075 if sum(map(len, p)) < l:
1097 if sum(map(len, p)) < l:
1076 bin = "".join(p)
1098 bin = "".join(p)
1077 else:
1099 else:
1078 bin = _compress(text)
1100 bin = _compress(text)
1079 if bin is None or len(bin) > l:
1101 if bin is None or len(bin) > l:
1080 if text[0] == '\0':
1102 if text[0] == '\0':
1081 return ("", text)
1103 return ("", text)
1082 return ('u', text)
1104 return ('u', text)
1083 return ("", bin)
1105 return ("", bin)
1084
1106
1085 def _addrevision(self, node, text, transaction, link, p1, p2,
1107 def _addrevision(self, node, text, transaction, link, p1, p2,
1086 cachedelta, ifh, dfh):
1108 cachedelta, ifh, dfh):
1087 """internal function to add revisions to the log
1109 """internal function to add revisions to the log
1088
1110
1089 see addrevision for argument descriptions.
1111 see addrevision for argument descriptions.
1090 invariants:
1112 invariants:
1091 - text is optional (can be None); if not set, cachedelta must be set.
1113 - text is optional (can be None); if not set, cachedelta must be set.
1092 if both are set, they must correspond to each other.
1114 if both are set, they must correspond to each other.
1093 """
1115 """
1094 btext = [text]
1116 btext = [text]
1095 def buildtext():
1117 def buildtext():
1096 if btext[0] is not None:
1118 if btext[0] is not None:
1097 return btext[0]
1119 return btext[0]
1098 # flush any pending writes here so we can read it in revision
1120 # flush any pending writes here so we can read it in revision
1099 if dfh:
1121 if dfh:
1100 dfh.flush()
1122 dfh.flush()
1101 ifh.flush()
1123 ifh.flush()
1102 basetext = self.revision(self.node(cachedelta[0]))
1124 basetext = self.revision(self.node(cachedelta[0]))
1103 btext[0] = mdiff.patch(basetext, cachedelta[1])
1125 btext[0] = mdiff.patch(basetext, cachedelta[1])
1104 self.checkhash(btext[0], p1, p2, node)
1126 self.checkhash(btext[0], p1, p2, node)
1105 return btext[0]
1127 return btext[0]
1106
1128
1107 def builddelta(rev):
1129 def builddelta(rev):
1108 # can we use the cached delta?
1130 # can we use the cached delta?
1109 if cachedelta and cachedelta[0] == rev:
1131 if cachedelta and cachedelta[0] == rev:
1110 delta = cachedelta[1]
1132 delta = cachedelta[1]
1111 else:
1133 else:
1112 t = buildtext()
1134 t = buildtext()
1113 ptext = self.revision(self.node(rev))
1135 ptext = self.revision(self.node(rev))
1114 delta = mdiff.textdiff(ptext, t)
1136 delta = mdiff.textdiff(ptext, t)
1115 data = self.compress(delta)
1137 data = self.compress(delta)
1116 l = len(data[1]) + len(data[0])
1138 l = len(data[1]) + len(data[0])
1117 if basecache[0] == rev:
1139 if basecache[0] == rev:
1118 chainbase = basecache[1]
1140 chainbase = basecache[1]
1119 else:
1141 else:
1120 chainbase = self.chainbase(rev)
1142 chainbase = self.chainbase(rev)
1121 dist = l + offset - self.start(chainbase)
1143 dist = l + offset - self.start(chainbase)
1122 if self._generaldelta:
1144 if self._generaldelta:
1123 base = rev
1145 base = rev
1124 else:
1146 else:
1125 base = chainbase
1147 base = chainbase
1126 return dist, l, data, base, chainbase
1148 return dist, l, data, base, chainbase
1127
1149
1128 curr = len(self)
1150 curr = len(self)
1129 prev = curr - 1
1151 prev = curr - 1
1130 base = chainbase = curr
1152 base = chainbase = curr
1131 offset = self.end(prev)
1153 offset = self.end(prev)
1132 flags = 0
1154 flags = 0
1133 d = None
1155 d = None
1134 if self._basecache is None:
1156 if self._basecache is None:
1135 self._basecache = (prev, self.chainbase(prev))
1157 self._basecache = (prev, self.chainbase(prev))
1136 basecache = self._basecache
1158 basecache = self._basecache
1137 p1r, p2r = self.rev(p1), self.rev(p2)
1159 p1r, p2r = self.rev(p1), self.rev(p2)
1138
1160
1139 # should we try to build a delta?
1161 # should we try to build a delta?
1140 if prev != nullrev:
1162 if prev != nullrev:
1141 if self._generaldelta:
1163 if self._generaldelta:
1142 if p1r >= basecache[1]:
1164 if p1r >= basecache[1]:
1143 d = builddelta(p1r)
1165 d = builddelta(p1r)
1144 elif p2r >= basecache[1]:
1166 elif p2r >= basecache[1]:
1145 d = builddelta(p2r)
1167 d = builddelta(p2r)
1146 else:
1168 else:
1147 d = builddelta(prev)
1169 d = builddelta(prev)
1148 else:
1170 else:
1149 d = builddelta(prev)
1171 d = builddelta(prev)
1150 dist, l, data, base, chainbase = d
1172 dist, l, data, base, chainbase = d
1151
1173
1152 # full versions are inserted when the needed deltas
1174 # full versions are inserted when the needed deltas
1153 # become comparable to the uncompressed text
1175 # become comparable to the uncompressed text
1154 if text is None:
1176 if text is None:
1155 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
1177 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
1156 cachedelta[1])
1178 cachedelta[1])
1157 else:
1179 else:
1158 textlen = len(text)
1180 textlen = len(text)
1159 if d is None or dist > textlen * 2:
1181 if d is None or dist > textlen * 2:
1160 text = buildtext()
1182 text = buildtext()
1161 data = self.compress(text)
1183 data = self.compress(text)
1162 l = len(data[1]) + len(data[0])
1184 l = len(data[1]) + len(data[0])
1163 base = chainbase = curr
1185 base = chainbase = curr
1164
1186
1165 e = (offset_type(offset, flags), l, textlen,
1187 e = (offset_type(offset, flags), l, textlen,
1166 base, link, p1r, p2r, node)
1188 base, link, p1r, p2r, node)
1167 self.index.insert(-1, e)
1189 self.index.insert(-1, e)
1168 self.nodemap[node] = curr
1190 self.nodemap[node] = curr
1169
1191
1170 entry = self._io.packentry(e, self.node, self.version, curr)
1192 entry = self._io.packentry(e, self.node, self.version, curr)
1171 if not self._inline:
1193 if not self._inline:
1172 transaction.add(self.datafile, offset)
1194 transaction.add(self.datafile, offset)
1173 transaction.add(self.indexfile, curr * len(entry))
1195 transaction.add(self.indexfile, curr * len(entry))
1174 if data[0]:
1196 if data[0]:
1175 dfh.write(data[0])
1197 dfh.write(data[0])
1176 dfh.write(data[1])
1198 dfh.write(data[1])
1177 dfh.flush()
1199 dfh.flush()
1178 ifh.write(entry)
1200 ifh.write(entry)
1179 else:
1201 else:
1180 offset += curr * self._io.size
1202 offset += curr * self._io.size
1181 transaction.add(self.indexfile, offset, curr)
1203 transaction.add(self.indexfile, offset, curr)
1182 ifh.write(entry)
1204 ifh.write(entry)
1183 ifh.write(data[0])
1205 ifh.write(data[0])
1184 ifh.write(data[1])
1206 ifh.write(data[1])
1185 self.checkinlinesize(transaction, ifh)
1207 self.checkinlinesize(transaction, ifh)
1186
1208
1187 if type(text) == str: # only accept immutable objects
1209 if type(text) == str: # only accept immutable objects
1188 self._cache = (node, curr, text)
1210 self._cache = (node, curr, text)
1189 self._basecache = (curr, chainbase)
1211 self._basecache = (curr, chainbase)
1190 return node
1212 return node
1191
1213
1192 def addgroup(self, bundle, linkmapper, transaction):
1214 def addgroup(self, bundle, linkmapper, transaction):
1193 """
1215 """
1194 add a delta group
1216 add a delta group
1195
1217
1196 given a set of deltas, add them to the revision log. the
1218 given a set of deltas, add them to the revision log. the
1197 first delta is against its parent, which should be in our
1219 first delta is against its parent, which should be in our
1198 log, the rest are against the previous delta.
1220 log, the rest are against the previous delta.
1199 """
1221 """
1200
1222
1201 # track the base of the current delta log
1223 # track the base of the current delta log
1202 content = []
1224 content = []
1203 node = None
1225 node = None
1204
1226
1205 r = len(self)
1227 r = len(self)
1206 end = 0
1228 end = 0
1207 if r:
1229 if r:
1208 end = self.end(r - 1)
1230 end = self.end(r - 1)
1209 ifh = self.opener(self.indexfile, "a+")
1231 ifh = self.opener(self.indexfile, "a+")
1210 isize = r * self._io.size
1232 isize = r * self._io.size
1211 if self._inline:
1233 if self._inline:
1212 transaction.add(self.indexfile, end + isize, r)
1234 transaction.add(self.indexfile, end + isize, r)
1213 dfh = None
1235 dfh = None
1214 else:
1236 else:
1215 transaction.add(self.indexfile, isize, r)
1237 transaction.add(self.indexfile, isize, r)
1216 transaction.add(self.datafile, end)
1238 transaction.add(self.datafile, end)
1217 dfh = self.opener(self.datafile, "a")
1239 dfh = self.opener(self.datafile, "a")
1218
1240
1219 try:
1241 try:
1220 # loop through our set of deltas
1242 # loop through our set of deltas
1221 chain = None
1243 chain = None
1222 while True:
1244 while True:
1223 chunkdata = bundle.deltachunk(chain)
1245 chunkdata = bundle.deltachunk(chain)
1224 if not chunkdata:
1246 if not chunkdata:
1225 break
1247 break
1226 node = chunkdata['node']
1248 node = chunkdata['node']
1227 p1 = chunkdata['p1']
1249 p1 = chunkdata['p1']
1228 p2 = chunkdata['p2']
1250 p2 = chunkdata['p2']
1229 cs = chunkdata['cs']
1251 cs = chunkdata['cs']
1230 deltabase = chunkdata['deltabase']
1252 deltabase = chunkdata['deltabase']
1231 delta = chunkdata['delta']
1253 delta = chunkdata['delta']
1232
1254
1233 content.append(node)
1255 content.append(node)
1234
1256
1235 link = linkmapper(cs)
1257 link = linkmapper(cs)
1236 if node in self.nodemap:
1258 if node in self.nodemap:
1237 # this can happen if two branches make the same change
1259 # this can happen if two branches make the same change
1238 chain = node
1260 chain = node
1239 continue
1261 continue
1240
1262
1241 for p in (p1, p2):
1263 for p in (p1, p2):
1242 if p not in self.nodemap:
1264 if p not in self.nodemap:
1243 raise LookupError(p, self.indexfile,
1265 raise LookupError(p, self.indexfile,
1244 _('unknown parent'))
1266 _('unknown parent'))
1245
1267
1246 if deltabase not in self.nodemap:
1268 if deltabase not in self.nodemap:
1247 raise LookupError(deltabase, self.indexfile,
1269 raise LookupError(deltabase, self.indexfile,
1248 _('unknown delta base'))
1270 _('unknown delta base'))
1249
1271
1250 baserev = self.rev(deltabase)
1272 baserev = self.rev(deltabase)
1251 chain = self._addrevision(node, None, transaction, link,
1273 chain = self._addrevision(node, None, transaction, link,
1252 p1, p2, (baserev, delta), ifh, dfh)
1274 p1, p2, (baserev, delta), ifh, dfh)
1253 if not dfh and not self._inline:
1275 if not dfh and not self._inline:
1254 # addrevision switched from inline to conventional
1276 # addrevision switched from inline to conventional
1255 # reopen the index
1277 # reopen the index
1256 ifh.close()
1278 ifh.close()
1257 dfh = self.opener(self.datafile, "a")
1279 dfh = self.opener(self.datafile, "a")
1258 ifh = self.opener(self.indexfile, "a")
1280 ifh = self.opener(self.indexfile, "a")
1259 finally:
1281 finally:
1260 if dfh:
1282 if dfh:
1261 dfh.close()
1283 dfh.close()
1262 ifh.close()
1284 ifh.close()
1263
1285
1264 return content
1286 return content
1265
1287
1266 def strip(self, minlink, transaction):
1288 def strip(self, minlink, transaction):
1267 """truncate the revlog on the first revision with a linkrev >= minlink
1289 """truncate the revlog on the first revision with a linkrev >= minlink
1268
1290
1269 This function is called when we're stripping revision minlink and
1291 This function is called when we're stripping revision minlink and
1270 its descendants from the repository.
1292 its descendants from the repository.
1271
1293
1272 We have to remove all revisions with linkrev >= minlink, because
1294 We have to remove all revisions with linkrev >= minlink, because
1273 the equivalent changelog revisions will be renumbered after the
1295 the equivalent changelog revisions will be renumbered after the
1274 strip.
1296 strip.
1275
1297
1276 So we truncate the revlog on the first of these revisions, and
1298 So we truncate the revlog on the first of these revisions, and
1277 trust that the caller has saved the revisions that shouldn't be
1299 trust that the caller has saved the revisions that shouldn't be
1278 removed and that it'll re-add them after this truncation.
1300 removed and that it'll re-add them after this truncation.
1279 """
1301 """
1280 if len(self) == 0:
1302 if len(self) == 0:
1281 return
1303 return
1282
1304
1283 for rev in self:
1305 for rev in self:
1284 if self.index[rev][4] >= minlink:
1306 if self.index[rev][4] >= minlink:
1285 break
1307 break
1286 else:
1308 else:
1287 return
1309 return
1288
1310
1289 # first truncate the files on disk
1311 # first truncate the files on disk
1290 end = self.start(rev)
1312 end = self.start(rev)
1291 if not self._inline:
1313 if not self._inline:
1292 transaction.add(self.datafile, end)
1314 transaction.add(self.datafile, end)
1293 end = rev * self._io.size
1315 end = rev * self._io.size
1294 else:
1316 else:
1295 end += rev * self._io.size
1317 end += rev * self._io.size
1296
1318
1297 transaction.add(self.indexfile, end)
1319 transaction.add(self.indexfile, end)
1298
1320
1299 # then reset internal state in memory to forget those revisions
1321 # then reset internal state in memory to forget those revisions
1300 self._cache = None
1322 self._cache = None
1301 self._chunkclear()
1323 self._chunkclear()
1302 for x in xrange(rev, len(self)):
1324 for x in xrange(rev, len(self)):
1303 del self.nodemap[self.node(x)]
1325 del self.nodemap[self.node(x)]
1304
1326
1305 del self.index[rev:-1]
1327 del self.index[rev:-1]
1306
1328
1307 def checksize(self):
1329 def checksize(self):
1308 expected = 0
1330 expected = 0
1309 if len(self):
1331 if len(self):
1310 expected = max(0, self.end(len(self) - 1))
1332 expected = max(0, self.end(len(self) - 1))
1311
1333
1312 try:
1334 try:
1313 f = self.opener(self.datafile)
1335 f = self.opener(self.datafile)
1314 f.seek(0, 2)
1336 f.seek(0, 2)
1315 actual = f.tell()
1337 actual = f.tell()
1316 f.close()
1338 f.close()
1317 dd = actual - expected
1339 dd = actual - expected
1318 except IOError, inst:
1340 except IOError, inst:
1319 if inst.errno != errno.ENOENT:
1341 if inst.errno != errno.ENOENT:
1320 raise
1342 raise
1321 dd = 0
1343 dd = 0
1322
1344
1323 try:
1345 try:
1324 f = self.opener(self.indexfile)
1346 f = self.opener(self.indexfile)
1325 f.seek(0, 2)
1347 f.seek(0, 2)
1326 actual = f.tell()
1348 actual = f.tell()
1327 f.close()
1349 f.close()
1328 s = self._io.size
1350 s = self._io.size
1329 i = max(0, actual // s)
1351 i = max(0, actual // s)
1330 di = actual - (i * s)
1352 di = actual - (i * s)
1331 if self._inline:
1353 if self._inline:
1332 databytes = 0
1354 databytes = 0
1333 for r in self:
1355 for r in self:
1334 databytes += max(0, self.length(r))
1356 databytes += max(0, self.length(r))
1335 dd = 0
1357 dd = 0
1336 di = actual - len(self) * s - databytes
1358 di = actual - len(self) * s - databytes
1337 except IOError, inst:
1359 except IOError, inst:
1338 if inst.errno != errno.ENOENT:
1360 if inst.errno != errno.ENOENT:
1339 raise
1361 raise
1340 di = 0
1362 di = 0
1341
1363
1342 return (dd, di)
1364 return (dd, di)
1343
1365
1344 def files(self):
1366 def files(self):
1345 res = [self.indexfile]
1367 res = [self.indexfile]
1346 if not self._inline:
1368 if not self._inline:
1347 res.append(self.datafile)
1369 res.append(self.datafile)
1348 return res
1370 return res
General Comments 0
You need to be logged in to leave comments. Login now