##// END OF EJS Templates
unionrepo: fix mismatches with revlog classes...
Matt Harbison -
r52768:8315175f default
parent child Browse files
Show More
@@ -1,360 +1,366
1 1 # unionrepo.py - repository class for viewing union of repository changesets
2 2 #
3 3 # Derived from bundlerepo.py
4 4 # Copyright 2006, 2007 Benoit Boissinot <bboissin@gmail.com>
5 5 # Copyright 2013 Unity Technologies, Mads Kiilerich <madski@unity3d.com>
6 6 #
7 7 # This software may be used and distributed according to the terms of the
8 8 # GNU General Public License version 2 or any later version.
9 9
10 10 """Repository class for "in-memory pull" of one local repository to another,
11 11 allowing operations like diff and log with revsets.
12 12 """
13 13
14 14 from __future__ import annotations
15 15
16 16 import contextlib
17 17 import typing
18 18
19 19 from .i18n import _
20 20
21 21 from . import (
22 22 changelog,
23 23 cmdutil,
24 24 encoding,
25 25 error,
26 26 filelog,
27 27 localrepo,
28 28 manifest,
29 29 mdiff,
30 30 pathutil,
31 31 revlog,
32 32 util,
33 33 vfs as vfsmod,
34 34 )
35 35
36 36 from .revlogutils import (
37 37 constants as revlog_constants,
38 38 )
39 39
40 40
41 41 class unionrevlog(revlog.revlog):
42 def __init__(self, opener, radix, revlog2, linkmapper):
42 def __init__(self, opener: typing.Any, radix, revlog2, linkmapper):
43 # TODO: figure out real type of opener
44 #
43 45 # How it works:
44 46 # To retrieve a revision, we just need to know the node id so we can
45 47 # look it up in revlog2.
46 48 #
47 49 # To differentiate a rev in the second revlog from a rev in the revlog,
48 50 # we check revision against repotiprev.
49 51 opener = vfsmod.readonlyvfs(opener)
50 52 target = getattr(revlog2, 'target', None)
51 53 if target is None:
54 # Help pytype- changelog and revlog are not possible here because
55 # they both have a 'target' attr.
56 assert not isinstance(revlog2, (changelog.changelog, revlog.revlog))
57
52 58 # a revlog wrapper, eg: the manifestlog that is not an actual revlog
53 59 target = revlog2._revlog.target
54 60 revlog.revlog.__init__(self, opener, target=target, radix=radix)
55 61 self.revlog2 = revlog2
56 62
57 63 n = len(self)
58 64 self.repotiprev = n - 1
59 65 self.bundlerevs = set() # used by 'bundle()' revset expression
60 66 for rev2 in self.revlog2:
61 67 rev = self.revlog2.index[rev2]
62 68 # rev numbers - in revlog2, very different from self.rev
63 69 (
64 70 _start,
65 71 _csize,
66 72 rsize,
67 73 base,
68 74 linkrev,
69 75 p1rev,
70 76 p2rev,
71 77 node,
72 78 _sdo,
73 79 _sds,
74 80 _dcm,
75 81 _sdcm,
76 82 rank,
77 83 ) = rev
78 84 flags = _start & 0xFFFF
79 85
80 86 if linkmapper is None: # link is to same revlog
81 87 assert linkrev == rev2 # we never link back
82 88 link = n
83 89 else: # rev must be mapped from repo2 cl to unified cl by linkmapper
84 90 link = linkmapper(linkrev)
85 91
86 92 if linkmapper is not None: # link is to same revlog
87 93 base = linkmapper(base)
88 94
89 95 this_rev = self.index.get_rev(node)
90 96 if this_rev is not None:
91 97 # this happens for the common revlog revisions
92 98 self.bundlerevs.add(this_rev)
93 99 continue
94 100
95 101 p1node = self.revlog2.node(p1rev)
96 102 p2node = self.revlog2.node(p2rev)
97 103
98 104 # TODO: it's probably wrong to set compressed length to -1, but
99 105 # I have no idea if csize is valid in the base revlog context.
100 106 e = (
101 107 flags,
102 108 -1,
103 109 rsize,
104 110 base,
105 111 link,
106 112 self.rev(p1node),
107 113 self.rev(p2node),
108 114 node,
109 115 0, # sidedata offset
110 116 0, # sidedata size
111 117 revlog_constants.COMP_MODE_INLINE,
112 118 revlog_constants.COMP_MODE_INLINE,
113 119 rank,
114 120 )
115 121 self.index.append(e)
116 122 self.bundlerevs.add(n)
117 123 n += 1
118 124
119 125 @contextlib.contextmanager
120 126 def reading(self):
121 127 if 0 <= len(self.bundlerevs) < len(self.index):
122 128 read_1 = super().reading
123 129 else:
124 130 read_1 = util.nullcontextmanager
125 131 if 0 < len(self.bundlerevs):
126 132 read_2 = self.revlog2.reading
127 133 else:
128 134 read_2 = util.nullcontextmanager
129 135 with read_1(), read_2():
130 136 yield
131 137
132 138 def _chunk(self, rev):
133 139 if rev <= self.repotiprev:
134 return revlog.revlog._chunk(self, rev)
140 return super(unionrevlog, self)._inner._chunk(rev)
135 141 return self.revlog2._chunk(self.node(rev))
136 142
137 143 def revdiff(self, rev1, rev2):
138 144 """return or calculate a delta between two revisions"""
139 145 if rev1 > self.repotiprev and rev2 > self.repotiprev:
140 146 return self.revlog2.revdiff(
141 147 self.revlog2.rev(self.node(rev1)),
142 148 self.revlog2.rev(self.node(rev2)),
143 149 )
144 150 elif rev1 <= self.repotiprev and rev2 <= self.repotiprev:
145 151 return super(unionrevlog, self).revdiff(rev1, rev2)
146 152
147 153 return mdiff.textdiff(self.rawdata(rev1), self.rawdata(rev2))
148 154
149 155 def _revisiondata(self, nodeorrev, raw=False):
150 156 if isinstance(nodeorrev, int):
151 157 rev = nodeorrev
152 158 node = self.node(rev)
153 159 else:
154 160 node = nodeorrev
155 161 rev = self.rev(node)
156 162
157 163 if rev > self.repotiprev:
158 164 # work around manifestrevlog NOT being a revlog
159 165 revlog2 = getattr(self.revlog2, '_revlog', self.revlog2)
160 166 func = revlog2._revisiondata
161 167 else:
162 168 func = super(unionrevlog, self)._revisiondata
163 169 return func(node, raw=raw)
164 170
165 171 def addrevision(
166 172 self,
167 173 text,
168 174 transaction,
169 175 link,
170 176 p1,
171 177 p2,
172 178 cachedelta=None,
173 179 node=None,
174 180 flags=revlog.REVIDX_DEFAULT_FLAGS,
175 181 deltacomputer=None,
176 182 sidedata=None,
177 183 ):
178 184 raise NotImplementedError
179 185
180 186 def addgroup(
181 187 self,
182 188 deltas,
183 189 linkmapper,
184 190 transaction,
185 191 alwayscache=False,
186 192 addrevisioncb=None,
187 193 duplicaterevisioncb=None,
188 194 debug_info=None,
189 195 delta_base_reuse_policy=None,
190 196 ):
191 197 raise NotImplementedError
192 198
193 199 def strip(self, minlink, transaction):
194 200 raise NotImplementedError
195 201
196 202 def checksize(self):
197 203 raise NotImplementedError
198 204
199 205
200 206 class unionchangelog(unionrevlog, changelog.changelog):
201 207 def __init__(self, opener, opener2):
202 208 changelog.changelog.__init__(self, opener)
203 209 linkmapper = None
204 210 changelog2 = changelog.changelog(opener2)
205 211 unionrevlog.__init__(self, opener, self.radix, changelog2, linkmapper)
206 212
207 213
208 214 class unionmanifest(unionrevlog, manifest.manifestrevlog):
209 215 repotiprev: int
210 216 revlog2: manifest.ManifestRevlog
211 217
212 218 def __init__(self, nodeconstants, opener, opener2, linkmapper):
213 219 # XXX manifestrevlog is not actually a revlog , so mixing it with
214 220 # bundlerevlog is not a good idea.
215 221 manifest.manifestrevlog.__init__(self, nodeconstants, opener)
216 222 manifest2 = manifest.manifestrevlog(nodeconstants, opener2)
217 223 unionrevlog.__init__(
218 224 self, opener, self._revlog.radix, manifest2, linkmapper
219 225 )
220 226
221 227
222 228 class unionfilelog(filelog.filelog):
223 229 _revlog: unionrevlog
224 230 repotiprev: int
225 231 revlog2: revlog.revlog
226 232
227 233 def __init__(self, opener, path, opener2, linkmapper, repo):
228 234 filelog.filelog.__init__(self, opener, path)
229 235 filelog2 = filelog.filelog(opener2, path)
230 236 self._revlog = unionrevlog(
231 237 opener, self._revlog.radix, filelog2._revlog, linkmapper
232 238 )
233 239 self._repo = repo
234 240 self.repotiprev = self._revlog.repotiprev
235 241 self.revlog2 = self._revlog.revlog2
236 242
237 243 def iscensored(self, rev):
238 244 """Check if a revision is censored."""
239 245 if rev <= self.repotiprev:
240 246 return filelog.filelog.iscensored(self, rev)
241 247 node = self.node(rev)
242 248 return self.revlog2.iscensored(self.revlog2.rev(node))
243 249
244 250
245 251 class unionpeer(localrepo.localpeer):
246 252 def canpush(self):
247 253 return False
248 254
249 255
250 256 _union_repo_baseclass = object
251 257
252 258 if typing.TYPE_CHECKING:
253 259 _union_repo_baseclass = localrepo.localrepository
254 260
255 261
256 262 class unionrepository(_union_repo_baseclass):
257 263 """Represents the union of data in 2 repositories.
258 264
259 265 Instances are not usable if constructed directly. Use ``instance()``
260 266 or ``makeunionrepository()`` to create a usable instance.
261 267 """
262 268
263 269 # noinspection PyMissingConstructor
264 270 def __init__(self, repo2, url):
265 271 self.repo2 = repo2
266 272 self._url = url
267 273
268 274 self.ui.setconfig(b'phases', b'publish', False, b'unionrepo')
269 275
270 276 @localrepo.unfilteredpropertycache
271 277 def changelog(self):
272 278 return unionchangelog(self.svfs, self.repo2.svfs)
273 279
274 280 @localrepo.unfilteredpropertycache
275 281 def manifestlog(self):
276 282 rootstore = unionmanifest(
277 283 self.nodeconstants,
278 284 self.svfs,
279 285 self.repo2.svfs,
280 286 self.unfiltered()._clrev,
281 287 )
282 288 return manifest.manifestlog(
283 289 self.svfs, self, rootstore, self.narrowmatch()
284 290 )
285 291
286 292 def _clrev(self, rev2):
287 293 """map from repo2 changelog rev to temporary rev in self.changelog"""
288 294 node = self.repo2.changelog.node(rev2)
289 295 return self.changelog.rev(node)
290 296
291 297 def url(self):
292 298 return self._url
293 299
294 300 def file(self, f):
295 301 return unionfilelog(
296 302 self.svfs, f, self.repo2.svfs, self.unfiltered()._clrev, self
297 303 )
298 304
299 305 def close(self):
300 306 self.repo2.close()
301 307
302 308 def cancopy(self):
303 309 return False
304 310
305 311 def peer(self, path=None, remotehidden=False):
306 312 return unionpeer(self, path=None, remotehidden=remotehidden)
307 313
308 314 def getcwd(self):
309 315 return encoding.getcwd() # always outside the repo
310 316
311 317
312 318 def instance(ui, path, create, intents=None, createopts=None):
313 319 if create:
314 320 raise error.Abort(_(b'cannot create new union repository'))
315 321 parentpath = ui.config(b"bundle", b"mainreporoot")
316 322 if not parentpath:
317 323 # try to find the correct path to the working directory repo
318 324 parentpath = cmdutil.findrepo(encoding.getcwd())
319 325 if parentpath is None:
320 326 parentpath = b''
321 327 if parentpath:
322 328 # Try to make the full path relative so we get a nice, short URL.
323 329 # In particular, we don't want temp dir names in test outputs.
324 330 cwd = encoding.getcwd()
325 331 if parentpath == cwd:
326 332 parentpath = b''
327 333 else:
328 334 cwd = pathutil.normasprefix(cwd)
329 335 if parentpath.startswith(cwd):
330 336 parentpath = parentpath[len(cwd) :]
331 337 if path.startswith(b'union:'):
332 338 s = path.split(b":", 1)[1].split(b"+", 1)
333 339 if len(s) == 1:
334 340 repopath, repopath2 = parentpath, s[0]
335 341 else:
336 342 repopath, repopath2 = s
337 343 else:
338 344 repopath, repopath2 = parentpath, path
339 345
340 346 return makeunionrepository(ui, repopath, repopath2)
341 347
342 348
343 349 def makeunionrepository(ui, repopath1, repopath2):
344 350 """Make a union repository object from 2 local repo paths."""
345 351 repo1 = localrepo.instance(ui, repopath1, create=False)
346 352 repo2 = localrepo.instance(ui, repopath2, create=False)
347 353
348 354 url = b'union:%s+%s' % (
349 355 util.expandpath(repopath1),
350 356 util.expandpath(repopath2),
351 357 )
352 358
353 359 class derivedunionrepository(unionrepository, repo1.__class__):
354 360 pass
355 361
356 362 repo = repo1
357 363 repo.__class__ = derivedunionrepository
358 364 unionrepository.__init__(repo1, repo2, url)
359 365
360 366 return repo
General Comments 0
You need to be logged in to leave comments. Login now