revlog.py
1297 lines
| 44.2 KiB
| text/x-python
|
PythonLexer
/ mercurial / revlog.py
mpm@selenic.com
|
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 | ||||
Thomas Arendsen Hein
|
r4635 | Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
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. | ||||
""" | ||||
mpm@selenic.com
|
r0 | |||
mpm@selenic.com
|
r1089 | from node import * | ||
Matt Mackall
|
r3891 | from i18n import _ | ||
Matt Mackall
|
r3877 | import binascii, changegroup, errno, ancestor, mdiff, os | ||
import sha, struct, util, zlib | ||||
mpm@selenic.com
|
r36 | |||
mason@suse.com
|
r2072 | # revlog version strings | ||
REVLOGV0 = 0 | ||||
REVLOGNG = 1 | ||||
mason@suse.com
|
r2073 | # revlog flags | ||
REVLOGNGINLINEDATA = (1 << 16) | ||||
mason@suse.com
|
r2222 | REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA | ||
REVLOG_DEFAULT_FORMAT = REVLOGNG | ||||
REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS | ||||
mason@suse.com
|
r2073 | |||
mpm@selenic.com
|
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() | ||||
s = sha.new(l[0]) | ||||
s.update(l[1]) | ||||
s.update(text) | ||||
return s.digest() | ||||
mpm@selenic.com
|
r0 | def compress(text): | ||
mpm@selenic.com
|
r1083 | """ generate a possibly-compressed representation of text """ | ||
mason@suse.com
|
r1533 | if not text: return ("", text) | ||
mpm@selenic.com
|
r112 | if len(text) < 44: | ||
mason@suse.com
|
r1533 | if text[0] == '\0': return ("", text) | ||
return ('u', text) | ||||
mpm@selenic.com
|
r112 | bin = zlib.compress(text) | ||
if len(bin) > len(text): | ||||
mason@suse.com
|
r1533 | if text[0] == '\0': return ("", text) | ||
return ('u', text) | ||||
return ("", bin) | ||||
mpm@selenic.com
|
r0 | |||
def decompress(bin): | ||||
mpm@selenic.com
|
r1083 | """ decompress the given input """ | ||
mpm@selenic.com
|
r112 | if not bin: return bin | ||
t = bin[0] | ||||
if t == '\0': return bin | ||||
if t == 'x': return zlib.decompress(bin) | ||||
if t == 'u': return bin[1:] | ||||
Thomas Arendsen Hein
|
r1853 | raise RevlogError(_("unknown compression type %r") % t) | ||
mpm@selenic.com
|
r0 | |||
mason@suse.com
|
r2072 | indexformatv0 = ">4l20s20s20s" | ||
mason@suse.com
|
r2079 | v0shaoffset = 56 | ||
mason@suse.com
|
r2072 | # 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" | ||||
mason@suse.com
|
r2079 | ngshaoffset = 32 | ||
Thomas Arendsen Hein
|
r3755 | versionformat = ">I" | ||
mpm@selenic.com
|
r0 | |||
Eric Hopper
|
r1559 | class lazyparser(object): | ||
mpm@selenic.com
|
r1083 | """ | ||
this class avoids the need to parse the entirety of large indices | ||||
""" | ||||
Vadim Gelfer
|
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. | ||||
safe_to_use = os.name != 'nt' or (not util.is_win_9x() and | ||||
hasattr(util, 'win32api')) | ||||
mason@suse.com
|
r2079 | def __init__(self, dataf, size, indexformat, shaoffset): | ||
self.dataf = dataf | ||||
self.format = indexformat | ||||
mpm@selenic.com
|
r76 | self.s = struct.calcsize(indexformat) | ||
mason@suse.com
|
r2072 | self.indexformat = indexformat | ||
mason@suse.com
|
r2079 | self.datasize = size | ||
self.l = size/self.s | ||||
mpm@selenic.com
|
r76 | self.index = [None] * self.l | ||
Thomas Arendsen Hein
|
r3578 | self.map = {nullid: nullrev} | ||
mason@suse.com
|
r2079 | self.allmap = 0 | ||
mpm@selenic.com
|
r323 | self.all = 0 | ||
mason@suse.com
|
r2079 | self.mapfind_count = 0 | ||
self.shaoffset = shaoffset | ||||
mpm@selenic.com
|
r76 | |||
mason@suse.com
|
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. | ||||
""" | ||||
if self.allmap: return | ||||
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): | ||||
n = data[off + self.shaoffset:off + self.shaoffset + 20] | ||||
self.map[n] = count | ||||
count += 1 | ||||
if count >= self.l: | ||||
break | ||||
off += self.s | ||||
cur += blocksize | ||||
def loadblock(self, blockstart, blocksize, data=None): | ||||
mpm@selenic.com
|
r323 | if self.all: return | ||
mason@suse.com
|
r2079 | if data is None: | ||
self.dataf.seek(blockstart) | ||||
Alexis S. L. Carvalho
|
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. | ||||
Alexis S. L. Carvalho
|
r3089 | blocksize = max(self.datasize - blockstart, 0) | ||
mason@suse.com
|
r2079 | data = self.dataf.read(blocksize) | ||
lend = len(data) / self.s | ||||
i = blockstart / self.s | ||||
off = 0 | ||||
Brendan Cully
|
r4061 | # lazyindex supports __delitem__ | ||
if lend > len(self.index) - i: | ||||
lend = len(self.index) - i | ||||
mason@suse.com
|
r2079 | for x in xrange(lend): | ||
if self.index[i + x] == None: | ||||
b = data[off : off + self.s] | ||||
mason@suse.com
|
r2080 | self.index[i + x] = b | ||
n = b[self.shaoffset:self.shaoffset + 20] | ||||
self.map[n] = i + x | ||||
mason@suse.com
|
r2079 | off += self.s | ||
def findnode(self, node): | ||||
"""search backwards through the index file for a specific node""" | ||||
if self.allmap: return None | ||||
# 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: | ||||
# we're searching backwards, so weh have to make sure | ||||
# we don't find a changeset where this node is a parent | ||||
off = data.rfind(node, 0, findend) | ||||
findend = off | ||||
if off >= 0: | ||||
i = off / self.s | ||||
off = i * self.s | ||||
n = data[off + self.shaoffset:off + self.shaoffset + 20] | ||||
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): | ||||
if self.all: return | ||||
all = False | ||||
if i == None: | ||||
blockstart = 0 | ||||
blocksize = (512 / self.s) * self.s | ||||
end = self.datasize | ||||
all = True | ||||
mpm@selenic.com
|
r323 | else: | ||
mason@suse.com
|
r2079 | if end: | ||
blockstart = i * self.s | ||||
end = end * self.s | ||||
blocksize = end - blockstart | ||||
else: | ||||
Alexis S. L. Carvalho
|
r4314 | blockstart = (i & ~63) * self.s | ||
mason@suse.com
|
r2079 | blocksize = self.s * 64 | ||
end = blockstart + blocksize | ||||
while blockstart < end: | ||||
self.loadblock(blockstart, blocksize) | ||||
blockstart += blocksize | ||||
if all: self.all = True | ||||
mpm@selenic.com
|
r515 | |||
Eric Hopper
|
r1559 | class lazyindex(object): | ||
mpm@selenic.com
|
r1083 | """a lazy version of the index array""" | ||
mpm@selenic.com
|
r76 | def __init__(self, parser): | ||
self.p = parser | ||||
def __len__(self): | ||||
return len(self.p.index) | ||||
mpm@selenic.com
|
r115 | def load(self, pos): | ||
Eric Hopper
|
r1403 | if pos < 0: | ||
pos += len(self.p.index) | ||||
mason@suse.com
|
r2079 | self.p.loadindex(pos) | ||
mpm@selenic.com
|
r115 | return self.p.index[pos] | ||
mpm@selenic.com
|
r76 | def __getitem__(self, pos): | ||
mason@suse.com
|
r2080 | ret = self.p.index[pos] or self.load(pos) | ||
if isinstance(ret, str): | ||||
ret = struct.unpack(self.p.indexformat, ret) | ||||
return ret | ||||
mason@suse.com
|
r2072 | def __setitem__(self, pos, item): | ||
self.p.index[pos] = item | ||||
mason@suse.com
|
r1535 | def __delitem__(self, pos): | ||
del self.p.index[pos] | ||||
mpm@selenic.com
|
r76 | def append(self, e): | ||
self.p.index.append(e) | ||||
mpm@selenic.com
|
r515 | |||
Eric Hopper
|
r1559 | class lazymap(object): | ||
mpm@selenic.com
|
r1083 | """a lazy version of the node map""" | ||
mpm@selenic.com
|
r76 | def __init__(self, parser): | ||
self.p = parser | ||||
def load(self, key): | ||||
mason@suse.com
|
r2079 | n = self.p.findnode(key) | ||
if n == None: | ||||
mpm@selenic.com
|
r1214 | raise KeyError(key) | ||
mpm@selenic.com
|
r76 | def __contains__(self, key): | ||
mason@suse.com
|
r2079 | if key in self.p.map: | ||
return True | ||||
self.p.loadmap() | ||||
mpm@selenic.com
|
r323 | return key in self.p.map | ||
mpm@selenic.com
|
r97 | def __iter__(self): | ||
mpm@selenic.com
|
r469 | yield nullid | ||
mpm@selenic.com
|
r97 | for i in xrange(self.p.l): | ||
mason@suse.com
|
r2080 | ret = self.p.index[i] | ||
if not ret: | ||||
mason@suse.com
|
r2079 | self.p.loadindex(i) | ||
mason@suse.com
|
r2080 | ret = self.p.index[i] | ||
if isinstance(ret, str): | ||||
ret = struct.unpack(self.p.indexformat, ret) | ||||
yield ret[-1] | ||||
mpm@selenic.com
|
r76 | def __getitem__(self, key): | ||
try: | ||||
return self.p.map[key] | ||||
except KeyError: | ||||
mpm@selenic.com
|
r86 | try: | ||
self.load(key) | ||||
return self.p.map[key] | ||||
except KeyError: | ||||
raise KeyError("node " + hex(key)) | ||||
mpm@selenic.com
|
r76 | def __setitem__(self, key, val): | ||
self.p.map[key] = val | ||||
mason@suse.com
|
r1535 | def __delitem__(self, key): | ||
del self.p.map[key] | ||||
mpm@selenic.com
|
r76 | |||
Bart Trojanowski
|
r1073 | class RevlogError(Exception): pass | ||
Brendan Cully
|
r3930 | class LookupError(RevlogError): pass | ||
Bart Trojanowski
|
r1073 | |||
Eric Hopper
|
r1559 | class revlog(object): | ||
mpm@selenic.com
|
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. | ||||
""" | ||||
Matt Mackall
|
r4258 | def __init__(self, opener, indexfile): | ||
mpm@selenic.com
|
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. | ||||
""" | ||||
mpm@selenic.com
|
r0 | self.indexfile = indexfile | ||
Matt Mackall
|
r4257 | self.datafile = indexfile[:-2] + ".d" | ||
mpm@selenic.com
|
r0 | self.opener = opener | ||
Benoit Boissinot
|
r1784 | |||
self.indexstat = None | ||||
mpm@selenic.com
|
r0 | self.cache = None | ||
Benoit Boissinot
|
r1598 | self.chunkcache = None | ||
Thomas Arendsen Hein
|
r4277 | self.defversion = REVLOG_DEFAULT_VERSION | ||
Matt Mackall
|
r4258 | if hasattr(opener, "defversion"): | ||
self.defversion = opener.defversion | ||||
Matt Mackall
|
r4302 | if self.defversion & REVLOGNG: | ||
self.defversion |= REVLOGNGINLINEDATA | ||||
Benoit Boissinot
|
r1784 | self.load() | ||
mpm@selenic.com
|
r116 | |||
Benoit Boissinot
|
r1784 | def load(self): | ||
mason@suse.com
|
r2072 | v = self.defversion | ||
mpm@selenic.com
|
r0 | try: | ||
Benoit Boissinot
|
r1784 | f = self.opener(self.indexfile) | ||
mason@suse.com
|
r2079 | i = f.read(4) | ||
f.seek(0) | ||||
Bryan O'Sullivan
|
r1322 | except IOError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
raise | ||||
mpm@selenic.com
|
r76 | i = "" | ||
Benoit Boissinot
|
r1784 | else: | ||
try: | ||||
Vadim Gelfer
|
r2176 | st = util.fstat(f) | ||
Benoit Boissinot
|
r1784 | except AttributeError, inst: | ||
st = None | ||||
else: | ||||
oldst = self.indexstat | ||||
if (oldst and st.st_dev == oldst.st_dev | ||||
and st.st_ino == oldst.st_ino | ||||
and st.st_mtime == oldst.st_mtime | ||||
Alexis S. L. Carvalho
|
r4483 | and st.st_ctime == oldst.st_ctime | ||
and st.st_size == oldst.st_size): | ||||
Benoit Boissinot
|
r1784 | return | ||
mason@suse.com
|
r2072 | self.indexstat = st | ||
Alexis S. L. Carvalho
|
r2138 | if len(i) > 0: | ||
v = struct.unpack(versionformat, i)[0] | ||||
mason@suse.com
|
r2073 | flags = v & ~0xFFFF | ||
fmt = v & 0xFFFF | ||||
Thomas Arendsen Hein
|
r2142 | if fmt == REVLOGV0: | ||
mason@suse.com
|
r2073 | if flags: | ||
Thomas Arendsen Hein
|
r3754 | raise RevlogError(_("index %s unknown flags %#04x for format v0") | ||
% (self.indexfile, flags >> 16)) | ||||
mason@suse.com
|
r2073 | elif fmt == REVLOGNG: | ||
if flags & ~REVLOGNGINLINEDATA: | ||||
Thomas Arendsen Hein
|
r3754 | raise RevlogError(_("index %s unknown flags %#04x for revlogng") | ||
% (self.indexfile, flags >> 16)) | ||||
mason@suse.com
|
r2073 | else: | ||
Matt Mackall
|
r3743 | raise RevlogError(_("index %s unknown format %d") | ||
Thomas Arendsen Hein
|
r3680 | % (self.indexfile, fmt)) | ||
mason@suse.com
|
r2072 | self.version = v | ||
Thomas Arendsen Hein
|
r2142 | if v == REVLOGV0: | ||
mason@suse.com
|
r2072 | self.indexformat = indexformatv0 | ||
mason@suse.com
|
r2079 | shaoffset = v0shaoffset | ||
mason@suse.com
|
r2072 | else: | ||
self.indexformat = indexformatng | ||||
mason@suse.com
|
r2079 | shaoffset = ngshaoffset | ||
mpm@selenic.com
|
r116 | |||
mason@suse.com
|
r2072 | if i: | ||
Vadim Gelfer
|
r2250 | if (lazyparser.safe_to_use and not self.inlinedata() and | ||
st and st.st_size > 10000): | ||||
mason@suse.com
|
r2072 | # big index, let's parse it on demand | ||
mason@suse.com
|
r2079 | parser = lazyparser(f, st.st_size, self.indexformat, shaoffset) | ||
mason@suse.com
|
r2072 | self.index = lazyindex(parser) | ||
self.nodemap = lazymap(parser) | ||||
else: | ||||
mason@suse.com
|
r2255 | self.parseindex(f, st) | ||
Thomas Arendsen Hein
|
r2142 | if self.version != REVLOGV0: | ||
mason@suse.com
|
r2072 | e = list(self.index[0]) | ||
type = self.ngtype(e[0]) | ||||
e[0] = self.offset_type(0, type) | ||||
self.index[0] = e | ||||
mpm@selenic.com
|
r116 | else: | ||
Thomas Arendsen Hein
|
r3578 | self.nodemap = {nullid: nullrev} | ||
mason@suse.com
|
r2072 | self.index = [] | ||
mason@suse.com
|
r2255 | def parseindex(self, fp, st): | ||
mason@suse.com
|
r2072 | s = struct.calcsize(self.indexformat) | ||
self.index = [] | ||||
Thomas Arendsen Hein
|
r3578 | self.nodemap = {nullid: nullrev} | ||
mason@suse.com
|
r2073 | inline = self.inlinedata() | ||
mason@suse.com
|
r2072 | n = 0 | ||
mason@suse.com
|
r2255 | leftover = None | ||
while True: | ||||
if st: | ||||
data = fp.read(65536) | ||||
else: | ||||
# hack for httprangereader, it doesn't do partial reads well | ||||
data = fp.read() | ||||
if not data: | ||||
break | ||||
if n == 0 and self.inlinedata(): | ||||
# cache the first chunk | ||||
self.chunkcache = (0, data) | ||||
Alexis S. L. Carvalho
|
r2289 | if leftover: | ||
data = leftover + data | ||||
leftover = None | ||||
mason@suse.com
|
r2255 | off = 0 | ||
l = len(data) | ||||
while off < l: | ||||
if l - off < s: | ||||
leftover = data[off:] | ||||
break | ||||
Alexis S. L. Carvalho
|
r2289 | cur = data[off:off + s] | ||
off += s | ||||
mason@suse.com
|
r2255 | e = struct.unpack(self.indexformat, cur) | ||
self.index.append(e) | ||||
self.nodemap[e[-1]] = n | ||||
n += 1 | ||||
if inline: | ||||
Matt Mackall
|
r4215 | if e[1] < 0: | ||
break | ||||
mason@suse.com
|
r2255 | off += e[1] | ||
if off > l: | ||||
# some things don't seek well, just read it | ||||
fp.read(off - l) | ||||
Matt Mackall
|
r4215 | break | ||
mason@suse.com
|
r2255 | if not st: | ||
break | ||||
Vadim Gelfer
|
r2600 | |||
mpm@selenic.com
|
r116 | |||
mason@suse.com
|
r2072 | def ngoffset(self, q): | ||
if q & 0xFFFF: | ||||
raise RevlogError(_('%s: incompatible revision flag %x') % | ||||
Thomas Arendsen Hein
|
r2141 | (self.indexfile, q)) | ||
mason@suse.com
|
r2072 | return long(q >> 16) | ||
def ngtype(self, q): | ||||
return int(q & 0xFFFF) | ||||
mpm@selenic.com
|
r116 | |||
mason@suse.com
|
r2072 | def offset_type(self, offset, type): | ||
return long(long(offset) << 16 | type) | ||||
mason@suse.com
|
r2079 | def loadindex(self, start, end): | ||
"""load a block of indexes all at once from the lazy parser""" | ||||
if isinstance(self.index, lazyindex): | ||||
self.index.p.loadindex(start, end) | ||||
mason@suse.com
|
r2072 | def loadindexmap(self): | ||
"""loads both the map and the index from the lazy parser""" | ||||
if isinstance(self.index, lazyindex): | ||||
p = self.index.p | ||||
mason@suse.com
|
r2079 | p.loadindex() | ||
self.nodemap = p.map | ||||
def loadmap(self): | ||||
"""loads the map from the lazy parser""" | ||||
if isinstance(self.nodemap, lazymap): | ||||
self.nodemap.p.loadmap() | ||||
self.nodemap = self.nodemap.p.map | ||||
mpm@selenic.com
|
r0 | |||
mason@suse.com
|
r2073 | def inlinedata(self): return self.version & REVLOGNGINLINEDATA | ||
mpm@selenic.com
|
r0 | def tip(self): return self.node(len(self.index) - 1) | ||
def count(self): return len(self.index) | ||||
mason@suse.com
|
r2072 | def node(self, rev): | ||
Benoit Boissinot
|
r3585 | return rev == nullrev and nullid or self.index[rev][-1] | ||
Bryan O'Sullivan
|
r1201 | def rev(self, node): | ||
try: | ||||
return self.nodemap[node] | ||||
except KeyError: | ||||
Brendan Cully
|
r3930 | raise LookupError(_('%s: no node %s') % (self.indexfile, hex(node))) | ||
Benoit Boissinot
|
r2642 | def linkrev(self, node): | ||
Thomas Arendsen Hein
|
r3578 | return (node == nullid) and nullrev or self.index[self.rev(node)][-4] | ||
mpm@selenic.com
|
r2 | def parents(self, node): | ||
if node == nullid: return (nullid, nullid) | ||||
mason@suse.com
|
r2072 | r = self.rev(node) | ||
d = self.index[r][-3:-1] | ||||
Thomas Arendsen Hein
|
r2142 | if self.version == REVLOGV0: | ||
mason@suse.com
|
r2072 | return d | ||
Alexis S. L. Carvalho
|
r3508 | return (self.node(d[0]), self.node(d[1])) | ||
Alexis S. L. Carvalho
|
r2489 | def parentrevs(self, rev): | ||
Thomas Arendsen Hein
|
r3578 | if rev == nullrev: | ||
return (nullrev, nullrev) | ||||
Alexis S. L. Carvalho
|
r2489 | d = self.index[rev][-3:-1] | ||
if self.version == REVLOGV0: | ||||
Alexis S. L. Carvalho
|
r3508 | return (self.rev(d[0]), self.rev(d[1])) | ||
Alexis S. L. Carvalho
|
r2489 | return d | ||
mason@suse.com
|
r2072 | def start(self, rev): | ||
Benoit Boissinot
|
r3585 | if rev == nullrev: | ||
Benoit Boissinot
|
r3584 | return 0 | ||
Thomas Arendsen Hein
|
r2142 | if self.version != REVLOGV0: | ||
mason@suse.com
|
r2072 | return self.ngoffset(self.index[rev][0]) | ||
return self.index[rev][0] | ||||
mason@suse.com
|
r2078 | |||
mason@suse.com
|
r2072 | def end(self, rev): return self.start(rev) + self.length(rev) | ||
mpm@selenic.com
|
r0 | |||
mason@suse.com
|
r2078 | def size(self, rev): | ||
"""return the length of the uncompressed text for a given revision""" | ||||
Benoit Boissinot
|
r3582 | if rev == nullrev: | ||
return 0 | ||||
mason@suse.com
|
r2078 | l = -1 | ||
Thomas Arendsen Hein
|
r2142 | if self.version != REVLOGV0: | ||
mason@suse.com
|
r2078 | l = self.index[rev][2] | ||
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 | ||||
""" | ||||
Benoit Boissinot
|
r1941 | def length(self, rev): | ||
Benoit Boissinot
|
r3585 | if rev == nullrev: | ||
Benoit Boissinot
|
r1941 | return 0 | ||
else: | ||||
return self.index[rev][1] | ||||
Benoit Boissinot
|
r3585 | def base(self, rev): | ||
if (rev == nullrev): | ||||
return nullrev | ||||
else: | ||||
return self.index[rev][-5] | ||||
mpm@selenic.com
|
r0 | |||
Matt Mackall
|
r3630 | def reachable(self, node, stop=None): | ||
Matt Mackall
|
r3683 | """return a hash of all nodes ancestral to a given node, including | ||
the node itself, stopping when stop is matched""" | ||||
mason@suse.com
|
r1074 | reachable = {} | ||
Matt Mackall
|
r3630 | visit = [node] | ||
reachable[node] = 1 | ||||
mason@suse.com
|
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 | ||||
Eric Hopper
|
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).""" | ||||
Eric Hopper
|
r1463 | nonodes = ([], [], []) | ||
Eric Hopper
|
r1457 | if roots is not None: | ||
roots = list(roots) | ||||
Eric Hopper
|
r1463 | if not roots: | ||
return nonodes | ||||
Eric Hopper
|
r1457 | lowestrev = min([self.rev(n) for n in roots]) | ||
else: | ||||
roots = [nullid] # Everybody's a descendent of nullid | ||||
Thomas Arendsen Hein
|
r3578 | lowestrev = nullrev | ||
if (lowestrev == nullrev) and (heads is None): | ||||
Eric Hopper
|
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: | ||||
Eric Hopper
|
r1463 | heads = list(heads) | ||
if not heads: | ||||
return nonodes | ||||
Eric Hopper
|
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) | ||||
Benoit Boissinot
|
r3360 | # Start at the top and keep marking parents until we're done. | ||
nodestotag = heads.keys() | ||||
Eric Hopper
|
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) | ||||
Eric Hopper
|
r1459 | if not ancestors: | ||
Eric Hopper
|
r1463 | return nonodes | ||
Eric Hopper
|
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. | ||||
Thomas Arendsen Hein
|
r3578 | if lowestrev > nullrev: | ||
Eric Hopper
|
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 | ||||
Eric Hopper
|
r1463 | return nonodes | ||
Eric Hopper
|
r1457 | else: | ||
# We are descending from nullid, and don't need to care about | ||||
# any other roots. | ||||
Thomas Arendsen Hein
|
r3578 | lowestrev = nullrev | ||
Eric Hopper
|
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 | ||||
Thomas Arendsen Hein
|
r3578 | if lowestrev == nullrev: # Everybody is a descendent of nullid | ||
Eric Hopper
|
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) | ||||
Benoit Boissinot
|
r3923 | def heads(self, start=None, stop=None): | ||
Benoit Boissinot
|
r1550 | """return the list of all nodes that have no children | ||
Thomas Arendsen Hein
|
r1551 | |||
if start is specified, only heads that are descendants of | ||||
start will be returned | ||||
Benoit Boissinot
|
r3923 | if stop is specified, it will consider all the revs from stop | ||
as if they had no children | ||||
Thomas Arendsen Hein
|
r1551 | """ | ||
if start is None: | ||||
start = nullid | ||||
Benoit Boissinot
|
r3923 | if stop is None: | ||
stop = [] | ||||
stoprevs = dict.fromkeys([self.rev(n) for n in stop]) | ||||
Benoit Boissinot
|
r1550 | startrev = self.rev(start) | ||
Alexis S. L. Carvalho
|
r2490 | reachable = {startrev: 1} | ||
heads = {startrev: 1} | ||||
mpm@selenic.com
|
r1083 | |||
Alexis S. L. Carvalho
|
r2490 | parentrevs = self.parentrevs | ||
Benoit Boissinot
|
r1550 | for r in xrange(startrev + 1, self.count()): | ||
Alexis S. L. Carvalho
|
r2490 | for p in parentrevs(r): | ||
if p in reachable: | ||||
Benoit Boissinot
|
r3923 | if r not in stoprevs: | ||
reachable[r] = 1 | ||||
Alexis S. L. Carvalho
|
r2490 | heads[r] = 1 | ||
Benoit Boissinot
|
r3923 | if p in heads and p not in stoprevs: | ||
Alexis S. L. Carvalho
|
r2490 | del heads[p] | ||
Benoit Boissinot
|
r3923 | |||
Alexis S. L. Carvalho
|
r2490 | return [self.node(r) for r in heads] | ||
mpm@selenic.com
|
r370 | |||
def children(self, node): | ||||
mpm@selenic.com
|
r1083 | """find the children of a given node""" | ||
mpm@selenic.com
|
r370 | c = [] | ||
p = self.rev(node) | ||||
for r in range(p + 1, self.count()): | ||||
Benoit Boissinot
|
r3474 | for pr in self.parentrevs(r): | ||
Benoit Boissinot
|
r3470 | if pr == p: | ||
c.append(self.node(r)) | ||||
mpm@selenic.com
|
r370 | return c | ||
mpm@selenic.com
|
r515 | |||
Matt Mackall
|
r3453 | def _match(self, id): | ||
Benoit Boissinot
|
r3210 | if isinstance(id, (long, int)): | ||
Benoit Boissinot
|
r3156 | # rev | ||
Benoit Boissinot
|
r2641 | return self.node(id) | ||
Matt Mackall
|
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 | ||||
Brendan Cully
|
r3930 | except LookupError: | ||
Matt Mackall
|
r3438 | pass # may be partial hex id | ||
mpm@selenic.com
|
r36 | try: | ||
Benoit Boissinot
|
r3156 | # str(rev) | ||
mpm@selenic.com
|
r36 | rev = int(id) | ||
mpm@selenic.com
|
r469 | if str(rev) != id: raise ValueError | ||
if rev < 0: rev = self.count() + rev | ||||
Thomas Arendsen Hein
|
r476 | if rev < 0 or rev >= self.count(): raise ValueError | ||
mpm@selenic.com
|
r36 | return self.node(rev) | ||
mpm@selenic.com
|
r469 | except (ValueError, OverflowError): | ||
Benoit Boissinot
|
r3156 | pass | ||
Matt Mackall
|
r3453 | if len(id) == 40: | ||
try: | ||||
Matt Mackall
|
r3438 | # a full hex nodeid? | ||
node = bin(id) | ||||
r = self.rev(node) | ||||
Benoit Boissinot
|
r3157 | return node | ||
Matt Mackall
|
r3453 | except TypeError: | ||
pass | ||||
def _partialmatch(self, id): | ||||
if len(id) < 40: | ||||
try: | ||||
Matt Mackall
|
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: | ||||
Brendan Cully
|
r3930 | raise LookupError(_("Ambiguous identifier")) | ||
Matt Mackall
|
r3438 | node = n | ||
if node is not None: | ||||
return node | ||||
Matt Mackall
|
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 | ||||
mpm@selenic.com
|
r515 | |||
Brendan Cully
|
r3930 | raise LookupError(_("No match found")) | ||
mpm@selenic.com
|
r36 | |||
Matt Mackall
|
r2890 | def cmp(self, node, text): | ||
"""compare text with a given file revision""" | ||||
p1, p2 = self.parents(node) | ||||
return hash(text, p1, p2) != node | ||||
def makenode(self, node, text): | ||||
"""calculate a file nodeid for text, descended or possibly | ||||
unchanged from node""" | ||||
if self.cmp(node, text): | ||||
return hash(text, node, nullid) | ||||
return node | ||||
mpm@selenic.com
|
r0 | def diff(self, a, b): | ||
mpm@selenic.com
|
r1083 | """return a delta between two revisions""" | ||
mpm@selenic.com
|
r0 | return mdiff.textdiff(a, b) | ||
mpm@selenic.com
|
r73 | def patches(self, t, pl): | ||
mpm@selenic.com
|
r1083 | """apply a list of patches to a string""" | ||
mpm@selenic.com
|
r73 | return mdiff.patches(t, pl) | ||
mason@suse.com
|
r2072 | def chunk(self, rev, df=None, cachelen=4096): | ||
Benoit Boissinot
|
r1598 | start, length = self.start(rev), self.length(rev) | ||
mason@suse.com
|
r2073 | inline = self.inlinedata() | ||
if inline: | ||||
start += (rev + 1) * struct.calcsize(self.indexformat) | ||||
Benoit Boissinot
|
r1598 | end = start + length | ||
mason@suse.com
|
r2072 | def loadcache(df): | ||
cache_length = max(cachelen, length) # 4k | ||||
if not df: | ||||
mason@suse.com
|
r2073 | if inline: | ||
df = self.opener(self.indexfile) | ||||
else: | ||||
df = self.opener(self.datafile) | ||||
Benoit Boissinot
|
r1598 | df.seek(start) | ||
self.chunkcache = (start, df.read(cache_length)) | ||||
if not self.chunkcache: | ||||
mason@suse.com
|
r2072 | loadcache(df) | ||
Benoit Boissinot
|
r1598 | |||
cache_start = self.chunkcache[0] | ||||
cache_end = cache_start + len(self.chunkcache[1]) | ||||
if start >= cache_start and end <= cache_end: | ||||
# it is cached | ||||
offset = start - cache_start | ||||
else: | ||||
mason@suse.com
|
r2072 | loadcache(df) | ||
Benoit Boissinot
|
r1598 | offset = 0 | ||
#def checkchunk(): | ||||
# df = self.opener(self.datafile) | ||||
# df.seek(start) | ||||
# return df.read(length) | ||||
#assert s == checkchunk() | ||||
return decompress(self.chunkcache[1][offset:offset + length]) | ||||
mpm@selenic.com
|
r119 | def delta(self, node): | ||
mpm@selenic.com
|
r1083 | """return or calculate a delta between a node and its predecessor""" | ||
mpm@selenic.com
|
r119 | r = self.rev(node) | ||
Benoit Boissinot
|
r1941 | return self.revdiff(r - 1, r) | ||
def revdiff(self, rev1, rev2): | ||||
"""return or calculate a delta between two revisions""" | ||||
b1 = self.base(rev1) | ||||
b2 = self.base(rev2) | ||||
if b1 == b2 and rev1 + 1 == rev2: | ||||
return self.chunk(rev2) | ||||
mpm@selenic.com
|
r119 | else: | ||
Benoit Boissinot
|
r1941 | return self.diff(self.revision(self.node(rev1)), | ||
self.revision(self.node(rev2))) | ||||
mpm@selenic.com
|
r119 | |||
mpm@selenic.com
|
r0 | def revision(self, node): | ||
mpm@selenic.com
|
r1083 | """return an uncompressed revision of a given""" | ||
mpm@selenic.com
|
r36 | if node == nullid: return "" | ||
mpm@selenic.com
|
r0 | if self.cache and self.cache[0] == node: return self.cache[2] | ||
mpm@selenic.com
|
r1083 | # look up what we need to read | ||
mpm@selenic.com
|
r0 | text = None | ||
rev = self.rev(node) | ||||
Benoit Boissinot
|
r1598 | base = self.base(rev) | ||
mpm@selenic.com
|
r0 | |||
mason@suse.com
|
r2073 | if self.inlinedata(): | ||
# we probably have the whole chunk cached | ||||
df = None | ||||
else: | ||||
df = self.opener(self.datafile) | ||||
mason@suse.com
|
r2072 | |||
mpm@selenic.com
|
r1083 | # do we have useful data cached? | ||
mpm@selenic.com
|
r0 | if self.cache and self.cache[1] >= base and self.cache[1] < rev: | ||
base = self.cache[1] | ||||
text = self.cache[2] | ||||
mason@suse.com
|
r2079 | self.loadindex(base, rev + 1) | ||
Benoit Boissinot
|
r1598 | else: | ||
mason@suse.com
|
r2079 | self.loadindex(base, rev + 1) | ||
mason@suse.com
|
r2072 | text = self.chunk(base, df=df) | ||
mpm@selenic.com
|
r0 | |||
mpm@selenic.com
|
r71 | bins = [] | ||
mpm@selenic.com
|
r64 | for r in xrange(base + 1, rev + 1): | ||
mason@suse.com
|
r2072 | bins.append(self.chunk(r, df=df)) | ||
mpm@selenic.com
|
r0 | |||
Benoit Boissinot
|
r1941 | text = self.patches(text, bins) | ||
mpm@selenic.com
|
r71 | |||
Benoit Boissinot
|
r1598 | p1, p2 = self.parents(node) | ||
mpm@selenic.com
|
r26 | if node != hash(text, p1, p2): | ||
Benoit Boissinot
|
r1402 | raise RevlogError(_("integrity check failed on %s:%d") | ||
Thomas Arendsen Hein
|
r3680 | % (self.datafile, rev)) | ||
mpm@selenic.com
|
r0 | |||
self.cache = (node, rev, text) | ||||
mpm@selenic.com
|
r515 | return text | ||
mpm@selenic.com
|
r0 | |||
mason@suse.com
|
r2075 | def checkinlinesize(self, tr, fp=None): | ||
mason@suse.com
|
r2073 | if not self.inlinedata(): | ||
return | ||||
mason@suse.com
|
r2075 | if not fp: | ||
fp = self.opener(self.indexfile, 'r') | ||||
mason@suse.com
|
r2082 | fp.seek(0, 2) | ||
mason@suse.com
|
r2073 | size = fp.tell() | ||
if size < 131072: | ||||
return | ||||
Chris Mason
|
r2084 | trinfo = tr.find(self.indexfile) | ||
if trinfo == None: | ||||
Thomas Arendsen Hein
|
r3680 | raise RevlogError(_("%s not found in the transaction") | ||
% self.indexfile) | ||||
Chris Mason
|
r2084 | |||
trindex = trinfo[2] | ||||
dataoff = self.start(trindex) | ||||
tr.add(self.datafile, dataoff) | ||||
mason@suse.com
|
r2073 | df = self.opener(self.datafile, 'w') | ||
calc = struct.calcsize(self.indexformat) | ||||
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) | ||||
fp.close() | ||||
df.close() | ||||
mason@suse.com
|
r2076 | fp = self.opener(self.indexfile, 'w', atomictemp=True) | ||
mason@suse.com
|
r2073 | self.version &= ~(REVLOGNGINLINEDATA) | ||
if self.count(): | ||||
x = self.index[0] | ||||
e = struct.pack(self.indexformat, *x)[4:] | ||||
l = struct.pack(versionformat, self.version) | ||||
fp.write(l) | ||||
fp.write(e) | ||||
for i in xrange(1, self.count()): | ||||
x = self.index[i] | ||||
e = struct.pack(self.indexformat, *x) | ||||
fp.write(e) | ||||
mason@suse.com
|
r2076 | # if we don't call rename, the temp file will never replace the | ||
# real index | ||||
fp.rename() | ||||
Chris Mason
|
r2084 | |||
tr.replace(self.indexfile, trindex * calc) | ||||
mason@suse.com
|
r2073 | self.chunkcache = None | ||
mason@suse.com
|
r644 | def addrevision(self, text, transaction, link, p1=None, p2=None, d=None): | ||
mpm@selenic.com
|
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 | ||||
""" | ||||
Alexis S. L. Carvalho
|
r3390 | if not self.inlinedata(): | ||
dfh = self.opener(self.datafile, "a") | ||||
else: | ||||
dfh = None | ||||
ifh = self.opener(self.indexfile, "a+") | ||||
return self._addrevision(text, transaction, link, p1, p2, d, ifh, dfh) | ||||
def _addrevision(self, text, transaction, link, p1, p2, d, ifh, dfh): | ||||
mpm@selenic.com
|
r0 | if text is None: text = "" | ||
if p1 is None: p1 = self.tip() | ||||
if p2 is None: p2 = nullid | ||||
node = hash(text, p1, p2) | ||||
mpm@selenic.com
|
r301 | if node in self.nodemap: | ||
return node | ||||
mpm@selenic.com
|
r0 | n = self.count() | ||
t = n - 1 | ||||
if n: | ||||
mpm@selenic.com
|
r64 | base = self.base(t) | ||
start = self.start(base) | ||||
mpm@selenic.com
|
r0 | end = self.end(t) | ||
mason@suse.com
|
r644 | if not d: | ||
prev = self.revision(self.tip()) | ||||
Brendan Cully
|
r3335 | d = self.diff(prev, text) | ||
mpm@selenic.com
|
r98 | data = compress(d) | ||
mason@suse.com
|
r1533 | l = len(data[1]) + len(data[0]) | ||
dist = end - start + l | ||||
mpm@selenic.com
|
r0 | |||
# full versions are inserted when the needed deltas | ||||
# become comparable to the uncompressed text | ||||
mpm@selenic.com
|
r64 | if not n or dist > len(text) * 2: | ||
mpm@selenic.com
|
r0 | data = compress(text) | ||
mason@suse.com
|
r1533 | l = len(data[1]) + len(data[0]) | ||
mpm@selenic.com
|
r0 | base = n | ||
else: | ||||
base = self.base(t) | ||||
offset = 0 | ||||
if t >= 0: | ||||
offset = self.end(t) | ||||
Thomas Arendsen Hein
|
r2142 | if self.version == REVLOGV0: | ||
mason@suse.com
|
r2072 | e = (offset, l, base, link, p1, p2, node) | ||
else: | ||||
e = (self.offset_type(offset, 0), l, len(text), | ||||
base, link, self.rev(p1), self.rev(p2), node) | ||||
mpm@selenic.com
|
r515 | |||
mpm@selenic.com
|
r0 | self.index.append(e) | ||
self.nodemap[node] = n | ||||
mason@suse.com
|
r2072 | entry = struct.pack(self.indexformat, *e) | ||
mpm@selenic.com
|
r0 | |||
mason@suse.com
|
r2073 | if not self.inlinedata(): | ||
transaction.add(self.datafile, offset) | ||||
transaction.add(self.indexfile, n * len(entry)) | ||||
if data[0]: | ||||
Alexis S. L. Carvalho
|
r3390 | dfh.write(data[0]) | ||
dfh.write(data[1]) | ||||
dfh.flush() | ||||
mason@suse.com
|
r2073 | else: | ||
Alexis S. L. Carvalho
|
r3390 | ifh.seek(0, 2) | ||
transaction.add(self.indexfile, ifh.tell(), self.count() - 1) | ||||
mason@suse.com
|
r2072 | |||
Thomas Arendsen Hein
|
r2142 | if len(self.index) == 1 and self.version != REVLOGV0: | ||
mason@suse.com
|
r2072 | l = struct.pack(versionformat, self.version) | ||
Alexis S. L. Carvalho
|
r3390 | ifh.write(l) | ||
mason@suse.com
|
r2072 | entry = entry[4:] | ||
Alexis S. L. Carvalho
|
r3390 | ifh.write(entry) | ||
mpm@selenic.com
|
r0 | |||
mason@suse.com
|
r2073 | if self.inlinedata(): | ||
Alexis S. L. Carvalho
|
r3390 | ifh.write(data[0]) | ||
ifh.write(data[1]) | ||||
self.checkinlinesize(transaction, ifh) | ||||
mason@suse.com
|
r2073 | |||
mpm@selenic.com
|
r0 | self.cache = (node, n, text) | ||
return node | ||||
def ancestor(self, a, b): | ||||
mpm@selenic.com
|
r1083 | """calculate the least common ancestor of nodes a and b""" | ||
Chris Mason
|
r2081 | |||
Matt Mackall
|
r3139 | def parents(rev): | ||
Thomas Arendsen Hein
|
r3578 | return [p for p in self.parentrevs(rev) if p != nullrev] | ||
mpm@selenic.com
|
r515 | |||
Matt Mackall
|
r3139 | c = ancestor.ancestor(self.rev(a), self.rev(b), parents) | ||
if c is None: | ||||
return nullid | ||||
return self.node(c) | ||||
mpm@selenic.com
|
r0 | |||
Benoit Boissinot
|
r1598 | def group(self, nodelist, lookup, infocollect=None): | ||
mpm@selenic.com
|
r1083 | """calculate a delta group | ||
mpm@selenic.com
|
r46 | |||
mpm@selenic.com
|
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] | ||||
""" | ||||
Eric Hopper
|
r1458 | revs = [self.rev(n) for n in nodelist] | ||
mpm@selenic.com
|
r46 | |||
# if we don't have any revisions touched by these changesets, bail | ||||
mpm@selenic.com
|
r192 | if not revs: | ||
Thomas Arendsen Hein
|
r1981 | yield changegroup.closechunk() | ||
mpm@selenic.com
|
r192 | return | ||
mpm@selenic.com
|
r46 | |||
# add the parent of the first rev | ||||
p = self.parents(self.node(revs[0]))[0] | ||||
revs.insert(0, self.rev(p)) | ||||
# build deltas | ||||
mpm@selenic.com
|
r71 | for d in xrange(0, len(revs) - 1): | ||
mpm@selenic.com
|
r46 | a, b = revs[d], revs[d + 1] | ||
Benoit Boissinot
|
r1598 | nb = self.node(b) | ||
mpm@selenic.com
|
r192 | |||
Eric Hopper
|
r1458 | if infocollect is not None: | ||
Benoit Boissinot
|
r1598 | infocollect(nb) | ||
Eric Hopper
|
r1458 | |||
Benoit Boissinot
|
r1941 | d = self.revdiff(a, b) | ||
Benoit Boissinot
|
r1598 | p = self.parents(nb) | ||
meta = nb + p[0] + p[1] + lookup(nb) | ||||
Thomas Arendsen Hein
|
r1981 | yield changegroup.genchunk("%s%s" % (meta, d)) | ||
mpm@selenic.com
|
r46 | |||
Thomas Arendsen Hein
|
r1981 | yield changegroup.closechunk() | ||
mpm@selenic.com
|
r192 | |||
benoit.boissinot@ens-lyon.fr
|
r1062 | def addgroup(self, revs, linkmapper, transaction, unique=0): | ||
mpm@selenic.com
|
r1083 | """ | ||
add a delta group | ||||
mpm@selenic.com
|
r46 | |||
mpm@selenic.com
|
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 | ||||
mpm@selenic.com
|
r46 | r = self.count() | ||
t = r - 1 | ||||
Thomas Arendsen Hein
|
r2002 | node = None | ||
mpm@selenic.com
|
r515 | |||
Thomas Arendsen Hein
|
r3578 | base = prev = nullrev | ||
mason@suse.com
|
r2078 | start = end = textlen = 0 | ||
mpm@selenic.com
|
r46 | if r: | ||
end = self.end(t) | ||||
mason@suse.com
|
r2072 | ifh = self.opener(self.indexfile, "a+") | ||
mason@suse.com
|
r2077 | ifh.seek(0, 2) | ||
Chris Mason
|
r2084 | transaction.add(self.indexfile, ifh.tell(), self.count()) | ||
mason@suse.com
|
r2073 | if self.inlinedata(): | ||
dfh = None | ||||
else: | ||||
transaction.add(self.datafile, end) | ||||
dfh = self.opener(self.datafile, "a") | ||||
mpm@selenic.com
|
r46 | |||
# loop through our set of deltas | ||||
mpm@selenic.com
|
r192 | chain = None | ||
for chunk in revs: | ||||
node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80]) | ||||
mpm@selenic.com
|
r94 | link = linkmapper(cs) | ||
mpm@selenic.com
|
r77 | if node in self.nodemap: | ||
mpm@selenic.com
|
r224 | # this can happen if two branches make the same change | ||
mpm@selenic.com
|
r1218 | # if unique: | ||
Benoit Boissinot
|
r1402 | # raise RevlogError(_("already have %s") % hex(node[:4])) | ||
Matt Mackall
|
r653 | chain = node | ||
mpm@selenic.com
|
r224 | continue | ||
mpm@selenic.com
|
r192 | delta = chunk[80:] | ||
Matt Mackall
|
r1509 | for p in (p1, p2): | ||
if not p in self.nodemap: | ||||
Brendan Cully
|
r3930 | raise LookupError(_("unknown parent %s") % short(p)) | ||
Matt Mackall
|
r1509 | |||
mpm@selenic.com
|
r192 | if not chain: | ||
# retrieve the parent revision of the delta chain | ||||
chain = p1 | ||||
if not chain in self.nodemap: | ||||
Brendan Cully
|
r3930 | raise LookupError(_("unknown base %s") % short(chain[:4])) | ||
mpm@selenic.com
|
r46 | |||
# 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: | ||||
mason@suse.com
|
r1533 | tempd = compress(delta) | ||
cdelta = tempd[0] + tempd[1] | ||||
mason@suse.com
|
r2078 | textlen = mdiff.patchedsize(textlen, delta) | ||
mpm@selenic.com
|
r46 | |||
mason@suse.com
|
r2078 | if chain != prev or (end - start + len(cdelta)) > textlen * 2: | ||
mpm@selenic.com
|
r46 | # flush our writes here so we can read it in revision | ||
mason@suse.com
|
r2072 | if dfh: | ||
dfh.flush() | ||||
mpm@selenic.com
|
r46 | ifh.flush() | ||
mpm@selenic.com
|
r65 | text = self.revision(chain) | ||
mpm@selenic.com
|
r73 | text = self.patches(text, [delta]) | ||
Alexis S. L. Carvalho
|
r3390 | chk = self._addrevision(text, transaction, link, p1, p2, None, | ||
ifh, dfh) | ||||
if not dfh and not self.inlinedata(): | ||||
# addrevision switched from inline to conventional | ||||
# reopen the index | ||||
dfh = self.opener(self.datafile, "a") | ||||
ifh = self.opener(self.indexfile, "a") | ||||
mpm@selenic.com
|
r46 | if chk != node: | ||
Benoit Boissinot
|
r1402 | raise RevlogError(_("consistency error adding group")) | ||
mason@suse.com
|
r2078 | textlen = len(text) | ||
mpm@selenic.com
|
r46 | else: | ||
Thomas Arendsen Hein
|
r2142 | if self.version == REVLOGV0: | ||
mason@suse.com
|
r2072 | e = (end, len(cdelta), base, link, p1, p2, node) | ||
else: | ||||
mason@suse.com
|
r2078 | e = (self.offset_type(end, 0), len(cdelta), textlen, base, | ||
mason@suse.com
|
r2072 | link, self.rev(p1), self.rev(p2), node) | ||
mpm@selenic.com
|
r46 | self.index.append(e) | ||
self.nodemap[node] = r | ||||
mason@suse.com
|
r2073 | if self.inlinedata(): | ||
ifh.write(struct.pack(self.indexformat, *e)) | ||||
ifh.write(cdelta) | ||||
mason@suse.com
|
r2075 | self.checkinlinesize(transaction, ifh) | ||
mason@suse.com
|
r2073 | if not self.inlinedata(): | ||
dfh = self.opener(self.datafile, "a") | ||||
ifh = self.opener(self.indexfile, "a") | ||||
else: | ||||
dfh.write(cdelta) | ||||
ifh.write(struct.pack(self.indexformat, *e)) | ||||
mpm@selenic.com
|
r46 | |||
mpm@selenic.com
|
r65 | t, r, chain, prev = r, r + 1, node, node | ||
Benoit Boissinot
|
r1749 | base = self.base(t) | ||
start = self.start(base) | ||||
mpm@selenic.com
|
r46 | end = self.end(t) | ||
return node | ||||
Matt Mackall
|
r1493 | |||
mason@suse.com
|
r1535 | def strip(self, rev, minlink): | ||
if self.count() == 0 or rev >= self.count(): | ||||
return | ||||
mason@suse.com
|
r2072 | if isinstance(self.index, lazyindex): | ||
self.loadindexmap() | ||||
mason@suse.com
|
r1535 | # When stripping away a revision, we need to make sure it | ||
# does not actually belong to an older changeset. | ||||
# The minlink parameter defines the oldest revision | ||||
# we're allowed to strip away. | ||||
mason@suse.com
|
r2072 | while minlink > self.index[rev][-4]: | ||
mason@suse.com
|
r1535 | rev += 1 | ||
if rev >= self.count(): | ||||
return | ||||
# first truncate the files on disk | ||||
end = self.start(rev) | ||||
mason@suse.com
|
r2073 | if not self.inlinedata(): | ||
df = self.opener(self.datafile, "a") | ||||
df.truncate(end) | ||||
end = rev * struct.calcsize(self.indexformat) | ||||
else: | ||||
end += rev * struct.calcsize(self.indexformat) | ||||
mason@suse.com
|
r2072 | |||
indexf = self.opener(self.indexfile, "a") | ||||
indexf.truncate(end) | ||||
mason@suse.com
|
r1535 | |||
# then reset internal state in memory to forget those revisions | ||||
self.cache = None | ||||
mason@suse.com
|
r1711 | self.chunkcache = None | ||
mason@suse.com
|
r2072 | for x in xrange(rev, self.count()): | ||
del self.nodemap[self.node(x)] | ||||
mason@suse.com
|
r1535 | |||
mason@suse.com
|
r2072 | del self.index[rev:] | ||
mason@suse.com
|
r1535 | |||
Matt Mackall
|
r1493 | def checksize(self): | ||
expected = 0 | ||||
if self.count(): | ||||
expected = self.end(self.count() - 1) | ||||
Matt Mackall
|
r1667 | |||
Matt Mackall
|
r1494 | try: | ||
f = self.opener(self.datafile) | ||||
f.seek(0, 2) | ||||
actual = f.tell() | ||||
Matt Mackall
|
r1667 | dd = actual - expected | ||
Matt Mackall
|
r1494 | except IOError, inst: | ||
Matt Mackall
|
r1667 | if inst.errno != errno.ENOENT: | ||
raise | ||||
dd = 0 | ||||
try: | ||||
f = self.opener(self.indexfile) | ||||
f.seek(0, 2) | ||||
actual = f.tell() | ||||
mason@suse.com
|
r2072 | s = struct.calcsize(self.indexformat) | ||
Matt Mackall
|
r1667 | i = actual / s | ||
di = actual - (i * s) | ||||
mason@suse.com
|
r2073 | if self.inlinedata(): | ||
databytes = 0 | ||||
for r in xrange(self.count()): | ||||
databytes += self.length(r) | ||||
dd = 0 | ||||
di = actual - self.count() * s - databytes | ||||
Matt Mackall
|
r1667 | except IOError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
raise | ||||
di = 0 | ||||
return (dd, di) | ||||
Matt Mackall
|
r1494 | |||