revlog.py
1279 lines
| 42.6 KiB
| text/x-python
|
PythonLexer
/ mercurial / revlog.py
|
r8226 | # revlog.py - storage back-end for mercurial | ||
# | ||||
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r0 | |||
|
r8227 | """Storage back-end for Mercurial. | ||
This provides efficient delta storage with O(1) retrieve and append | ||||
and O(changes) merge between branches. | ||||
""" | ||||
|
r7873 | # import stuff from node for others to import from revlog | ||
|
r14393 | from node import bin, hex, nullid, nullrev | ||
|
r3891 | from i18n import _ | ||
|
r14365 | import ancestor, mdiff, parsers, error, util, dagutil | ||
|
r8312 | import struct, zlib, errno | ||
|
r36 | |||
|
r5007 | _pack = struct.pack | ||
_unpack = struct.unpack | ||||
_compress = zlib.compress | ||||
_decompress = zlib.decompress | ||||
|
r6470 | _sha = util.sha1 | ||
|
r5007 | |||
|
r11746 | # revlog header flags | ||
|
r2072 | REVLOGV0 = 0 | ||
REVLOGNG = 1 | ||||
|
r2073 | REVLOGNGINLINEDATA = (1 << 16) | ||
|
r14253 | REVLOGGENERALDELTA = (1 << 17) | ||
|
r2222 | REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA | ||
REVLOG_DEFAULT_FORMAT = REVLOGNG | ||||
REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS | ||||
|
r14253 | REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGGENERALDELTA | ||
|
r11746 | |||
# revlog index flags | ||||
|
r14196 | REVIDX_KNOWN_FLAGS = 0 | ||
|
r2073 | |||
|
r10916 | # max size of revlog with inline data | ||
_maxinline = 131072 | ||||
|
r13253 | _chunksize = 1048576 | ||
|
r10913 | |||
|
r7633 | RevlogError = error.RevlogError | ||
LookupError = error.LookupError | ||||
|
r6703 | |||
|
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) | ||||
|
r7883 | nullhash = _sha(nullid) | ||
|
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. | ||||
""" | ||||
|
r7883 | # As of now, if one of the parent node is null, p2 is null | ||
if p2 == nullid: | ||||
# deep copy of a hash is faster than creating one | ||||
s = nullhash.copy() | ||||
s.update(p1) | ||||
else: | ||||
# none of the parent nodes are nullid | ||||
l = [p1, p2] | ||||
l.sort() | ||||
s = _sha(l[0]) | ||||
s.update(l[1]) | ||||
|
r1091 | 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 | |||
|
r4987 | indexformatv0 = ">4l20s20s20s" | ||
v0shaoffset = 56 | ||||
|
r4918 | |||
|
r4972 | class revlogoldio(object): | ||
def __init__(self): | ||||
|
r4977 | self.size = struct.calcsize(indexformatv0) | ||
|
r4972 | |||
|
r13264 | def parseindex(self, data, inline): | ||
|
r4977 | s = self.size | ||
|
r4972 | index = [] | ||
nodemap = {nullid: nullrev} | ||||
|
r4973 | n = off = 0 | ||
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 | |||
|
r13265 | # add the magic null revision at -1 | ||
index.append((0, 0, 0, -1, -1, -1, -1, nullid)) | ||||
|
r4983 | return index, nodemap, None | ||
|
r4972 | |||
|
r5338 | def packentry(self, entry, node, version, rev): | ||
|
r10395 | if gettype(entry[0]): | ||
raise RevlogError(_("index entry flags need RevlogNG")) | ||||
|
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: | ||
|
r11323 | # 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 | ||||
|
r4987 | # 32 bytes: nodeid | ||
indexformatng = ">Qiiiiii20s12x" | ||||
ngshaoffset = 32 | ||||
versionformat = ">I" | ||||
|
r4972 | class revlogio(object): | ||
def __init__(self): | ||||
|
r4977 | self.size = struct.calcsize(indexformatng) | ||
|
r4972 | |||
|
r13264 | def parseindex(self, data, inline): | ||
|
r7109 | # call the C implementation to parse the index data | ||
|
r13254 | index, cache = parsers.parse_index2(data, inline) | ||
|
r13275 | return index, None, 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 | ||||
|
r6912 | information on each revision, including its nodeid (hash), the | ||
|
r1083 | 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. | ||||
""" | ||||
|
r14251 | 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 | ||
|
r14296 | self._basecache = (0, 0) | ||
|
r8316 | self._chunkcache = (0, '') | ||
|
r4985 | self.index = [] | ||
|
r13258 | self._pcache = {} | ||
|
r13275 | self._nodecache = {nullid: nullrev} | ||
self._nodepos = None | ||||
|
r4985 | |||
v = REVLOG_DEFAULT_VERSION | ||||
|
r14960 | opts = getattr(opener, 'options', None) | ||
if opts is not None: | ||||
if 'revlogv1' in opts: | ||||
if 'generaldelta' in opts: | ||||
|
r14333 | v |= REVLOGGENERALDELTA | ||
else: | ||||
v = 0 | ||||
|
r11928 | |||
|
r8314 | i = '' | ||
|
r14334 | self._initempty = True | ||
|
r0 | try: | ||
|
r1784 | f = self.opener(self.indexfile) | ||
|
r13253 | i = f.read() | ||
|
r13400 | f.close() | ||
|
r4918 | if len(i) > 0: | ||
|
r8314 | v = struct.unpack(versionformat, i[:4])[0] | ||
|
r14334 | self._initempty = False | ||
|
r1322 | except IOError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
raise | ||||
|
r4985 | |||
self.version = v | ||||
self._inline = v & REVLOGNGINLINEDATA | ||||
|
r14253 | self._generaldelta = v & REVLOGGENERALDELTA | ||
|
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)) | ||||
|
r11746 | elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS: | ||
|
r4985 | 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() | ||
|
r13265 | try: | ||
d = self._io.parseindex(i, self._inline) | ||||
except (ValueError, IndexError): | ||||
raise RevlogError(_("index %s is corrupted") % (self.indexfile)) | ||||
|
r13268 | self.index, nodemap, self._chunkcache = d | ||
if nodemap is not None: | ||||
|
r13275 | self.nodemap = self._nodecache = nodemap | ||
|
r13265 | if not self._chunkcache: | ||
self._chunkclear() | ||||
|
r116 | |||
|
r4980 | def tip(self): | ||
return self.node(len(self.index) - 2) | ||||
|
r6750 | def __len__(self): | ||
|
r4980 | return len(self.index) - 1 | ||
|
r6750 | def __iter__(self): | ||
for i in xrange(len(self)): | ||||
yield i | ||||
|
r13275 | |||
@util.propertycache | ||||
def nodemap(self): | ||||
|
r14064 | self.rev(self.node(0)) | ||
|
r13275 | return self._nodecache | ||
|
r13259 | |||
def rev(self, node): | ||||
|
r13275 | try: | ||
return self._nodecache[node] | ||||
except KeyError: | ||||
n = self._nodecache | ||||
i = self.index | ||||
p = self._nodepos | ||||
if p is None: | ||||
p = len(i) - 2 | ||||
for r in xrange(p, -1, -1): | ||||
v = i[r][7] | ||||
n[v] = r | ||||
if v == node: | ||||
self._nodepos = r - 1 | ||||
return r | ||||
raise LookupError(node, self.indexfile, _('no node')) | ||||
|
r4980 | def node(self, rev): | ||
return self.index[rev][7] | ||||
|
r7361 | def linkrev(self, rev): | ||
return self.index[rev][4] | ||||
|
r2 | def parents(self, node): | ||
|
r7363 | i = self.index | ||
d = i[self.rev(node)] | ||||
return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline | ||||
|
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] | ||||
|
r14252 | def chainbase(self, rev): | ||
index = self.index | ||||
base = index[rev][3] | ||||
while base != rev: | ||||
rev = base | ||||
base = index[rev][3] | ||||
return base | ||||
|
r11693 | def flags(self, rev): | ||
return self.index[rev][0] & 0xFFFF | ||||
|
r12024 | def rawsize(self, rev): | ||
|
r2078 | """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) | ||||
|
r12024 | size = rawsize | ||
|
r2078 | |||
|
r3630 | def reachable(self, node, stop=None): | ||
|
r8464 | """return the set of all nodes ancestral to a given node, including | ||
|
r3683 | the node itself, stopping when stop is matched""" | ||
|
r8464 | reachable = set((node,)) | ||
|
r3630 | visit = [node] | ||
|
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: | ||||
|
r8464 | reachable.add(p) | ||
|
r1074 | visit.append(p) | ||
return reachable | ||||
|
r6872 | def ancestors(self, *revs): | ||
|
r10047 | """Generate the ancestors of 'revs' in reverse topological order. | ||
Yield a sequence of revision numbers starting with the parents | ||||
of each revision in revs, i.e., each revision is *not* considered | ||||
an ancestor of itself. Results are in breadth-first order: | ||||
parents of each rev in revs, then parents of those, etc. Result | ||||
does not include the null revision.""" | ||||
|
r6872 | visit = list(revs) | ||
|
r8150 | seen = set([nullrev]) | ||
|
r6872 | while visit: | ||
for parent in self.parentrevs(visit.pop(0)): | ||||
if parent not in seen: | ||||
visit.append(parent) | ||||
seen.add(parent) | ||||
yield parent | ||||
def descendants(self, *revs): | ||||
|
r10047 | """Generate the descendants of 'revs' in revision order. | ||
Yield a sequence of revision numbers starting with a child of | ||||
some rev in revs, i.e., each revision is *not* considered a | ||||
descendant of itself. Results are ordered by revision number (a | ||||
topological sort).""" | ||||
|
r12950 | first = min(revs) | ||
if first == nullrev: | ||||
for i in self: | ||||
yield i | ||||
return | ||||
|
r8150 | seen = set(revs) | ||
|
r12950 | for i in xrange(first + 1, len(self)): | ||
|
r6872 | for x in self.parentrevs(i): | ||
if x != nullrev and x in seen: | ||||
seen.add(i) | ||||
yield i | ||||
break | ||||
|
r13741 | def findcommonmissing(self, common=None, heads=None): | ||
"""Return a tuple of the ancestors of common and the ancestors of heads | ||||
that are not ancestors of common. | ||||
|
r10047 | |||
|
r13741 | More specifically, the second element is a list of nodes N such that | ||
every N satisfies the following constraints: | ||||
|
r7233 | |||
|
r10047 | 1. N is an ancestor of some node in 'heads' | ||
2. N is not an ancestor of any node in 'common' | ||||
|
r7233 | |||
|
r10047 | The list is sorted by revision number, meaning it is | ||
topologically sorted. | ||||
'heads' and 'common' are both lists of node IDs. If heads is | ||||
not supplied, uses all of the revlog's heads. If common is not | ||||
supplied, uses nullid.""" | ||||
|
r7233 | if common is None: | ||
common = [nullid] | ||||
if heads is None: | ||||
heads = self.heads() | ||||
common = [self.rev(n) for n in common] | ||||
heads = [self.rev(n) for n in heads] | ||||
# we want the ancestors, but inclusive | ||||
|
r8152 | has = set(self.ancestors(*common)) | ||
has.add(nullrev) | ||||
has.update(common) | ||||
|
r7233 | |||
# take all ancestors from heads that aren't in has | ||||
|
r8453 | missing = set() | ||
|
r7233 | visit = [r for r in heads if r not in has] | ||
while visit: | ||||
r = visit.pop(0) | ||||
if r in missing: | ||||
continue | ||||
else: | ||||
|
r8453 | missing.add(r) | ||
|
r7233 | for p in self.parentrevs(r): | ||
if p not in has: | ||||
visit.append(p) | ||||
|
r8453 | missing = list(missing) | ||
|
r7233 | missing.sort() | ||
|
r13741 | return has, [self.node(r) for r in missing] | ||
def findmissing(self, common=None, heads=None): | ||||
"""Return the ancestors of heads that are not ancestors of common. | ||||
More specifically, return a list of nodes N such that every N | ||||
satisfies the following constraints: | ||||
1. N is an ancestor of some node in 'heads' | ||||
2. N is not an ancestor of any node in 'common' | ||||
The list is sorted by revision number, meaning it is | ||||
topologically sorted. | ||||
'heads' and 'common' are both lists of node IDs. If heads is | ||||
not supplied, uses all of the revlog's heads. If common is not | ||||
supplied, uses nullid.""" | ||||
_common, missing = self.findcommonmissing(common, heads) | ||||
return missing | ||||
|
r7233 | |||
|
r1457 | def nodesbetween(self, roots=None, heads=None): | ||
|
r10047 | """Return a topological path from 'roots' to 'heads'. | ||
Return a tuple (nodes, outroots, outheads) where 'nodes' is a | ||||
topologically sorted list of all nodes N that satisfy both of | ||||
these constraints: | ||||
1. N is a descendant of some node in 'roots' | ||||
2. N is an ancestor of some node in 'heads' | ||||
|
r1457 | |||
|
r10047 | Every node is considered to be both a descendant and an ancestor | ||
of itself, so every reachable node in 'roots' and 'heads' will be | ||||
included in 'nodes'. | ||||
|
r1457 | |||
|
r10047 | 'outroots' is the list of reachable nodes in 'roots', i.e., the | ||
subset of 'roots' that is returned in 'nodes'. Likewise, | ||||
'outheads' is the subset of 'heads' that is also in 'nodes'. | ||||
'roots' and 'heads' are both lists of node IDs. If 'roots' is | ||||
unspecified, uses nullid as the only root. If 'heads' is | ||||
unspecified, uses list of all of the revlog's heads.""" | ||||
|
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: | ||||
|
r14549 | roots = [nullid] # Everybody's a descendant of nullid | ||
|
r3578 | lowestrev = nullrev | ||
if (lowestrev == nullrev) and (heads is None): | ||||
|
r1457 | # We want _all_ the nodes! | ||
|
r6750 | return ([self.node(r) for r in self], [nullid], list(self.heads())) | ||
|
r1457 | if heads is None: | ||
# All nodes are ancestors, so the latest ancestor is the last | ||||
# node. | ||||
|
r6750 | highestrev = len(self) - 1 | ||
|
r1457 | # 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 | ||||
|
r8464 | ancestors = set() | ||
|
r1457 | # 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. | ||||
|
r14219 | heads = dict.fromkeys(heads, False) | ||
|
r3360 | # Start at the top and keep marking parents until we're done. | ||
|
r8163 | nodestotag = set(heads) | ||
|
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: | ||||
|
r14549 | # If we are possibly a descendant of one of the roots | ||
|
r1457 | # and we haven't already been marked as an ancestor | ||
|
r8464 | ancestors.add(n) # Mark as ancestor | ||
|
r1457 | # Add non-nullid parents to list of nodes to tag. | ||
|
r8153 | nodestotag.update([p for p in self.parents(n) if | ||
|
r1457 | 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] | ||
|
r8152 | # Transform our roots list into a set. | ||
|
r14549 | descendants = set(roots) | ||
|
r1457 | # Also, keep the original roots so we can filter out roots that aren't | ||
# 'real' roots (i.e. are descended from other roots). | ||||
|
r14549 | roots = descendants.copy() | ||
|
r1457 | # 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 | ||||
|
r14549 | # they're descendants. | ||
|
r1457 | for r in xrange(max(lowestrev, 0), highestrev + 1): | ||
n = self.node(r) | ||||
|
r14549 | isdescendant = False | ||
if lowestrev == nullrev: # Everybody is a descendant of nullid | ||||
isdescendant = True | ||||
elif n in descendants: | ||||
# n is already a descendant | ||||
isdescendant = True | ||||
|
r1457 | # This check only needs to be done here because all the roots | ||
|
r14549 | # will start being marked is descendants before the loop. | ||
|
r1457 | if n in roots: | ||
# If n was a root, check if it's a 'real' root. | ||||
p = tuple(self.parents(n)) | ||||
|
r14549 | # If any of its parents are descendants, it's not a root. | ||
if (p[0] in descendants) or (p[1] in descendants): | ||||
|
r8152 | roots.remove(n) | ||
|
r1457 | else: | ||
p = tuple(self.parents(n)) | ||||
|
r14549 | # A node is a descendant if either of its parents are | ||
# descendants. (We seeded the dependents list with the roots | ||||
|
r1457 | # up there, remember?) | ||
|
r14549 | if (p[0] in descendants) or (p[1] in descendants): | ||
descendants.add(n) | ||||
isdescendant = True | ||||
if isdescendant and ((ancestors is None) or (n in ancestors)): | ||||
# Only include nodes that are both descendants and ancestors. | ||||
|
r1457 | 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 | ||||
|
r14219 | heads[n] = True | ||
|
r1457 | 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. | ||||
|
r14219 | heads[n] = True | ||
|
r1457 | # But, obviously its parents aren't. | ||
for p in self.parents(n): | ||||
heads.pop(p, None) | ||||
|
r14219 | heads = [n for n, flag in heads.iteritems() if flag] | ||
|
r8152 | roots = list(roots) | ||
|
r1457 | assert orderedout | ||
assert roots | ||||
assert heads | ||||
return (orderedout, roots, heads) | ||||
|
r14164 | def headrevs(self): | ||
count = len(self) | ||||
if not count: | ||||
return [nullrev] | ||||
ishead = [1] * (count + 1) | ||||
index = self.index | ||||
for r in xrange(count): | ||||
e = index[r] | ||||
ishead[e[5]] = ishead[e[6]] = 0 | ||||
return [r for r in xrange(count) if ishead[r]] | ||||
|
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: | ||
|
r14164 | if not len(self): | ||
|
r4991 | return [nullid] | ||
|
r14164 | return [self.node(r) for r in self.headrevs()] | ||
|
r4991 | |||
|
r1551 | if start is None: | ||
start = nullid | ||||
|
r3923 | if stop is None: | ||
stop = [] | ||||
|
r8152 | stoprevs = set([self.rev(n) for n in stop]) | ||
|
r1550 | startrev = self.rev(start) | ||
|
r8464 | reachable = set((startrev,)) | ||
heads = set((startrev,)) | ||||
|
r1083 | |||
|
r2490 | parentrevs = self.parentrevs | ||
|
r6750 | for r in xrange(startrev + 1, len(self)): | ||
|
r2490 | for p in parentrevs(r): | ||
if p in reachable: | ||||
|
r3923 | if r not in stoprevs: | ||
|
r8464 | reachable.add(r) | ||
heads.add(r) | ||||
|
r3923 | if p in heads and p not in stoprevs: | ||
|
r8464 | heads.remove(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) | ||||
|
r6750 | for r in range(p + 1, len(self)): | ||
|
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 | |||
|
r10897 | def descendant(self, start, end): | ||
|
r12949 | if start == nullrev: | ||
return True | ||||
|
r10897 | for i in self.descendants(start): | ||
if i == end: | ||||
return True | ||||
elif i > end: | ||||
break | ||||
return False | ||||
def ancestor(self, a, b): | ||||
"""calculate the least common ancestor of nodes a and b""" | ||||
# fast path, check if it is a descendant | ||||
a, b = self.rev(a), self.rev(b) | ||||
start, end = sorted((a, b)) | ||||
if self.descendant(start, end): | ||||
return self.node(start) | ||||
def parents(rev): | ||||
return [p for p in self.parentrevs(rev) if p != nullrev] | ||||
c = ancestor.ancestor(a, b, parents) | ||||
if c is None: | ||||
return nullid | ||||
return self.node(c) | ||||
|
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 | ||||
|
r7874 | self.rev(node) # quick search the index | ||
|
r3438 | 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: | ||||
|
r6750 | rev = len(self) + rev | ||
if rev < 0 or rev >= len(self): | ||||
|
r4980 | 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) | ||||
|
r7874 | self.rev(node) | ||
|
r3157 | return node | ||
|
r7062 | except (TypeError, LookupError): | ||
|
r3453 | pass | ||
def _partialmatch(self, id): | ||||
|
r13258 | if id in self._pcache: | ||
return self._pcache[id] | ||||
|
r3453 | if len(id) < 40: | ||
try: | ||||
|
r3438 | # hex(node)[:...] | ||
|
r9029 | l = len(id) // 2 # grab an even number of digits | ||
|
r13259 | prefix = bin(id[:l * 2]) | ||
nl = [e[7] for e in self.index if e[7].startswith(prefix)] | ||||
|
r7365 | nl = [n for n in nl if hex(n).startswith(id)] | ||
if len(nl) > 0: | ||||
if len(nl) == 1: | ||||
|
r13258 | self._pcache[id] = nl[0] | ||
|
r7365 | return nl[0] | ||
raise LookupError(id, self.indexfile, | ||||
_('ambiguous identifier')) | ||||
return None | ||||
|
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): | ||
|
r11539 | """compare text with a given file revision | ||
returns True if text is different than what is stored. | ||||
""" | ||||
|
r2890 | p1, p2 = self.parents(node) | ||
return hash(text, p1, p2) != node | ||||
|
r8316 | def _addchunk(self, offset, data): | ||
o, d = self._chunkcache | ||||
# try to add to existing cache | ||||
|
r13253 | if o + len(d) == offset and len(d) + len(data) < _chunksize: | ||
|
r8316 | self._chunkcache = o, d + data | ||
else: | ||||
self._chunkcache = offset, data | ||||
|
r8650 | def _loadchunk(self, offset, length): | ||
if self._inline: | ||||
df = self.opener(self.indexfile) | ||||
else: | ||||
df = self.opener(self.datafile) | ||||
|
r8316 | |||
readahead = max(65536, length) | ||||
df.seek(offset) | ||||
d = df.read(readahead) | ||||
self._addchunk(offset, d) | ||||
if readahead > length: | ||||
return d[:length] | ||||
return d | ||||
|
r8650 | def _getchunk(self, offset, length): | ||
|
r8316 | o, d = self._chunkcache | ||
l = len(d) | ||||
# is it in the cache? | ||||
cachestart = offset - o | ||||
cacheend = cachestart + length | ||||
if cachestart >= 0 and cacheend <= l: | ||||
if cachestart == 0 and cacheend == l: | ||||
return d # avoid a copy | ||||
return d[cachestart:cacheend] | ||||
|
r8650 | return self._loadchunk(offset, length) | ||
|
r8316 | |||
|
r8650 | def _chunkraw(self, startrev, endrev): | ||
|
r8318 | start = self.start(startrev) | ||
|
r8650 | length = self.end(endrev) - start | ||
|
r8318 | if self._inline: | ||
start += (startrev + 1) * self._io.size | ||||
|
r8650 | return self._getchunk(start, length) | ||
|
r8318 | |||
|
r8650 | def _chunk(self, rev): | ||
return decompress(self._chunkraw(rev, rev)) | ||||
|
r14075 | def _chunkbase(self, rev): | ||
return self._chunk(rev) | ||||
|
r8650 | def _chunkclear(self): | ||
self._chunkcache = (0, '') | ||||
|
r1598 | |||
|
r11929 | def deltaparent(self, rev): | ||
|
r14195 | """return deltaparent of the given revision""" | ||
|
r14253 | base = self.index[rev][3] | ||
if base == rev: | ||||
|
r14208 | return nullrev | ||
|
r14253 | elif self._generaldelta: | ||
return base | ||||
|
r14208 | else: | ||
return rev - 1 | ||||
|
r11929 | |||
|
r1941 | def revdiff(self, rev1, rev2): | ||
"""return or calculate a delta between two revisions""" | ||||
|
r14208 | if rev1 != nullrev and self.deltaparent(rev2) == rev1: | ||
|
r8650 | return self._chunk(rev2) | ||
|
r5005 | |||
return mdiff.textdiff(self.revision(self.node(rev1)), | ||||
self.revision(self.node(rev2))) | ||||
|
r119 | |||
|
r0 | def revision(self, node): | ||
|
r6912 | """return an uncompressed revision of a given node""" | ||
|
r11996 | cachedrev = None | ||
|
r4980 | if node == nullid: | ||
return "" | ||||
|
r11930 | if self._cache: | ||
if self._cache[0] == node: | ||||
return self._cache[2] | ||||
|
r11995 | cachedrev = self._cache[1] | ||
|
r0 | |||
|
r1083 | # look up what we need to read | ||
|
r0 | text = None | ||
rev = self.rev(node) | ||||
|
r5004 | # check rev flags | ||
|
r11745 | if self.flags(rev) & ~REVIDX_KNOWN_FLAGS: | ||
|
r5312 | raise RevlogError(_('incompatible revision flag %x') % | ||
|
r11745 | (self.flags(rev) & ~REVIDX_KNOWN_FLAGS)) | ||
|
r5004 | |||
|
r11995 | # build delta chain | ||
|
r11998 | chain = [] | ||
|
r14252 | index = self.index # for performance | ||
|
r14253 | generaldelta = self._generaldelta | ||
|
r11998 | iterrev = rev | ||
|
r14252 | e = index[iterrev] | ||
while iterrev != e[3] and iterrev != cachedrev: | ||||
|
r11998 | chain.append(iterrev) | ||
|
r14253 | if generaldelta: | ||
iterrev = e[3] | ||||
else: | ||||
iterrev -= 1 | ||||
|
r14252 | e = index[iterrev] | ||
|
r11998 | chain.reverse() | ||
base = iterrev | ||||
|
r11995 | |||
|
r11998 | if iterrev == cachedrev: | ||
# cache hit | ||||
|
r9420 | text = self._cache[2] | ||
|
r0 | |||
|
r11754 | # drop cache to save memory | ||
self._cache = None | ||||
|
r8650 | self._chunkraw(base, rev) | ||
if text is None: | ||||
|
r14075 | text = self._chunkbase(base) | ||
|
r8650 | |||
|
r11930 | bins = [self._chunk(r) for r in chain] | ||
|
r4989 | text = mdiff.patches(text, bins) | ||
|
r13239 | |||
|
r13276 | text = self._checkhash(text, node, rev) | ||
|
r13239 | |||
self._cache = (node, rev, text) | ||||
return text | ||||
|
r13276 | def _checkhash(self, text, node, rev): | ||
|
r1598 | p1, p2 = self.parents(node) | ||
|
r14196 | if node != hash(text, p1, p2): | ||
|
r1402 | raise RevlogError(_("integrity check failed on %s:%d") | ||
|
r8643 | % (self.indexfile, rev)) | ||
|
r515 | return text | ||
|
r0 | |||
|
r2075 | def checkinlinesize(self, tr, fp=None): | ||
|
r10913 | if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline: | ||
|
r2073 | return | ||
|
r8315 | |||
|
r2084 | trinfo = tr.find(self.indexfile) | ||
|
r8527 | if trinfo is 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) | ||||
|
r8315 | |||
|
r8317 | if fp: | ||
fp.flush() | ||||
fp.close() | ||||
|
r8315 | |||
|
r2073 | df = self.opener(self.datafile, 'w') | ||
|
r6261 | try: | ||
|
r6750 | for r in self: | ||
|
r8650 | df.write(self._chunkraw(r, r)) | ||
|
r6261 | finally: | ||
df.close() | ||||
|
r2076 | fp = self.opener(self.indexfile, 'w', atomictemp=True) | ||
|
r2073 | self.version &= ~(REVLOGNGINLINEDATA) | ||
|
r4982 | self._inline = False | ||
|
r6750 | for i in self: | ||
|
r5338 | e = self._io.packentry(self.index[i], self.node, self.version, i) | ||
|
r2073 | fp.write(e) | ||
|
r15057 | # if we don't call close, the temp file will never replace the | ||
|
r2076 | # real index | ||
|
r15057 | fp.close() | ||
|
r2084 | |||
|
r8650 | tr.replace(self.indexfile, trindex * self._io.size) | ||
self._chunkclear() | ||||
|
r2073 | |||
|
r11962 | def addrevision(self, text, transaction, link, p1, p2, cachedelta=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 | ||||
|
r12012 | cachedelta - an optional precomputed delta | ||
|
r1083 | """ | ||
|
r12023 | node = hash(text, p1, p2) | ||
|
r14196 | if node in self.nodemap: | ||
|
r12023 | return node | ||
|
r4981 | dfh = None | ||
|
r4982 | if not self._inline: | ||
|
r3390 | dfh = self.opener(self.datafile, "a") | ||
ifh = self.opener(self.indexfile, "a+") | ||||
|
r6261 | try: | ||
|
r12023 | return self._addrevision(node, text, transaction, link, p1, p2, | ||
|
r11962 | cachedelta, ifh, dfh) | ||
|
r6261 | finally: | ||
if dfh: | ||||
dfh.close() | ||||
ifh.close() | ||||
|
r3390 | |||
|
r12023 | def _addrevision(self, node, text, transaction, link, p1, p2, | ||
|
r11962 | cachedelta, ifh, dfh): | ||
|
r14292 | """internal function to add revisions to the log | ||
|
r12623 | |||
|
r14292 | see addrevision for argument descriptions. | ||
invariants: | ||||
- text is optional (can be None); if not set, cachedelta must be set. | ||||
if both are set, they must correspond to eachother. | ||||
""" | ||||
|
r12886 | btext = [text] | ||
def buildtext(): | ||||
if btext[0] is not None: | ||||
return btext[0] | ||||
|
r12623 | # flush any pending writes here so we can read it in revision | ||
if dfh: | ||||
dfh.flush() | ||||
ifh.flush() | ||||
basetext = self.revision(self.node(cachedelta[0])) | ||||
|
r12886 | btext[0] = mdiff.patch(basetext, cachedelta[1]) | ||
chk = hash(btext[0], p1, p2) | ||||
|
r12623 | if chk != node: | ||
raise RevlogError(_("consistency error in delta")) | ||||
|
r12886 | return btext[0] | ||
|
r12623 | |||
|
r12888 | def builddelta(rev): | ||
# can we use the cached delta? | ||||
if cachedelta and cachedelta[0] == rev: | ||||
delta = cachedelta[1] | ||||
else: | ||||
t = buildtext() | ||||
ptext = self.revision(self.node(rev)) | ||||
delta = mdiff.textdiff(ptext, t) | ||||
data = compress(delta) | ||||
l = len(data[1]) + len(data[0]) | ||||
|
r14296 | if basecache[0] == rev: | ||
|
r14270 | chainbase = basecache[1] | ||
|
r14252 | else: | ||
|
r14270 | chainbase = self.chainbase(rev) | ||
dist = l + offset - self.start(chainbase) | ||||
if self._generaldelta: | ||||
base = rev | ||||
else: | ||||
base = chainbase | ||||
|
r14296 | return dist, l, data, base, chainbase | ||
|
r12888 | |||
|
r6750 | curr = len(self) | ||
|
r4981 | prev = curr - 1 | ||
|
r14296 | base = chainbase = curr | ||
|
r4981 | offset = self.end(prev) | ||
|
r11931 | flags = 0 | ||
|
r11962 | d = None | ||
|
r14296 | basecache = self._basecache | ||
|
r12889 | p1r, p2r = self.rev(p1), self.rev(p2) | ||
|
r0 | |||
|
r11963 | # should we try to build a delta? | ||
|
r12890 | if prev != nullrev: | ||
|
r14270 | if self._generaldelta: | ||
|
r14301 | if p1r >= basecache[1]: | ||
d = builddelta(p1r) | ||||
elif p2r >= basecache[1]: | ||||
d = builddelta(p2r) | ||||
else: | ||||
d = builddelta(prev) | ||||
|
r14270 | else: | ||
d = builddelta(prev) | ||||
|
r14296 | dist, l, data, base, chainbase = d | ||
|
r0 | |||
# full versions are inserted when the needed deltas | ||||
# become comparable to the uncompressed text | ||||
|
r12623 | if text is None: | ||
textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]), | ||||
cachedelta[1]) | ||||
else: | ||||
textlen = len(text) | ||||
|
r14196 | if d is None or dist > textlen * 2: | ||
|
r12886 | text = buildtext() | ||
|
r0 | data = compress(text) | ||
|
r1533 | l = len(data[1]) + len(data[0]) | ||
|
r14296 | base = chainbase = curr | ||
|
r0 | |||
|
r12623 | e = (offset_type(offset, flags), l, textlen, | ||
|
r12889 | base, link, p1r, p2r, 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 | |||
|
r9420 | if type(text) == str: # only accept immutable objects | ||
self._cache = (node, curr, text) | ||||
|
r14296 | self._basecache = (curr, chainbase) | ||
|
r0 | return node | ||
|
r14365 | def group(self, nodelist, bundler, reorder=None): | ||
|
r9437 | """Calculate a delta group, yielding a sequence of changegroup chunks | ||
(strings). | ||||
|
r46 | |||
|
r1083 | Given a list of changeset revs, return a set of deltas and | ||
|
r11999 | metadata corresponding to nodes. The first delta is | ||
first parent(nodelist[0]) -> nodelist[0], the receiver is | ||||
guaranteed to have this parent as it has all history before | ||||
these changesets. In the case firstparent is nullrev the | ||||
changegroup starts with a full revision. | ||||
|
r1083 | """ | ||
|
r46 | |||
|
r14523 | # if we don't have any revisions touched by these changesets, bail | ||
if len(nodelist) == 0: | ||||
yield bundler.close() | ||||
return | ||||
|
r14365 | # for generaldelta revlogs, we linearize the revs; this will both be | ||
# much quicker and generate a much smaller bundle | ||||
if (self._generaldelta and reorder is not False) or reorder: | ||||
dag = dagutil.revlogdag(self) | ||||
revs = set(self.rev(n) for n in nodelist) | ||||
revs = dag.linearize(revs) | ||||
else: | ||||
revs = sorted([self.rev(n) for n in nodelist]) | ||||
|
r8634 | |||
|
r46 | # add the parent of the first rev | ||
|
r8391 | p = self.parentrevs(revs[0])[0] | ||
revs.insert(0, p) | ||||
|
r46 | |||
# build deltas | ||||
|
r13768 | for r in xrange(len(revs) - 1): | ||
|
r14143 | prev, curr = revs[r], revs[r + 1] | ||
for c in bundler.revchunk(self, curr, prev): | ||||
|
r13831 | yield c | ||
|
r46 | |||
|
r13831 | yield bundler.close() | ||
|
r192 | |||
|
r12335 | def addgroup(self, bundle, linkmapper, transaction): | ||
|
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. | ||||
""" | ||||
|
r12624 | # track the base of the current delta log | ||
|
r2002 | node = None | ||
|
r515 | |||
|
r12624 | r = len(self) | ||
end = 0 | ||||
|
r46 | if r: | ||
|
r12624 | end = self.end(r - 1) | ||
|
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 | ||||
|
r14494 | while True: | ||
|
r14144 | chunkdata = bundle.deltachunk(chain) | ||
|
r12336 | if not chunkdata: | ||
|
r12335 | break | ||
|
r12336 | node = chunkdata['node'] | ||
p1 = chunkdata['p1'] | ||||
p2 = chunkdata['p2'] | ||||
cs = chunkdata['cs'] | ||||
|
r14141 | deltabase = chunkdata['deltabase'] | ||
delta = chunkdata['delta'] | ||||
|
r12336 | |||
|
r6261 | link = linkmapper(cs) | ||
|
r14196 | if node in self.nodemap: | ||
|
r6261 | # this can happen if two branches make the same change | ||
chain = node | ||||
continue | ||||
|
r192 | |||
|
r6261 | for p in (p1, p2): | ||
if not p in self.nodemap: | ||||
|
r14196 | raise LookupError(p, self.indexfile, | ||
_('unknown parent')) | ||||
|
r46 | |||
|
r14141 | if deltabase not in self.nodemap: | ||
raise LookupError(deltabase, self.indexfile, | ||||
_('unknown delta base')) | ||||
|
r46 | |||
|
r14141 | baserev = self.rev(deltabase) | ||
|
r12624 | chain = self._addrevision(node, None, transaction, link, | ||
|
r14141 | p1, p2, (baserev, delta), ifh, dfh) | ||
|
r12624 | if not dfh and not self._inline: | ||
# addrevision switched from inline to conventional | ||||
# reopen the index | ||||
|
r13400 | ifh.close() | ||
|
r12624 | dfh = self.opener(self.datafile, "a") | ||
ifh = self.opener(self.indexfile, "a") | ||||
|
r6261 | finally: | ||
if dfh: | ||||
dfh.close() | ||||
ifh.close() | ||||
|
r46 | |||
return node | ||||
|
r1493 | |||
|
r8073 | def strip(self, minlink, transaction): | ||
|
r5910 | """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. | ||||
""" | ||||
|
r6750 | if len(self) == 0: | ||
|
r1535 | return | ||
|
r6750 | for rev in self: | ||
|
r5909 | 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: | ||
|
r8073 | transaction.add(self.datafile, end) | ||
|
r4977 | end = rev * self._io.size | ||
|
r2073 | else: | ||
|
r4977 | end += rev * self._io.size | ||
|
r2072 | |||
|
r8073 | transaction.add(self.indexfile, end) | ||
|
r1535 | |||
# then reset internal state in memory to forget those revisions | ||||
|
r4984 | self._cache = None | ||
|
r8650 | self._chunkclear() | ||
|
r6750 | for x in xrange(rev, len(self)): | ||
|
r2072 | del self.nodemap[self.node(x)] | ||
|
r1535 | |||
|
r4979 | del self.index[rev:-1] | ||
|
r1535 | |||
|
r1493 | def checksize(self): | ||
expected = 0 | ||||
|
r6750 | if len(self): | ||
expected = max(0, self.end(len(self) - 1)) | ||||
|
r1667 | |||
|
r1494 | try: | ||
f = self.opener(self.datafile) | ||||
f.seek(0, 2) | ||||
actual = f.tell() | ||||
|
r13400 | f.close() | ||
|
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() | ||||
|
r13400 | f.close() | ||
|
r4977 | s = self._io.size | ||
|
r9029 | i = max(0, actual // s) | ||
|
r1667 | di = actual - (i * s) | ||
|
r4982 | if self._inline: | ||
|
r2073 | databytes = 0 | ||
|
r6750 | for r in self: | ||
|
r5312 | databytes += max(0, self.length(r)) | ||
|
r2073 | dd = 0 | ||
|
r6750 | di = actual - len(self) * s - databytes | ||
|
r1667 | except IOError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
raise | ||||
di = 0 | ||||
return (dd, di) | ||||
|
r6891 | |||
def files(self): | ||||
|
r10282 | res = [self.indexfile] | ||
|
r6891 | if not self._inline: | ||
res.append(self.datafile) | ||||
return res | ||||