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