Show More
@@ -1,129 +1,137 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 error, mdiff, revlog | |
|
9 | import re, struct | |
|
8 | from __future__ import absolute_import | |
|
9 | ||
|
10 | import re | |
|
11 | import struct | |
|
12 | ||
|
13 | from . import ( | |
|
14 | error, | |
|
15 | mdiff, | |
|
16 | revlog, | |
|
17 | ) | |
|
10 | 18 | |
|
11 | 19 | _mdre = re.compile('\1\n') |
|
12 | 20 | def parsemeta(text): |
|
13 | 21 | """return (metadatadict, keylist, metadatasize)""" |
|
14 | 22 | # text can be buffer, so we can't use .startswith or .index |
|
15 | 23 | if text[:2] != '\1\n': |
|
16 | 24 | return None, None |
|
17 | 25 | s = _mdre.search(text, 2).start() |
|
18 | 26 | mtext = text[2:s] |
|
19 | 27 | meta = {} |
|
20 | 28 | for l in mtext.splitlines(): |
|
21 | 29 | k, v = l.split(": ", 1) |
|
22 | 30 | meta[k] = v |
|
23 | 31 | return meta, (s + 2) |
|
24 | 32 | |
|
25 | 33 | def packmeta(meta, text): |
|
26 | 34 | keys = sorted(meta.iterkeys()) |
|
27 | 35 | metatext = "".join("%s: %s\n" % (k, meta[k]) for k in keys) |
|
28 | 36 | return "\1\n%s\1\n%s" % (metatext, text) |
|
29 | 37 | |
|
30 | 38 | def _censoredtext(text): |
|
31 | 39 | m, offs = parsemeta(text) |
|
32 | 40 | return m and "censored" in m |
|
33 | 41 | |
|
34 | 42 | class filelog(revlog.revlog): |
|
35 | 43 | def __init__(self, opener, path): |
|
36 | 44 | super(filelog, self).__init__(opener, |
|
37 | 45 | "/".join(("data", path + ".i"))) |
|
38 | 46 | |
|
39 | 47 | def read(self, node): |
|
40 | 48 | t = self.revision(node) |
|
41 | 49 | if not t.startswith('\1\n'): |
|
42 | 50 | return t |
|
43 | 51 | s = t.index('\1\n', 2) |
|
44 | 52 | return t[s + 2:] |
|
45 | 53 | |
|
46 | 54 | def add(self, text, meta, transaction, link, p1=None, p2=None): |
|
47 | 55 | if meta or text.startswith('\1\n'): |
|
48 | 56 | text = packmeta(meta, text) |
|
49 | 57 | return self.addrevision(text, transaction, link, p1, p2) |
|
50 | 58 | |
|
51 | 59 | def renamed(self, node): |
|
52 | 60 | if self.parents(node)[0] != revlog.nullid: |
|
53 | 61 | return False |
|
54 | 62 | t = self.revision(node) |
|
55 | 63 | m = parsemeta(t)[0] |
|
56 | 64 | if m and "copy" in m: |
|
57 | 65 | return (m["copy"], revlog.bin(m["copyrev"])) |
|
58 | 66 | return False |
|
59 | 67 | |
|
60 | 68 | def size(self, rev): |
|
61 | 69 | """return the size of a given revision""" |
|
62 | 70 | |
|
63 | 71 | # for revisions with renames, we have to go the slow way |
|
64 | 72 | node = self.node(rev) |
|
65 | 73 | if self.renamed(node): |
|
66 | 74 | return len(self.read(node)) |
|
67 | 75 | if self.iscensored(rev): |
|
68 | 76 | return 0 |
|
69 | 77 | |
|
70 | 78 | # XXX if self.read(node).startswith("\1\n"), this returns (size+4) |
|
71 | 79 | return super(filelog, self).size(rev) |
|
72 | 80 | |
|
73 | 81 | def cmp(self, node, text): |
|
74 | 82 | """compare text with a given file revision |
|
75 | 83 | |
|
76 | 84 | returns True if text is different than what is stored. |
|
77 | 85 | """ |
|
78 | 86 | |
|
79 | 87 | t = text |
|
80 | 88 | if text.startswith('\1\n'): |
|
81 | 89 | t = '\1\n\1\n' + text |
|
82 | 90 | |
|
83 | 91 | samehashes = not super(filelog, self).cmp(node, t) |
|
84 | 92 | if samehashes: |
|
85 | 93 | return False |
|
86 | 94 | |
|
87 | 95 | # censored files compare against the empty file |
|
88 | 96 | if self.iscensored(self.rev(node)): |
|
89 | 97 | return text != '' |
|
90 | 98 | |
|
91 | 99 | # renaming a file produces a different hash, even if the data |
|
92 | 100 | # remains unchanged. Check if it's the case (slow): |
|
93 | 101 | if self.renamed(node): |
|
94 | 102 | t2 = self.read(node) |
|
95 | 103 | return t2 != text |
|
96 | 104 | |
|
97 | 105 | return True |
|
98 | 106 | |
|
99 | 107 | def checkhash(self, text, p1, p2, node, rev=None): |
|
100 | 108 | try: |
|
101 | 109 | super(filelog, self).checkhash(text, p1, p2, node, rev=rev) |
|
102 | 110 | except error.RevlogError: |
|
103 | 111 | if _censoredtext(text): |
|
104 | 112 | raise error.CensoredNodeError(self.indexfile, node, text) |
|
105 | 113 | raise |
|
106 | 114 | |
|
107 | 115 | def iscensored(self, rev): |
|
108 | 116 | """Check if a file revision is censored.""" |
|
109 | 117 | return self.flags(rev) & revlog.REVIDX_ISCENSORED |
|
110 | 118 | |
|
111 | 119 | def _peek_iscensored(self, baserev, delta, flush): |
|
112 | 120 | """Quickly check if a delta produces a censored revision.""" |
|
113 | 121 | # Fragile heuristic: unless new file meta keys are added alphabetically |
|
114 | 122 | # preceding "censored", all censored revisions are prefixed by |
|
115 | 123 | # "\1\ncensored:". A delta producing such a censored revision must be a |
|
116 | 124 | # full-replacement delta, so we inspect the first and only patch in the |
|
117 | 125 | # delta for this prefix. |
|
118 | 126 | hlen = struct.calcsize(">lll") |
|
119 | 127 | if len(delta) <= hlen: |
|
120 | 128 | return False |
|
121 | 129 | |
|
122 | 130 | oldlen = self.rawsize(baserev) |
|
123 | 131 | newlen = len(delta) - hlen |
|
124 | 132 | if delta[:hlen] != mdiff.replacediffheader(oldlen, newlen): |
|
125 | 133 | return False |
|
126 | 134 | |
|
127 | 135 | add = "\1\ncensored:" |
|
128 | 136 | addlen = len(add) |
|
129 | 137 | return newlen >= addlen and delta[hlen:hlen + addlen] == add |
General Comments 0
You need to be logged in to leave comments.
Login now