Show More
@@ -1,303 +1,303 b'' | |||
|
1 | 1 | # bundlerepo.py - repository class for viewing uncompressed bundles |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006, 2007 Benoit Boissinot <bboissin@gmail.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, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | """Repository class for viewing uncompressed bundles. |
|
9 | 9 | |
|
10 | 10 | This provides a read-only repository interface to bundles as if they |
|
11 | 11 | were part of the actual repository. |
|
12 | 12 | """ |
|
13 | 13 | |
|
14 | 14 | from node import nullid |
|
15 | 15 | from i18n import _ |
|
16 | 16 | import os, struct, bz2, zlib, tempfile, shutil |
|
17 | 17 | import changegroup, util, mdiff |
|
18 | 18 | import localrepo, changelog, manifest, filelog, revlog, error |
|
19 | 19 | |
|
20 | 20 | class bundlerevlog(revlog.revlog): |
|
21 | 21 | def __init__(self, opener, indexfile, bundlefile, |
|
22 | 22 | linkmapper=None): |
|
23 | 23 | # How it works: |
|
24 | 24 | # to retrieve a revision, we need to know the offset of |
|
25 | 25 | # the revision in the bundlefile (an opened file). |
|
26 | 26 | # |
|
27 | 27 | # We store this offset in the index (start), to differentiate a |
|
28 | 28 | # rev in the bundle and from a rev in the revlog, we check |
|
29 | 29 | # len(index[r]). If the tuple is bigger than 7, it is a bundle |
|
30 | 30 | # (it is bigger since we store the node to which the delta is) |
|
31 | 31 | # |
|
32 | 32 | revlog.revlog.__init__(self, opener, indexfile) |
|
33 | 33 | self.bundlefile = bundlefile |
|
34 | 34 | self.basemap = {} |
|
35 | 35 | def chunkpositer(): |
|
36 | 36 | for chunk in changegroup.chunkiter(bundlefile): |
|
37 | 37 | pos = bundlefile.tell() |
|
38 | 38 | yield chunk, pos - len(chunk) |
|
39 | 39 | n = len(self) |
|
40 | 40 | prev = None |
|
41 | 41 | for chunk, start in chunkpositer(): |
|
42 | 42 | size = len(chunk) |
|
43 | 43 | if size < 80: |
|
44 | 44 | raise util.Abort(_("invalid changegroup")) |
|
45 | 45 | start += 80 |
|
46 | 46 | size -= 80 |
|
47 | 47 | node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80]) |
|
48 | 48 | if node in self.nodemap: |
|
49 | 49 | prev = node |
|
50 | 50 | continue |
|
51 | 51 | for p in (p1, p2): |
|
52 | 52 | if not p in self.nodemap: |
|
53 | 53 | raise error.LookupError(p, self.indexfile, |
|
54 | 54 | _("unknown parent")) |
|
55 | 55 | if linkmapper is None: |
|
56 | 56 | link = n |
|
57 | 57 | else: |
|
58 | 58 | link = linkmapper(cs) |
|
59 | 59 | |
|
60 | 60 | if not prev: |
|
61 | 61 | prev = p1 |
|
62 | 62 | # start, size, full unc. size, base (unused), link, p1, p2, node |
|
63 | 63 | e = (revlog.offset_type(start, 0), size, -1, -1, link, |
|
64 | 64 | self.rev(p1), self.rev(p2), node) |
|
65 | 65 | self.basemap[n] = prev |
|
66 | 66 | self.index.insert(-1, e) |
|
67 | 67 | self.nodemap[node] = n |
|
68 | 68 | prev = node |
|
69 | 69 | n += 1 |
|
70 | 70 | |
|
71 | 71 | def bundle(self, rev): |
|
72 | 72 | """is rev from the bundle""" |
|
73 | 73 | if rev < 0: |
|
74 | 74 | return False |
|
75 | 75 | return rev in self.basemap |
|
76 | 76 | def bundlebase(self, rev): return self.basemap[rev] |
|
77 |
def chunk(self, rev |
|
|
77 | def _chunk(self, rev): | |
|
78 | 78 | # Warning: in case of bundle, the diff is against bundlebase, |
|
79 | 79 | # not against rev - 1 |
|
80 | 80 | # XXX: could use some caching |
|
81 | 81 | if not self.bundle(rev): |
|
82 |
return revlog.revlog.chunk(self, rev |
|
|
82 | return revlog.revlog._chunk(self, rev) | |
|
83 | 83 | self.bundlefile.seek(self.start(rev)) |
|
84 | 84 | return self.bundlefile.read(self.length(rev)) |
|
85 | 85 | |
|
86 | 86 | def revdiff(self, rev1, rev2): |
|
87 | 87 | """return or calculate a delta between two revisions""" |
|
88 | 88 | if self.bundle(rev1) and self.bundle(rev2): |
|
89 | 89 | # hot path for bundle |
|
90 | 90 | revb = self.rev(self.bundlebase(rev2)) |
|
91 | 91 | if revb == rev1: |
|
92 | return self.chunk(rev2) | |
|
92 | return self._chunk(rev2) | |
|
93 | 93 | elif not self.bundle(rev1) and not self.bundle(rev2): |
|
94 | 94 | return revlog.revlog.revdiff(self, rev1, rev2) |
|
95 | 95 | |
|
96 | 96 | return mdiff.textdiff(self.revision(self.node(rev1)), |
|
97 | 97 | self.revision(self.node(rev2))) |
|
98 | 98 | |
|
99 | 99 | def revision(self, node): |
|
100 | 100 | """return an uncompressed revision of a given""" |
|
101 | 101 | if node == nullid: return "" |
|
102 | 102 | |
|
103 | 103 | text = None |
|
104 | 104 | chain = [] |
|
105 | 105 | iter_node = node |
|
106 | 106 | rev = self.rev(iter_node) |
|
107 | 107 | # reconstruct the revision if it is from a changegroup |
|
108 | 108 | while self.bundle(rev): |
|
109 | 109 | if self._cache and self._cache[0] == iter_node: |
|
110 | 110 | text = self._cache[2] |
|
111 | 111 | break |
|
112 | 112 | chain.append(rev) |
|
113 | 113 | iter_node = self.bundlebase(rev) |
|
114 | 114 | rev = self.rev(iter_node) |
|
115 | 115 | if text is None: |
|
116 | 116 | text = revlog.revlog.revision(self, iter_node) |
|
117 | 117 | |
|
118 | 118 | while chain: |
|
119 | delta = self.chunk(chain.pop()) | |
|
119 | delta = self._chunk(chain.pop()) | |
|
120 | 120 | text = mdiff.patches(text, [delta]) |
|
121 | 121 | |
|
122 | 122 | p1, p2 = self.parents(node) |
|
123 | 123 | if node != revlog.hash(text, p1, p2): |
|
124 | 124 | raise error.RevlogError(_("integrity check failed on %s:%d") |
|
125 | 125 | % (self.datafile, self.rev(node))) |
|
126 | 126 | |
|
127 | 127 | self._cache = (node, self.rev(node), text) |
|
128 | 128 | return text |
|
129 | 129 | |
|
130 | 130 | def addrevision(self, text, transaction, link, p1=None, p2=None, d=None): |
|
131 | 131 | raise NotImplementedError |
|
132 | 132 | def addgroup(self, revs, linkmapper, transaction): |
|
133 | 133 | raise NotImplementedError |
|
134 | 134 | def strip(self, rev, minlink): |
|
135 | 135 | raise NotImplementedError |
|
136 | 136 | def checksize(self): |
|
137 | 137 | raise NotImplementedError |
|
138 | 138 | |
|
139 | 139 | class bundlechangelog(bundlerevlog, changelog.changelog): |
|
140 | 140 | def __init__(self, opener, bundlefile): |
|
141 | 141 | changelog.changelog.__init__(self, opener) |
|
142 | 142 | bundlerevlog.__init__(self, opener, self.indexfile, bundlefile) |
|
143 | 143 | |
|
144 | 144 | class bundlemanifest(bundlerevlog, manifest.manifest): |
|
145 | 145 | def __init__(self, opener, bundlefile, linkmapper): |
|
146 | 146 | manifest.manifest.__init__(self, opener) |
|
147 | 147 | bundlerevlog.__init__(self, opener, self.indexfile, bundlefile, |
|
148 | 148 | linkmapper) |
|
149 | 149 | |
|
150 | 150 | class bundlefilelog(bundlerevlog, filelog.filelog): |
|
151 | 151 | def __init__(self, opener, path, bundlefile, linkmapper): |
|
152 | 152 | filelog.filelog.__init__(self, opener, path) |
|
153 | 153 | bundlerevlog.__init__(self, opener, self.indexfile, bundlefile, |
|
154 | 154 | linkmapper) |
|
155 | 155 | |
|
156 | 156 | class bundlerepository(localrepo.localrepository): |
|
157 | 157 | def __init__(self, ui, path, bundlename): |
|
158 | 158 | self._tempparent = None |
|
159 | 159 | try: |
|
160 | 160 | localrepo.localrepository.__init__(self, ui, path) |
|
161 | 161 | except error.RepoError: |
|
162 | 162 | self._tempparent = tempfile.mkdtemp() |
|
163 | 163 | localrepo.instance(ui, self._tempparent, 1) |
|
164 | 164 | localrepo.localrepository.__init__(self, ui, self._tempparent) |
|
165 | 165 | |
|
166 | 166 | if path: |
|
167 | 167 | self._url = 'bundle:' + path + '+' + bundlename |
|
168 | 168 | else: |
|
169 | 169 | self._url = 'bundle:' + bundlename |
|
170 | 170 | |
|
171 | 171 | self.tempfile = None |
|
172 | 172 | self.bundlefile = open(bundlename, "rb") |
|
173 | 173 | header = self.bundlefile.read(6) |
|
174 | 174 | if not header.startswith("HG"): |
|
175 | 175 | raise util.Abort(_("%s: not a Mercurial bundle file") % bundlename) |
|
176 | 176 | elif not header.startswith("HG10"): |
|
177 | 177 | raise util.Abort(_("%s: unknown bundle version") % bundlename) |
|
178 | 178 | elif (header == "HG10BZ") or (header == "HG10GZ"): |
|
179 | 179 | fdtemp, temp = tempfile.mkstemp(prefix="hg-bundle-", |
|
180 | 180 | suffix=".hg10un", dir=self.path) |
|
181 | 181 | self.tempfile = temp |
|
182 | 182 | fptemp = os.fdopen(fdtemp, 'wb') |
|
183 | 183 | def generator(f): |
|
184 | 184 | if header == "HG10BZ": |
|
185 | 185 | zd = bz2.BZ2Decompressor() |
|
186 | 186 | zd.decompress("BZ") |
|
187 | 187 | elif header == "HG10GZ": |
|
188 | 188 | zd = zlib.decompressobj() |
|
189 | 189 | for chunk in f: |
|
190 | 190 | yield zd.decompress(chunk) |
|
191 | 191 | gen = generator(util.filechunkiter(self.bundlefile, 4096)) |
|
192 | 192 | |
|
193 | 193 | try: |
|
194 | 194 | fptemp.write("HG10UN") |
|
195 | 195 | for chunk in gen: |
|
196 | 196 | fptemp.write(chunk) |
|
197 | 197 | finally: |
|
198 | 198 | fptemp.close() |
|
199 | 199 | self.bundlefile.close() |
|
200 | 200 | |
|
201 | 201 | self.bundlefile = open(self.tempfile, "rb") |
|
202 | 202 | # seek right after the header |
|
203 | 203 | self.bundlefile.seek(6) |
|
204 | 204 | elif header == "HG10UN": |
|
205 | 205 | # nothing to do |
|
206 | 206 | pass |
|
207 | 207 | else: |
|
208 | 208 | raise util.Abort(_("%s: unknown bundle compression type") |
|
209 | 209 | % bundlename) |
|
210 | 210 | # dict with the mapping 'filename' -> position in the bundle |
|
211 | 211 | self.bundlefilespos = {} |
|
212 | 212 | |
|
213 | 213 | @util.propertycache |
|
214 | 214 | def changelog(self): |
|
215 | 215 | c = bundlechangelog(self.sopener, self.bundlefile) |
|
216 | 216 | self.manstart = self.bundlefile.tell() |
|
217 | 217 | return c |
|
218 | 218 | |
|
219 | 219 | @util.propertycache |
|
220 | 220 | def manifest(self): |
|
221 | 221 | self.bundlefile.seek(self.manstart) |
|
222 | 222 | m = bundlemanifest(self.sopener, self.bundlefile, self.changelog.rev) |
|
223 | 223 | self.filestart = self.bundlefile.tell() |
|
224 | 224 | return m |
|
225 | 225 | |
|
226 | 226 | @util.propertycache |
|
227 | 227 | def manstart(self): |
|
228 | 228 | self.changelog |
|
229 | 229 | return self.manstart |
|
230 | 230 | |
|
231 | 231 | @util.propertycache |
|
232 | 232 | def filestart(self): |
|
233 | 233 | self.manifest |
|
234 | 234 | return self.filestart |
|
235 | 235 | |
|
236 | 236 | def url(self): |
|
237 | 237 | return self._url |
|
238 | 238 | |
|
239 | 239 | def file(self, f): |
|
240 | 240 | if not self.bundlefilespos: |
|
241 | 241 | self.bundlefile.seek(self.filestart) |
|
242 | 242 | while 1: |
|
243 | 243 | chunk = changegroup.getchunk(self.bundlefile) |
|
244 | 244 | if not chunk: |
|
245 | 245 | break |
|
246 | 246 | self.bundlefilespos[chunk] = self.bundlefile.tell() |
|
247 | 247 | for c in changegroup.chunkiter(self.bundlefile): |
|
248 | 248 | pass |
|
249 | 249 | |
|
250 | 250 | if f[0] == '/': |
|
251 | 251 | f = f[1:] |
|
252 | 252 | if f in self.bundlefilespos: |
|
253 | 253 | self.bundlefile.seek(self.bundlefilespos[f]) |
|
254 | 254 | return bundlefilelog(self.sopener, f, self.bundlefile, |
|
255 | 255 | self.changelog.rev) |
|
256 | 256 | else: |
|
257 | 257 | return filelog.filelog(self.sopener, f) |
|
258 | 258 | |
|
259 | 259 | def close(self): |
|
260 | 260 | """Close assigned bundle file immediately.""" |
|
261 | 261 | self.bundlefile.close() |
|
262 | 262 | |
|
263 | 263 | def __del__(self): |
|
264 | 264 | bundlefile = getattr(self, 'bundlefile', None) |
|
265 | 265 | if bundlefile and not bundlefile.closed: |
|
266 | 266 | bundlefile.close() |
|
267 | 267 | tempfile = getattr(self, 'tempfile', None) |
|
268 | 268 | if tempfile is not None: |
|
269 | 269 | os.unlink(tempfile) |
|
270 | 270 | if self._tempparent: |
|
271 | 271 | shutil.rmtree(self._tempparent, True) |
|
272 | 272 | |
|
273 | 273 | def cancopy(self): |
|
274 | 274 | return False |
|
275 | 275 | |
|
276 | 276 | def getcwd(self): |
|
277 | 277 | return os.getcwd() # always outside the repo |
|
278 | 278 | |
|
279 | 279 | def instance(ui, path, create): |
|
280 | 280 | if create: |
|
281 | 281 | raise util.Abort(_('cannot create new bundle repository')) |
|
282 | 282 | parentpath = ui.config("bundle", "mainreporoot", "") |
|
283 | 283 | if parentpath: |
|
284 | 284 | # Try to make the full path relative so we get a nice, short URL. |
|
285 | 285 | # In particular, we don't want temp dir names in test outputs. |
|
286 | 286 | cwd = os.getcwd() |
|
287 | 287 | if parentpath == cwd: |
|
288 | 288 | parentpath = '' |
|
289 | 289 | else: |
|
290 | 290 | cwd = os.path.join(cwd,'') |
|
291 | 291 | if parentpath.startswith(cwd): |
|
292 | 292 | parentpath = parentpath[len(cwd):] |
|
293 | 293 | path = util.drop_scheme('file', path) |
|
294 | 294 | if path.startswith('bundle:'): |
|
295 | 295 | path = util.drop_scheme('bundle', path) |
|
296 | 296 | s = path.split("+", 1) |
|
297 | 297 | if len(s) == 1: |
|
298 | 298 | repopath, bundlename = parentpath, s[0] |
|
299 | 299 | else: |
|
300 | 300 | repopath, bundlename = s |
|
301 | 301 | else: |
|
302 | 302 | repopath, bundlename = parentpath, path |
|
303 | 303 | return bundlerepository(ui, repopath, bundlename) |
General Comments 0
You need to be logged in to leave comments.
Login now