##// END OF EJS Templates
filelog: raise CensoredNodeError when hash checks fail with censor metadata...
Mike Edgar -
r22596:27e2317e default
parent child Browse files
Show More
@@ -1,90 +1,102 b''
1 1 # filelog.py - file history class for mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 import revlog
8 import error, revlog
9 9 import re
10 10
11 11 _mdre = re.compile('\1\n')
12 12 def parsemeta(text):
13 13 """return (metadatadict, keylist, metadatasize)"""
14 14 # text can be buffer, so we can't use .startswith or .index
15 15 if text[:2] != '\1\n':
16 16 return None, None
17 17 s = _mdre.search(text, 2).start()
18 18 mtext = text[2:s]
19 19 meta = {}
20 20 for l in mtext.splitlines():
21 21 k, v = l.split(": ", 1)
22 22 meta[k] = v
23 23 return meta, (s + 2)
24 24
25 25 def packmeta(meta, text):
26 26 keys = sorted(meta.iterkeys())
27 27 metatext = "".join("%s: %s\n" % (k, meta[k]) for k in keys)
28 28 return "\1\n%s\1\n%s" % (metatext, text)
29 29
30 def _censoredtext(text):
31 m, offs = parsemeta(text)
32 return m and "censored" in m and not text[offs:]
33
30 34 class filelog(revlog.revlog):
31 35 def __init__(self, opener, path):
32 36 super(filelog, self).__init__(opener,
33 37 "/".join(("data", path + ".i")))
34 38
35 39 def read(self, node):
36 40 t = self.revision(node)
37 41 if not t.startswith('\1\n'):
38 42 return t
39 43 s = t.index('\1\n', 2)
40 44 return t[s + 2:]
41 45
42 46 def add(self, text, meta, transaction, link, p1=None, p2=None):
43 47 if meta or text.startswith('\1\n'):
44 48 text = packmeta(meta, text)
45 49 return self.addrevision(text, transaction, link, p1, p2)
46 50
47 51 def renamed(self, node):
48 52 if self.parents(node)[0] != revlog.nullid:
49 53 return False
50 54 t = self.revision(node)
51 55 m = parsemeta(t)[0]
52 56 if m and "copy" in m:
53 57 return (m["copy"], revlog.bin(m["copyrev"]))
54 58 return False
55 59
56 60 def size(self, rev):
57 61 """return the size of a given revision"""
58 62
59 63 # for revisions with renames, we have to go the slow way
60 64 node = self.node(rev)
61 65 if self.renamed(node):
62 66 return len(self.read(node))
63 67
64 68 # XXX if self.read(node).startswith("\1\n"), this returns (size+4)
65 69 return super(filelog, self).size(rev)
66 70
67 71 def cmp(self, node, text):
68 72 """compare text with a given file revision
69 73
70 74 returns True if text is different than what is stored.
71 75 """
72 76
73 77 t = text
74 78 if text.startswith('\1\n'):
75 79 t = '\1\n\1\n' + text
76 80
77 81 samehashes = not super(filelog, self).cmp(node, t)
78 82 if samehashes:
79 83 return False
80 84
81 85 # renaming a file produces a different hash, even if the data
82 86 # remains unchanged. Check if it's the case (slow):
83 87 if self.renamed(node):
84 88 t2 = self.read(node)
85 89 return t2 != text
86 90
87 91 return True
88 92
93 def checkhash(self, text, p1, p2, node, rev=None):
94 try:
95 super(filelog, self).checkhash(text, p1, p2, node, rev=rev)
96 except error.RevlogError:
97 if _censoredtext(text):
98 raise error.CensoredNodeError(self.indexfile, node)
99 raise
100
89 101 def _file(self, f):
90 102 return filelog(self.opener, f)
General Comments 0
You need to be logged in to leave comments. Login now