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