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