Show More
@@ -1,200 +1,202 | |||
|
1 | 1 | # manifest.py - manifest revision 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 i18n import _ |
|
9 | 9 | import mdiff, parsers, error, revlog |
|
10 | 10 | import array, struct |
|
11 | 11 | |
|
12 | 12 | class manifestdict(dict): |
|
13 | 13 | def __init__(self, mapping=None, flags=None): |
|
14 | 14 | if mapping is None: |
|
15 | 15 | mapping = {} |
|
16 | 16 | if flags is None: |
|
17 | 17 | flags = {} |
|
18 | 18 | dict.__init__(self, mapping) |
|
19 | 19 | self._flags = flags |
|
20 | 20 | def flags(self, f): |
|
21 | 21 | return self._flags.get(f, "") |
|
22 | 22 | def set(self, f, flags): |
|
23 | 23 | self._flags[f] = flags |
|
24 | 24 | def copy(self): |
|
25 | 25 | return manifestdict(self, dict.copy(self._flags)) |
|
26 | 26 | |
|
27 | 27 | class manifest(revlog.revlog): |
|
28 | 28 | def __init__(self, opener): |
|
29 | 29 | self._mancache = None |
|
30 | 30 | revlog.revlog.__init__(self, opener, "00manifest.i") |
|
31 | 31 | |
|
32 | 32 | def parse(self, lines): |
|
33 | 33 | mfdict = manifestdict() |
|
34 | 34 | parsers.parse_manifest(mfdict, mfdict._flags, lines) |
|
35 | 35 | return mfdict |
|
36 | 36 | |
|
37 | 37 | def readdelta(self, node): |
|
38 | 38 | r = self.rev(node) |
|
39 | if self._parentdelta: | |
|
40 | return self.parse(mdiff.patchtext(self.revdiff(self.deltaparent(r), r))) | |
|
39 | 41 | return self.parse(mdiff.patchtext(self.revdiff(r - 1, r))) |
|
40 | 42 | |
|
41 | 43 | def read(self, node): |
|
42 | 44 | if node == revlog.nullid: |
|
43 | 45 | return manifestdict() # don't upset local cache |
|
44 | 46 | if self._mancache and self._mancache[0] == node: |
|
45 | 47 | return self._mancache[1] |
|
46 | 48 | text = self.revision(node) |
|
47 | 49 | arraytext = array.array('c', text) |
|
48 | 50 | mapping = self.parse(text) |
|
49 | 51 | self._mancache = (node, mapping, arraytext) |
|
50 | 52 | return mapping |
|
51 | 53 | |
|
52 | 54 | def _search(self, m, s, lo=0, hi=None): |
|
53 | 55 | '''return a tuple (start, end) that says where to find s within m. |
|
54 | 56 | |
|
55 | 57 | If the string is found m[start:end] are the line containing |
|
56 | 58 | that string. If start == end the string was not found and |
|
57 | 59 | they indicate the proper sorted insertion point. This was |
|
58 | 60 | taken from bisect_left, and modified to find line start/end as |
|
59 | 61 | it goes along. |
|
60 | 62 | |
|
61 | 63 | m should be a buffer or a string |
|
62 | 64 | s is a string''' |
|
63 | 65 | def advance(i, c): |
|
64 | 66 | while i < lenm and m[i] != c: |
|
65 | 67 | i += 1 |
|
66 | 68 | return i |
|
67 | 69 | if not s: |
|
68 | 70 | return (lo, lo) |
|
69 | 71 | lenm = len(m) |
|
70 | 72 | if not hi: |
|
71 | 73 | hi = lenm |
|
72 | 74 | while lo < hi: |
|
73 | 75 | mid = (lo + hi) // 2 |
|
74 | 76 | start = mid |
|
75 | 77 | while start > 0 and m[start - 1] != '\n': |
|
76 | 78 | start -= 1 |
|
77 | 79 | end = advance(start, '\0') |
|
78 | 80 | if m[start:end] < s: |
|
79 | 81 | # we know that after the null there are 40 bytes of sha1 |
|
80 | 82 | # this translates to the bisect lo = mid + 1 |
|
81 | 83 | lo = advance(end + 40, '\n') + 1 |
|
82 | 84 | else: |
|
83 | 85 | # this translates to the bisect hi = mid |
|
84 | 86 | hi = start |
|
85 | 87 | end = advance(lo, '\0') |
|
86 | 88 | found = m[lo:end] |
|
87 | 89 | if s == found: |
|
88 | 90 | # we know that after the null there are 40 bytes of sha1 |
|
89 | 91 | end = advance(end + 40, '\n') |
|
90 | 92 | return (lo, end + 1) |
|
91 | 93 | else: |
|
92 | 94 | return (lo, lo) |
|
93 | 95 | |
|
94 | 96 | def find(self, node, f): |
|
95 | 97 | '''look up entry for a single file efficiently. |
|
96 | 98 | return (node, flags) pair if found, (None, None) if not.''' |
|
97 | 99 | if self._mancache and self._mancache[0] == node: |
|
98 | 100 | return self._mancache[1].get(f), self._mancache[1].flags(f) |
|
99 | 101 | text = self.revision(node) |
|
100 | 102 | start, end = self._search(text, f) |
|
101 | 103 | if start == end: |
|
102 | 104 | return None, None |
|
103 | 105 | l = text[start:end] |
|
104 | 106 | f, n = l.split('\0') |
|
105 | 107 | return revlog.bin(n[:40]), n[40:-1] |
|
106 | 108 | |
|
107 | 109 | def add(self, map, transaction, link, p1=None, p2=None, |
|
108 | 110 | changed=None): |
|
109 | 111 | # apply the changes collected during the bisect loop to our addlist |
|
110 | 112 | # return a delta suitable for addrevision |
|
111 | 113 | def addlistdelta(addlist, x): |
|
112 | 114 | # start from the bottom up |
|
113 | 115 | # so changes to the offsets don't mess things up. |
|
114 | 116 | for start, end, content in reversed(x): |
|
115 | 117 | if content: |
|
116 | 118 | addlist[start:end] = array.array('c', content) |
|
117 | 119 | else: |
|
118 | 120 | del addlist[start:end] |
|
119 | 121 | return "".join(struct.pack(">lll", start, end, len(content)) + content |
|
120 | 122 | for start, end, content in x) |
|
121 | 123 | |
|
122 | 124 | def checkforbidden(l): |
|
123 | 125 | for f in l: |
|
124 | 126 | if '\n' in f or '\r' in f: |
|
125 | 127 | raise error.RevlogError( |
|
126 | 128 | _("'\\n' and '\\r' disallowed in filenames: %r") % f) |
|
127 | 129 | |
|
128 | 130 | # if we're using the cache, make sure it is valid and |
|
129 | 131 | # parented by the same node we're diffing against |
|
130 | 132 | if not (changed and self._mancache and p1 and self._mancache[0] == p1): |
|
131 | 133 | files = sorted(map) |
|
132 | 134 | checkforbidden(files) |
|
133 | 135 | |
|
134 | 136 | # if this is changed to support newlines in filenames, |
|
135 | 137 | # be sure to check the templates/ dir again (especially *-raw.tmpl) |
|
136 | 138 | hex, flags = revlog.hex, map.flags |
|
137 | 139 | text = ''.join("%s\000%s%s\n" % (f, hex(map[f]), flags(f)) |
|
138 | 140 | for f in files) |
|
139 | 141 | arraytext = array.array('c', text) |
|
140 | 142 | cachedelta = None |
|
141 | 143 | else: |
|
142 | 144 | added, removed = changed |
|
143 | 145 | addlist = self._mancache[2] |
|
144 | 146 | |
|
145 | 147 | checkforbidden(added) |
|
146 | 148 | # combine the changed lists into one list for sorting |
|
147 | 149 | work = [(x, False) for x in added] |
|
148 | 150 | work.extend((x, True) for x in removed) |
|
149 | 151 | # this could use heapq.merge() (from python2.6+) or equivalent |
|
150 | 152 | # since the lists are already sorted |
|
151 | 153 | work.sort() |
|
152 | 154 | |
|
153 | 155 | delta = [] |
|
154 | 156 | dstart = None |
|
155 | 157 | dend = None |
|
156 | 158 | dline = [""] |
|
157 | 159 | start = 0 |
|
158 | 160 | # zero copy representation of addlist as a buffer |
|
159 | 161 | addbuf = buffer(addlist) |
|
160 | 162 | |
|
161 | 163 | # start with a readonly loop that finds the offset of |
|
162 | 164 | # each line and creates the deltas |
|
163 | 165 | for f, todelete in work: |
|
164 | 166 | # bs will either be the index of the item or the insert point |
|
165 | 167 | start, end = self._search(addbuf, f, start) |
|
166 | 168 | if not todelete: |
|
167 | 169 | l = "%s\000%s%s\n" % (f, revlog.hex(map[f]), map.flags(f)) |
|
168 | 170 | else: |
|
169 | 171 | if start == end: |
|
170 | 172 | # item we want to delete was not found, error out |
|
171 | 173 | raise AssertionError( |
|
172 | 174 | _("failed to remove %s from manifest") % f) |
|
173 | 175 | l = "" |
|
174 | 176 | if dstart != None and dstart <= start and dend >= start: |
|
175 | 177 | if dend < end: |
|
176 | 178 | dend = end |
|
177 | 179 | if l: |
|
178 | 180 | dline.append(l) |
|
179 | 181 | else: |
|
180 | 182 | if dstart != None: |
|
181 | 183 | delta.append([dstart, dend, "".join(dline)]) |
|
182 | 184 | dstart = start |
|
183 | 185 | dend = end |
|
184 | 186 | dline = [l] |
|
185 | 187 | |
|
186 | 188 | if dstart != None: |
|
187 | 189 | delta.append([dstart, dend, "".join(dline)]) |
|
188 | 190 | # apply the delta to the addlist, and get a delta for addrevision |
|
189 | 191 | cachedelta = addlistdelta(addlist, delta) |
|
190 | 192 | |
|
191 | 193 | # the delta is only valid if we've been processing the tip revision |
|
192 | 194 | if p1 != self.tip(): |
|
193 | 195 | cachedelta = None |
|
194 | 196 | arraytext = addlist |
|
195 | 197 | text = buffer(arraytext) |
|
196 | 198 | |
|
197 | 199 | n = self.addrevision(text, transaction, link, p1, p2, cachedelta) |
|
198 | 200 | self._mancache = (n, map, arraytext) |
|
199 | 201 | |
|
200 | 202 | return n |
General Comments 0
You need to be logged in to leave comments.
Login now