filelog.py
112 lines
| 3.4 KiB
| text/x-python
|
PythonLexer
/ mercurial / filelog.py
mpm@selenic.com
|
r1089 | # filelog.py - file history class for mercurial | ||
# | ||||
Thomas Arendsen Hein
|
r4635 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
mpm@selenic.com
|
r1089 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
mpm@selenic.com
|
r1089 | |||
Mike Edgar
|
r22596 | import error, revlog | ||
Sune Foldager
|
r14074 | import re | ||
mpm@selenic.com
|
r1089 | |||
Sune Foldager
|
r14074 | _mdre = re.compile('\1\n') | ||
Mike Edgar
|
r22421 | def parsemeta(text): | ||
Sune Foldager
|
r14074 | """return (metadatadict, keylist, metadatasize)""" | ||
# text can be buffer, so we can't use .startswith or .index | ||||
if text[:2] != '\1\n': | ||||
Mike Edgar
|
r22422 | return None, None | ||
Sune Foldager
|
r14074 | s = _mdre.search(text, 2).start() | ||
mtext = text[2:s] | ||||
meta = {} | ||||
for l in mtext.splitlines(): | ||||
Matt Mackall
|
r13240 | k, v = l.split(": ", 1) | ||
Sune Foldager
|
r14074 | meta[k] = v | ||
Mike Edgar
|
r22422 | return meta, (s + 2) | ||
Sune Foldager
|
r14074 | |||
Mike Edgar
|
r22420 | def packmeta(meta, text): | ||
keys = sorted(meta.iterkeys()) | ||||
metatext = "".join("%s: %s\n" % (k, meta[k]) for k in keys) | ||||
return "\1\n%s\1\n%s" % (metatext, text) | ||||
Matt Mackall
|
r13240 | |||
Mike Edgar
|
r22596 | def _censoredtext(text): | ||
m, offs = parsemeta(text) | ||||
return m and "censored" in m and not text[offs:] | ||||
Matt Mackall
|
r7634 | class filelog(revlog.revlog): | ||
Matt Mackall
|
r4258 | def __init__(self, opener, path): | ||
Durham Goode
|
r19148 | super(filelog, self).__init__(opener, | ||
Benoit Boissinot
|
r8531 | "/".join(("data", path + ".i"))) | ||
mpm@selenic.com
|
r1089 | |||
def read(self, node): | ||||
t = self.revision(node) | ||||
if not t.startswith('\1\n'): | ||||
return t | ||||
Benoit Boissinot
|
r2579 | s = t.index('\1\n', 2) | ||
Matt Mackall
|
r10282 | return t[s + 2:] | ||
mpm@selenic.com
|
r1089 | |||
def add(self, text, meta, transaction, link, p1=None, p2=None): | ||||
if meta or text.startswith('\1\n'): | ||||
Mike Edgar
|
r22420 | text = packmeta(meta, text) | ||
mpm@selenic.com
|
r1089 | return self.addrevision(text, transaction, link, p1, p2) | ||
mpm@selenic.com
|
r1116 | def renamed(self, node): | ||
Matt Mackall
|
r7634 | if self.parents(node)[0] != revlog.nullid: | ||
mpm@selenic.com
|
r1116 | return False | ||
Matt Mackall
|
r13240 | t = self.revision(node) | ||
Mike Edgar
|
r22421 | m = parsemeta(t)[0] | ||
Christian Ebert
|
r5915 | if m and "copy" in m: | ||
Matt Mackall
|
r7634 | return (m["copy"], revlog.bin(m["copyrev"])) | ||
mpm@selenic.com
|
r1116 | return False | ||
Matt Mackall
|
r2898 | def size(self, rev): | ||
"""return the size of a given revision""" | ||||
# for revisions with renames, we have to go the slow way | ||||
node = self.node(rev) | ||||
if self.renamed(node): | ||||
return len(self.read(node)) | ||||
Mike Edgar
|
r22597 | if self._iscensored(rev): | ||
return 0 | ||||
Matt Mackall
|
r2898 | |||
Nicolas Dumazet
|
r11540 | # XXX if self.read(node).startswith("\1\n"), this returns (size+4) | ||
Durham Goode
|
r19148 | return super(filelog, self).size(rev) | ||
Matt Mackall
|
r2898 | |||
Matt Mackall
|
r2887 | def cmp(self, node, text): | ||
Nicolas Dumazet
|
r11539 | """compare text with a given file revision | ||
returns True if text is different than what is stored. | ||||
""" | ||||
Matt Mackall
|
r2887 | |||
Nicolas Dumazet
|
r11541 | t = text | ||
if text.startswith('\1\n'): | ||||
t = '\1\n\1\n' + text | ||||
Durham Goode
|
r19148 | samehashes = not super(filelog, self).cmp(node, t) | ||
Nicolas Dumazet
|
r11541 | if samehashes: | ||
return False | ||||
Mike Edgar
|
r22597 | # censored files compare against the empty file | ||
Mike Edgar
|
r23858 | if self._iscensored(self.rev(node)): | ||
Mike Edgar
|
r22597 | return text != '' | ||
Nicolas Dumazet
|
r11541 | # renaming a file produces a different hash, even if the data | ||
# remains unchanged. Check if it's the case (slow): | ||||
if self.renamed(node): | ||||
Matt Mackall
|
r2887 | t2 = self.read(node) | ||
Matt Mackall
|
r2895 | return t2 != text | ||
Matt Mackall
|
r2887 | |||
Nicolas Dumazet
|
r11541 | return True | ||
Sune Foldager
|
r14287 | |||
Mike Edgar
|
r22596 | def checkhash(self, text, p1, p2, node, rev=None): | ||
try: | ||||
super(filelog, self).checkhash(text, p1, p2, node, rev=rev) | ||||
except error.RevlogError: | ||||
if _censoredtext(text): | ||||
raise error.CensoredNodeError(self.indexfile, node) | ||||
raise | ||||
Sune Foldager
|
r14287 | def _file(self, f): | ||
return filelog(self.opener, f) | ||||
Mike Edgar
|
r22597 | |||
Mike Edgar
|
r23858 | def _iscensored(self, rev): | ||
Mike Edgar
|
r22597 | """Check if a file revision is censored.""" | ||
Mike Edgar
|
r23858 | return self.flags(rev) & revlog.REVIDX_ISCENSORED | ||