revlog.py
1332 lines
| 45.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / revlog.py
|
r1083 | """ | ||
revlog.py - storage back-end for mercurial | ||||
This provides efficient delta storage with O(1) retrieve and append | ||||
and O(changes) merge between branches | ||||
|
r4635 | Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
|
r1083 | |||
This software may be used and distributed according to the terms | ||||
of the GNU General Public License, incorporated herein by reference. | ||||
""" | ||||
|
r0 | |||
|
r6211 | from node import bin, hex, nullid, nullrev, short | ||
|
r3891 | from i18n import _ | ||
|
r6212 | import changegroup, errno, ancestor, mdiff | ||
|
r6470 | import struct, util, zlib | ||
|
r36 | |||
|
r5007 | _pack = struct.pack | ||
_unpack = struct.unpack | ||||
_compress = zlib.compress | ||||
_decompress = zlib.decompress | ||||
|
r6470 | _sha = util.sha1 | ||
|
r5007 | |||
|
r4987 | # revlog flags | ||
|
r2072 | REVLOGV0 = 0 | ||
REVLOGNG = 1 | ||||
|
r2073 | REVLOGNGINLINEDATA = (1 << 16) | ||
|
r2222 | REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA | ||
REVLOG_DEFAULT_FORMAT = REVLOGNG | ||||
REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS | ||||
|
r2073 | |||
|
r4987 | class RevlogError(Exception): | ||
pass | ||||
|
r5558 | |||
|
r4987 | class LookupError(RevlogError): | ||
|
r6228 | def __init__(self, name, index, message): | ||
|
r5558 | self.name = name | ||
|
r6228 | if isinstance(name, str) and len(name) == 20: | ||
name = short(name) | ||||
RevlogError.__init__(self, _('%s@%s: %s') % (index, name, message)) | ||||
|
r4987 | |||
def getoffset(q): | ||||
return int(q >> 16) | ||||
def gettype(q): | ||||
return int(q & 0xFFFF) | ||||
def offset_type(offset, type): | ||||
return long(long(offset) << 16 | type) | ||||
|
r1091 | def hash(text, p1, p2): | ||
"""generate a hash from the given text and its parent hashes | ||||
This hash combines both the current file contents and its history | ||||
in a manner that makes it easy to distinguish nodes with the same | ||||
content in the revision graph. | ||||
""" | ||||
l = [p1, p2] | ||||
l.sort() | ||||
|
r5007 | s = _sha(l[0]) | ||
|
r1091 | s.update(l[1]) | ||
s.update(text) | ||||
return s.digest() | ||||
|
r0 | def compress(text): | ||
|
r1083 | """ generate a possibly-compressed representation of text """ | ||
|
r4980 | if not text: | ||
return ("", text) | ||||
|
r5451 | l = len(text) | ||
|
r5453 | bin = None | ||
|
r5451 | if l < 44: | ||
|
r5453 | pass | ||
|
r5451 | elif l > 1000000: | ||
# zlib makes an internal copy, thus doubling memory usage for | ||||
# large files, so lets do this in pieces | ||||
z = zlib.compressobj() | ||||
p = [] | ||||
pos = 0 | ||||
while pos < l: | ||||
pos2 = pos + 2**20 | ||||
p.append(z.compress(text[pos:pos2])) | ||||
pos = pos2 | ||||
p.append(z.flush()) | ||||
if sum(map(len, p)) < l: | ||||
bin = "".join(p) | ||||
else: | ||||
bin = _compress(text) | ||||
|
r5453 | if bin is None or len(bin) > l: | ||
|
r4980 | if text[0] == '\0': | ||
return ("", text) | ||||
|
r1533 | return ('u', text) | ||
return ("", bin) | ||||
|
r0 | |||
def decompress(bin): | ||||
|
r1083 | """ decompress the given input """ | ||
|
r4980 | if not bin: | ||
return bin | ||||
|
r112 | t = bin[0] | ||
|
r4980 | if t == '\0': | ||
return bin | ||||
if t == 'x': | ||||
|
r5007 | return _decompress(bin) | ||
|
r4980 | if t == 'u': | ||
return bin[1:] | ||||
|
r1853 | raise RevlogError(_("unknown compression type %r") % t) | ||
|
r0 | |||
|
r1559 | class lazyparser(object): | ||
|
r1083 | """ | ||
this class avoids the need to parse the entirety of large indices | ||||
""" | ||||
|
r2250 | |||
# lazyparser is not safe to use on windows if win32 extensions not | ||||
# available. it keeps file handle open, which make it not possible | ||||
# to break hardlinks on local cloned repos. | ||||
|
r4976 | def __init__(self, dataf, size): | ||
|
r2079 | self.dataf = dataf | ||
|
r4976 | self.s = struct.calcsize(indexformatng) | ||
|
r2079 | self.datasize = size | ||
self.l = size/self.s | ||||
|
r76 | self.index = [None] * self.l | ||
|
r3578 | self.map = {nullid: nullrev} | ||
|
r2079 | self.allmap = 0 | ||
|
r323 | self.all = 0 | ||
|
r2079 | self.mapfind_count = 0 | ||
|
r76 | |||
|
r2079 | def loadmap(self): | ||
""" | ||||
during a commit, we need to make sure the rev being added is | ||||
not a duplicate. This requires loading the entire index, | ||||
which is fairly slow. loadmap can load up just the node map, | ||||
which takes much less time. | ||||
""" | ||||
|
r4980 | if self.allmap: | ||
return | ||||
|
r2079 | end = self.datasize | ||
self.allmap = 1 | ||||
cur = 0 | ||||
count = 0 | ||||
blocksize = self.s * 256 | ||||
self.dataf.seek(0) | ||||
while cur < end: | ||||
data = self.dataf.read(blocksize) | ||||
off = 0 | ||||
for x in xrange(256): | ||||
|
r4976 | n = data[off + ngshaoffset:off + ngshaoffset + 20] | ||
|
r2079 | self.map[n] = count | ||
count += 1 | ||||
if count >= self.l: | ||||
break | ||||
off += self.s | ||||
cur += blocksize | ||||
def loadblock(self, blockstart, blocksize, data=None): | ||||
|
r4980 | if self.all: | ||
return | ||||
|
r2079 | if data is None: | ||
self.dataf.seek(blockstart) | ||||
|
r3078 | if blockstart + blocksize > self.datasize: | ||
# the revlog may have grown since we've started running, | ||||
# but we don't have space in self.index for more entries. | ||||
# limit blocksize so that we don't get too much data. | ||||
|
r3089 | blocksize = max(self.datasize - blockstart, 0) | ||
|
r2079 | data = self.dataf.read(blocksize) | ||
lend = len(data) / self.s | ||||
i = blockstart / self.s | ||||
off = 0 | ||||
|
r4061 | # lazyindex supports __delitem__ | ||
if lend > len(self.index) - i: | ||||
lend = len(self.index) - i | ||||
|
r2079 | for x in xrange(lend): | ||
if self.index[i + x] == None: | ||||
b = data[off : off + self.s] | ||||
|
r2080 | self.index[i + x] = b | ||
|
r4976 | n = b[ngshaoffset:ngshaoffset + 20] | ||
|
r2080 | self.map[n] = i + x | ||
|
r2079 | off += self.s | ||
def findnode(self, node): | ||||
"""search backwards through the index file for a specific node""" | ||||
|
r4980 | if self.allmap: | ||
return None | ||||
|
r2079 | |||
# hg log will cause many many searches for the manifest | ||||
# nodes. After we get called a few times, just load the whole | ||||
# thing. | ||||
if self.mapfind_count > 8: | ||||
self.loadmap() | ||||
if node in self.map: | ||||
return node | ||||
return None | ||||
self.mapfind_count += 1 | ||||
last = self.l - 1 | ||||
while self.index[last] != None: | ||||
if last == 0: | ||||
self.all = 1 | ||||
self.allmap = 1 | ||||
return None | ||||
last -= 1 | ||||
end = (last + 1) * self.s | ||||
blocksize = self.s * 256 | ||||
while end >= 0: | ||||
start = max(end - blocksize, 0) | ||||
self.dataf.seek(start) | ||||
data = self.dataf.read(end - start) | ||||
findend = end - start | ||||
while True: | ||||
|
r4994 | # we're searching backwards, so we have to make sure | ||
|
r2079 | # we don't find a changeset where this node is a parent | ||
|
r4994 | off = data.find(node, 0, findend) | ||
|
r2079 | findend = off | ||
if off >= 0: | ||||
i = off / self.s | ||||
off = i * self.s | ||||
|
r4976 | n = data[off + ngshaoffset:off + ngshaoffset + 20] | ||
|
r2079 | if n == node: | ||
self.map[n] = i + start / self.s | ||||
return node | ||||
else: | ||||
break | ||||
end -= blocksize | ||||
return None | ||||
def loadindex(self, i=None, end=None): | ||||
|
r4980 | if self.all: | ||
return | ||||
|
r2079 | all = False | ||
if i == None: | ||||
blockstart = 0 | ||||
|
r4992 | blocksize = (65536 / self.s) * self.s | ||
|
r2079 | end = self.datasize | ||
all = True | ||||
|
r323 | else: | ||
|
r2079 | if end: | ||
blockstart = i * self.s | ||||
end = end * self.s | ||||
blocksize = end - blockstart | ||||
else: | ||||
|
r4992 | blockstart = (i & ~1023) * self.s | ||
blocksize = self.s * 1024 | ||||
|
r2079 | end = blockstart + blocksize | ||
while blockstart < end: | ||||
self.loadblock(blockstart, blocksize) | ||||
blockstart += blocksize | ||||
|
r4980 | if all: | ||
self.all = True | ||||
|
r515 | |||
|
r1559 | class lazyindex(object): | ||
|
r1083 | """a lazy version of the index array""" | ||
|
r76 | def __init__(self, parser): | ||
self.p = parser | ||||
def __len__(self): | ||||
return len(self.p.index) | ||||
|
r115 | def load(self, pos): | ||
|
r1403 | if pos < 0: | ||
pos += len(self.p.index) | ||||
|
r2079 | self.p.loadindex(pos) | ||
|
r115 | return self.p.index[pos] | ||
|
r76 | def __getitem__(self, pos): | ||
|
r5007 | return _unpack(indexformatng, self.p.index[pos] or self.load(pos)) | ||
|
r2072 | def __setitem__(self, pos, item): | ||
|
r5007 | self.p.index[pos] = _pack(indexformatng, *item) | ||
|
r1535 | def __delitem__(self, pos): | ||
del self.p.index[pos] | ||||
|
r4979 | def insert(self, pos, e): | ||
|
r5007 | self.p.index.insert(pos, _pack(indexformatng, *e)) | ||
|
r76 | def append(self, e): | ||
|
r5007 | self.p.index.append(_pack(indexformatng, *e)) | ||
|
r515 | |||
|
r1559 | class lazymap(object): | ||
|
r1083 | """a lazy version of the node map""" | ||
|
r76 | def __init__(self, parser): | ||
self.p = parser | ||||
def load(self, key): | ||||
|
r2079 | n = self.p.findnode(key) | ||
if n == None: | ||||
|
r1214 | raise KeyError(key) | ||
|
r76 | def __contains__(self, key): | ||
|
r2079 | if key in self.p.map: | ||
return True | ||||
self.p.loadmap() | ||||
|
r323 | return key in self.p.map | ||
|
r97 | def __iter__(self): | ||
|
r469 | yield nullid | ||
|
r97 | for i in xrange(self.p.l): | ||
|
r2080 | ret = self.p.index[i] | ||
if not ret: | ||||
|
r2079 | self.p.loadindex(i) | ||
|
r2080 | ret = self.p.index[i] | ||
if isinstance(ret, str): | ||||
|
r5007 | ret = _unpack(indexformatng, ret) | ||
|
r4978 | yield ret[7] | ||
|
r76 | def __getitem__(self, key): | ||
try: | ||||
return self.p.map[key] | ||||
except KeyError: | ||||
|
r86 | try: | ||
self.load(key) | ||||
return self.p.map[key] | ||||
except KeyError: | ||||
raise KeyError("node " + hex(key)) | ||||
|
r76 | def __setitem__(self, key, val): | ||
self.p.map[key] = val | ||||
|
r1535 | def __delitem__(self, key): | ||
del self.p.map[key] | ||||
|
r76 | |||
|
r4987 | indexformatv0 = ">4l20s20s20s" | ||
v0shaoffset = 56 | ||||
|
r4918 | |||
|
r4972 | class revlogoldio(object): | ||
def __init__(self): | ||||
|
r4977 | self.size = struct.calcsize(indexformatv0) | ||
|
r4972 | |||
|
r4985 | def parseindex(self, fp, inline): | ||
|
r4977 | s = self.size | ||
|
r4972 | index = [] | ||
nodemap = {nullid: nullrev} | ||||
|
r4973 | n = off = 0 | ||
data = fp.read() | ||||
l = len(data) | ||||
while off + s <= l: | ||||
cur = data[off:off + s] | ||||
off += s | ||||
|
r5007 | e = _unpack(indexformatv0, cur) | ||
|
r4977 | # transform to revlogv1 format | ||
e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3], | ||||
|
r5544 | nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6]) | ||
|
r4977 | index.append(e2) | ||
nodemap[e[6]] = n | ||||
|
r4973 | n += 1 | ||
|
r4972 | |||
|
r4983 | return index, nodemap, None | ||
|
r4972 | |||
|
r5338 | def packentry(self, entry, node, version, rev): | ||
|
r4986 | e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4], | ||
node(entry[5]), node(entry[6]), entry[7]) | ||||
|
r5007 | return _pack(indexformatv0, *e2) | ||
|
r4986 | |||
|
r4987 | # index ng: | ||
# 6 bytes offset | ||||
# 2 bytes flags | ||||
# 4 bytes compressed length | ||||
# 4 bytes uncompressed length | ||||
# 4 bytes: base rev | ||||
# 4 bytes link rev | ||||
# 4 bytes parent 1 rev | ||||
# 4 bytes parent 2 rev | ||||
# 32 bytes: nodeid | ||||
indexformatng = ">Qiiiiii20s12x" | ||||
ngshaoffset = 32 | ||||
versionformat = ">I" | ||||
|
r4972 | class revlogio(object): | ||
def __init__(self): | ||||
|
r4977 | self.size = struct.calcsize(indexformatng) | ||
|
r4972 | |||
|
r4985 | def parseindex(self, fp, inline): | ||
try: | ||||
size = util.fstat(fp).st_size | ||||
except AttributeError: | ||||
size = 0 | ||||
|
r5659 | if util.openhardlinks() and not inline and size > 1000000: | ||
|
r4972 | # big index, let's parse it on demand | ||
|
r4985 | parser = lazyparser(fp, size) | ||
|
r4972 | index = lazyindex(parser) | ||
nodemap = lazymap(parser) | ||||
e = list(index[0]) | ||||
type = gettype(e[0]) | ||||
e[0] = offset_type(0, type) | ||||
index[0] = e | ||||
|
r4983 | return index, nodemap, None | ||
|
r4972 | |||
|
r4977 | s = self.size | ||
|
r4983 | cache = None | ||
|
r4972 | index = [] | ||
nodemap = {nullid: nullrev} | ||||
|
r4975 | n = off = 0 | ||
# if we're not using lazymap, always read the whole index | ||||
data = fp.read() | ||||
|
r4990 | l = len(data) - s | ||
append = index.append | ||||
|
r4975 | if inline: | ||
|
r4983 | cache = (0, data) | ||
|
r4990 | while off <= l: | ||
|
r5007 | e = _unpack(indexformatng, data[off:off + s]) | ||
|
r4990 | nodemap[e[7]] = n | ||
append(e) | ||||
n += 1 | ||||
|
r4975 | if e[1] < 0: | ||
|
r4972 | break | ||
|
r4990 | off += e[1] + s | ||
else: | ||||
while off <= l: | ||||
|
r5007 | e = _unpack(indexformatng, data[off:off + s]) | ||
|
r4990 | nodemap[e[7]] = n | ||
append(e) | ||||
n += 1 | ||||
off += s | ||||
|
r4972 | |||
e = list(index[0]) | ||||
type = gettype(e[0]) | ||||
e[0] = offset_type(0, type) | ||||
index[0] = e | ||||
|
r4983 | return index, nodemap, cache | ||
|
r4972 | |||
|
r5338 | def packentry(self, entry, node, version, rev): | ||
|
r5007 | p = _pack(indexformatng, *entry) | ||
|
r5338 | if rev == 0: | ||
|
r5007 | p = _pack(versionformat, version) + p[4:] | ||
|
r4986 | return p | ||
|
r1559 | class revlog(object): | ||
|
r1083 | """ | ||
the underlying revision storage object | ||||
A revlog consists of two parts, an index and the revision data. | ||||
The index is a file with a fixed record size containing | ||||
information on each revision, includings its nodeid (hash), the | ||||
nodeids of its parents, the position and offset of its data within | ||||
the data file, and the revision it's based on. Finally, each entry | ||||
contains a linkrev entry that can serve as a pointer to external | ||||
data. | ||||
The revision data itself is a linear collection of data chunks. | ||||
Each chunk represents a revision and is usually represented as a | ||||
delta against the previous chunk. To bound lookup time, runs of | ||||
deltas are limited to about 2 times the length of the original | ||||
version data. This makes retrieval of a version proportional to | ||||
its size, or O(1) relative to the number of revisions. | ||||
Both pieces of the revlog are written to in an append-only | ||||
fashion, which means we never need to rewrite a file to insert or | ||||
remove data, and can use some simple techniques to avoid the need | ||||
for locking while reading. | ||||
""" | ||||
|
r4258 | def __init__(self, opener, indexfile): | ||
|
r1083 | """ | ||
create a revlog object | ||||
opener is a function that abstracts the file opening operation | ||||
and can be used to implement COW semantics or the like. | ||||
""" | ||||
|
r0 | self.indexfile = indexfile | ||
|
r4257 | self.datafile = indexfile[:-2] + ".d" | ||
|
r0 | self.opener = opener | ||
|
r4984 | self._cache = None | ||
|
r4983 | self._chunkcache = None | ||
|
r4985 | self.nodemap = {nullid: nullrev} | ||
self.index = [] | ||||
v = REVLOG_DEFAULT_VERSION | ||||
|
r4258 | if hasattr(opener, "defversion"): | ||
|
r4985 | v = opener.defversion | ||
if v & REVLOGNG: | ||||
v |= REVLOGNGINLINEDATA | ||||
|
r116 | |||
|
r4985 | i = "" | ||
|
r0 | try: | ||
|
r1784 | f = self.opener(self.indexfile) | ||
|
r2079 | i = f.read(4) | ||
f.seek(0) | ||||
|
r4918 | if len(i) > 0: | ||
v = struct.unpack(versionformat, i)[0] | ||||
|
r1322 | except IOError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
raise | ||||
|
r4985 | |||
self.version = v | ||||
self._inline = v & REVLOGNGINLINEDATA | ||||
|
r2073 | flags = v & ~0xFFFF | ||
fmt = v & 0xFFFF | ||||
|
r4985 | if fmt == REVLOGV0 and flags: | ||
raise RevlogError(_("index %s unknown flags %#04x for format v0") | ||||
% (self.indexfile, flags >> 16)) | ||||
elif fmt == REVLOGNG and flags & ~REVLOGNGINLINEDATA: | ||||
raise RevlogError(_("index %s unknown flags %#04x for revlogng") | ||||
% (self.indexfile, flags >> 16)) | ||||
elif fmt > REVLOGNG: | ||||
|
r3743 | raise RevlogError(_("index %s unknown format %d") | ||
|
r3680 | % (self.indexfile, fmt)) | ||
|
r4985 | |||
|
r4972 | self._io = revlogio() | ||
|
r4971 | if self.version == REVLOGV0: | ||
|
r4972 | self._io = revlogoldio() | ||
|
r2072 | if i: | ||
|
r4985 | d = self._io.parseindex(f, self._inline) | ||
|
r4983 | self.index, self.nodemap, self._chunkcache = d | ||
|
r4985 | |||
|
r4979 | # add the magic null revision at -1 | ||
self.index.append((0, 0, 0, -1, -1, -1, -1, nullid)) | ||||
|
r116 | |||
|
r4920 | def _loadindex(self, start, end): | ||
|
r2079 | """load a block of indexes all at once from the lazy parser""" | ||
if isinstance(self.index, lazyindex): | ||||
self.index.p.loadindex(start, end) | ||||
|
r4920 | def _loadindexmap(self): | ||
|
r2072 | """loads both the map and the index from the lazy parser""" | ||
if isinstance(self.index, lazyindex): | ||||
p = self.index.p | ||||
|
r2079 | p.loadindex() | ||
self.nodemap = p.map | ||||
|
r4920 | def _loadmap(self): | ||
|
r2079 | """loads the map from the lazy parser""" | ||
if isinstance(self.nodemap, lazymap): | ||||
self.nodemap.p.loadmap() | ||||
self.nodemap = self.nodemap.p.map | ||||
|
r0 | |||
|
r4980 | def tip(self): | ||
return self.node(len(self.index) - 2) | ||||
def count(self): | ||||
return len(self.index) - 1 | ||||
|
r4920 | |||
|
r1201 | def rev(self, node): | ||
try: | ||||
return self.nodemap[node] | ||||
except KeyError: | ||||
|
r6228 | raise LookupError(node, self.indexfile, _('no node')) | ||
|
r4980 | def node(self, rev): | ||
return self.index[rev][7] | ||||
|
r2642 | def linkrev(self, node): | ||
|
r4979 | return self.index[self.rev(node)][4] | ||
|
r2 | def parents(self, node): | ||
|
r4979 | d = self.index[self.rev(node)][5:7] | ||
|
r3508 | return (self.node(d[0]), self.node(d[1])) | ||
|
r2489 | def parentrevs(self, rev): | ||
|
r4979 | return self.index[rev][5:7] | ||
|
r2072 | def start(self, rev): | ||
|
r5006 | return int(self.index[rev][0] >> 16) | ||
|
r4980 | def end(self, rev): | ||
return self.start(rev) + self.length(rev) | ||||
def length(self, rev): | ||||
return self.index[rev][1] | ||||
def base(self, rev): | ||||
return self.index[rev][3] | ||||
|
r0 | |||
|
r2078 | def size(self, rev): | ||
"""return the length of the uncompressed text for a given revision""" | ||||
|
r4977 | l = self.index[rev][2] | ||
|
r2078 | if l >= 0: | ||
return l | ||||
t = self.revision(self.node(rev)) | ||||
return len(t) | ||||
# alternate implementation, The advantage to this code is it | ||||
# will be faster for a single revision. But, the results are not | ||||
# cached, so finding the size of every revision will be slower. | ||||
""" | ||||
if self.cache and self.cache[1] == rev: | ||||
return len(self.cache[2]) | ||||
base = self.base(rev) | ||||
if self.cache and self.cache[1] >= base and self.cache[1] < rev: | ||||
base = self.cache[1] | ||||
text = self.cache[2] | ||||
else: | ||||
text = self.revision(self.node(base)) | ||||
l = len(text) | ||||
for x in xrange(base + 1, rev + 1): | ||||
l = mdiff.patchedsize(l, self.chunk(x)) | ||||
return l | ||||
""" | ||||
|
r3630 | def reachable(self, node, stop=None): | ||
|
r3683 | """return a hash of all nodes ancestral to a given node, including | ||
the node itself, stopping when stop is matched""" | ||||
|
r1074 | reachable = {} | ||
|
r3630 | visit = [node] | ||
reachable[node] = 1 | ||||
|
r1074 | if stop: | ||
stopn = self.rev(stop) | ||||
else: | ||||
stopn = 0 | ||||
while visit: | ||||
n = visit.pop(0) | ||||
if n == stop: | ||||
continue | ||||
if n == nullid: | ||||
continue | ||||
for p in self.parents(n): | ||||
if self.rev(p) < stopn: | ||||
continue | ||||
if p not in reachable: | ||||
reachable[p] = 1 | ||||
visit.append(p) | ||||
return reachable | ||||
|
r1457 | def nodesbetween(self, roots=None, heads=None): | ||
"""Return a tuple containing three elements. Elements 1 and 2 contain | ||||
a final list bases and heads after all the unreachable ones have been | ||||
pruned. Element 0 contains a topologically sorted list of all | ||||
nodes that satisfy these constraints: | ||||
1. All nodes must be descended from a node in roots (the nodes on | ||||
roots are considered descended from themselves). | ||||
2. All nodes must also be ancestors of a node in heads (the nodes in | ||||
heads are considered to be their own ancestors). | ||||
If roots is unspecified, nullid is assumed as the only root. | ||||
If heads is unspecified, it is taken to be the output of the | ||||
heads method (i.e. a list of all nodes in the repository that | ||||
have no children).""" | ||||
|
r1463 | nonodes = ([], [], []) | ||
|
r1457 | if roots is not None: | ||
roots = list(roots) | ||||
|
r1463 | if not roots: | ||
return nonodes | ||||
|
r1457 | lowestrev = min([self.rev(n) for n in roots]) | ||
else: | ||||
roots = [nullid] # Everybody's a descendent of nullid | ||||
|
r3578 | lowestrev = nullrev | ||
if (lowestrev == nullrev) and (heads is None): | ||||
|
r1457 | # We want _all_ the nodes! | ||
return ([self.node(r) for r in xrange(0, self.count())], | ||||
[nullid], list(self.heads())) | ||||
if heads is None: | ||||
# All nodes are ancestors, so the latest ancestor is the last | ||||
# node. | ||||
highestrev = self.count() - 1 | ||||
# Set ancestors to None to signal that every node is an ancestor. | ||||
ancestors = None | ||||
# Set heads to an empty dictionary for later discovery of heads | ||||
heads = {} | ||||
else: | ||||
|
r1463 | heads = list(heads) | ||
if not heads: | ||||
return nonodes | ||||
|
r1457 | ancestors = {} | ||
# Turn heads into a dictionary so we can remove 'fake' heads. | ||||
# Also, later we will be using it to filter out the heads we can't | ||||
# find from roots. | ||||
heads = dict.fromkeys(heads, 0) | ||||
|
r3360 | # Start at the top and keep marking parents until we're done. | ||
nodestotag = heads.keys() | ||||
|
r1457 | # Remember where the top was so we can use it as a limit later. | ||
highestrev = max([self.rev(n) for n in nodestotag]) | ||||
while nodestotag: | ||||
# grab a node to tag | ||||
n = nodestotag.pop() | ||||
# Never tag nullid | ||||
if n == nullid: | ||||
continue | ||||
# A node's revision number represents its place in a | ||||
# topologically sorted list of nodes. | ||||
r = self.rev(n) | ||||
if r >= lowestrev: | ||||
if n not in ancestors: | ||||
# If we are possibly a descendent of one of the roots | ||||
# and we haven't already been marked as an ancestor | ||||
ancestors[n] = 1 # Mark as ancestor | ||||
# Add non-nullid parents to list of nodes to tag. | ||||
nodestotag.extend([p for p in self.parents(n) if | ||||
p != nullid]) | ||||
elif n in heads: # We've seen it before, is it a fake head? | ||||
# So it is, real heads should not be the ancestors of | ||||
# any other heads. | ||||
heads.pop(n) | ||||
|
r1459 | if not ancestors: | ||
|
r1463 | return nonodes | ||
|
r1457 | # Now that we have our set of ancestors, we want to remove any | ||
# roots that are not ancestors. | ||||
# If one of the roots was nullid, everything is included anyway. | ||||
|
r3578 | if lowestrev > nullrev: | ||
|
r1457 | # But, since we weren't, let's recompute the lowest rev to not | ||
# include roots that aren't ancestors. | ||||
# Filter out roots that aren't ancestors of heads | ||||
roots = [n for n in roots if n in ancestors] | ||||
# Recompute the lowest revision | ||||
if roots: | ||||
lowestrev = min([self.rev(n) for n in roots]) | ||||
else: | ||||
# No more roots? Return empty list | ||||
|
r1463 | return nonodes | ||
|
r1457 | else: | ||
# We are descending from nullid, and don't need to care about | ||||
# any other roots. | ||||
|
r3578 | lowestrev = nullrev | ||
|
r1457 | roots = [nullid] | ||
# Transform our roots list into a 'set' (i.e. a dictionary where the | ||||
# values don't matter. | ||||
descendents = dict.fromkeys(roots, 1) | ||||
# Also, keep the original roots so we can filter out roots that aren't | ||||
# 'real' roots (i.e. are descended from other roots). | ||||
roots = descendents.copy() | ||||
# Our topologically sorted list of output nodes. | ||||
orderedout = [] | ||||
# Don't start at nullid since we don't want nullid in our output list, | ||||
# and if nullid shows up in descedents, empty parents will look like | ||||
# they're descendents. | ||||
for r in xrange(max(lowestrev, 0), highestrev + 1): | ||||
n = self.node(r) | ||||
isdescendent = False | ||||
|
r3578 | if lowestrev == nullrev: # Everybody is a descendent of nullid | ||
|
r1457 | isdescendent = True | ||
elif n in descendents: | ||||
# n is already a descendent | ||||
isdescendent = True | ||||
# This check only needs to be done here because all the roots | ||||
# will start being marked is descendents before the loop. | ||||
if n in roots: | ||||
# If n was a root, check if it's a 'real' root. | ||||
p = tuple(self.parents(n)) | ||||
# If any of its parents are descendents, it's not a root. | ||||
if (p[0] in descendents) or (p[1] in descendents): | ||||
roots.pop(n) | ||||
else: | ||||
p = tuple(self.parents(n)) | ||||
# A node is a descendent if either of its parents are | ||||
# descendents. (We seeded the dependents list with the roots | ||||
# up there, remember?) | ||||
if (p[0] in descendents) or (p[1] in descendents): | ||||
descendents[n] = 1 | ||||
isdescendent = True | ||||
if isdescendent and ((ancestors is None) or (n in ancestors)): | ||||
# Only include nodes that are both descendents and ancestors. | ||||
orderedout.append(n) | ||||
if (ancestors is not None) and (n in heads): | ||||
# We're trying to figure out which heads are reachable | ||||
# from roots. | ||||
# Mark this head as having been reached | ||||
heads[n] = 1 | ||||
elif ancestors is None: | ||||
# Otherwise, we're trying to discover the heads. | ||||
# Assume this is a head because if it isn't, the next step | ||||
# will eventually remove it. | ||||
heads[n] = 1 | ||||
# But, obviously its parents aren't. | ||||
for p in self.parents(n): | ||||
heads.pop(p, None) | ||||
heads = [n for n in heads.iterkeys() if heads[n] != 0] | ||||
roots = roots.keys() | ||||
assert orderedout | ||||
assert roots | ||||
assert heads | ||||
return (orderedout, roots, heads) | ||||
|
r3923 | def heads(self, start=None, stop=None): | ||
|
r1550 | """return the list of all nodes that have no children | ||
|
r1551 | |||
if start is specified, only heads that are descendants of | ||||
start will be returned | ||||
|
r3923 | if stop is specified, it will consider all the revs from stop | ||
as if they had no children | ||||
|
r1551 | """ | ||
|
r4991 | if start is None and stop is None: | ||
count = self.count() | ||||
if not count: | ||||
return [nullid] | ||||
ishead = [1] * (count + 1) | ||||
index = self.index | ||||
for r in xrange(count): | ||||
e = index[r] | ||||
ishead[e[5]] = ishead[e[6]] = 0 | ||||
return [self.node(r) for r in xrange(count) if ishead[r]] | ||||
|
r1551 | if start is None: | ||
start = nullid | ||||
|
r3923 | if stop is None: | ||
stop = [] | ||||
stoprevs = dict.fromkeys([self.rev(n) for n in stop]) | ||||
|
r1550 | startrev = self.rev(start) | ||
|
r2490 | reachable = {startrev: 1} | ||
heads = {startrev: 1} | ||||
|
r1083 | |||
|
r2490 | parentrevs = self.parentrevs | ||
|
r1550 | for r in xrange(startrev + 1, self.count()): | ||
|
r2490 | for p in parentrevs(r): | ||
if p in reachable: | ||||
|
r3923 | if r not in stoprevs: | ||
reachable[r] = 1 | ||||
|
r2490 | heads[r] = 1 | ||
|
r3923 | if p in heads and p not in stoprevs: | ||
|
r2490 | del heads[p] | ||
|
r3923 | |||
|
r2490 | return [self.node(r) for r in heads] | ||
|
r370 | |||
def children(self, node): | ||||
|
r1083 | """find the children of a given node""" | ||
|
r370 | c = [] | ||
p = self.rev(node) | ||||
for r in range(p + 1, self.count()): | ||||
|
r4746 | prevs = [pr for pr in self.parentrevs(r) if pr != nullrev] | ||
if prevs: | ||||
for pr in prevs: | ||||
if pr == p: | ||||
c.append(self.node(r)) | ||||
elif p == nullrev: | ||||
c.append(self.node(r)) | ||||
|
r370 | return c | ||
|
r515 | |||
|
r3453 | def _match(self, id): | ||
|
r3210 | if isinstance(id, (long, int)): | ||
|
r3156 | # rev | ||
|
r2641 | return self.node(id) | ||
|
r3438 | if len(id) == 20: | ||
# possibly a binary node | ||||
# odds of a binary node being all hex in ASCII are 1 in 10**25 | ||||
try: | ||||
node = id | ||||
r = self.rev(node) # quick search the index | ||||
return node | ||||
|
r3930 | except LookupError: | ||
|
r3438 | pass # may be partial hex id | ||
|
r36 | try: | ||
|
r3156 | # str(rev) | ||
|
r36 | rev = int(id) | ||
|
r4980 | if str(rev) != id: | ||
raise ValueError | ||||
if rev < 0: | ||||
rev = self.count() + rev | ||||
if rev < 0 or rev >= self.count(): | ||||
raise ValueError | ||||
|
r36 | return self.node(rev) | ||
|
r469 | except (ValueError, OverflowError): | ||
|
r3156 | pass | ||
|
r3453 | if len(id) == 40: | ||
try: | ||||
|
r3438 | # a full hex nodeid? | ||
node = bin(id) | ||||
r = self.rev(node) | ||||
|
r3157 | return node | ||
|
r3453 | except TypeError: | ||
pass | ||||
def _partialmatch(self, id): | ||||
if len(id) < 40: | ||||
try: | ||||
|
r3438 | # hex(node)[:...] | ||
bin_id = bin(id[:len(id) & ~1]) # grab an even number of digits | ||||
node = None | ||||
for n in self.nodemap: | ||||
if n.startswith(bin_id) and hex(n).startswith(id): | ||||
if node is not None: | ||||
|
r6228 | raise LookupError(id, self.indexfile, | ||
_('ambiguous identifier')) | ||||
|
r3438 | node = n | ||
if node is not None: | ||||
return node | ||||
|
r3453 | except TypeError: | ||
pass | ||||
def lookup(self, id): | ||||
"""locate a node based on: | ||||
- revision number or str(revision number) | ||||
- nodeid or subset of hex nodeid | ||||
""" | ||||
n = self._match(id) | ||||
if n is not None: | ||||
return n | ||||
n = self._partialmatch(id) | ||||
if n: | ||||
return n | ||||
|
r515 | |||
|
r6228 | raise LookupError(id, self.indexfile, _('no match found')) | ||
|
r36 | |||
|
r2890 | def cmp(self, node, text): | ||
"""compare text with a given file revision""" | ||||
p1, p2 = self.parents(node) | ||||
return hash(text, p1, p2) != node | ||||
|
r4988 | def chunk(self, rev, df=None): | ||
|
r2072 | def loadcache(df): | ||
if not df: | ||||
|
r4988 | if self._inline: | ||
|
r2073 | df = self.opener(self.indexfile) | ||
else: | ||||
df = self.opener(self.datafile) | ||||
|
r1598 | df.seek(start) | ||
|
r4983 | self._chunkcache = (start, df.read(cache_length)) | ||
|
r1598 | |||
|
r5006 | start, length = self.start(rev), self.length(rev) | ||
if self._inline: | ||||
start += (rev + 1) * self._io.size | ||||
end = start + length | ||||
|
r1598 | |||
|
r5006 | offset = 0 | ||
if not self._chunkcache: | ||||
cache_length = max(65536, length) | ||||
loadcache(df) | ||||
|
r1598 | else: | ||
|
r5006 | cache_start = self._chunkcache[0] | ||
cache_length = len(self._chunkcache[1]) | ||||
cache_end = cache_start + cache_length | ||||
if start >= cache_start and end <= cache_end: | ||||
# it is cached | ||||
offset = start - cache_start | ||||
else: | ||||
cache_length = max(65536, length) | ||||
loadcache(df) | ||||
|
r1598 | |||
|
r4988 | # avoid copying large chunks | ||
c = self._chunkcache[1] | ||||
|
r5006 | if cache_length != length: | ||
|
r4988 | c = c[offset:offset + length] | ||
return decompress(c) | ||||
|
r1598 | |||
|
r119 | def delta(self, node): | ||
|
r1083 | """return or calculate a delta between a node and its predecessor""" | ||
|
r119 | r = self.rev(node) | ||
|
r1941 | return self.revdiff(r - 1, r) | ||
def revdiff(self, rev1, rev2): | ||||
"""return or calculate a delta between two revisions""" | ||||
|
r5005 | if rev1 + 1 == rev2 and self.base(rev1) == self.base(rev2): | ||
|
r1941 | return self.chunk(rev2) | ||
|
r5005 | |||
return mdiff.textdiff(self.revision(self.node(rev1)), | ||||
self.revision(self.node(rev2))) | ||||
|
r119 | |||
|
r0 | def revision(self, node): | ||
|
r1083 | """return an uncompressed revision of a given""" | ||
|
r4980 | if node == nullid: | ||
return "" | ||||
|
r4984 | if self._cache and self._cache[0] == node: | ||
|
r5450 | return str(self._cache[2]) | ||
|
r0 | |||
|
r1083 | # look up what we need to read | ||
|
r0 | text = None | ||
rev = self.rev(node) | ||||
|
r1598 | base = self.base(rev) | ||
|
r0 | |||
|
r5004 | # check rev flags | ||
if self.index[rev][0] & 0xFFFF: | ||||
|
r5312 | raise RevlogError(_('incompatible revision flag %x') % | ||
(self.index[rev][0] & 0xFFFF)) | ||||
|
r5004 | |||
|
r6144 | df = None | ||
|
r2072 | |||
|
r1083 | # do we have useful data cached? | ||
|
r4984 | if self._cache and self._cache[1] >= base and self._cache[1] < rev: | ||
base = self._cache[1] | ||||
|
r5450 | text = str(self._cache[2]) | ||
|
r4920 | self._loadindex(base, rev + 1) | ||
|
r6144 | if not self._inline and rev > base + 1: | ||
df = self.opener(self.datafile) | ||||
|
r1598 | else: | ||
|
r4920 | self._loadindex(base, rev + 1) | ||
|
r6144 | if not self._inline and rev > base: | ||
df = self.opener(self.datafile) | ||||
|
r2072 | text = self.chunk(base, df=df) | ||
|
r0 | |||
|
r4989 | bins = [self.chunk(r, df) for r in xrange(base + 1, rev + 1)] | ||
text = mdiff.patches(text, bins) | ||||
|
r1598 | p1, p2 = self.parents(node) | ||
|
r26 | if node != hash(text, p1, p2): | ||
|
r1402 | raise RevlogError(_("integrity check failed on %s:%d") | ||
|
r3680 | % (self.datafile, rev)) | ||
|
r0 | |||
|
r4984 | self._cache = (node, rev, text) | ||
|
r515 | return text | ||
|
r0 | |||
|
r2075 | def checkinlinesize(self, tr, fp=None): | ||
|
r4982 | if not self._inline: | ||
|
r2073 | return | ||
|
r2075 | if not fp: | ||
fp = self.opener(self.indexfile, 'r') | ||||
|
r2082 | fp.seek(0, 2) | ||
|
r2073 | size = fp.tell() | ||
if size < 131072: | ||||
return | ||||
|
r2084 | trinfo = tr.find(self.indexfile) | ||
if trinfo == None: | ||||
|
r3680 | raise RevlogError(_("%s not found in the transaction") | ||
% self.indexfile) | ||||
|
r2084 | |||
trindex = trinfo[2] | ||||
dataoff = self.start(trindex) | ||||
tr.add(self.datafile, dataoff) | ||||
|
r2073 | df = self.opener(self.datafile, 'w') | ||
|
r6261 | try: | ||
calc = self._io.size | ||||
for r in xrange(self.count()): | ||||
start = self.start(r) + (r + 1) * calc | ||||
length = self.length(r) | ||||
fp.seek(start) | ||||
d = fp.read(length) | ||||
df.write(d) | ||||
finally: | ||||
df.close() | ||||
|
r2073 | fp.close() | ||
|
r2076 | fp = self.opener(self.indexfile, 'w', atomictemp=True) | ||
|
r2073 | self.version &= ~(REVLOGNGINLINEDATA) | ||
|
r4982 | self._inline = False | ||
|
r4986 | for i in xrange(self.count()): | ||
|
r5338 | e = self._io.packentry(self.index[i], self.node, self.version, i) | ||
|
r2073 | fp.write(e) | ||
|
r2076 | # if we don't call rename, the temp file will never replace the | ||
# real index | ||||
fp.rename() | ||||
|
r2084 | |||
tr.replace(self.indexfile, trindex * calc) | ||||
|
r4983 | self._chunkcache = None | ||
|
r2073 | |||
|
r4981 | def addrevision(self, text, transaction, link, p1, p2, d=None): | ||
|
r1083 | """add a revision to the log | ||
text - the revision data to add | ||||
transaction - the transaction object used for rollback | ||||
link - the linkrev data to add | ||||
p1, p2 - the parent nodeids of the revision | ||||
d - an optional precomputed delta | ||||
""" | ||||
|
r4981 | dfh = None | ||
|
r4982 | if not self._inline: | ||
|
r3390 | dfh = self.opener(self.datafile, "a") | ||
ifh = self.opener(self.indexfile, "a+") | ||||
|
r6261 | try: | ||
return self._addrevision(text, transaction, link, p1, p2, d, ifh, dfh) | ||||
finally: | ||||
if dfh: | ||||
dfh.close() | ||||
ifh.close() | ||||
|
r3390 | |||
def _addrevision(self, text, transaction, link, p1, p2, d, ifh, dfh): | ||||
|
r0 | node = hash(text, p1, p2) | ||
|
r301 | if node in self.nodemap: | ||
return node | ||||
|
r4981 | curr = self.count() | ||
prev = curr - 1 | ||||
base = self.base(prev) | ||||
offset = self.end(prev) | ||||
|
r0 | |||
|
r4981 | if curr: | ||
|
r644 | if not d: | ||
|
r4981 | ptext = self.revision(self.node(prev)) | ||
|
r4989 | d = mdiff.textdiff(ptext, text) | ||
|
r98 | data = compress(d) | ||
|
r1533 | l = len(data[1]) + len(data[0]) | ||
|
r4981 | dist = l + offset - self.start(base) | ||
|
r0 | |||
# full versions are inserted when the needed deltas | ||||
# become comparable to the uncompressed text | ||||
|
r4981 | if not curr or dist > len(text) * 2: | ||
|
r0 | data = compress(text) | ||
|
r1533 | l = len(data[1]) + len(data[0]) | ||
|
r4981 | base = curr | ||
|
r0 | |||
|
r4977 | e = (offset_type(offset, 0), l, len(text), | ||
base, link, self.rev(p1), self.rev(p2), node) | ||||
|
r4979 | self.index.insert(-1, e) | ||
|
r4981 | self.nodemap[node] = curr | ||
|
r4977 | |||
|
r5338 | entry = self._io.packentry(e, self.node, self.version, curr) | ||
|
r4982 | if not self._inline: | ||
|
r2073 | transaction.add(self.datafile, offset) | ||
|
r4981 | transaction.add(self.indexfile, curr * len(entry)) | ||
|
r2073 | if data[0]: | ||
|
r3390 | dfh.write(data[0]) | ||
dfh.write(data[1]) | ||||
dfh.flush() | ||||
|
r4981 | ifh.write(entry) | ||
|
r2073 | else: | ||
|
r4996 | offset += curr * self._io.size | ||
|
r5324 | transaction.add(self.indexfile, offset, curr) | ||
|
r4981 | ifh.write(entry) | ||
|
r3390 | ifh.write(data[0]) | ||
ifh.write(data[1]) | ||||
self.checkinlinesize(transaction, ifh) | ||||
|
r2073 | |||
|
r4984 | self._cache = (node, curr, text) | ||
|
r0 | return node | ||
def ancestor(self, a, b): | ||||
|
r1083 | """calculate the least common ancestor of nodes a and b""" | ||
|
r2081 | |||
|
r3139 | def parents(rev): | ||
|
r3578 | return [p for p in self.parentrevs(rev) if p != nullrev] | ||
|
r515 | |||
|
r3139 | c = ancestor.ancestor(self.rev(a), self.rev(b), parents) | ||
if c is None: | ||||
return nullid | ||||
return self.node(c) | ||||
|
r0 | |||
|
r1598 | def group(self, nodelist, lookup, infocollect=None): | ||
|
r1083 | """calculate a delta group | ||
|
r46 | |||
|
r1083 | Given a list of changeset revs, return a set of deltas and | ||
metadata corresponding to nodes. the first delta is | ||||
parent(nodes[0]) -> nodes[0] the receiver is guaranteed to | ||||
have this parent as it has all history before these | ||||
changesets. parent is parent[0] | ||||
""" | ||||
|
r1458 | revs = [self.rev(n) for n in nodelist] | ||
|
r46 | |||
# if we don't have any revisions touched by these changesets, bail | ||||
|
r192 | if not revs: | ||
|
r1981 | yield changegroup.closechunk() | ||
|
r192 | return | ||
|
r46 | |||
# add the parent of the first rev | ||||
p = self.parents(self.node(revs[0]))[0] | ||||
revs.insert(0, self.rev(p)) | ||||
# build deltas | ||||
|
r71 | for d in xrange(0, len(revs) - 1): | ||
|
r46 | a, b = revs[d], revs[d + 1] | ||
|
r1598 | nb = self.node(b) | ||
|
r192 | |||
|
r1458 | if infocollect is not None: | ||
|
r1598 | infocollect(nb) | ||
|
r1458 | |||
|
r1598 | p = self.parents(nb) | ||
meta = nb + p[0] + p[1] + lookup(nb) | ||||
|
r5367 | if a == -1: | ||
d = self.revision(nb) | ||||
meta += mdiff.trivialdiffheader(len(d)) | ||||
else: | ||||
d = self.revdiff(a, b) | ||||
|
r5368 | yield changegroup.chunkheader(len(meta) + len(d)) | ||
yield meta | ||||
|
r5448 | if len(d) > 2**20: | ||
pos = 0 | ||||
while pos < len(d): | ||||
pos2 = pos + 2 ** 18 | ||||
yield d[pos:pos2] | ||||
pos = pos2 | ||||
else: | ||||
yield d | ||||
|
r46 | |||
|
r1981 | yield changegroup.closechunk() | ||
|
r192 | |||
|
r1062 | def addgroup(self, revs, linkmapper, transaction, unique=0): | ||
|
r1083 | """ | ||
add a delta group | ||||
|
r46 | |||
|
r1083 | given a set of deltas, add them to the revision log. the | ||
first delta is against its parent, which should be in our | ||||
log, the rest are against the previous delta. | ||||
""" | ||||
#track the base of the current delta log | ||||
|
r46 | r = self.count() | ||
t = r - 1 | ||||
|
r2002 | node = None | ||
|
r515 | |||
|
r3578 | base = prev = nullrev | ||
|
r2078 | start = end = textlen = 0 | ||
|
r46 | if r: | ||
end = self.end(t) | ||||
|
r2072 | ifh = self.opener(self.indexfile, "a+") | ||
|
r4996 | isize = r * self._io.size | ||
|
r4982 | if self._inline: | ||
|
r4996 | transaction.add(self.indexfile, end + isize, r) | ||
|
r2073 | dfh = None | ||
else: | ||||
|
r4996 | transaction.add(self.indexfile, isize, r) | ||
|
r2073 | transaction.add(self.datafile, end) | ||
dfh = self.opener(self.datafile, "a") | ||||
|
r46 | |||
|
r6261 | try: | ||
# loop through our set of deltas | ||||
chain = None | ||||
for chunk in revs: | ||||
node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80]) | ||||
link = linkmapper(cs) | ||||
if node in self.nodemap: | ||||
# this can happen if two branches make the same change | ||||
# if unique: | ||||
# raise RevlogError(_("already have %s") % hex(node[:4])) | ||||
chain = node | ||||
continue | ||||
delta = buffer(chunk, 80) | ||||
del chunk | ||||
|
r192 | |||
|
r6261 | for p in (p1, p2): | ||
if not p in self.nodemap: | ||||
raise LookupError(p, self.indexfile, _('unknown parent')) | ||||
|
r46 | |||
|
r6261 | if not chain: | ||
# retrieve the parent revision of the delta chain | ||||
chain = p1 | ||||
if not chain in self.nodemap: | ||||
raise LookupError(chain, self.indexfile, _('unknown base')) | ||||
|
r46 | |||
|
r6261 | # full versions are inserted when the needed deltas become | ||
# comparable to the uncompressed text or when the previous | ||||
# version is not the one we have a delta against. We use | ||||
# the size of the previous full rev as a proxy for the | ||||
# current size. | ||||
if chain == prev: | ||||
cdelta = compress(delta) | ||||
cdeltalen = len(cdelta[0]) + len(cdelta[1]) | ||||
textlen = mdiff.patchedsize(textlen, delta) | ||||
if chain != prev or (end - start + cdeltalen) > textlen * 2: | ||||
# flush our writes here so we can read it in revision | ||||
if dfh: | ||||
dfh.flush() | ||||
ifh.flush() | ||||
text = self.revision(chain) | ||||
if len(text) == 0: | ||||
# skip over trivial delta header | ||||
text = buffer(delta, 12) | ||||
else: | ||||
text = mdiff.patches(text, [delta]) | ||||
del delta | ||||
chk = self._addrevision(text, transaction, link, p1, p2, None, | ||||
ifh, dfh) | ||||
if not dfh and not self._inline: | ||||
# addrevision switched from inline to conventional | ||||
# reopen the index | ||||
|
r2073 | dfh = self.opener(self.datafile, "a") | ||
ifh = self.opener(self.indexfile, "a") | ||||
|
r6261 | if chk != node: | ||
raise RevlogError(_("consistency error adding group")) | ||||
textlen = len(text) | ||||
|
r2073 | else: | ||
|
r6261 | e = (offset_type(end, 0), cdeltalen, textlen, base, | ||
link, self.rev(p1), self.rev(p2), node) | ||||
self.index.insert(-1, e) | ||||
self.nodemap[node] = r | ||||
entry = self._io.packentry(e, self.node, self.version, r) | ||||
if self._inline: | ||||
ifh.write(entry) | ||||
ifh.write(cdelta[0]) | ||||
ifh.write(cdelta[1]) | ||||
self.checkinlinesize(transaction, ifh) | ||||
if not self._inline: | ||||
dfh = self.opener(self.datafile, "a") | ||||
ifh = self.opener(self.indexfile, "a") | ||||
else: | ||||
dfh.write(cdelta[0]) | ||||
dfh.write(cdelta[1]) | ||||
ifh.write(entry) | ||||
|
r46 | |||
|
r6261 | t, r, chain, prev = r, r + 1, node, node | ||
base = self.base(t) | ||||
start = self.start(base) | ||||
end = self.end(t) | ||||
finally: | ||||
if dfh: | ||||
dfh.close() | ||||
ifh.close() | ||||
|
r46 | |||
return node | ||||
|
r1493 | |||
|
r5910 | def strip(self, minlink): | ||
"""truncate the revlog on the first revision with a linkrev >= minlink | ||||
This function is called when we're stripping revision minlink and | ||||
its descendants from the repository. | ||||
We have to remove all revisions with linkrev >= minlink, because | ||||
the equivalent changelog revisions will be renumbered after the | ||||
strip. | ||||
So we truncate the revlog on the first of these revisions, and | ||||
trust that the caller has saved the revisions that shouldn't be | ||||
removed and that it'll readd them after this truncation. | ||||
""" | ||||
|
r5909 | if self.count() == 0: | ||
|
r1535 | return | ||
|
r2072 | if isinstance(self.index, lazyindex): | ||
|
r4920 | self._loadindexmap() | ||
|
r2072 | |||
|
r5909 | for rev in xrange(0, self.count()): | ||
if self.index[rev][4] >= minlink: | ||||
break | ||||
else: | ||||
return | ||||
|
r1535 | |||
# first truncate the files on disk | ||||
end = self.start(rev) | ||||
|
r4982 | if not self._inline: | ||
|
r2073 | df = self.opener(self.datafile, "a") | ||
df.truncate(end) | ||||
|
r4977 | end = rev * self._io.size | ||
|
r2073 | else: | ||
|
r4977 | end += rev * self._io.size | ||
|
r2072 | |||
indexf = self.opener(self.indexfile, "a") | ||||
indexf.truncate(end) | ||||
|
r1535 | |||
# then reset internal state in memory to forget those revisions | ||||
|
r4984 | self._cache = None | ||
|
r4983 | self._chunkcache = None | ||
|
r2072 | for x in xrange(rev, self.count()): | ||
del self.nodemap[self.node(x)] | ||||
|
r1535 | |||
|
r4979 | del self.index[rev:-1] | ||
|
r1535 | |||
|
r1493 | def checksize(self): | ||
expected = 0 | ||||
if self.count(): | ||||
|
r5312 | expected = max(0, self.end(self.count() - 1)) | ||
|
r1667 | |||
|
r1494 | try: | ||
f = self.opener(self.datafile) | ||||
f.seek(0, 2) | ||||
actual = f.tell() | ||||
|
r1667 | dd = actual - expected | ||
|
r1494 | except IOError, inst: | ||
|
r1667 | if inst.errno != errno.ENOENT: | ||
raise | ||||
dd = 0 | ||||
try: | ||||
f = self.opener(self.indexfile) | ||||
f.seek(0, 2) | ||||
actual = f.tell() | ||||
|
r4977 | s = self._io.size | ||
|
r5312 | i = max(0, actual / s) | ||
|
r1667 | di = actual - (i * s) | ||
|
r4982 | if self._inline: | ||
|
r2073 | databytes = 0 | ||
for r in xrange(self.count()): | ||||
|
r5312 | databytes += max(0, self.length(r)) | ||
|
r2073 | dd = 0 | ||
di = actual - self.count() * s - databytes | ||||
|
r1667 | except IOError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
raise | ||||
di = 0 | ||||
return (dd, di) | ||||