##// END OF EJS Templates
revlog: skip opener options to pass sparse_revlog value...
marmoute -
r51936:8ed03f77 default
parent child Browse files
Show More
@@ -1,4047 +1,4047 b''
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 # coding: utf-8
2 # coding: utf-8
3 #
3 #
4 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
4 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9
9
10 import functools
10 import functools
11 import os
11 import os
12 import random
12 import random
13 import re
13 import re
14 import sys
14 import sys
15 import time
15 import time
16 import weakref
16 import weakref
17
17
18 from concurrent import futures
18 from concurrent import futures
19 from typing import (
19 from typing import (
20 Optional,
20 Optional,
21 )
21 )
22
22
23 from .i18n import _
23 from .i18n import _
24 from .node import (
24 from .node import (
25 bin,
25 bin,
26 hex,
26 hex,
27 nullrev,
27 nullrev,
28 sha1nodeconstants,
28 sha1nodeconstants,
29 short,
29 short,
30 )
30 )
31 from . import (
31 from . import (
32 bookmarks,
32 bookmarks,
33 branchmap,
33 branchmap,
34 bundle2,
34 bundle2,
35 bundlecaches,
35 bundlecaches,
36 changegroup,
36 changegroup,
37 color,
37 color,
38 commit,
38 commit,
39 context,
39 context,
40 dirstate,
40 dirstate,
41 discovery,
41 discovery,
42 encoding,
42 encoding,
43 error,
43 error,
44 exchange,
44 exchange,
45 extensions,
45 extensions,
46 filelog,
46 filelog,
47 hook,
47 hook,
48 lock as lockmod,
48 lock as lockmod,
49 match as matchmod,
49 match as matchmod,
50 mergestate as mergestatemod,
50 mergestate as mergestatemod,
51 mergeutil,
51 mergeutil,
52 namespaces,
52 namespaces,
53 narrowspec,
53 narrowspec,
54 obsolete,
54 obsolete,
55 pathutil,
55 pathutil,
56 phases,
56 phases,
57 policy,
57 policy,
58 pushkey,
58 pushkey,
59 pycompat,
59 pycompat,
60 rcutil,
60 rcutil,
61 repoview,
61 repoview,
62 requirements as requirementsmod,
62 requirements as requirementsmod,
63 revlog,
63 revlog,
64 revset,
64 revset,
65 revsetlang,
65 revsetlang,
66 scmutil,
66 scmutil,
67 sparse,
67 sparse,
68 store as storemod,
68 store as storemod,
69 subrepoutil,
69 subrepoutil,
70 tags as tagsmod,
70 tags as tagsmod,
71 transaction,
71 transaction,
72 txnutil,
72 txnutil,
73 util,
73 util,
74 vfs as vfsmod,
74 vfs as vfsmod,
75 wireprototypes,
75 wireprototypes,
76 )
76 )
77
77
78 from .interfaces import (
78 from .interfaces import (
79 repository,
79 repository,
80 util as interfaceutil,
80 util as interfaceutil,
81 )
81 )
82
82
83 from .utils import (
83 from .utils import (
84 hashutil,
84 hashutil,
85 procutil,
85 procutil,
86 stringutil,
86 stringutil,
87 urlutil,
87 urlutil,
88 )
88 )
89
89
90 from .revlogutils import (
90 from .revlogutils import (
91 concurrency_checker as revlogchecker,
91 concurrency_checker as revlogchecker,
92 constants as revlogconst,
92 constants as revlogconst,
93 sidedata as sidedatamod,
93 sidedata as sidedatamod,
94 )
94 )
95
95
96 release = lockmod.release
96 release = lockmod.release
97 urlerr = util.urlerr
97 urlerr = util.urlerr
98 urlreq = util.urlreq
98 urlreq = util.urlreq
99
99
100 RE_SKIP_DIRSTATE_ROLLBACK = re.compile(
100 RE_SKIP_DIRSTATE_ROLLBACK = re.compile(
101 b"^((dirstate|narrowspec.dirstate).*|branch$)"
101 b"^((dirstate|narrowspec.dirstate).*|branch$)"
102 )
102 )
103
103
104 # set of (path, vfs-location) tuples. vfs-location is:
104 # set of (path, vfs-location) tuples. vfs-location is:
105 # - 'plain for vfs relative paths
105 # - 'plain for vfs relative paths
106 # - '' for svfs relative paths
106 # - '' for svfs relative paths
107 _cachedfiles = set()
107 _cachedfiles = set()
108
108
109
109
110 class _basefilecache(scmutil.filecache):
110 class _basefilecache(scmutil.filecache):
111 """All filecache usage on repo are done for logic that should be unfiltered"""
111 """All filecache usage on repo are done for logic that should be unfiltered"""
112
112
113 def __get__(self, repo, type=None):
113 def __get__(self, repo, type=None):
114 if repo is None:
114 if repo is None:
115 return self
115 return self
116 # proxy to unfiltered __dict__ since filtered repo has no entry
116 # proxy to unfiltered __dict__ since filtered repo has no entry
117 unfi = repo.unfiltered()
117 unfi = repo.unfiltered()
118 try:
118 try:
119 return unfi.__dict__[self.sname]
119 return unfi.__dict__[self.sname]
120 except KeyError:
120 except KeyError:
121 pass
121 pass
122 return super(_basefilecache, self).__get__(unfi, type)
122 return super(_basefilecache, self).__get__(unfi, type)
123
123
124 def set(self, repo, value):
124 def set(self, repo, value):
125 return super(_basefilecache, self).set(repo.unfiltered(), value)
125 return super(_basefilecache, self).set(repo.unfiltered(), value)
126
126
127
127
128 class repofilecache(_basefilecache):
128 class repofilecache(_basefilecache):
129 """filecache for files in .hg but outside of .hg/store"""
129 """filecache for files in .hg but outside of .hg/store"""
130
130
131 def __init__(self, *paths):
131 def __init__(self, *paths):
132 super(repofilecache, self).__init__(*paths)
132 super(repofilecache, self).__init__(*paths)
133 for path in paths:
133 for path in paths:
134 _cachedfiles.add((path, b'plain'))
134 _cachedfiles.add((path, b'plain'))
135
135
136 def join(self, obj, fname):
136 def join(self, obj, fname):
137 return obj.vfs.join(fname)
137 return obj.vfs.join(fname)
138
138
139
139
140 class storecache(_basefilecache):
140 class storecache(_basefilecache):
141 """filecache for files in the store"""
141 """filecache for files in the store"""
142
142
143 def __init__(self, *paths):
143 def __init__(self, *paths):
144 super(storecache, self).__init__(*paths)
144 super(storecache, self).__init__(*paths)
145 for path in paths:
145 for path in paths:
146 _cachedfiles.add((path, b''))
146 _cachedfiles.add((path, b''))
147
147
148 def join(self, obj, fname):
148 def join(self, obj, fname):
149 return obj.sjoin(fname)
149 return obj.sjoin(fname)
150
150
151
151
152 class changelogcache(storecache):
152 class changelogcache(storecache):
153 """filecache for the changelog"""
153 """filecache for the changelog"""
154
154
155 def __init__(self):
155 def __init__(self):
156 super(changelogcache, self).__init__()
156 super(changelogcache, self).__init__()
157 _cachedfiles.add((b'00changelog.i', b''))
157 _cachedfiles.add((b'00changelog.i', b''))
158 _cachedfiles.add((b'00changelog.n', b''))
158 _cachedfiles.add((b'00changelog.n', b''))
159
159
160 def tracked_paths(self, obj):
160 def tracked_paths(self, obj):
161 paths = [self.join(obj, b'00changelog.i')]
161 paths = [self.join(obj, b'00changelog.i')]
162 if obj.store.opener.options.get(b'persistent-nodemap', False):
162 if obj.store.opener.options.get(b'persistent-nodemap', False):
163 paths.append(self.join(obj, b'00changelog.n'))
163 paths.append(self.join(obj, b'00changelog.n'))
164 return paths
164 return paths
165
165
166
166
167 class manifestlogcache(storecache):
167 class manifestlogcache(storecache):
168 """filecache for the manifestlog"""
168 """filecache for the manifestlog"""
169
169
170 def __init__(self):
170 def __init__(self):
171 super(manifestlogcache, self).__init__()
171 super(manifestlogcache, self).__init__()
172 _cachedfiles.add((b'00manifest.i', b''))
172 _cachedfiles.add((b'00manifest.i', b''))
173 _cachedfiles.add((b'00manifest.n', b''))
173 _cachedfiles.add((b'00manifest.n', b''))
174
174
175 def tracked_paths(self, obj):
175 def tracked_paths(self, obj):
176 paths = [self.join(obj, b'00manifest.i')]
176 paths = [self.join(obj, b'00manifest.i')]
177 if obj.store.opener.options.get(b'persistent-nodemap', False):
177 if obj.store.opener.options.get(b'persistent-nodemap', False):
178 paths.append(self.join(obj, b'00manifest.n'))
178 paths.append(self.join(obj, b'00manifest.n'))
179 return paths
179 return paths
180
180
181
181
182 class mixedrepostorecache(_basefilecache):
182 class mixedrepostorecache(_basefilecache):
183 """filecache for a mix files in .hg/store and outside"""
183 """filecache for a mix files in .hg/store and outside"""
184
184
185 def __init__(self, *pathsandlocations):
185 def __init__(self, *pathsandlocations):
186 # scmutil.filecache only uses the path for passing back into our
186 # scmutil.filecache only uses the path for passing back into our
187 # join(), so we can safely pass a list of paths and locations
187 # join(), so we can safely pass a list of paths and locations
188 super(mixedrepostorecache, self).__init__(*pathsandlocations)
188 super(mixedrepostorecache, self).__init__(*pathsandlocations)
189 _cachedfiles.update(pathsandlocations)
189 _cachedfiles.update(pathsandlocations)
190
190
191 def join(self, obj, fnameandlocation):
191 def join(self, obj, fnameandlocation):
192 fname, location = fnameandlocation
192 fname, location = fnameandlocation
193 if location == b'plain':
193 if location == b'plain':
194 return obj.vfs.join(fname)
194 return obj.vfs.join(fname)
195 else:
195 else:
196 if location != b'':
196 if location != b'':
197 raise error.ProgrammingError(
197 raise error.ProgrammingError(
198 b'unexpected location: %s' % location
198 b'unexpected location: %s' % location
199 )
199 )
200 return obj.sjoin(fname)
200 return obj.sjoin(fname)
201
201
202
202
203 def isfilecached(repo, name):
203 def isfilecached(repo, name):
204 """check if a repo has already cached "name" filecache-ed property
204 """check if a repo has already cached "name" filecache-ed property
205
205
206 This returns (cachedobj-or-None, iscached) tuple.
206 This returns (cachedobj-or-None, iscached) tuple.
207 """
207 """
208 cacheentry = repo.unfiltered()._filecache.get(name, None)
208 cacheentry = repo.unfiltered()._filecache.get(name, None)
209 if not cacheentry:
209 if not cacheentry:
210 return None, False
210 return None, False
211 return cacheentry.obj, True
211 return cacheentry.obj, True
212
212
213
213
214 class unfilteredpropertycache(util.propertycache):
214 class unfilteredpropertycache(util.propertycache):
215 """propertycache that apply to unfiltered repo only"""
215 """propertycache that apply to unfiltered repo only"""
216
216
217 def __get__(self, repo, type=None):
217 def __get__(self, repo, type=None):
218 unfi = repo.unfiltered()
218 unfi = repo.unfiltered()
219 if unfi is repo:
219 if unfi is repo:
220 return super(unfilteredpropertycache, self).__get__(unfi)
220 return super(unfilteredpropertycache, self).__get__(unfi)
221 return getattr(unfi, self.name)
221 return getattr(unfi, self.name)
222
222
223
223
224 class filteredpropertycache(util.propertycache):
224 class filteredpropertycache(util.propertycache):
225 """propertycache that must take filtering in account"""
225 """propertycache that must take filtering in account"""
226
226
227 def cachevalue(self, obj, value):
227 def cachevalue(self, obj, value):
228 object.__setattr__(obj, self.name, value)
228 object.__setattr__(obj, self.name, value)
229
229
230
230
231 def hasunfilteredcache(repo, name):
231 def hasunfilteredcache(repo, name):
232 """check if a repo has an unfilteredpropertycache value for <name>"""
232 """check if a repo has an unfilteredpropertycache value for <name>"""
233 return name in vars(repo.unfiltered())
233 return name in vars(repo.unfiltered())
234
234
235
235
236 def unfilteredmethod(orig):
236 def unfilteredmethod(orig):
237 """decorate method that always need to be run on unfiltered version"""
237 """decorate method that always need to be run on unfiltered version"""
238
238
239 @functools.wraps(orig)
239 @functools.wraps(orig)
240 def wrapper(repo, *args, **kwargs):
240 def wrapper(repo, *args, **kwargs):
241 return orig(repo.unfiltered(), *args, **kwargs)
241 return orig(repo.unfiltered(), *args, **kwargs)
242
242
243 return wrapper
243 return wrapper
244
244
245
245
246 moderncaps = {
246 moderncaps = {
247 b'lookup',
247 b'lookup',
248 b'branchmap',
248 b'branchmap',
249 b'pushkey',
249 b'pushkey',
250 b'known',
250 b'known',
251 b'getbundle',
251 b'getbundle',
252 b'unbundle',
252 b'unbundle',
253 }
253 }
254 legacycaps = moderncaps.union({b'changegroupsubset'})
254 legacycaps = moderncaps.union({b'changegroupsubset'})
255
255
256
256
257 @interfaceutil.implementer(repository.ipeercommandexecutor)
257 @interfaceutil.implementer(repository.ipeercommandexecutor)
258 class localcommandexecutor:
258 class localcommandexecutor:
259 def __init__(self, peer):
259 def __init__(self, peer):
260 self._peer = peer
260 self._peer = peer
261 self._sent = False
261 self._sent = False
262 self._closed = False
262 self._closed = False
263
263
264 def __enter__(self):
264 def __enter__(self):
265 return self
265 return self
266
266
267 def __exit__(self, exctype, excvalue, exctb):
267 def __exit__(self, exctype, excvalue, exctb):
268 self.close()
268 self.close()
269
269
270 def callcommand(self, command, args):
270 def callcommand(self, command, args):
271 if self._sent:
271 if self._sent:
272 raise error.ProgrammingError(
272 raise error.ProgrammingError(
273 b'callcommand() cannot be used after sendcommands()'
273 b'callcommand() cannot be used after sendcommands()'
274 )
274 )
275
275
276 if self._closed:
276 if self._closed:
277 raise error.ProgrammingError(
277 raise error.ProgrammingError(
278 b'callcommand() cannot be used after close()'
278 b'callcommand() cannot be used after close()'
279 )
279 )
280
280
281 # We don't need to support anything fancy. Just call the named
281 # We don't need to support anything fancy. Just call the named
282 # method on the peer and return a resolved future.
282 # method on the peer and return a resolved future.
283 fn = getattr(self._peer, pycompat.sysstr(command))
283 fn = getattr(self._peer, pycompat.sysstr(command))
284
284
285 f = futures.Future()
285 f = futures.Future()
286
286
287 try:
287 try:
288 result = fn(**pycompat.strkwargs(args))
288 result = fn(**pycompat.strkwargs(args))
289 except Exception:
289 except Exception:
290 pycompat.future_set_exception_info(f, sys.exc_info()[1:])
290 pycompat.future_set_exception_info(f, sys.exc_info()[1:])
291 else:
291 else:
292 f.set_result(result)
292 f.set_result(result)
293
293
294 return f
294 return f
295
295
296 def sendcommands(self):
296 def sendcommands(self):
297 self._sent = True
297 self._sent = True
298
298
299 def close(self):
299 def close(self):
300 self._closed = True
300 self._closed = True
301
301
302
302
303 @interfaceutil.implementer(repository.ipeercommands)
303 @interfaceutil.implementer(repository.ipeercommands)
304 class localpeer(repository.peer):
304 class localpeer(repository.peer):
305 '''peer for a local repo; reflects only the most recent API'''
305 '''peer for a local repo; reflects only the most recent API'''
306
306
307 def __init__(self, repo, caps=None, path=None, remotehidden=False):
307 def __init__(self, repo, caps=None, path=None, remotehidden=False):
308 super(localpeer, self).__init__(
308 super(localpeer, self).__init__(
309 repo.ui, path=path, remotehidden=remotehidden
309 repo.ui, path=path, remotehidden=remotehidden
310 )
310 )
311
311
312 if caps is None:
312 if caps is None:
313 caps = moderncaps.copy()
313 caps = moderncaps.copy()
314 if remotehidden:
314 if remotehidden:
315 self._repo = repo.filtered(b'served.hidden')
315 self._repo = repo.filtered(b'served.hidden')
316 else:
316 else:
317 self._repo = repo.filtered(b'served')
317 self._repo = repo.filtered(b'served')
318 if repo._wanted_sidedata:
318 if repo._wanted_sidedata:
319 formatted = bundle2.format_remote_wanted_sidedata(repo)
319 formatted = bundle2.format_remote_wanted_sidedata(repo)
320 caps.add(b'exp-wanted-sidedata=' + formatted)
320 caps.add(b'exp-wanted-sidedata=' + formatted)
321
321
322 self._caps = repo._restrictcapabilities(caps)
322 self._caps = repo._restrictcapabilities(caps)
323
323
324 # Begin of _basepeer interface.
324 # Begin of _basepeer interface.
325
325
326 def url(self):
326 def url(self):
327 return self._repo.url()
327 return self._repo.url()
328
328
329 def local(self):
329 def local(self):
330 return self._repo
330 return self._repo
331
331
332 def canpush(self):
332 def canpush(self):
333 return True
333 return True
334
334
335 def close(self):
335 def close(self):
336 self._repo.close()
336 self._repo.close()
337
337
338 # End of _basepeer interface.
338 # End of _basepeer interface.
339
339
340 # Begin of _basewirecommands interface.
340 # Begin of _basewirecommands interface.
341
341
342 def branchmap(self):
342 def branchmap(self):
343 return self._repo.branchmap()
343 return self._repo.branchmap()
344
344
345 def capabilities(self):
345 def capabilities(self):
346 return self._caps
346 return self._caps
347
347
348 def get_cached_bundle_inline(self, path):
348 def get_cached_bundle_inline(self, path):
349 # not needed with local peer
349 # not needed with local peer
350 raise NotImplementedError
350 raise NotImplementedError
351
351
352 def clonebundles(self):
352 def clonebundles(self):
353 return bundlecaches.get_manifest(self._repo)
353 return bundlecaches.get_manifest(self._repo)
354
354
355 def debugwireargs(self, one, two, three=None, four=None, five=None):
355 def debugwireargs(self, one, two, three=None, four=None, five=None):
356 """Used to test argument passing over the wire"""
356 """Used to test argument passing over the wire"""
357 return b"%s %s %s %s %s" % (
357 return b"%s %s %s %s %s" % (
358 one,
358 one,
359 two,
359 two,
360 pycompat.bytestr(three),
360 pycompat.bytestr(three),
361 pycompat.bytestr(four),
361 pycompat.bytestr(four),
362 pycompat.bytestr(five),
362 pycompat.bytestr(five),
363 )
363 )
364
364
365 def getbundle(
365 def getbundle(
366 self,
366 self,
367 source,
367 source,
368 heads=None,
368 heads=None,
369 common=None,
369 common=None,
370 bundlecaps=None,
370 bundlecaps=None,
371 remote_sidedata=None,
371 remote_sidedata=None,
372 **kwargs
372 **kwargs
373 ):
373 ):
374 chunks = exchange.getbundlechunks(
374 chunks = exchange.getbundlechunks(
375 self._repo,
375 self._repo,
376 source,
376 source,
377 heads=heads,
377 heads=heads,
378 common=common,
378 common=common,
379 bundlecaps=bundlecaps,
379 bundlecaps=bundlecaps,
380 remote_sidedata=remote_sidedata,
380 remote_sidedata=remote_sidedata,
381 **kwargs
381 **kwargs
382 )[1]
382 )[1]
383 cb = util.chunkbuffer(chunks)
383 cb = util.chunkbuffer(chunks)
384
384
385 if exchange.bundle2requested(bundlecaps):
385 if exchange.bundle2requested(bundlecaps):
386 # When requesting a bundle2, getbundle returns a stream to make the
386 # When requesting a bundle2, getbundle returns a stream to make the
387 # wire level function happier. We need to build a proper object
387 # wire level function happier. We need to build a proper object
388 # from it in local peer.
388 # from it in local peer.
389 return bundle2.getunbundler(self.ui, cb)
389 return bundle2.getunbundler(self.ui, cb)
390 else:
390 else:
391 return changegroup.getunbundler(b'01', cb, None)
391 return changegroup.getunbundler(b'01', cb, None)
392
392
393 def heads(self):
393 def heads(self):
394 return self._repo.heads()
394 return self._repo.heads()
395
395
396 def known(self, nodes):
396 def known(self, nodes):
397 return self._repo.known(nodes)
397 return self._repo.known(nodes)
398
398
399 def listkeys(self, namespace):
399 def listkeys(self, namespace):
400 return self._repo.listkeys(namespace)
400 return self._repo.listkeys(namespace)
401
401
402 def lookup(self, key):
402 def lookup(self, key):
403 return self._repo.lookup(key)
403 return self._repo.lookup(key)
404
404
405 def pushkey(self, namespace, key, old, new):
405 def pushkey(self, namespace, key, old, new):
406 return self._repo.pushkey(namespace, key, old, new)
406 return self._repo.pushkey(namespace, key, old, new)
407
407
408 def stream_out(self):
408 def stream_out(self):
409 raise error.Abort(_(b'cannot perform stream clone against local peer'))
409 raise error.Abort(_(b'cannot perform stream clone against local peer'))
410
410
411 def unbundle(self, bundle, heads, url):
411 def unbundle(self, bundle, heads, url):
412 """apply a bundle on a repo
412 """apply a bundle on a repo
413
413
414 This function handles the repo locking itself."""
414 This function handles the repo locking itself."""
415 try:
415 try:
416 try:
416 try:
417 bundle = exchange.readbundle(self.ui, bundle, None)
417 bundle = exchange.readbundle(self.ui, bundle, None)
418 ret = exchange.unbundle(self._repo, bundle, heads, b'push', url)
418 ret = exchange.unbundle(self._repo, bundle, heads, b'push', url)
419 if hasattr(ret, 'getchunks'):
419 if hasattr(ret, 'getchunks'):
420 # This is a bundle20 object, turn it into an unbundler.
420 # This is a bundle20 object, turn it into an unbundler.
421 # This little dance should be dropped eventually when the
421 # This little dance should be dropped eventually when the
422 # API is finally improved.
422 # API is finally improved.
423 stream = util.chunkbuffer(ret.getchunks())
423 stream = util.chunkbuffer(ret.getchunks())
424 ret = bundle2.getunbundler(self.ui, stream)
424 ret = bundle2.getunbundler(self.ui, stream)
425 return ret
425 return ret
426 except Exception as exc:
426 except Exception as exc:
427 # If the exception contains output salvaged from a bundle2
427 # If the exception contains output salvaged from a bundle2
428 # reply, we need to make sure it is printed before continuing
428 # reply, we need to make sure it is printed before continuing
429 # to fail. So we build a bundle2 with such output and consume
429 # to fail. So we build a bundle2 with such output and consume
430 # it directly.
430 # it directly.
431 #
431 #
432 # This is not very elegant but allows a "simple" solution for
432 # This is not very elegant but allows a "simple" solution for
433 # issue4594
433 # issue4594
434 output = getattr(exc, '_bundle2salvagedoutput', ())
434 output = getattr(exc, '_bundle2salvagedoutput', ())
435 if output:
435 if output:
436 bundler = bundle2.bundle20(self._repo.ui)
436 bundler = bundle2.bundle20(self._repo.ui)
437 for out in output:
437 for out in output:
438 bundler.addpart(out)
438 bundler.addpart(out)
439 stream = util.chunkbuffer(bundler.getchunks())
439 stream = util.chunkbuffer(bundler.getchunks())
440 b = bundle2.getunbundler(self.ui, stream)
440 b = bundle2.getunbundler(self.ui, stream)
441 bundle2.processbundle(self._repo, b)
441 bundle2.processbundle(self._repo, b)
442 raise
442 raise
443 except error.PushRaced as exc:
443 except error.PushRaced as exc:
444 raise error.ResponseError(
444 raise error.ResponseError(
445 _(b'push failed:'), stringutil.forcebytestr(exc)
445 _(b'push failed:'), stringutil.forcebytestr(exc)
446 )
446 )
447
447
448 # End of _basewirecommands interface.
448 # End of _basewirecommands interface.
449
449
450 # Begin of peer interface.
450 # Begin of peer interface.
451
451
452 def commandexecutor(self):
452 def commandexecutor(self):
453 return localcommandexecutor(self)
453 return localcommandexecutor(self)
454
454
455 # End of peer interface.
455 # End of peer interface.
456
456
457
457
458 @interfaceutil.implementer(repository.ipeerlegacycommands)
458 @interfaceutil.implementer(repository.ipeerlegacycommands)
459 class locallegacypeer(localpeer):
459 class locallegacypeer(localpeer):
460 """peer extension which implements legacy methods too; used for tests with
460 """peer extension which implements legacy methods too; used for tests with
461 restricted capabilities"""
461 restricted capabilities"""
462
462
463 def __init__(self, repo, path=None, remotehidden=False):
463 def __init__(self, repo, path=None, remotehidden=False):
464 super(locallegacypeer, self).__init__(
464 super(locallegacypeer, self).__init__(
465 repo, caps=legacycaps, path=path, remotehidden=remotehidden
465 repo, caps=legacycaps, path=path, remotehidden=remotehidden
466 )
466 )
467
467
468 # Begin of baselegacywirecommands interface.
468 # Begin of baselegacywirecommands interface.
469
469
470 def between(self, pairs):
470 def between(self, pairs):
471 return self._repo.between(pairs)
471 return self._repo.between(pairs)
472
472
473 def branches(self, nodes):
473 def branches(self, nodes):
474 return self._repo.branches(nodes)
474 return self._repo.branches(nodes)
475
475
476 def changegroup(self, nodes, source):
476 def changegroup(self, nodes, source):
477 outgoing = discovery.outgoing(
477 outgoing = discovery.outgoing(
478 self._repo, missingroots=nodes, ancestorsof=self._repo.heads()
478 self._repo, missingroots=nodes, ancestorsof=self._repo.heads()
479 )
479 )
480 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
480 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
481
481
482 def changegroupsubset(self, bases, heads, source):
482 def changegroupsubset(self, bases, heads, source):
483 outgoing = discovery.outgoing(
483 outgoing = discovery.outgoing(
484 self._repo, missingroots=bases, ancestorsof=heads
484 self._repo, missingroots=bases, ancestorsof=heads
485 )
485 )
486 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
486 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
487
487
488 # End of baselegacywirecommands interface.
488 # End of baselegacywirecommands interface.
489
489
490
490
491 # Functions receiving (ui, features) that extensions can register to impact
491 # Functions receiving (ui, features) that extensions can register to impact
492 # the ability to load repositories with custom requirements. Only
492 # the ability to load repositories with custom requirements. Only
493 # functions defined in loaded extensions are called.
493 # functions defined in loaded extensions are called.
494 #
494 #
495 # The function receives a set of requirement strings that the repository
495 # The function receives a set of requirement strings that the repository
496 # is capable of opening. Functions will typically add elements to the
496 # is capable of opening. Functions will typically add elements to the
497 # set to reflect that the extension knows how to handle that requirements.
497 # set to reflect that the extension knows how to handle that requirements.
498 featuresetupfuncs = set()
498 featuresetupfuncs = set()
499
499
500
500
501 def _getsharedvfs(hgvfs, requirements):
501 def _getsharedvfs(hgvfs, requirements):
502 """returns the vfs object pointing to root of shared source
502 """returns the vfs object pointing to root of shared source
503 repo for a shared repository
503 repo for a shared repository
504
504
505 hgvfs is vfs pointing at .hg/ of current repo (shared one)
505 hgvfs is vfs pointing at .hg/ of current repo (shared one)
506 requirements is a set of requirements of current repo (shared one)
506 requirements is a set of requirements of current repo (shared one)
507 """
507 """
508 # The ``shared`` or ``relshared`` requirements indicate the
508 # The ``shared`` or ``relshared`` requirements indicate the
509 # store lives in the path contained in the ``.hg/sharedpath`` file.
509 # store lives in the path contained in the ``.hg/sharedpath`` file.
510 # This is an absolute path for ``shared`` and relative to
510 # This is an absolute path for ``shared`` and relative to
511 # ``.hg/`` for ``relshared``.
511 # ``.hg/`` for ``relshared``.
512 sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
512 sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
513 if requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements:
513 if requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements:
514 sharedpath = util.normpath(hgvfs.join(sharedpath))
514 sharedpath = util.normpath(hgvfs.join(sharedpath))
515
515
516 sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
516 sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
517
517
518 if not sharedvfs.exists():
518 if not sharedvfs.exists():
519 raise error.RepoError(
519 raise error.RepoError(
520 _(b'.hg/sharedpath points to nonexistent directory %s')
520 _(b'.hg/sharedpath points to nonexistent directory %s')
521 % sharedvfs.base
521 % sharedvfs.base
522 )
522 )
523 return sharedvfs
523 return sharedvfs
524
524
525
525
526 def _readrequires(vfs, allowmissing):
526 def _readrequires(vfs, allowmissing):
527 """reads the require file present at root of this vfs
527 """reads the require file present at root of this vfs
528 and return a set of requirements
528 and return a set of requirements
529
529
530 If allowmissing is True, we suppress FileNotFoundError if raised"""
530 If allowmissing is True, we suppress FileNotFoundError if raised"""
531 # requires file contains a newline-delimited list of
531 # requires file contains a newline-delimited list of
532 # features/capabilities the opener (us) must have in order to use
532 # features/capabilities the opener (us) must have in order to use
533 # the repository. This file was introduced in Mercurial 0.9.2,
533 # the repository. This file was introduced in Mercurial 0.9.2,
534 # which means very old repositories may not have one. We assume
534 # which means very old repositories may not have one. We assume
535 # a missing file translates to no requirements.
535 # a missing file translates to no requirements.
536 read = vfs.tryread if allowmissing else vfs.read
536 read = vfs.tryread if allowmissing else vfs.read
537 return set(read(b'requires').splitlines())
537 return set(read(b'requires').splitlines())
538
538
539
539
540 def makelocalrepository(baseui, path: bytes, intents=None):
540 def makelocalrepository(baseui, path: bytes, intents=None):
541 """Create a local repository object.
541 """Create a local repository object.
542
542
543 Given arguments needed to construct a local repository, this function
543 Given arguments needed to construct a local repository, this function
544 performs various early repository loading functionality (such as
544 performs various early repository loading functionality (such as
545 reading the ``.hg/requires`` and ``.hg/hgrc`` files), validates that
545 reading the ``.hg/requires`` and ``.hg/hgrc`` files), validates that
546 the repository can be opened, derives a type suitable for representing
546 the repository can be opened, derives a type suitable for representing
547 that repository, and returns an instance of it.
547 that repository, and returns an instance of it.
548
548
549 The returned object conforms to the ``repository.completelocalrepository``
549 The returned object conforms to the ``repository.completelocalrepository``
550 interface.
550 interface.
551
551
552 The repository type is derived by calling a series of factory functions
552 The repository type is derived by calling a series of factory functions
553 for each aspect/interface of the final repository. These are defined by
553 for each aspect/interface of the final repository. These are defined by
554 ``REPO_INTERFACES``.
554 ``REPO_INTERFACES``.
555
555
556 Each factory function is called to produce a type implementing a specific
556 Each factory function is called to produce a type implementing a specific
557 interface. The cumulative list of returned types will be combined into a
557 interface. The cumulative list of returned types will be combined into a
558 new type and that type will be instantiated to represent the local
558 new type and that type will be instantiated to represent the local
559 repository.
559 repository.
560
560
561 The factory functions each receive various state that may be consulted
561 The factory functions each receive various state that may be consulted
562 as part of deriving a type.
562 as part of deriving a type.
563
563
564 Extensions should wrap these factory functions to customize repository type
564 Extensions should wrap these factory functions to customize repository type
565 creation. Note that an extension's wrapped function may be called even if
565 creation. Note that an extension's wrapped function may be called even if
566 that extension is not loaded for the repo being constructed. Extensions
566 that extension is not loaded for the repo being constructed. Extensions
567 should check if their ``__name__`` appears in the
567 should check if their ``__name__`` appears in the
568 ``extensionmodulenames`` set passed to the factory function and no-op if
568 ``extensionmodulenames`` set passed to the factory function and no-op if
569 not.
569 not.
570 """
570 """
571 ui = baseui.copy()
571 ui = baseui.copy()
572 # Prevent copying repo configuration.
572 # Prevent copying repo configuration.
573 ui.copy = baseui.copy
573 ui.copy = baseui.copy
574
574
575 # Working directory VFS rooted at repository root.
575 # Working directory VFS rooted at repository root.
576 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
576 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
577
577
578 # Main VFS for .hg/ directory.
578 # Main VFS for .hg/ directory.
579 hgpath = wdirvfs.join(b'.hg')
579 hgpath = wdirvfs.join(b'.hg')
580 hgvfs = vfsmod.vfs(hgpath, cacheaudited=True)
580 hgvfs = vfsmod.vfs(hgpath, cacheaudited=True)
581 # Whether this repository is shared one or not
581 # Whether this repository is shared one or not
582 shared = False
582 shared = False
583 # If this repository is shared, vfs pointing to shared repo
583 # If this repository is shared, vfs pointing to shared repo
584 sharedvfs = None
584 sharedvfs = None
585
585
586 # The .hg/ path should exist and should be a directory. All other
586 # The .hg/ path should exist and should be a directory. All other
587 # cases are errors.
587 # cases are errors.
588 if not hgvfs.isdir():
588 if not hgvfs.isdir():
589 try:
589 try:
590 hgvfs.stat()
590 hgvfs.stat()
591 except FileNotFoundError:
591 except FileNotFoundError:
592 pass
592 pass
593 except ValueError as e:
593 except ValueError as e:
594 # Can be raised on Python 3.8 when path is invalid.
594 # Can be raised on Python 3.8 when path is invalid.
595 raise error.Abort(
595 raise error.Abort(
596 _(b'invalid path %s: %s') % (path, stringutil.forcebytestr(e))
596 _(b'invalid path %s: %s') % (path, stringutil.forcebytestr(e))
597 )
597 )
598
598
599 raise error.RepoError(_(b'repository %s not found') % path)
599 raise error.RepoError(_(b'repository %s not found') % path)
600
600
601 requirements = _readrequires(hgvfs, True)
601 requirements = _readrequires(hgvfs, True)
602 shared = (
602 shared = (
603 requirementsmod.SHARED_REQUIREMENT in requirements
603 requirementsmod.SHARED_REQUIREMENT in requirements
604 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
604 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
605 )
605 )
606 storevfs = None
606 storevfs = None
607 if shared:
607 if shared:
608 # This is a shared repo
608 # This is a shared repo
609 sharedvfs = _getsharedvfs(hgvfs, requirements)
609 sharedvfs = _getsharedvfs(hgvfs, requirements)
610 storevfs = vfsmod.vfs(sharedvfs.join(b'store'))
610 storevfs = vfsmod.vfs(sharedvfs.join(b'store'))
611 else:
611 else:
612 storevfs = vfsmod.vfs(hgvfs.join(b'store'))
612 storevfs = vfsmod.vfs(hgvfs.join(b'store'))
613
613
614 # if .hg/requires contains the sharesafe requirement, it means
614 # if .hg/requires contains the sharesafe requirement, it means
615 # there exists a `.hg/store/requires` too and we should read it
615 # there exists a `.hg/store/requires` too and we should read it
616 # NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement
616 # NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement
617 # is present. We never write SHARESAFE_REQUIREMENT for a repo if store
617 # is present. We never write SHARESAFE_REQUIREMENT for a repo if store
618 # is not present, refer checkrequirementscompat() for that
618 # is not present, refer checkrequirementscompat() for that
619 #
619 #
620 # However, if SHARESAFE_REQUIREMENT is not present, it means that the
620 # However, if SHARESAFE_REQUIREMENT is not present, it means that the
621 # repository was shared the old way. We check the share source .hg/requires
621 # repository was shared the old way. We check the share source .hg/requires
622 # for SHARESAFE_REQUIREMENT to detect whether the current repository needs
622 # for SHARESAFE_REQUIREMENT to detect whether the current repository needs
623 # to be reshared
623 # to be reshared
624 hint = _(b"see `hg help config.format.use-share-safe` for more information")
624 hint = _(b"see `hg help config.format.use-share-safe` for more information")
625 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
625 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
626 if (
626 if (
627 shared
627 shared
628 and requirementsmod.SHARESAFE_REQUIREMENT
628 and requirementsmod.SHARESAFE_REQUIREMENT
629 not in _readrequires(sharedvfs, True)
629 not in _readrequires(sharedvfs, True)
630 ):
630 ):
631 mismatch_warn = ui.configbool(
631 mismatch_warn = ui.configbool(
632 b'share', b'safe-mismatch.source-not-safe.warn'
632 b'share', b'safe-mismatch.source-not-safe.warn'
633 )
633 )
634 mismatch_config = ui.config(
634 mismatch_config = ui.config(
635 b'share', b'safe-mismatch.source-not-safe'
635 b'share', b'safe-mismatch.source-not-safe'
636 )
636 )
637 mismatch_verbose_upgrade = ui.configbool(
637 mismatch_verbose_upgrade = ui.configbool(
638 b'share', b'safe-mismatch.source-not-safe:verbose-upgrade'
638 b'share', b'safe-mismatch.source-not-safe:verbose-upgrade'
639 )
639 )
640 if mismatch_config in (
640 if mismatch_config in (
641 b'downgrade-allow',
641 b'downgrade-allow',
642 b'allow',
642 b'allow',
643 b'downgrade-abort',
643 b'downgrade-abort',
644 ):
644 ):
645 # prevent cyclic import localrepo -> upgrade -> localrepo
645 # prevent cyclic import localrepo -> upgrade -> localrepo
646 from . import upgrade
646 from . import upgrade
647
647
648 upgrade.downgrade_share_to_non_safe(
648 upgrade.downgrade_share_to_non_safe(
649 ui,
649 ui,
650 hgvfs,
650 hgvfs,
651 sharedvfs,
651 sharedvfs,
652 requirements,
652 requirements,
653 mismatch_config,
653 mismatch_config,
654 mismatch_warn,
654 mismatch_warn,
655 mismatch_verbose_upgrade,
655 mismatch_verbose_upgrade,
656 )
656 )
657 elif mismatch_config == b'abort':
657 elif mismatch_config == b'abort':
658 raise error.Abort(
658 raise error.Abort(
659 _(b"share source does not support share-safe requirement"),
659 _(b"share source does not support share-safe requirement"),
660 hint=hint,
660 hint=hint,
661 )
661 )
662 else:
662 else:
663 raise error.Abort(
663 raise error.Abort(
664 _(
664 _(
665 b"share-safe mismatch with source.\nUnrecognized"
665 b"share-safe mismatch with source.\nUnrecognized"
666 b" value '%s' of `share.safe-mismatch.source-not-safe`"
666 b" value '%s' of `share.safe-mismatch.source-not-safe`"
667 b" set."
667 b" set."
668 )
668 )
669 % mismatch_config,
669 % mismatch_config,
670 hint=hint,
670 hint=hint,
671 )
671 )
672 else:
672 else:
673 requirements |= _readrequires(storevfs, False)
673 requirements |= _readrequires(storevfs, False)
674 elif shared:
674 elif shared:
675 sourcerequires = _readrequires(sharedvfs, False)
675 sourcerequires = _readrequires(sharedvfs, False)
676 if requirementsmod.SHARESAFE_REQUIREMENT in sourcerequires:
676 if requirementsmod.SHARESAFE_REQUIREMENT in sourcerequires:
677 mismatch_config = ui.config(b'share', b'safe-mismatch.source-safe')
677 mismatch_config = ui.config(b'share', b'safe-mismatch.source-safe')
678 mismatch_warn = ui.configbool(
678 mismatch_warn = ui.configbool(
679 b'share', b'safe-mismatch.source-safe.warn'
679 b'share', b'safe-mismatch.source-safe.warn'
680 )
680 )
681 mismatch_verbose_upgrade = ui.configbool(
681 mismatch_verbose_upgrade = ui.configbool(
682 b'share', b'safe-mismatch.source-safe:verbose-upgrade'
682 b'share', b'safe-mismatch.source-safe:verbose-upgrade'
683 )
683 )
684 if mismatch_config in (
684 if mismatch_config in (
685 b'upgrade-allow',
685 b'upgrade-allow',
686 b'allow',
686 b'allow',
687 b'upgrade-abort',
687 b'upgrade-abort',
688 ):
688 ):
689 # prevent cyclic import localrepo -> upgrade -> localrepo
689 # prevent cyclic import localrepo -> upgrade -> localrepo
690 from . import upgrade
690 from . import upgrade
691
691
692 upgrade.upgrade_share_to_safe(
692 upgrade.upgrade_share_to_safe(
693 ui,
693 ui,
694 hgvfs,
694 hgvfs,
695 storevfs,
695 storevfs,
696 requirements,
696 requirements,
697 mismatch_config,
697 mismatch_config,
698 mismatch_warn,
698 mismatch_warn,
699 mismatch_verbose_upgrade,
699 mismatch_verbose_upgrade,
700 )
700 )
701 elif mismatch_config == b'abort':
701 elif mismatch_config == b'abort':
702 raise error.Abort(
702 raise error.Abort(
703 _(
703 _(
704 b'version mismatch: source uses share-safe'
704 b'version mismatch: source uses share-safe'
705 b' functionality while the current share does not'
705 b' functionality while the current share does not'
706 ),
706 ),
707 hint=hint,
707 hint=hint,
708 )
708 )
709 else:
709 else:
710 raise error.Abort(
710 raise error.Abort(
711 _(
711 _(
712 b"share-safe mismatch with source.\nUnrecognized"
712 b"share-safe mismatch with source.\nUnrecognized"
713 b" value '%s' of `share.safe-mismatch.source-safe` set."
713 b" value '%s' of `share.safe-mismatch.source-safe` set."
714 )
714 )
715 % mismatch_config,
715 % mismatch_config,
716 hint=hint,
716 hint=hint,
717 )
717 )
718
718
719 # The .hg/hgrc file may load extensions or contain config options
719 # The .hg/hgrc file may load extensions or contain config options
720 # that influence repository construction. Attempt to load it and
720 # that influence repository construction. Attempt to load it and
721 # process any new extensions that it may have pulled in.
721 # process any new extensions that it may have pulled in.
722 if loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs):
722 if loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs):
723 afterhgrcload(ui, wdirvfs, hgvfs, requirements)
723 afterhgrcload(ui, wdirvfs, hgvfs, requirements)
724 extensions.loadall(ui)
724 extensions.loadall(ui)
725 extensions.populateui(ui)
725 extensions.populateui(ui)
726
726
727 # Set of module names of extensions loaded for this repository.
727 # Set of module names of extensions loaded for this repository.
728 extensionmodulenames = {m.__name__ for n, m in extensions.extensions(ui)}
728 extensionmodulenames = {m.__name__ for n, m in extensions.extensions(ui)}
729
729
730 supportedrequirements = gathersupportedrequirements(ui)
730 supportedrequirements = gathersupportedrequirements(ui)
731
731
732 # We first validate the requirements are known.
732 # We first validate the requirements are known.
733 ensurerequirementsrecognized(requirements, supportedrequirements)
733 ensurerequirementsrecognized(requirements, supportedrequirements)
734
734
735 # Then we validate that the known set is reasonable to use together.
735 # Then we validate that the known set is reasonable to use together.
736 ensurerequirementscompatible(ui, requirements)
736 ensurerequirementscompatible(ui, requirements)
737
737
738 # TODO there are unhandled edge cases related to opening repositories with
738 # TODO there are unhandled edge cases related to opening repositories with
739 # shared storage. If storage is shared, we should also test for requirements
739 # shared storage. If storage is shared, we should also test for requirements
740 # compatibility in the pointed-to repo. This entails loading the .hg/hgrc in
740 # compatibility in the pointed-to repo. This entails loading the .hg/hgrc in
741 # that repo, as that repo may load extensions needed to open it. This is a
741 # that repo, as that repo may load extensions needed to open it. This is a
742 # bit complicated because we don't want the other hgrc to overwrite settings
742 # bit complicated because we don't want the other hgrc to overwrite settings
743 # in this hgrc.
743 # in this hgrc.
744 #
744 #
745 # This bug is somewhat mitigated by the fact that we copy the .hg/requires
745 # This bug is somewhat mitigated by the fact that we copy the .hg/requires
746 # file when sharing repos. But if a requirement is added after the share is
746 # file when sharing repos. But if a requirement is added after the share is
747 # performed, thereby introducing a new requirement for the opener, we may
747 # performed, thereby introducing a new requirement for the opener, we may
748 # will not see that and could encounter a run-time error interacting with
748 # will not see that and could encounter a run-time error interacting with
749 # that shared store since it has an unknown-to-us requirement.
749 # that shared store since it has an unknown-to-us requirement.
750
750
751 # At this point, we know we should be capable of opening the repository.
751 # At this point, we know we should be capable of opening the repository.
752 # Now get on with doing that.
752 # Now get on with doing that.
753
753
754 features = set()
754 features = set()
755
755
756 # The "store" part of the repository holds versioned data. How it is
756 # The "store" part of the repository holds versioned data. How it is
757 # accessed is determined by various requirements. If `shared` or
757 # accessed is determined by various requirements. If `shared` or
758 # `relshared` requirements are present, this indicates current repository
758 # `relshared` requirements are present, this indicates current repository
759 # is a share and store exists in path mentioned in `.hg/sharedpath`
759 # is a share and store exists in path mentioned in `.hg/sharedpath`
760 if shared:
760 if shared:
761 storebasepath = sharedvfs.base
761 storebasepath = sharedvfs.base
762 cachepath = sharedvfs.join(b'cache')
762 cachepath = sharedvfs.join(b'cache')
763 features.add(repository.REPO_FEATURE_SHARED_STORAGE)
763 features.add(repository.REPO_FEATURE_SHARED_STORAGE)
764 else:
764 else:
765 storebasepath = hgvfs.base
765 storebasepath = hgvfs.base
766 cachepath = hgvfs.join(b'cache')
766 cachepath = hgvfs.join(b'cache')
767 wcachepath = hgvfs.join(b'wcache')
767 wcachepath = hgvfs.join(b'wcache')
768
768
769 # The store has changed over time and the exact layout is dictated by
769 # The store has changed over time and the exact layout is dictated by
770 # requirements. The store interface abstracts differences across all
770 # requirements. The store interface abstracts differences across all
771 # of them.
771 # of them.
772 store = makestore(
772 store = makestore(
773 requirements,
773 requirements,
774 storebasepath,
774 storebasepath,
775 lambda base: vfsmod.vfs(base, cacheaudited=True),
775 lambda base: vfsmod.vfs(base, cacheaudited=True),
776 )
776 )
777 hgvfs.createmode = store.createmode
777 hgvfs.createmode = store.createmode
778
778
779 storevfs = store.vfs
779 storevfs = store.vfs
780 storevfs.options = resolvestorevfsoptions(ui, requirements, features)
780 storevfs.options = resolvestorevfsoptions(ui, requirements, features)
781
781
782 if (
782 if (
783 requirementsmod.REVLOGV2_REQUIREMENT in requirements
783 requirementsmod.REVLOGV2_REQUIREMENT in requirements
784 or requirementsmod.CHANGELOGV2_REQUIREMENT in requirements
784 or requirementsmod.CHANGELOGV2_REQUIREMENT in requirements
785 ):
785 ):
786 features.add(repository.REPO_FEATURE_SIDE_DATA)
786 features.add(repository.REPO_FEATURE_SIDE_DATA)
787 # the revlogv2 docket introduced race condition that we need to fix
787 # the revlogv2 docket introduced race condition that we need to fix
788 features.discard(repository.REPO_FEATURE_STREAM_CLONE)
788 features.discard(repository.REPO_FEATURE_STREAM_CLONE)
789
789
790 # The cache vfs is used to manage cache files.
790 # The cache vfs is used to manage cache files.
791 cachevfs = vfsmod.vfs(cachepath, cacheaudited=True)
791 cachevfs = vfsmod.vfs(cachepath, cacheaudited=True)
792 cachevfs.createmode = store.createmode
792 cachevfs.createmode = store.createmode
793 # The cache vfs is used to manage cache files related to the working copy
793 # The cache vfs is used to manage cache files related to the working copy
794 wcachevfs = vfsmod.vfs(wcachepath, cacheaudited=True)
794 wcachevfs = vfsmod.vfs(wcachepath, cacheaudited=True)
795 wcachevfs.createmode = store.createmode
795 wcachevfs.createmode = store.createmode
796
796
797 # Now resolve the type for the repository object. We do this by repeatedly
797 # Now resolve the type for the repository object. We do this by repeatedly
798 # calling a factory function to produces types for specific aspects of the
798 # calling a factory function to produces types for specific aspects of the
799 # repo's operation. The aggregate returned types are used as base classes
799 # repo's operation. The aggregate returned types are used as base classes
800 # for a dynamically-derived type, which will represent our new repository.
800 # for a dynamically-derived type, which will represent our new repository.
801
801
802 bases = []
802 bases = []
803 extrastate = {}
803 extrastate = {}
804
804
805 for iface, fn in REPO_INTERFACES:
805 for iface, fn in REPO_INTERFACES:
806 # We pass all potentially useful state to give extensions tons of
806 # We pass all potentially useful state to give extensions tons of
807 # flexibility.
807 # flexibility.
808 typ = fn()(
808 typ = fn()(
809 ui=ui,
809 ui=ui,
810 intents=intents,
810 intents=intents,
811 requirements=requirements,
811 requirements=requirements,
812 features=features,
812 features=features,
813 wdirvfs=wdirvfs,
813 wdirvfs=wdirvfs,
814 hgvfs=hgvfs,
814 hgvfs=hgvfs,
815 store=store,
815 store=store,
816 storevfs=storevfs,
816 storevfs=storevfs,
817 storeoptions=storevfs.options,
817 storeoptions=storevfs.options,
818 cachevfs=cachevfs,
818 cachevfs=cachevfs,
819 wcachevfs=wcachevfs,
819 wcachevfs=wcachevfs,
820 extensionmodulenames=extensionmodulenames,
820 extensionmodulenames=extensionmodulenames,
821 extrastate=extrastate,
821 extrastate=extrastate,
822 baseclasses=bases,
822 baseclasses=bases,
823 )
823 )
824
824
825 if not isinstance(typ, type):
825 if not isinstance(typ, type):
826 raise error.ProgrammingError(
826 raise error.ProgrammingError(
827 b'unable to construct type for %s' % iface
827 b'unable to construct type for %s' % iface
828 )
828 )
829
829
830 bases.append(typ)
830 bases.append(typ)
831
831
832 # type() allows you to use characters in type names that wouldn't be
832 # type() allows you to use characters in type names that wouldn't be
833 # recognized as Python symbols in source code. We abuse that to add
833 # recognized as Python symbols in source code. We abuse that to add
834 # rich information about our constructed repo.
834 # rich information about our constructed repo.
835 name = pycompat.sysstr(
835 name = pycompat.sysstr(
836 b'derivedrepo:%s<%s>' % (wdirvfs.base, b','.join(sorted(requirements)))
836 b'derivedrepo:%s<%s>' % (wdirvfs.base, b','.join(sorted(requirements)))
837 )
837 )
838
838
839 cls = type(name, tuple(bases), {})
839 cls = type(name, tuple(bases), {})
840
840
841 return cls(
841 return cls(
842 baseui=baseui,
842 baseui=baseui,
843 ui=ui,
843 ui=ui,
844 origroot=path,
844 origroot=path,
845 wdirvfs=wdirvfs,
845 wdirvfs=wdirvfs,
846 hgvfs=hgvfs,
846 hgvfs=hgvfs,
847 requirements=requirements,
847 requirements=requirements,
848 supportedrequirements=supportedrequirements,
848 supportedrequirements=supportedrequirements,
849 sharedpath=storebasepath,
849 sharedpath=storebasepath,
850 store=store,
850 store=store,
851 cachevfs=cachevfs,
851 cachevfs=cachevfs,
852 wcachevfs=wcachevfs,
852 wcachevfs=wcachevfs,
853 features=features,
853 features=features,
854 intents=intents,
854 intents=intents,
855 )
855 )
856
856
857
857
858 def loadhgrc(
858 def loadhgrc(
859 ui,
859 ui,
860 wdirvfs: vfsmod.vfs,
860 wdirvfs: vfsmod.vfs,
861 hgvfs: vfsmod.vfs,
861 hgvfs: vfsmod.vfs,
862 requirements,
862 requirements,
863 sharedvfs: Optional[vfsmod.vfs] = None,
863 sharedvfs: Optional[vfsmod.vfs] = None,
864 ):
864 ):
865 """Load hgrc files/content into a ui instance.
865 """Load hgrc files/content into a ui instance.
866
866
867 This is called during repository opening to load any additional
867 This is called during repository opening to load any additional
868 config files or settings relevant to the current repository.
868 config files or settings relevant to the current repository.
869
869
870 Returns a bool indicating whether any additional configs were loaded.
870 Returns a bool indicating whether any additional configs were loaded.
871
871
872 Extensions should monkeypatch this function to modify how per-repo
872 Extensions should monkeypatch this function to modify how per-repo
873 configs are loaded. For example, an extension may wish to pull in
873 configs are loaded. For example, an extension may wish to pull in
874 configs from alternate files or sources.
874 configs from alternate files or sources.
875
875
876 sharedvfs is vfs object pointing to source repo if the current one is a
876 sharedvfs is vfs object pointing to source repo if the current one is a
877 shared one
877 shared one
878 """
878 """
879 if not rcutil.use_repo_hgrc():
879 if not rcutil.use_repo_hgrc():
880 return False
880 return False
881
881
882 ret = False
882 ret = False
883 # first load config from shared source if we has to
883 # first load config from shared source if we has to
884 if requirementsmod.SHARESAFE_REQUIREMENT in requirements and sharedvfs:
884 if requirementsmod.SHARESAFE_REQUIREMENT in requirements and sharedvfs:
885 try:
885 try:
886 ui.readconfig(sharedvfs.join(b'hgrc'), root=sharedvfs.base)
886 ui.readconfig(sharedvfs.join(b'hgrc'), root=sharedvfs.base)
887 ret = True
887 ret = True
888 except IOError:
888 except IOError:
889 pass
889 pass
890
890
891 try:
891 try:
892 ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
892 ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
893 ret = True
893 ret = True
894 except IOError:
894 except IOError:
895 pass
895 pass
896
896
897 try:
897 try:
898 ui.readconfig(hgvfs.join(b'hgrc-not-shared'), root=wdirvfs.base)
898 ui.readconfig(hgvfs.join(b'hgrc-not-shared'), root=wdirvfs.base)
899 ret = True
899 ret = True
900 except IOError:
900 except IOError:
901 pass
901 pass
902
902
903 return ret
903 return ret
904
904
905
905
906 def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
906 def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
907 """Perform additional actions after .hg/hgrc is loaded.
907 """Perform additional actions after .hg/hgrc is loaded.
908
908
909 This function is called during repository loading immediately after
909 This function is called during repository loading immediately after
910 the .hg/hgrc file is loaded and before per-repo extensions are loaded.
910 the .hg/hgrc file is loaded and before per-repo extensions are loaded.
911
911
912 The function can be used to validate configs, automatically add
912 The function can be used to validate configs, automatically add
913 options (including extensions) based on requirements, etc.
913 options (including extensions) based on requirements, etc.
914 """
914 """
915
915
916 # Map of requirements to list of extensions to load automatically when
916 # Map of requirements to list of extensions to load automatically when
917 # requirement is present.
917 # requirement is present.
918 autoextensions = {
918 autoextensions = {
919 b'git': [b'git'],
919 b'git': [b'git'],
920 b'largefiles': [b'largefiles'],
920 b'largefiles': [b'largefiles'],
921 b'lfs': [b'lfs'],
921 b'lfs': [b'lfs'],
922 }
922 }
923
923
924 for requirement, names in sorted(autoextensions.items()):
924 for requirement, names in sorted(autoextensions.items()):
925 if requirement not in requirements:
925 if requirement not in requirements:
926 continue
926 continue
927
927
928 for name in names:
928 for name in names:
929 if not ui.hasconfig(b'extensions', name):
929 if not ui.hasconfig(b'extensions', name):
930 ui.setconfig(b'extensions', name, b'', source=b'autoload')
930 ui.setconfig(b'extensions', name, b'', source=b'autoload')
931
931
932
932
933 def gathersupportedrequirements(ui):
933 def gathersupportedrequirements(ui):
934 """Determine the complete set of recognized requirements."""
934 """Determine the complete set of recognized requirements."""
935 # Start with all requirements supported by this file.
935 # Start with all requirements supported by this file.
936 supported = set(localrepository._basesupported)
936 supported = set(localrepository._basesupported)
937
937
938 # Execute ``featuresetupfuncs`` entries if they belong to an extension
938 # Execute ``featuresetupfuncs`` entries if they belong to an extension
939 # relevant to this ui instance.
939 # relevant to this ui instance.
940 modules = {m.__name__ for n, m in extensions.extensions(ui)}
940 modules = {m.__name__ for n, m in extensions.extensions(ui)}
941
941
942 for fn in featuresetupfuncs:
942 for fn in featuresetupfuncs:
943 if fn.__module__ in modules:
943 if fn.__module__ in modules:
944 fn(ui, supported)
944 fn(ui, supported)
945
945
946 # Add derived requirements from registered compression engines.
946 # Add derived requirements from registered compression engines.
947 for name in util.compengines:
947 for name in util.compengines:
948 engine = util.compengines[name]
948 engine = util.compengines[name]
949 if engine.available() and engine.revlogheader():
949 if engine.available() and engine.revlogheader():
950 supported.add(b'exp-compression-%s' % name)
950 supported.add(b'exp-compression-%s' % name)
951 if engine.name() == b'zstd':
951 if engine.name() == b'zstd':
952 supported.add(requirementsmod.REVLOG_COMPRESSION_ZSTD)
952 supported.add(requirementsmod.REVLOG_COMPRESSION_ZSTD)
953
953
954 return supported
954 return supported
955
955
956
956
957 def ensurerequirementsrecognized(requirements, supported):
957 def ensurerequirementsrecognized(requirements, supported):
958 """Validate that a set of local requirements is recognized.
958 """Validate that a set of local requirements is recognized.
959
959
960 Receives a set of requirements. Raises an ``error.RepoError`` if there
960 Receives a set of requirements. Raises an ``error.RepoError`` if there
961 exists any requirement in that set that currently loaded code doesn't
961 exists any requirement in that set that currently loaded code doesn't
962 recognize.
962 recognize.
963
963
964 Returns a set of supported requirements.
964 Returns a set of supported requirements.
965 """
965 """
966 missing = set()
966 missing = set()
967
967
968 for requirement in requirements:
968 for requirement in requirements:
969 if requirement in supported:
969 if requirement in supported:
970 continue
970 continue
971
971
972 if not requirement or not requirement[0:1].isalnum():
972 if not requirement or not requirement[0:1].isalnum():
973 raise error.RequirementError(_(b'.hg/requires file is corrupt'))
973 raise error.RequirementError(_(b'.hg/requires file is corrupt'))
974
974
975 missing.add(requirement)
975 missing.add(requirement)
976
976
977 if missing:
977 if missing:
978 raise error.RequirementError(
978 raise error.RequirementError(
979 _(b'repository requires features unknown to this Mercurial: %s')
979 _(b'repository requires features unknown to this Mercurial: %s')
980 % b' '.join(sorted(missing)),
980 % b' '.join(sorted(missing)),
981 hint=_(
981 hint=_(
982 b'see https://mercurial-scm.org/wiki/MissingRequirement '
982 b'see https://mercurial-scm.org/wiki/MissingRequirement '
983 b'for more information'
983 b'for more information'
984 ),
984 ),
985 )
985 )
986
986
987
987
988 def ensurerequirementscompatible(ui, requirements):
988 def ensurerequirementscompatible(ui, requirements):
989 """Validates that a set of recognized requirements is mutually compatible.
989 """Validates that a set of recognized requirements is mutually compatible.
990
990
991 Some requirements may not be compatible with others or require
991 Some requirements may not be compatible with others or require
992 config options that aren't enabled. This function is called during
992 config options that aren't enabled. This function is called during
993 repository opening to ensure that the set of requirements needed
993 repository opening to ensure that the set of requirements needed
994 to open a repository is sane and compatible with config options.
994 to open a repository is sane and compatible with config options.
995
995
996 Extensions can monkeypatch this function to perform additional
996 Extensions can monkeypatch this function to perform additional
997 checking.
997 checking.
998
998
999 ``error.RepoError`` should be raised on failure.
999 ``error.RepoError`` should be raised on failure.
1000 """
1000 """
1001 if (
1001 if (
1002 requirementsmod.SPARSE_REQUIREMENT in requirements
1002 requirementsmod.SPARSE_REQUIREMENT in requirements
1003 and not sparse.enabled
1003 and not sparse.enabled
1004 ):
1004 ):
1005 raise error.RepoError(
1005 raise error.RepoError(
1006 _(
1006 _(
1007 b'repository is using sparse feature but '
1007 b'repository is using sparse feature but '
1008 b'sparse is not enabled; enable the '
1008 b'sparse is not enabled; enable the '
1009 b'"sparse" extensions to access'
1009 b'"sparse" extensions to access'
1010 )
1010 )
1011 )
1011 )
1012
1012
1013
1013
1014 def makestore(requirements, path, vfstype):
1014 def makestore(requirements, path, vfstype):
1015 """Construct a storage object for a repository."""
1015 """Construct a storage object for a repository."""
1016 if requirementsmod.STORE_REQUIREMENT in requirements:
1016 if requirementsmod.STORE_REQUIREMENT in requirements:
1017 if requirementsmod.FNCACHE_REQUIREMENT in requirements:
1017 if requirementsmod.FNCACHE_REQUIREMENT in requirements:
1018 dotencode = requirementsmod.DOTENCODE_REQUIREMENT in requirements
1018 dotencode = requirementsmod.DOTENCODE_REQUIREMENT in requirements
1019 return storemod.fncachestore(path, vfstype, dotencode)
1019 return storemod.fncachestore(path, vfstype, dotencode)
1020
1020
1021 return storemod.encodedstore(path, vfstype)
1021 return storemod.encodedstore(path, vfstype)
1022
1022
1023 return storemod.basicstore(path, vfstype)
1023 return storemod.basicstore(path, vfstype)
1024
1024
1025
1025
1026 def resolvestorevfsoptions(ui, requirements, features):
1026 def resolvestorevfsoptions(ui, requirements, features):
1027 """Resolve the options to pass to the store vfs opener.
1027 """Resolve the options to pass to the store vfs opener.
1028
1028
1029 The returned dict is used to influence behavior of the storage layer.
1029 The returned dict is used to influence behavior of the storage layer.
1030 """
1030 """
1031 options = {}
1031 options = {}
1032
1032
1033 if requirementsmod.TREEMANIFEST_REQUIREMENT in requirements:
1033 if requirementsmod.TREEMANIFEST_REQUIREMENT in requirements:
1034 options[b'treemanifest'] = True
1034 options[b'treemanifest'] = True
1035
1035
1036 # experimental config: format.manifestcachesize
1036 # experimental config: format.manifestcachesize
1037 manifestcachesize = ui.configint(b'format', b'manifestcachesize')
1037 manifestcachesize = ui.configint(b'format', b'manifestcachesize')
1038 if manifestcachesize is not None:
1038 if manifestcachesize is not None:
1039 options[b'manifestcachesize'] = manifestcachesize
1039 options[b'manifestcachesize'] = manifestcachesize
1040
1040
1041 # In the absence of another requirement superseding a revlog-related
1041 # In the absence of another requirement superseding a revlog-related
1042 # requirement, we have to assume the repo is using revlog version 0.
1042 # requirement, we have to assume the repo is using revlog version 0.
1043 # This revlog format is super old and we don't bother trying to parse
1043 # This revlog format is super old and we don't bother trying to parse
1044 # opener options for it because those options wouldn't do anything
1044 # opener options for it because those options wouldn't do anything
1045 # meaningful on such old repos.
1045 # meaningful on such old repos.
1046 if (
1046 if (
1047 requirementsmod.REVLOGV1_REQUIREMENT in requirements
1047 requirementsmod.REVLOGV1_REQUIREMENT in requirements
1048 or requirementsmod.REVLOGV2_REQUIREMENT in requirements
1048 or requirementsmod.REVLOGV2_REQUIREMENT in requirements
1049 ):
1049 ):
1050 options.update(resolverevlogstorevfsoptions(ui, requirements, features))
1050 options.update(resolverevlogstorevfsoptions(ui, requirements, features))
1051 else: # explicitly mark repo as using revlogv0
1051 else: # explicitly mark repo as using revlogv0
1052 options[b'revlogv0'] = True
1052 options[b'revlogv0'] = True
1053
1053
1054 if requirementsmod.COPIESSDC_REQUIREMENT in requirements:
1054 if requirementsmod.COPIESSDC_REQUIREMENT in requirements:
1055 options[b'copies-storage'] = b'changeset-sidedata'
1055 options[b'copies-storage'] = b'changeset-sidedata'
1056 else:
1056 else:
1057 writecopiesto = ui.config(b'experimental', b'copies.write-to')
1057 writecopiesto = ui.config(b'experimental', b'copies.write-to')
1058 copiesextramode = (b'changeset-only', b'compatibility')
1058 copiesextramode = (b'changeset-only', b'compatibility')
1059 if writecopiesto in copiesextramode:
1059 if writecopiesto in copiesextramode:
1060 options[b'copies-storage'] = b'extra'
1060 options[b'copies-storage'] = b'extra'
1061
1061
1062 return options
1062 return options
1063
1063
1064
1064
1065 def resolverevlogstorevfsoptions(ui, requirements, features):
1065 def resolverevlogstorevfsoptions(ui, requirements, features):
1066 """Resolve opener options specific to revlogs."""
1066 """Resolve opener options specific to revlogs."""
1067
1067
1068 options = {}
1068 options = {}
1069 options[b'flagprocessors'] = {}
1069 options[b'flagprocessors'] = {}
1070
1070
1071 feature_config = options[b'feature-config'] = revlog.FeatureConfig()
1071 feature_config = options[b'feature-config'] = revlog.FeatureConfig()
1072 data_config = options[b'data-config'] = revlog.DataConfig()
1072 data_config = options[b'data-config'] = revlog.DataConfig()
1073 delta_config = options[b'delta-config'] = revlog.DeltaConfig()
1073 delta_config = options[b'delta-config'] = revlog.DeltaConfig()
1074
1074
1075 if requirementsmod.REVLOGV1_REQUIREMENT in requirements:
1075 if requirementsmod.REVLOGV1_REQUIREMENT in requirements:
1076 options[b'revlogv1'] = True
1076 options[b'revlogv1'] = True
1077 if requirementsmod.REVLOGV2_REQUIREMENT in requirements:
1077 if requirementsmod.REVLOGV2_REQUIREMENT in requirements:
1078 options[b'revlogv2'] = True
1078 options[b'revlogv2'] = True
1079 if requirementsmod.CHANGELOGV2_REQUIREMENT in requirements:
1079 if requirementsmod.CHANGELOGV2_REQUIREMENT in requirements:
1080 options[b'changelogv2'] = True
1080 options[b'changelogv2'] = True
1081 cmp_rank = ui.configbool(b'experimental', b'changelog-v2.compute-rank')
1081 cmp_rank = ui.configbool(b'experimental', b'changelog-v2.compute-rank')
1082 options[b'changelogv2.compute-rank'] = cmp_rank
1082 options[b'changelogv2.compute-rank'] = cmp_rank
1083
1083
1084 if requirementsmod.GENERALDELTA_REQUIREMENT in requirements:
1084 if requirementsmod.GENERALDELTA_REQUIREMENT in requirements:
1085 options[b'generaldelta'] = True
1085 options[b'generaldelta'] = True
1086
1086
1087 # experimental config: format.chunkcachesize
1087 # experimental config: format.chunkcachesize
1088 chunkcachesize = ui.configint(b'format', b'chunkcachesize')
1088 chunkcachesize = ui.configint(b'format', b'chunkcachesize')
1089 if chunkcachesize is not None:
1089 if chunkcachesize is not None:
1090 data_config.chunk_cache_size = chunkcachesize
1090 data_config.chunk_cache_size = chunkcachesize
1091
1091
1092 delta_config.delta_both_parents = ui.configbool(
1092 delta_config.delta_both_parents = ui.configbool(
1093 b'storage', b'revlog.optimize-delta-parent-choice'
1093 b'storage', b'revlog.optimize-delta-parent-choice'
1094 )
1094 )
1095 delta_config.candidate_group_chunk_size = ui.configint(
1095 delta_config.candidate_group_chunk_size = ui.configint(
1096 b'storage',
1096 b'storage',
1097 b'revlog.delta-parent-search.candidate-group-chunk-size',
1097 b'revlog.delta-parent-search.candidate-group-chunk-size',
1098 )
1098 )
1099 delta_config.debug_delta = ui.configbool(b'debug', b'revlog.debug-delta')
1099 delta_config.debug_delta = ui.configbool(b'debug', b'revlog.debug-delta')
1100
1100
1101 issue6528 = ui.configbool(b'storage', b'revlog.issue6528.fix-incoming')
1101 issue6528 = ui.configbool(b'storage', b'revlog.issue6528.fix-incoming')
1102 options[b'issue6528.fix-incoming'] = issue6528
1102 options[b'issue6528.fix-incoming'] = issue6528
1103
1103
1104 lazydelta = ui.configbool(b'storage', b'revlog.reuse-external-delta')
1104 lazydelta = ui.configbool(b'storage', b'revlog.reuse-external-delta')
1105 lazydeltabase = False
1105 lazydeltabase = False
1106 if lazydelta:
1106 if lazydelta:
1107 lazydeltabase = ui.configbool(
1107 lazydeltabase = ui.configbool(
1108 b'storage', b'revlog.reuse-external-delta-parent'
1108 b'storage', b'revlog.reuse-external-delta-parent'
1109 )
1109 )
1110 if lazydeltabase is None:
1110 if lazydeltabase is None:
1111 lazydeltabase = not scmutil.gddeltaconfig(ui)
1111 lazydeltabase = not scmutil.gddeltaconfig(ui)
1112 delta_config.lazy_delta = lazydelta
1112 delta_config.lazy_delta = lazydelta
1113 delta_config.lazy_delta_base = lazydeltabase
1113 delta_config.lazy_delta_base = lazydeltabase
1114
1114
1115 chainspan = ui.configbytes(b'experimental', b'maxdeltachainspan')
1115 chainspan = ui.configbytes(b'experimental', b'maxdeltachainspan')
1116 if 0 <= chainspan:
1116 if 0 <= chainspan:
1117 delta_config.max_deltachain_span = chainspan
1117 delta_config.max_deltachain_span = chainspan
1118
1118
1119 mmapindexthreshold = ui.configbytes(b'experimental', b'mmapindexthreshold')
1119 mmapindexthreshold = ui.configbytes(b'experimental', b'mmapindexthreshold')
1120 if mmapindexthreshold is not None:
1120 if mmapindexthreshold is not None:
1121 data_config.mmap_index_threshold = mmapindexthreshold
1121 data_config.mmap_index_threshold = mmapindexthreshold
1122
1122
1123 withsparseread = ui.configbool(b'experimental', b'sparse-read')
1123 withsparseread = ui.configbool(b'experimental', b'sparse-read')
1124 srdensitythres = float(
1124 srdensitythres = float(
1125 ui.config(b'experimental', b'sparse-read.density-threshold')
1125 ui.config(b'experimental', b'sparse-read.density-threshold')
1126 )
1126 )
1127 srmingapsize = ui.configbytes(b'experimental', b'sparse-read.min-gap-size')
1127 srmingapsize = ui.configbytes(b'experimental', b'sparse-read.min-gap-size')
1128 options[b'with-sparse-read'] = withsparseread
1128 options[b'with-sparse-read'] = withsparseread
1129 options[b'sparse-read-density-threshold'] = srdensitythres
1129 options[b'sparse-read-density-threshold'] = srdensitythres
1130 options[b'sparse-read-min-gap-size'] = srmingapsize
1130 options[b'sparse-read-min-gap-size'] = srmingapsize
1131
1131
1132 sparserevlog = requirementsmod.SPARSEREVLOG_REQUIREMENT in requirements
1132 sparserevlog = requirementsmod.SPARSEREVLOG_REQUIREMENT in requirements
1133 options[b'sparse-revlog'] = sparserevlog
1133 delta_config.sparse_revlog = sparserevlog
1134 if sparserevlog:
1134 if sparserevlog:
1135 options[b'generaldelta'] = True
1135 options[b'generaldelta'] = True
1136
1136
1137 maxchainlen = None
1137 maxchainlen = None
1138 if sparserevlog:
1138 if sparserevlog:
1139 maxchainlen = revlogconst.SPARSE_REVLOG_MAX_CHAIN_LENGTH
1139 maxchainlen = revlogconst.SPARSE_REVLOG_MAX_CHAIN_LENGTH
1140 # experimental config: format.maxchainlen
1140 # experimental config: format.maxchainlen
1141 maxchainlen = ui.configint(b'format', b'maxchainlen', maxchainlen)
1141 maxchainlen = ui.configint(b'format', b'maxchainlen', maxchainlen)
1142 if maxchainlen is not None:
1142 if maxchainlen is not None:
1143 delta_config.max_chain_len = maxchainlen
1143 delta_config.max_chain_len = maxchainlen
1144
1144
1145 for r in requirements:
1145 for r in requirements:
1146 # we allow multiple compression engine requirement to co-exist because
1146 # we allow multiple compression engine requirement to co-exist because
1147 # strickly speaking, revlog seems to support mixed compression style.
1147 # strickly speaking, revlog seems to support mixed compression style.
1148 #
1148 #
1149 # The compression used for new entries will be "the last one"
1149 # The compression used for new entries will be "the last one"
1150 prefix = r.startswith
1150 prefix = r.startswith
1151 if prefix(b'revlog-compression-') or prefix(b'exp-compression-'):
1151 if prefix(b'revlog-compression-') or prefix(b'exp-compression-'):
1152 feature_config.compression_engine = r.split(b'-', 2)[2]
1152 feature_config.compression_engine = r.split(b'-', 2)[2]
1153
1153
1154 zlib_level = ui.configint(b'storage', b'revlog.zlib.level')
1154 zlib_level = ui.configint(b'storage', b'revlog.zlib.level')
1155 if zlib_level is not None:
1155 if zlib_level is not None:
1156 if not (0 <= zlib_level <= 9):
1156 if not (0 <= zlib_level <= 9):
1157 msg = _(b'invalid value for `storage.revlog.zlib.level` config: %d')
1157 msg = _(b'invalid value for `storage.revlog.zlib.level` config: %d')
1158 raise error.Abort(msg % zlib_level)
1158 raise error.Abort(msg % zlib_level)
1159 feature_config.compression_engine_options[b'zlib.level'] = zlib_level
1159 feature_config.compression_engine_options[b'zlib.level'] = zlib_level
1160 zstd_level = ui.configint(b'storage', b'revlog.zstd.level')
1160 zstd_level = ui.configint(b'storage', b'revlog.zstd.level')
1161 if zstd_level is not None:
1161 if zstd_level is not None:
1162 if not (0 <= zstd_level <= 22):
1162 if not (0 <= zstd_level <= 22):
1163 msg = _(b'invalid value for `storage.revlog.zstd.level` config: %d')
1163 msg = _(b'invalid value for `storage.revlog.zstd.level` config: %d')
1164 raise error.Abort(msg % zstd_level)
1164 raise error.Abort(msg % zstd_level)
1165 feature_config.compression_engine_options[b'zstd.level'] = zstd_level
1165 feature_config.compression_engine_options[b'zstd.level'] = zstd_level
1166
1166
1167 if requirementsmod.NARROW_REQUIREMENT in requirements:
1167 if requirementsmod.NARROW_REQUIREMENT in requirements:
1168 feature_config.enable_ellipsis = True
1168 feature_config.enable_ellipsis = True
1169
1169
1170 if ui.configbool(b'experimental', b'rust.index'):
1170 if ui.configbool(b'experimental', b'rust.index'):
1171 options[b'rust.index'] = True
1171 options[b'rust.index'] = True
1172 if requirementsmod.NODEMAP_REQUIREMENT in requirements:
1172 if requirementsmod.NODEMAP_REQUIREMENT in requirements:
1173 slow_path = ui.config(
1173 slow_path = ui.config(
1174 b'storage', b'revlog.persistent-nodemap.slow-path'
1174 b'storage', b'revlog.persistent-nodemap.slow-path'
1175 )
1175 )
1176 if slow_path not in (b'allow', b'warn', b'abort'):
1176 if slow_path not in (b'allow', b'warn', b'abort'):
1177 default = ui.config_default(
1177 default = ui.config_default(
1178 b'storage', b'revlog.persistent-nodemap.slow-path'
1178 b'storage', b'revlog.persistent-nodemap.slow-path'
1179 )
1179 )
1180 msg = _(
1180 msg = _(
1181 b'unknown value for config '
1181 b'unknown value for config '
1182 b'"storage.revlog.persistent-nodemap.slow-path": "%s"\n'
1182 b'"storage.revlog.persistent-nodemap.slow-path": "%s"\n'
1183 )
1183 )
1184 ui.warn(msg % slow_path)
1184 ui.warn(msg % slow_path)
1185 if not ui.quiet:
1185 if not ui.quiet:
1186 ui.warn(_(b'falling back to default value: %s\n') % default)
1186 ui.warn(_(b'falling back to default value: %s\n') % default)
1187 slow_path = default
1187 slow_path = default
1188
1188
1189 msg = _(
1189 msg = _(
1190 b"accessing `persistent-nodemap` repository without associated "
1190 b"accessing `persistent-nodemap` repository without associated "
1191 b"fast implementation."
1191 b"fast implementation."
1192 )
1192 )
1193 hint = _(
1193 hint = _(
1194 b"check `hg help config.format.use-persistent-nodemap` "
1194 b"check `hg help config.format.use-persistent-nodemap` "
1195 b"for details"
1195 b"for details"
1196 )
1196 )
1197 if not revlog.HAS_FAST_PERSISTENT_NODEMAP:
1197 if not revlog.HAS_FAST_PERSISTENT_NODEMAP:
1198 if slow_path == b'warn':
1198 if slow_path == b'warn':
1199 msg = b"warning: " + msg + b'\n'
1199 msg = b"warning: " + msg + b'\n'
1200 ui.warn(msg)
1200 ui.warn(msg)
1201 if not ui.quiet:
1201 if not ui.quiet:
1202 hint = b'(' + hint + b')\n'
1202 hint = b'(' + hint + b')\n'
1203 ui.warn(hint)
1203 ui.warn(hint)
1204 if slow_path == b'abort':
1204 if slow_path == b'abort':
1205 raise error.Abort(msg, hint=hint)
1205 raise error.Abort(msg, hint=hint)
1206 options[b'persistent-nodemap'] = True
1206 options[b'persistent-nodemap'] = True
1207 if requirementsmod.DIRSTATE_V2_REQUIREMENT in requirements:
1207 if requirementsmod.DIRSTATE_V2_REQUIREMENT in requirements:
1208 slow_path = ui.config(b'storage', b'dirstate-v2.slow-path')
1208 slow_path = ui.config(b'storage', b'dirstate-v2.slow-path')
1209 if slow_path not in (b'allow', b'warn', b'abort'):
1209 if slow_path not in (b'allow', b'warn', b'abort'):
1210 default = ui.config_default(b'storage', b'dirstate-v2.slow-path')
1210 default = ui.config_default(b'storage', b'dirstate-v2.slow-path')
1211 msg = _(b'unknown value for config "dirstate-v2.slow-path": "%s"\n')
1211 msg = _(b'unknown value for config "dirstate-v2.slow-path": "%s"\n')
1212 ui.warn(msg % slow_path)
1212 ui.warn(msg % slow_path)
1213 if not ui.quiet:
1213 if not ui.quiet:
1214 ui.warn(_(b'falling back to default value: %s\n') % default)
1214 ui.warn(_(b'falling back to default value: %s\n') % default)
1215 slow_path = default
1215 slow_path = default
1216
1216
1217 msg = _(
1217 msg = _(
1218 b"accessing `dirstate-v2` repository without associated "
1218 b"accessing `dirstate-v2` repository without associated "
1219 b"fast implementation."
1219 b"fast implementation."
1220 )
1220 )
1221 hint = _(
1221 hint = _(
1222 b"check `hg help config.format.use-dirstate-v2` " b"for details"
1222 b"check `hg help config.format.use-dirstate-v2` " b"for details"
1223 )
1223 )
1224 if not dirstate.HAS_FAST_DIRSTATE_V2:
1224 if not dirstate.HAS_FAST_DIRSTATE_V2:
1225 if slow_path == b'warn':
1225 if slow_path == b'warn':
1226 msg = b"warning: " + msg + b'\n'
1226 msg = b"warning: " + msg + b'\n'
1227 ui.warn(msg)
1227 ui.warn(msg)
1228 if not ui.quiet:
1228 if not ui.quiet:
1229 hint = b'(' + hint + b')\n'
1229 hint = b'(' + hint + b')\n'
1230 ui.warn(hint)
1230 ui.warn(hint)
1231 if slow_path == b'abort':
1231 if slow_path == b'abort':
1232 raise error.Abort(msg, hint=hint)
1232 raise error.Abort(msg, hint=hint)
1233 if ui.configbool(b'storage', b'revlog.persistent-nodemap.mmap'):
1233 if ui.configbool(b'storage', b'revlog.persistent-nodemap.mmap'):
1234 options[b'persistent-nodemap.mmap'] = True
1234 options[b'persistent-nodemap.mmap'] = True
1235 if ui.configbool(b'devel', b'persistent-nodemap'):
1235 if ui.configbool(b'devel', b'persistent-nodemap'):
1236 options[b'devel-force-nodemap'] = True
1236 options[b'devel-force-nodemap'] = True
1237
1237
1238 return options
1238 return options
1239
1239
1240
1240
1241 def makemain(**kwargs):
1241 def makemain(**kwargs):
1242 """Produce a type conforming to ``ilocalrepositorymain``."""
1242 """Produce a type conforming to ``ilocalrepositorymain``."""
1243 return localrepository
1243 return localrepository
1244
1244
1245
1245
1246 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1246 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1247 class revlogfilestorage:
1247 class revlogfilestorage:
1248 """File storage when using revlogs."""
1248 """File storage when using revlogs."""
1249
1249
1250 def file(self, path):
1250 def file(self, path):
1251 if path.startswith(b'/'):
1251 if path.startswith(b'/'):
1252 path = path[1:]
1252 path = path[1:]
1253
1253
1254 try_split = (
1254 try_split = (
1255 self.currenttransaction() is not None
1255 self.currenttransaction() is not None
1256 or txnutil.mayhavepending(self.root)
1256 or txnutil.mayhavepending(self.root)
1257 )
1257 )
1258
1258
1259 return filelog.filelog(self.svfs, path, try_split=try_split)
1259 return filelog.filelog(self.svfs, path, try_split=try_split)
1260
1260
1261
1261
1262 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1262 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1263 class revlognarrowfilestorage:
1263 class revlognarrowfilestorage:
1264 """File storage when using revlogs and narrow files."""
1264 """File storage when using revlogs and narrow files."""
1265
1265
1266 def file(self, path):
1266 def file(self, path):
1267 if path.startswith(b'/'):
1267 if path.startswith(b'/'):
1268 path = path[1:]
1268 path = path[1:]
1269
1269
1270 try_split = (
1270 try_split = (
1271 self.currenttransaction() is not None
1271 self.currenttransaction() is not None
1272 or txnutil.mayhavepending(self.root)
1272 or txnutil.mayhavepending(self.root)
1273 )
1273 )
1274 return filelog.narrowfilelog(
1274 return filelog.narrowfilelog(
1275 self.svfs, path, self._storenarrowmatch, try_split=try_split
1275 self.svfs, path, self._storenarrowmatch, try_split=try_split
1276 )
1276 )
1277
1277
1278
1278
1279 def makefilestorage(requirements, features, **kwargs):
1279 def makefilestorage(requirements, features, **kwargs):
1280 """Produce a type conforming to ``ilocalrepositoryfilestorage``."""
1280 """Produce a type conforming to ``ilocalrepositoryfilestorage``."""
1281 features.add(repository.REPO_FEATURE_REVLOG_FILE_STORAGE)
1281 features.add(repository.REPO_FEATURE_REVLOG_FILE_STORAGE)
1282 features.add(repository.REPO_FEATURE_STREAM_CLONE)
1282 features.add(repository.REPO_FEATURE_STREAM_CLONE)
1283
1283
1284 if requirementsmod.NARROW_REQUIREMENT in requirements:
1284 if requirementsmod.NARROW_REQUIREMENT in requirements:
1285 return revlognarrowfilestorage
1285 return revlognarrowfilestorage
1286 else:
1286 else:
1287 return revlogfilestorage
1287 return revlogfilestorage
1288
1288
1289
1289
1290 # List of repository interfaces and factory functions for them. Each
1290 # List of repository interfaces and factory functions for them. Each
1291 # will be called in order during ``makelocalrepository()`` to iteratively
1291 # will be called in order during ``makelocalrepository()`` to iteratively
1292 # derive the final type for a local repository instance. We capture the
1292 # derive the final type for a local repository instance. We capture the
1293 # function as a lambda so we don't hold a reference and the module-level
1293 # function as a lambda so we don't hold a reference and the module-level
1294 # functions can be wrapped.
1294 # functions can be wrapped.
1295 REPO_INTERFACES = [
1295 REPO_INTERFACES = [
1296 (repository.ilocalrepositorymain, lambda: makemain),
1296 (repository.ilocalrepositorymain, lambda: makemain),
1297 (repository.ilocalrepositoryfilestorage, lambda: makefilestorage),
1297 (repository.ilocalrepositoryfilestorage, lambda: makefilestorage),
1298 ]
1298 ]
1299
1299
1300
1300
1301 @interfaceutil.implementer(repository.ilocalrepositorymain)
1301 @interfaceutil.implementer(repository.ilocalrepositorymain)
1302 class localrepository:
1302 class localrepository:
1303 """Main class for representing local repositories.
1303 """Main class for representing local repositories.
1304
1304
1305 All local repositories are instances of this class.
1305 All local repositories are instances of this class.
1306
1306
1307 Constructed on its own, instances of this class are not usable as
1307 Constructed on its own, instances of this class are not usable as
1308 repository objects. To obtain a usable repository object, call
1308 repository objects. To obtain a usable repository object, call
1309 ``hg.repository()``, ``localrepo.instance()``, or
1309 ``hg.repository()``, ``localrepo.instance()``, or
1310 ``localrepo.makelocalrepository()``. The latter is the lowest-level.
1310 ``localrepo.makelocalrepository()``. The latter is the lowest-level.
1311 ``instance()`` adds support for creating new repositories.
1311 ``instance()`` adds support for creating new repositories.
1312 ``hg.repository()`` adds more extension integration, including calling
1312 ``hg.repository()`` adds more extension integration, including calling
1313 ``reposetup()``. Generally speaking, ``hg.repository()`` should be
1313 ``reposetup()``. Generally speaking, ``hg.repository()`` should be
1314 used.
1314 used.
1315 """
1315 """
1316
1316
1317 _basesupported = {
1317 _basesupported = {
1318 requirementsmod.ARCHIVED_PHASE_REQUIREMENT,
1318 requirementsmod.ARCHIVED_PHASE_REQUIREMENT,
1319 requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT,
1319 requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT,
1320 requirementsmod.CHANGELOGV2_REQUIREMENT,
1320 requirementsmod.CHANGELOGV2_REQUIREMENT,
1321 requirementsmod.COPIESSDC_REQUIREMENT,
1321 requirementsmod.COPIESSDC_REQUIREMENT,
1322 requirementsmod.DIRSTATE_TRACKED_HINT_V1,
1322 requirementsmod.DIRSTATE_TRACKED_HINT_V1,
1323 requirementsmod.DIRSTATE_V2_REQUIREMENT,
1323 requirementsmod.DIRSTATE_V2_REQUIREMENT,
1324 requirementsmod.DOTENCODE_REQUIREMENT,
1324 requirementsmod.DOTENCODE_REQUIREMENT,
1325 requirementsmod.FNCACHE_REQUIREMENT,
1325 requirementsmod.FNCACHE_REQUIREMENT,
1326 requirementsmod.GENERALDELTA_REQUIREMENT,
1326 requirementsmod.GENERALDELTA_REQUIREMENT,
1327 requirementsmod.INTERNAL_PHASE_REQUIREMENT,
1327 requirementsmod.INTERNAL_PHASE_REQUIREMENT,
1328 requirementsmod.NODEMAP_REQUIREMENT,
1328 requirementsmod.NODEMAP_REQUIREMENT,
1329 requirementsmod.RELATIVE_SHARED_REQUIREMENT,
1329 requirementsmod.RELATIVE_SHARED_REQUIREMENT,
1330 requirementsmod.REVLOGV1_REQUIREMENT,
1330 requirementsmod.REVLOGV1_REQUIREMENT,
1331 requirementsmod.REVLOGV2_REQUIREMENT,
1331 requirementsmod.REVLOGV2_REQUIREMENT,
1332 requirementsmod.SHARED_REQUIREMENT,
1332 requirementsmod.SHARED_REQUIREMENT,
1333 requirementsmod.SHARESAFE_REQUIREMENT,
1333 requirementsmod.SHARESAFE_REQUIREMENT,
1334 requirementsmod.SPARSE_REQUIREMENT,
1334 requirementsmod.SPARSE_REQUIREMENT,
1335 requirementsmod.SPARSEREVLOG_REQUIREMENT,
1335 requirementsmod.SPARSEREVLOG_REQUIREMENT,
1336 requirementsmod.STORE_REQUIREMENT,
1336 requirementsmod.STORE_REQUIREMENT,
1337 requirementsmod.TREEMANIFEST_REQUIREMENT,
1337 requirementsmod.TREEMANIFEST_REQUIREMENT,
1338 }
1338 }
1339
1339
1340 # list of prefix for file which can be written without 'wlock'
1340 # list of prefix for file which can be written without 'wlock'
1341 # Extensions should extend this list when needed
1341 # Extensions should extend this list when needed
1342 _wlockfreeprefix = {
1342 _wlockfreeprefix = {
1343 # We migh consider requiring 'wlock' for the next
1343 # We migh consider requiring 'wlock' for the next
1344 # two, but pretty much all the existing code assume
1344 # two, but pretty much all the existing code assume
1345 # wlock is not needed so we keep them excluded for
1345 # wlock is not needed so we keep them excluded for
1346 # now.
1346 # now.
1347 b'hgrc',
1347 b'hgrc',
1348 b'requires',
1348 b'requires',
1349 # XXX cache is a complicatged business someone
1349 # XXX cache is a complicatged business someone
1350 # should investigate this in depth at some point
1350 # should investigate this in depth at some point
1351 b'cache/',
1351 b'cache/',
1352 # XXX bisect was still a bit too messy at the time
1352 # XXX bisect was still a bit too messy at the time
1353 # this changeset was introduced. Someone should fix
1353 # this changeset was introduced. Someone should fix
1354 # the remainig bit and drop this line
1354 # the remainig bit and drop this line
1355 b'bisect.state',
1355 b'bisect.state',
1356 }
1356 }
1357
1357
1358 def __init__(
1358 def __init__(
1359 self,
1359 self,
1360 baseui,
1360 baseui,
1361 ui,
1361 ui,
1362 origroot: bytes,
1362 origroot: bytes,
1363 wdirvfs: vfsmod.vfs,
1363 wdirvfs: vfsmod.vfs,
1364 hgvfs: vfsmod.vfs,
1364 hgvfs: vfsmod.vfs,
1365 requirements,
1365 requirements,
1366 supportedrequirements,
1366 supportedrequirements,
1367 sharedpath: bytes,
1367 sharedpath: bytes,
1368 store,
1368 store,
1369 cachevfs: vfsmod.vfs,
1369 cachevfs: vfsmod.vfs,
1370 wcachevfs: vfsmod.vfs,
1370 wcachevfs: vfsmod.vfs,
1371 features,
1371 features,
1372 intents=None,
1372 intents=None,
1373 ):
1373 ):
1374 """Create a new local repository instance.
1374 """Create a new local repository instance.
1375
1375
1376 Most callers should use ``hg.repository()``, ``localrepo.instance()``,
1376 Most callers should use ``hg.repository()``, ``localrepo.instance()``,
1377 or ``localrepo.makelocalrepository()`` for obtaining a new repository
1377 or ``localrepo.makelocalrepository()`` for obtaining a new repository
1378 object.
1378 object.
1379
1379
1380 Arguments:
1380 Arguments:
1381
1381
1382 baseui
1382 baseui
1383 ``ui.ui`` instance that ``ui`` argument was based off of.
1383 ``ui.ui`` instance that ``ui`` argument was based off of.
1384
1384
1385 ui
1385 ui
1386 ``ui.ui`` instance for use by the repository.
1386 ``ui.ui`` instance for use by the repository.
1387
1387
1388 origroot
1388 origroot
1389 ``bytes`` path to working directory root of this repository.
1389 ``bytes`` path to working directory root of this repository.
1390
1390
1391 wdirvfs
1391 wdirvfs
1392 ``vfs.vfs`` rooted at the working directory.
1392 ``vfs.vfs`` rooted at the working directory.
1393
1393
1394 hgvfs
1394 hgvfs
1395 ``vfs.vfs`` rooted at .hg/
1395 ``vfs.vfs`` rooted at .hg/
1396
1396
1397 requirements
1397 requirements
1398 ``set`` of bytestrings representing repository opening requirements.
1398 ``set`` of bytestrings representing repository opening requirements.
1399
1399
1400 supportedrequirements
1400 supportedrequirements
1401 ``set`` of bytestrings representing repository requirements that we
1401 ``set`` of bytestrings representing repository requirements that we
1402 know how to open. May be a supetset of ``requirements``.
1402 know how to open. May be a supetset of ``requirements``.
1403
1403
1404 sharedpath
1404 sharedpath
1405 ``bytes`` Defining path to storage base directory. Points to a
1405 ``bytes`` Defining path to storage base directory. Points to a
1406 ``.hg/`` directory somewhere.
1406 ``.hg/`` directory somewhere.
1407
1407
1408 store
1408 store
1409 ``store.basicstore`` (or derived) instance providing access to
1409 ``store.basicstore`` (or derived) instance providing access to
1410 versioned storage.
1410 versioned storage.
1411
1411
1412 cachevfs
1412 cachevfs
1413 ``vfs.vfs`` used for cache files.
1413 ``vfs.vfs`` used for cache files.
1414
1414
1415 wcachevfs
1415 wcachevfs
1416 ``vfs.vfs`` used for cache files related to the working copy.
1416 ``vfs.vfs`` used for cache files related to the working copy.
1417
1417
1418 features
1418 features
1419 ``set`` of bytestrings defining features/capabilities of this
1419 ``set`` of bytestrings defining features/capabilities of this
1420 instance.
1420 instance.
1421
1421
1422 intents
1422 intents
1423 ``set`` of system strings indicating what this repo will be used
1423 ``set`` of system strings indicating what this repo will be used
1424 for.
1424 for.
1425 """
1425 """
1426 self.baseui = baseui
1426 self.baseui = baseui
1427 self.ui = ui
1427 self.ui = ui
1428 self.origroot = origroot
1428 self.origroot = origroot
1429 # vfs rooted at working directory.
1429 # vfs rooted at working directory.
1430 self.wvfs = wdirvfs
1430 self.wvfs = wdirvfs
1431 self.root = wdirvfs.base
1431 self.root = wdirvfs.base
1432 # vfs rooted at .hg/. Used to access most non-store paths.
1432 # vfs rooted at .hg/. Used to access most non-store paths.
1433 self.vfs = hgvfs
1433 self.vfs = hgvfs
1434 self.path = hgvfs.base
1434 self.path = hgvfs.base
1435 self.requirements = requirements
1435 self.requirements = requirements
1436 self.nodeconstants = sha1nodeconstants
1436 self.nodeconstants = sha1nodeconstants
1437 self.nullid = self.nodeconstants.nullid
1437 self.nullid = self.nodeconstants.nullid
1438 self.supported = supportedrequirements
1438 self.supported = supportedrequirements
1439 self.sharedpath = sharedpath
1439 self.sharedpath = sharedpath
1440 self.store = store
1440 self.store = store
1441 self.cachevfs = cachevfs
1441 self.cachevfs = cachevfs
1442 self.wcachevfs = wcachevfs
1442 self.wcachevfs = wcachevfs
1443 self.features = features
1443 self.features = features
1444
1444
1445 self.filtername = None
1445 self.filtername = None
1446
1446
1447 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1447 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1448 b'devel', b'check-locks'
1448 b'devel', b'check-locks'
1449 ):
1449 ):
1450 self.vfs.audit = self._getvfsward(self.vfs.audit)
1450 self.vfs.audit = self._getvfsward(self.vfs.audit)
1451 # A list of callback to shape the phase if no data were found.
1451 # A list of callback to shape the phase if no data were found.
1452 # Callback are in the form: func(repo, roots) --> processed root.
1452 # Callback are in the form: func(repo, roots) --> processed root.
1453 # This list it to be filled by extension during repo setup
1453 # This list it to be filled by extension during repo setup
1454 self._phasedefaults = []
1454 self._phasedefaults = []
1455
1455
1456 color.setup(self.ui)
1456 color.setup(self.ui)
1457
1457
1458 self.spath = self.store.path
1458 self.spath = self.store.path
1459 self.svfs = self.store.vfs
1459 self.svfs = self.store.vfs
1460 self.sjoin = self.store.join
1460 self.sjoin = self.store.join
1461 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1461 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1462 b'devel', b'check-locks'
1462 b'devel', b'check-locks'
1463 ):
1463 ):
1464 if hasattr(self.svfs, 'vfs'): # this is filtervfs
1464 if hasattr(self.svfs, 'vfs'): # this is filtervfs
1465 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
1465 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
1466 else: # standard vfs
1466 else: # standard vfs
1467 self.svfs.audit = self._getsvfsward(self.svfs.audit)
1467 self.svfs.audit = self._getsvfsward(self.svfs.audit)
1468
1468
1469 self._dirstatevalidatewarned = False
1469 self._dirstatevalidatewarned = False
1470
1470
1471 self._branchcaches = branchmap.BranchMapCache()
1471 self._branchcaches = branchmap.BranchMapCache()
1472 self._revbranchcache = None
1472 self._revbranchcache = None
1473 self._filterpats = {}
1473 self._filterpats = {}
1474 self._datafilters = {}
1474 self._datafilters = {}
1475 self._transref = self._lockref = self._wlockref = None
1475 self._transref = self._lockref = self._wlockref = None
1476
1476
1477 # A cache for various files under .hg/ that tracks file changes,
1477 # A cache for various files under .hg/ that tracks file changes,
1478 # (used by the filecache decorator)
1478 # (used by the filecache decorator)
1479 #
1479 #
1480 # Maps a property name to its util.filecacheentry
1480 # Maps a property name to its util.filecacheentry
1481 self._filecache = {}
1481 self._filecache = {}
1482
1482
1483 # hold sets of revision to be filtered
1483 # hold sets of revision to be filtered
1484 # should be cleared when something might have changed the filter value:
1484 # should be cleared when something might have changed the filter value:
1485 # - new changesets,
1485 # - new changesets,
1486 # - phase change,
1486 # - phase change,
1487 # - new obsolescence marker,
1487 # - new obsolescence marker,
1488 # - working directory parent change,
1488 # - working directory parent change,
1489 # - bookmark changes
1489 # - bookmark changes
1490 self.filteredrevcache = {}
1490 self.filteredrevcache = {}
1491
1491
1492 self._dirstate = None
1492 self._dirstate = None
1493 # post-dirstate-status hooks
1493 # post-dirstate-status hooks
1494 self._postdsstatus = []
1494 self._postdsstatus = []
1495
1495
1496 self._pending_narrow_pats = None
1496 self._pending_narrow_pats = None
1497 self._pending_narrow_pats_dirstate = None
1497 self._pending_narrow_pats_dirstate = None
1498
1498
1499 # generic mapping between names and nodes
1499 # generic mapping between names and nodes
1500 self.names = namespaces.namespaces()
1500 self.names = namespaces.namespaces()
1501
1501
1502 # Key to signature value.
1502 # Key to signature value.
1503 self._sparsesignaturecache = {}
1503 self._sparsesignaturecache = {}
1504 # Signature to cached matcher instance.
1504 # Signature to cached matcher instance.
1505 self._sparsematchercache = {}
1505 self._sparsematchercache = {}
1506
1506
1507 self._extrafilterid = repoview.extrafilter(ui)
1507 self._extrafilterid = repoview.extrafilter(ui)
1508
1508
1509 self.filecopiesmode = None
1509 self.filecopiesmode = None
1510 if requirementsmod.COPIESSDC_REQUIREMENT in self.requirements:
1510 if requirementsmod.COPIESSDC_REQUIREMENT in self.requirements:
1511 self.filecopiesmode = b'changeset-sidedata'
1511 self.filecopiesmode = b'changeset-sidedata'
1512
1512
1513 self._wanted_sidedata = set()
1513 self._wanted_sidedata = set()
1514 self._sidedata_computers = {}
1514 self._sidedata_computers = {}
1515 sidedatamod.set_sidedata_spec_for_repo(self)
1515 sidedatamod.set_sidedata_spec_for_repo(self)
1516
1516
1517 def _getvfsward(self, origfunc):
1517 def _getvfsward(self, origfunc):
1518 """build a ward for self.vfs"""
1518 """build a ward for self.vfs"""
1519 rref = weakref.ref(self)
1519 rref = weakref.ref(self)
1520
1520
1521 def checkvfs(path, mode=None):
1521 def checkvfs(path, mode=None):
1522 ret = origfunc(path, mode=mode)
1522 ret = origfunc(path, mode=mode)
1523 repo = rref()
1523 repo = rref()
1524 if (
1524 if (
1525 repo is None
1525 repo is None
1526 or not hasattr(repo, '_wlockref')
1526 or not hasattr(repo, '_wlockref')
1527 or not hasattr(repo, '_lockref')
1527 or not hasattr(repo, '_lockref')
1528 ):
1528 ):
1529 return
1529 return
1530 if mode in (None, b'r', b'rb'):
1530 if mode in (None, b'r', b'rb'):
1531 return
1531 return
1532 if path.startswith(repo.path):
1532 if path.startswith(repo.path):
1533 # truncate name relative to the repository (.hg)
1533 # truncate name relative to the repository (.hg)
1534 path = path[len(repo.path) + 1 :]
1534 path = path[len(repo.path) + 1 :]
1535 if path.startswith(b'cache/'):
1535 if path.startswith(b'cache/'):
1536 msg = b'accessing cache with vfs instead of cachevfs: "%s"'
1536 msg = b'accessing cache with vfs instead of cachevfs: "%s"'
1537 repo.ui.develwarn(msg % path, stacklevel=3, config=b"cache-vfs")
1537 repo.ui.develwarn(msg % path, stacklevel=3, config=b"cache-vfs")
1538 # path prefixes covered by 'lock'
1538 # path prefixes covered by 'lock'
1539 vfs_path_prefixes = (
1539 vfs_path_prefixes = (
1540 b'journal.',
1540 b'journal.',
1541 b'undo.',
1541 b'undo.',
1542 b'strip-backup/',
1542 b'strip-backup/',
1543 b'cache/',
1543 b'cache/',
1544 )
1544 )
1545 if any(path.startswith(prefix) for prefix in vfs_path_prefixes):
1545 if any(path.startswith(prefix) for prefix in vfs_path_prefixes):
1546 if repo._currentlock(repo._lockref) is None:
1546 if repo._currentlock(repo._lockref) is None:
1547 repo.ui.develwarn(
1547 repo.ui.develwarn(
1548 b'write with no lock: "%s"' % path,
1548 b'write with no lock: "%s"' % path,
1549 stacklevel=3,
1549 stacklevel=3,
1550 config=b'check-locks',
1550 config=b'check-locks',
1551 )
1551 )
1552 elif repo._currentlock(repo._wlockref) is None:
1552 elif repo._currentlock(repo._wlockref) is None:
1553 # rest of vfs files are covered by 'wlock'
1553 # rest of vfs files are covered by 'wlock'
1554 #
1554 #
1555 # exclude special files
1555 # exclude special files
1556 for prefix in self._wlockfreeprefix:
1556 for prefix in self._wlockfreeprefix:
1557 if path.startswith(prefix):
1557 if path.startswith(prefix):
1558 return
1558 return
1559 repo.ui.develwarn(
1559 repo.ui.develwarn(
1560 b'write with no wlock: "%s"' % path,
1560 b'write with no wlock: "%s"' % path,
1561 stacklevel=3,
1561 stacklevel=3,
1562 config=b'check-locks',
1562 config=b'check-locks',
1563 )
1563 )
1564 return ret
1564 return ret
1565
1565
1566 return checkvfs
1566 return checkvfs
1567
1567
1568 def _getsvfsward(self, origfunc):
1568 def _getsvfsward(self, origfunc):
1569 """build a ward for self.svfs"""
1569 """build a ward for self.svfs"""
1570 rref = weakref.ref(self)
1570 rref = weakref.ref(self)
1571
1571
1572 def checksvfs(path, mode=None):
1572 def checksvfs(path, mode=None):
1573 ret = origfunc(path, mode=mode)
1573 ret = origfunc(path, mode=mode)
1574 repo = rref()
1574 repo = rref()
1575 if repo is None or not hasattr(repo, '_lockref'):
1575 if repo is None or not hasattr(repo, '_lockref'):
1576 return
1576 return
1577 if mode in (None, b'r', b'rb'):
1577 if mode in (None, b'r', b'rb'):
1578 return
1578 return
1579 if path.startswith(repo.sharedpath):
1579 if path.startswith(repo.sharedpath):
1580 # truncate name relative to the repository (.hg)
1580 # truncate name relative to the repository (.hg)
1581 path = path[len(repo.sharedpath) + 1 :]
1581 path = path[len(repo.sharedpath) + 1 :]
1582 if repo._currentlock(repo._lockref) is None:
1582 if repo._currentlock(repo._lockref) is None:
1583 repo.ui.develwarn(
1583 repo.ui.develwarn(
1584 b'write with no lock: "%s"' % path, stacklevel=4
1584 b'write with no lock: "%s"' % path, stacklevel=4
1585 )
1585 )
1586 return ret
1586 return ret
1587
1587
1588 return checksvfs
1588 return checksvfs
1589
1589
1590 @property
1590 @property
1591 def vfs_map(self):
1591 def vfs_map(self):
1592 return {
1592 return {
1593 b'': self.svfs,
1593 b'': self.svfs,
1594 b'plain': self.vfs,
1594 b'plain': self.vfs,
1595 b'store': self.svfs,
1595 b'store': self.svfs,
1596 }
1596 }
1597
1597
1598 def close(self):
1598 def close(self):
1599 self._writecaches()
1599 self._writecaches()
1600
1600
1601 def _writecaches(self):
1601 def _writecaches(self):
1602 if self._revbranchcache:
1602 if self._revbranchcache:
1603 self._revbranchcache.write()
1603 self._revbranchcache.write()
1604
1604
1605 def _restrictcapabilities(self, caps):
1605 def _restrictcapabilities(self, caps):
1606 if self.ui.configbool(b'experimental', b'bundle2-advertise'):
1606 if self.ui.configbool(b'experimental', b'bundle2-advertise'):
1607 caps = set(caps)
1607 caps = set(caps)
1608 capsblob = bundle2.encodecaps(
1608 capsblob = bundle2.encodecaps(
1609 bundle2.getrepocaps(self, role=b'client')
1609 bundle2.getrepocaps(self, role=b'client')
1610 )
1610 )
1611 caps.add(b'bundle2=' + urlreq.quote(capsblob))
1611 caps.add(b'bundle2=' + urlreq.quote(capsblob))
1612 if self.ui.configbool(b'experimental', b'narrow'):
1612 if self.ui.configbool(b'experimental', b'narrow'):
1613 caps.add(wireprototypes.NARROWCAP)
1613 caps.add(wireprototypes.NARROWCAP)
1614 return caps
1614 return caps
1615
1615
1616 # Don't cache auditor/nofsauditor, or you'll end up with reference cycle:
1616 # Don't cache auditor/nofsauditor, or you'll end up with reference cycle:
1617 # self -> auditor -> self._checknested -> self
1617 # self -> auditor -> self._checknested -> self
1618
1618
1619 @property
1619 @property
1620 def auditor(self):
1620 def auditor(self):
1621 # This is only used by context.workingctx.match in order to
1621 # This is only used by context.workingctx.match in order to
1622 # detect files in subrepos.
1622 # detect files in subrepos.
1623 return pathutil.pathauditor(self.root, callback=self._checknested)
1623 return pathutil.pathauditor(self.root, callback=self._checknested)
1624
1624
1625 @property
1625 @property
1626 def nofsauditor(self):
1626 def nofsauditor(self):
1627 # This is only used by context.basectx.match in order to detect
1627 # This is only used by context.basectx.match in order to detect
1628 # files in subrepos.
1628 # files in subrepos.
1629 return pathutil.pathauditor(
1629 return pathutil.pathauditor(
1630 self.root, callback=self._checknested, realfs=False, cached=True
1630 self.root, callback=self._checknested, realfs=False, cached=True
1631 )
1631 )
1632
1632
1633 def _checknested(self, path):
1633 def _checknested(self, path):
1634 """Determine if path is a legal nested repository."""
1634 """Determine if path is a legal nested repository."""
1635 if not path.startswith(self.root):
1635 if not path.startswith(self.root):
1636 return False
1636 return False
1637 subpath = path[len(self.root) + 1 :]
1637 subpath = path[len(self.root) + 1 :]
1638 normsubpath = util.pconvert(subpath)
1638 normsubpath = util.pconvert(subpath)
1639
1639
1640 # XXX: Checking against the current working copy is wrong in
1640 # XXX: Checking against the current working copy is wrong in
1641 # the sense that it can reject things like
1641 # the sense that it can reject things like
1642 #
1642 #
1643 # $ hg cat -r 10 sub/x.txt
1643 # $ hg cat -r 10 sub/x.txt
1644 #
1644 #
1645 # if sub/ is no longer a subrepository in the working copy
1645 # if sub/ is no longer a subrepository in the working copy
1646 # parent revision.
1646 # parent revision.
1647 #
1647 #
1648 # However, it can of course also allow things that would have
1648 # However, it can of course also allow things that would have
1649 # been rejected before, such as the above cat command if sub/
1649 # been rejected before, such as the above cat command if sub/
1650 # is a subrepository now, but was a normal directory before.
1650 # is a subrepository now, but was a normal directory before.
1651 # The old path auditor would have rejected by mistake since it
1651 # The old path auditor would have rejected by mistake since it
1652 # panics when it sees sub/.hg/.
1652 # panics when it sees sub/.hg/.
1653 #
1653 #
1654 # All in all, checking against the working copy seems sensible
1654 # All in all, checking against the working copy seems sensible
1655 # since we want to prevent access to nested repositories on
1655 # since we want to prevent access to nested repositories on
1656 # the filesystem *now*.
1656 # the filesystem *now*.
1657 ctx = self[None]
1657 ctx = self[None]
1658 parts = util.splitpath(subpath)
1658 parts = util.splitpath(subpath)
1659 while parts:
1659 while parts:
1660 prefix = b'/'.join(parts)
1660 prefix = b'/'.join(parts)
1661 if prefix in ctx.substate:
1661 if prefix in ctx.substate:
1662 if prefix == normsubpath:
1662 if prefix == normsubpath:
1663 return True
1663 return True
1664 else:
1664 else:
1665 sub = ctx.sub(prefix)
1665 sub = ctx.sub(prefix)
1666 return sub.checknested(subpath[len(prefix) + 1 :])
1666 return sub.checknested(subpath[len(prefix) + 1 :])
1667 else:
1667 else:
1668 parts.pop()
1668 parts.pop()
1669 return False
1669 return False
1670
1670
1671 def peer(self, path=None, remotehidden=False):
1671 def peer(self, path=None, remotehidden=False):
1672 return localpeer(
1672 return localpeer(
1673 self, path=path, remotehidden=remotehidden
1673 self, path=path, remotehidden=remotehidden
1674 ) # not cached to avoid reference cycle
1674 ) # not cached to avoid reference cycle
1675
1675
1676 def unfiltered(self):
1676 def unfiltered(self):
1677 """Return unfiltered version of the repository
1677 """Return unfiltered version of the repository
1678
1678
1679 Intended to be overwritten by filtered repo."""
1679 Intended to be overwritten by filtered repo."""
1680 return self
1680 return self
1681
1681
1682 def filtered(self, name, visibilityexceptions=None):
1682 def filtered(self, name, visibilityexceptions=None):
1683 """Return a filtered version of a repository
1683 """Return a filtered version of a repository
1684
1684
1685 The `name` parameter is the identifier of the requested view. This
1685 The `name` parameter is the identifier of the requested view. This
1686 will return a repoview object set "exactly" to the specified view.
1686 will return a repoview object set "exactly" to the specified view.
1687
1687
1688 This function does not apply recursive filtering to a repository. For
1688 This function does not apply recursive filtering to a repository. For
1689 example calling `repo.filtered("served")` will return a repoview using
1689 example calling `repo.filtered("served")` will return a repoview using
1690 the "served" view, regardless of the initial view used by `repo`.
1690 the "served" view, regardless of the initial view used by `repo`.
1691
1691
1692 In other word, there is always only one level of `repoview` "filtering".
1692 In other word, there is always only one level of `repoview` "filtering".
1693 """
1693 """
1694 if self._extrafilterid is not None and b'%' not in name:
1694 if self._extrafilterid is not None and b'%' not in name:
1695 name = name + b'%' + self._extrafilterid
1695 name = name + b'%' + self._extrafilterid
1696
1696
1697 cls = repoview.newtype(self.unfiltered().__class__)
1697 cls = repoview.newtype(self.unfiltered().__class__)
1698 return cls(self, name, visibilityexceptions)
1698 return cls(self, name, visibilityexceptions)
1699
1699
1700 @mixedrepostorecache(
1700 @mixedrepostorecache(
1701 (b'bookmarks', b'plain'),
1701 (b'bookmarks', b'plain'),
1702 (b'bookmarks.current', b'plain'),
1702 (b'bookmarks.current', b'plain'),
1703 (b'bookmarks', b''),
1703 (b'bookmarks', b''),
1704 (b'00changelog.i', b''),
1704 (b'00changelog.i', b''),
1705 )
1705 )
1706 def _bookmarks(self):
1706 def _bookmarks(self):
1707 # Since the multiple files involved in the transaction cannot be
1707 # Since the multiple files involved in the transaction cannot be
1708 # written atomically (with current repository format), there is a race
1708 # written atomically (with current repository format), there is a race
1709 # condition here.
1709 # condition here.
1710 #
1710 #
1711 # 1) changelog content A is read
1711 # 1) changelog content A is read
1712 # 2) outside transaction update changelog to content B
1712 # 2) outside transaction update changelog to content B
1713 # 3) outside transaction update bookmark file referring to content B
1713 # 3) outside transaction update bookmark file referring to content B
1714 # 4) bookmarks file content is read and filtered against changelog-A
1714 # 4) bookmarks file content is read and filtered against changelog-A
1715 #
1715 #
1716 # When this happens, bookmarks against nodes missing from A are dropped.
1716 # When this happens, bookmarks against nodes missing from A are dropped.
1717 #
1717 #
1718 # Having this happening during read is not great, but it become worse
1718 # Having this happening during read is not great, but it become worse
1719 # when this happen during write because the bookmarks to the "unknown"
1719 # when this happen during write because the bookmarks to the "unknown"
1720 # nodes will be dropped for good. However, writes happen within locks.
1720 # nodes will be dropped for good. However, writes happen within locks.
1721 # This locking makes it possible to have a race free consistent read.
1721 # This locking makes it possible to have a race free consistent read.
1722 # For this purpose data read from disc before locking are
1722 # For this purpose data read from disc before locking are
1723 # "invalidated" right after the locks are taken. This invalidations are
1723 # "invalidated" right after the locks are taken. This invalidations are
1724 # "light", the `filecache` mechanism keep the data in memory and will
1724 # "light", the `filecache` mechanism keep the data in memory and will
1725 # reuse them if the underlying files did not changed. Not parsing the
1725 # reuse them if the underlying files did not changed. Not parsing the
1726 # same data multiple times helps performances.
1726 # same data multiple times helps performances.
1727 #
1727 #
1728 # Unfortunately in the case describe above, the files tracked by the
1728 # Unfortunately in the case describe above, the files tracked by the
1729 # bookmarks file cache might not have changed, but the in-memory
1729 # bookmarks file cache might not have changed, but the in-memory
1730 # content is still "wrong" because we used an older changelog content
1730 # content is still "wrong" because we used an older changelog content
1731 # to process the on-disk data. So after locking, the changelog would be
1731 # to process the on-disk data. So after locking, the changelog would be
1732 # refreshed but `_bookmarks` would be preserved.
1732 # refreshed but `_bookmarks` would be preserved.
1733 # Adding `00changelog.i` to the list of tracked file is not
1733 # Adding `00changelog.i` to the list of tracked file is not
1734 # enough, because at the time we build the content for `_bookmarks` in
1734 # enough, because at the time we build the content for `_bookmarks` in
1735 # (4), the changelog file has already diverged from the content used
1735 # (4), the changelog file has already diverged from the content used
1736 # for loading `changelog` in (1)
1736 # for loading `changelog` in (1)
1737 #
1737 #
1738 # To prevent the issue, we force the changelog to be explicitly
1738 # To prevent the issue, we force the changelog to be explicitly
1739 # reloaded while computing `_bookmarks`. The data race can still happen
1739 # reloaded while computing `_bookmarks`. The data race can still happen
1740 # without the lock (with a narrower window), but it would no longer go
1740 # without the lock (with a narrower window), but it would no longer go
1741 # undetected during the lock time refresh.
1741 # undetected during the lock time refresh.
1742 #
1742 #
1743 # The new schedule is as follow
1743 # The new schedule is as follow
1744 #
1744 #
1745 # 1) filecache logic detect that `_bookmarks` needs to be computed
1745 # 1) filecache logic detect that `_bookmarks` needs to be computed
1746 # 2) cachestat for `bookmarks` and `changelog` are captured (for book)
1746 # 2) cachestat for `bookmarks` and `changelog` are captured (for book)
1747 # 3) We force `changelog` filecache to be tested
1747 # 3) We force `changelog` filecache to be tested
1748 # 4) cachestat for `changelog` are captured (for changelog)
1748 # 4) cachestat for `changelog` are captured (for changelog)
1749 # 5) `_bookmarks` is computed and cached
1749 # 5) `_bookmarks` is computed and cached
1750 #
1750 #
1751 # The step in (3) ensure we have a changelog at least as recent as the
1751 # The step in (3) ensure we have a changelog at least as recent as the
1752 # cache stat computed in (1). As a result at locking time:
1752 # cache stat computed in (1). As a result at locking time:
1753 # * if the changelog did not changed since (1) -> we can reuse the data
1753 # * if the changelog did not changed since (1) -> we can reuse the data
1754 # * otherwise -> the bookmarks get refreshed.
1754 # * otherwise -> the bookmarks get refreshed.
1755 self._refreshchangelog()
1755 self._refreshchangelog()
1756 return bookmarks.bmstore(self)
1756 return bookmarks.bmstore(self)
1757
1757
1758 def _refreshchangelog(self):
1758 def _refreshchangelog(self):
1759 """make sure the in memory changelog match the on-disk one"""
1759 """make sure the in memory changelog match the on-disk one"""
1760 if 'changelog' in vars(self) and self.currenttransaction() is None:
1760 if 'changelog' in vars(self) and self.currenttransaction() is None:
1761 del self.changelog
1761 del self.changelog
1762
1762
1763 @property
1763 @property
1764 def _activebookmark(self):
1764 def _activebookmark(self):
1765 return self._bookmarks.active
1765 return self._bookmarks.active
1766
1766
1767 # _phasesets depend on changelog. what we need is to call
1767 # _phasesets depend on changelog. what we need is to call
1768 # _phasecache.invalidate() if '00changelog.i' was changed, but it
1768 # _phasecache.invalidate() if '00changelog.i' was changed, but it
1769 # can't be easily expressed in filecache mechanism.
1769 # can't be easily expressed in filecache mechanism.
1770 @storecache(b'phaseroots', b'00changelog.i')
1770 @storecache(b'phaseroots', b'00changelog.i')
1771 def _phasecache(self):
1771 def _phasecache(self):
1772 return phases.phasecache(self, self._phasedefaults)
1772 return phases.phasecache(self, self._phasedefaults)
1773
1773
1774 @storecache(b'obsstore')
1774 @storecache(b'obsstore')
1775 def obsstore(self):
1775 def obsstore(self):
1776 return obsolete.makestore(self.ui, self)
1776 return obsolete.makestore(self.ui, self)
1777
1777
1778 @changelogcache()
1778 @changelogcache()
1779 def changelog(repo):
1779 def changelog(repo):
1780 # load dirstate before changelog to avoid race see issue6303
1780 # load dirstate before changelog to avoid race see issue6303
1781 repo.dirstate.prefetch_parents()
1781 repo.dirstate.prefetch_parents()
1782 return repo.store.changelog(
1782 return repo.store.changelog(
1783 txnutil.mayhavepending(repo.root),
1783 txnutil.mayhavepending(repo.root),
1784 concurrencychecker=revlogchecker.get_checker(repo.ui, b'changelog'),
1784 concurrencychecker=revlogchecker.get_checker(repo.ui, b'changelog'),
1785 )
1785 )
1786
1786
1787 @manifestlogcache()
1787 @manifestlogcache()
1788 def manifestlog(self):
1788 def manifestlog(self):
1789 return self.store.manifestlog(self, self._storenarrowmatch)
1789 return self.store.manifestlog(self, self._storenarrowmatch)
1790
1790
1791 @unfilteredpropertycache
1791 @unfilteredpropertycache
1792 def dirstate(self):
1792 def dirstate(self):
1793 if self._dirstate is None:
1793 if self._dirstate is None:
1794 self._dirstate = self._makedirstate()
1794 self._dirstate = self._makedirstate()
1795 else:
1795 else:
1796 self._dirstate.refresh()
1796 self._dirstate.refresh()
1797 return self._dirstate
1797 return self._dirstate
1798
1798
1799 def _makedirstate(self):
1799 def _makedirstate(self):
1800 """Extension point for wrapping the dirstate per-repo."""
1800 """Extension point for wrapping the dirstate per-repo."""
1801 sparsematchfn = None
1801 sparsematchfn = None
1802 if sparse.use_sparse(self):
1802 if sparse.use_sparse(self):
1803 sparsematchfn = lambda: sparse.matcher(self)
1803 sparsematchfn = lambda: sparse.matcher(self)
1804 v2_req = requirementsmod.DIRSTATE_V2_REQUIREMENT
1804 v2_req = requirementsmod.DIRSTATE_V2_REQUIREMENT
1805 th = requirementsmod.DIRSTATE_TRACKED_HINT_V1
1805 th = requirementsmod.DIRSTATE_TRACKED_HINT_V1
1806 use_dirstate_v2 = v2_req in self.requirements
1806 use_dirstate_v2 = v2_req in self.requirements
1807 use_tracked_hint = th in self.requirements
1807 use_tracked_hint = th in self.requirements
1808
1808
1809 return dirstate.dirstate(
1809 return dirstate.dirstate(
1810 self.vfs,
1810 self.vfs,
1811 self.ui,
1811 self.ui,
1812 self.root,
1812 self.root,
1813 self._dirstatevalidate,
1813 self._dirstatevalidate,
1814 sparsematchfn,
1814 sparsematchfn,
1815 self.nodeconstants,
1815 self.nodeconstants,
1816 use_dirstate_v2,
1816 use_dirstate_v2,
1817 use_tracked_hint=use_tracked_hint,
1817 use_tracked_hint=use_tracked_hint,
1818 )
1818 )
1819
1819
1820 def _dirstatevalidate(self, node):
1820 def _dirstatevalidate(self, node):
1821 okay = True
1821 okay = True
1822 try:
1822 try:
1823 self.changelog.rev(node)
1823 self.changelog.rev(node)
1824 except error.LookupError:
1824 except error.LookupError:
1825 # If the parent are unknown it might just be because the changelog
1825 # If the parent are unknown it might just be because the changelog
1826 # in memory is lagging behind the dirstate in memory. So try to
1826 # in memory is lagging behind the dirstate in memory. So try to
1827 # refresh the changelog first.
1827 # refresh the changelog first.
1828 #
1828 #
1829 # We only do so if we don't hold the lock, if we do hold the lock
1829 # We only do so if we don't hold the lock, if we do hold the lock
1830 # the invalidation at that time should have taken care of this and
1830 # the invalidation at that time should have taken care of this and
1831 # something is very fishy.
1831 # something is very fishy.
1832 if self.currentlock() is None:
1832 if self.currentlock() is None:
1833 self.invalidate()
1833 self.invalidate()
1834 try:
1834 try:
1835 self.changelog.rev(node)
1835 self.changelog.rev(node)
1836 except error.LookupError:
1836 except error.LookupError:
1837 okay = False
1837 okay = False
1838 else:
1838 else:
1839 # XXX we should consider raising an error here.
1839 # XXX we should consider raising an error here.
1840 okay = False
1840 okay = False
1841 if okay:
1841 if okay:
1842 return node
1842 return node
1843 else:
1843 else:
1844 if not self._dirstatevalidatewarned:
1844 if not self._dirstatevalidatewarned:
1845 self._dirstatevalidatewarned = True
1845 self._dirstatevalidatewarned = True
1846 self.ui.warn(
1846 self.ui.warn(
1847 _(b"warning: ignoring unknown working parent %s!\n")
1847 _(b"warning: ignoring unknown working parent %s!\n")
1848 % short(node)
1848 % short(node)
1849 )
1849 )
1850 return self.nullid
1850 return self.nullid
1851
1851
1852 @storecache(narrowspec.FILENAME)
1852 @storecache(narrowspec.FILENAME)
1853 def narrowpats(self):
1853 def narrowpats(self):
1854 """matcher patterns for this repository's narrowspec
1854 """matcher patterns for this repository's narrowspec
1855
1855
1856 A tuple of (includes, excludes).
1856 A tuple of (includes, excludes).
1857 """
1857 """
1858 # the narrow management should probably move into its own object
1858 # the narrow management should probably move into its own object
1859 val = self._pending_narrow_pats
1859 val = self._pending_narrow_pats
1860 if val is None:
1860 if val is None:
1861 val = narrowspec.load(self)
1861 val = narrowspec.load(self)
1862 return val
1862 return val
1863
1863
1864 @storecache(narrowspec.FILENAME)
1864 @storecache(narrowspec.FILENAME)
1865 def _storenarrowmatch(self):
1865 def _storenarrowmatch(self):
1866 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1866 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1867 return matchmod.always()
1867 return matchmod.always()
1868 include, exclude = self.narrowpats
1868 include, exclude = self.narrowpats
1869 return narrowspec.match(self.root, include=include, exclude=exclude)
1869 return narrowspec.match(self.root, include=include, exclude=exclude)
1870
1870
1871 @storecache(narrowspec.FILENAME)
1871 @storecache(narrowspec.FILENAME)
1872 def _narrowmatch(self):
1872 def _narrowmatch(self):
1873 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1873 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1874 return matchmod.always()
1874 return matchmod.always()
1875 narrowspec.checkworkingcopynarrowspec(self)
1875 narrowspec.checkworkingcopynarrowspec(self)
1876 include, exclude = self.narrowpats
1876 include, exclude = self.narrowpats
1877 return narrowspec.match(self.root, include=include, exclude=exclude)
1877 return narrowspec.match(self.root, include=include, exclude=exclude)
1878
1878
1879 def narrowmatch(self, match=None, includeexact=False):
1879 def narrowmatch(self, match=None, includeexact=False):
1880 """matcher corresponding the the repo's narrowspec
1880 """matcher corresponding the the repo's narrowspec
1881
1881
1882 If `match` is given, then that will be intersected with the narrow
1882 If `match` is given, then that will be intersected with the narrow
1883 matcher.
1883 matcher.
1884
1884
1885 If `includeexact` is True, then any exact matches from `match` will
1885 If `includeexact` is True, then any exact matches from `match` will
1886 be included even if they're outside the narrowspec.
1886 be included even if they're outside the narrowspec.
1887 """
1887 """
1888 if match:
1888 if match:
1889 if includeexact and not self._narrowmatch.always():
1889 if includeexact and not self._narrowmatch.always():
1890 # do not exclude explicitly-specified paths so that they can
1890 # do not exclude explicitly-specified paths so that they can
1891 # be warned later on
1891 # be warned later on
1892 em = matchmod.exact(match.files())
1892 em = matchmod.exact(match.files())
1893 nm = matchmod.unionmatcher([self._narrowmatch, em])
1893 nm = matchmod.unionmatcher([self._narrowmatch, em])
1894 return matchmod.intersectmatchers(match, nm)
1894 return matchmod.intersectmatchers(match, nm)
1895 return matchmod.intersectmatchers(match, self._narrowmatch)
1895 return matchmod.intersectmatchers(match, self._narrowmatch)
1896 return self._narrowmatch
1896 return self._narrowmatch
1897
1897
1898 def setnarrowpats(self, newincludes, newexcludes):
1898 def setnarrowpats(self, newincludes, newexcludes):
1899 narrowspec.save(self, newincludes, newexcludes)
1899 narrowspec.save(self, newincludes, newexcludes)
1900 self.invalidate(clearfilecache=True)
1900 self.invalidate(clearfilecache=True)
1901
1901
1902 @unfilteredpropertycache
1902 @unfilteredpropertycache
1903 def _quick_access_changeid_null(self):
1903 def _quick_access_changeid_null(self):
1904 return {
1904 return {
1905 b'null': (nullrev, self.nodeconstants.nullid),
1905 b'null': (nullrev, self.nodeconstants.nullid),
1906 nullrev: (nullrev, self.nodeconstants.nullid),
1906 nullrev: (nullrev, self.nodeconstants.nullid),
1907 self.nullid: (nullrev, self.nullid),
1907 self.nullid: (nullrev, self.nullid),
1908 }
1908 }
1909
1909
1910 @unfilteredpropertycache
1910 @unfilteredpropertycache
1911 def _quick_access_changeid_wc(self):
1911 def _quick_access_changeid_wc(self):
1912 # also fast path access to the working copy parents
1912 # also fast path access to the working copy parents
1913 # however, only do it for filter that ensure wc is visible.
1913 # however, only do it for filter that ensure wc is visible.
1914 quick = self._quick_access_changeid_null.copy()
1914 quick = self._quick_access_changeid_null.copy()
1915 cl = self.unfiltered().changelog
1915 cl = self.unfiltered().changelog
1916 for node in self.dirstate.parents():
1916 for node in self.dirstate.parents():
1917 if node == self.nullid:
1917 if node == self.nullid:
1918 continue
1918 continue
1919 rev = cl.index.get_rev(node)
1919 rev = cl.index.get_rev(node)
1920 if rev is None:
1920 if rev is None:
1921 # unknown working copy parent case:
1921 # unknown working copy parent case:
1922 #
1922 #
1923 # skip the fast path and let higher code deal with it
1923 # skip the fast path and let higher code deal with it
1924 continue
1924 continue
1925 pair = (rev, node)
1925 pair = (rev, node)
1926 quick[rev] = pair
1926 quick[rev] = pair
1927 quick[node] = pair
1927 quick[node] = pair
1928 # also add the parents of the parents
1928 # also add the parents of the parents
1929 for r in cl.parentrevs(rev):
1929 for r in cl.parentrevs(rev):
1930 if r == nullrev:
1930 if r == nullrev:
1931 continue
1931 continue
1932 n = cl.node(r)
1932 n = cl.node(r)
1933 pair = (r, n)
1933 pair = (r, n)
1934 quick[r] = pair
1934 quick[r] = pair
1935 quick[n] = pair
1935 quick[n] = pair
1936 p1node = self.dirstate.p1()
1936 p1node = self.dirstate.p1()
1937 if p1node != self.nullid:
1937 if p1node != self.nullid:
1938 quick[b'.'] = quick[p1node]
1938 quick[b'.'] = quick[p1node]
1939 return quick
1939 return quick
1940
1940
1941 @unfilteredmethod
1941 @unfilteredmethod
1942 def _quick_access_changeid_invalidate(self):
1942 def _quick_access_changeid_invalidate(self):
1943 if '_quick_access_changeid_wc' in vars(self):
1943 if '_quick_access_changeid_wc' in vars(self):
1944 del self.__dict__['_quick_access_changeid_wc']
1944 del self.__dict__['_quick_access_changeid_wc']
1945
1945
1946 @property
1946 @property
1947 def _quick_access_changeid(self):
1947 def _quick_access_changeid(self):
1948 """an helper dictionnary for __getitem__ calls
1948 """an helper dictionnary for __getitem__ calls
1949
1949
1950 This contains a list of symbol we can recognise right away without
1950 This contains a list of symbol we can recognise right away without
1951 further processing.
1951 further processing.
1952 """
1952 """
1953 if self.filtername in repoview.filter_has_wc:
1953 if self.filtername in repoview.filter_has_wc:
1954 return self._quick_access_changeid_wc
1954 return self._quick_access_changeid_wc
1955 return self._quick_access_changeid_null
1955 return self._quick_access_changeid_null
1956
1956
1957 def __getitem__(self, changeid):
1957 def __getitem__(self, changeid):
1958 # dealing with special cases
1958 # dealing with special cases
1959 if changeid is None:
1959 if changeid is None:
1960 return context.workingctx(self)
1960 return context.workingctx(self)
1961 if isinstance(changeid, context.basectx):
1961 if isinstance(changeid, context.basectx):
1962 return changeid
1962 return changeid
1963
1963
1964 # dealing with multiple revisions
1964 # dealing with multiple revisions
1965 if isinstance(changeid, slice):
1965 if isinstance(changeid, slice):
1966 # wdirrev isn't contiguous so the slice shouldn't include it
1966 # wdirrev isn't contiguous so the slice shouldn't include it
1967 return [
1967 return [
1968 self[i]
1968 self[i]
1969 for i in range(*changeid.indices(len(self)))
1969 for i in range(*changeid.indices(len(self)))
1970 if i not in self.changelog.filteredrevs
1970 if i not in self.changelog.filteredrevs
1971 ]
1971 ]
1972
1972
1973 # dealing with some special values
1973 # dealing with some special values
1974 quick_access = self._quick_access_changeid.get(changeid)
1974 quick_access = self._quick_access_changeid.get(changeid)
1975 if quick_access is not None:
1975 if quick_access is not None:
1976 rev, node = quick_access
1976 rev, node = quick_access
1977 return context.changectx(self, rev, node, maybe_filtered=False)
1977 return context.changectx(self, rev, node, maybe_filtered=False)
1978 if changeid == b'tip':
1978 if changeid == b'tip':
1979 node = self.changelog.tip()
1979 node = self.changelog.tip()
1980 rev = self.changelog.rev(node)
1980 rev = self.changelog.rev(node)
1981 return context.changectx(self, rev, node)
1981 return context.changectx(self, rev, node)
1982
1982
1983 # dealing with arbitrary values
1983 # dealing with arbitrary values
1984 try:
1984 try:
1985 if isinstance(changeid, int):
1985 if isinstance(changeid, int):
1986 node = self.changelog.node(changeid)
1986 node = self.changelog.node(changeid)
1987 rev = changeid
1987 rev = changeid
1988 elif changeid == b'.':
1988 elif changeid == b'.':
1989 # this is a hack to delay/avoid loading obsmarkers
1989 # this is a hack to delay/avoid loading obsmarkers
1990 # when we know that '.' won't be hidden
1990 # when we know that '.' won't be hidden
1991 node = self.dirstate.p1()
1991 node = self.dirstate.p1()
1992 rev = self.unfiltered().changelog.rev(node)
1992 rev = self.unfiltered().changelog.rev(node)
1993 elif len(changeid) == self.nodeconstants.nodelen:
1993 elif len(changeid) == self.nodeconstants.nodelen:
1994 try:
1994 try:
1995 node = changeid
1995 node = changeid
1996 rev = self.changelog.rev(changeid)
1996 rev = self.changelog.rev(changeid)
1997 except error.FilteredLookupError:
1997 except error.FilteredLookupError:
1998 changeid = hex(changeid) # for the error message
1998 changeid = hex(changeid) # for the error message
1999 raise
1999 raise
2000 except LookupError:
2000 except LookupError:
2001 # check if it might have come from damaged dirstate
2001 # check if it might have come from damaged dirstate
2002 #
2002 #
2003 # XXX we could avoid the unfiltered if we had a recognizable
2003 # XXX we could avoid the unfiltered if we had a recognizable
2004 # exception for filtered changeset access
2004 # exception for filtered changeset access
2005 if (
2005 if (
2006 self.local()
2006 self.local()
2007 and changeid in self.unfiltered().dirstate.parents()
2007 and changeid in self.unfiltered().dirstate.parents()
2008 ):
2008 ):
2009 msg = _(b"working directory has unknown parent '%s'!")
2009 msg = _(b"working directory has unknown parent '%s'!")
2010 raise error.Abort(msg % short(changeid))
2010 raise error.Abort(msg % short(changeid))
2011 changeid = hex(changeid) # for the error message
2011 changeid = hex(changeid) # for the error message
2012 raise
2012 raise
2013
2013
2014 elif len(changeid) == 2 * self.nodeconstants.nodelen:
2014 elif len(changeid) == 2 * self.nodeconstants.nodelen:
2015 node = bin(changeid)
2015 node = bin(changeid)
2016 rev = self.changelog.rev(node)
2016 rev = self.changelog.rev(node)
2017 else:
2017 else:
2018 raise error.ProgrammingError(
2018 raise error.ProgrammingError(
2019 b"unsupported changeid '%s' of type %s"
2019 b"unsupported changeid '%s' of type %s"
2020 % (changeid, pycompat.bytestr(type(changeid)))
2020 % (changeid, pycompat.bytestr(type(changeid)))
2021 )
2021 )
2022
2022
2023 return context.changectx(self, rev, node)
2023 return context.changectx(self, rev, node)
2024
2024
2025 except (error.FilteredIndexError, error.FilteredLookupError):
2025 except (error.FilteredIndexError, error.FilteredLookupError):
2026 raise error.FilteredRepoLookupError(
2026 raise error.FilteredRepoLookupError(
2027 _(b"filtered revision '%s'") % pycompat.bytestr(changeid)
2027 _(b"filtered revision '%s'") % pycompat.bytestr(changeid)
2028 )
2028 )
2029 except (IndexError, LookupError):
2029 except (IndexError, LookupError):
2030 raise error.RepoLookupError(
2030 raise error.RepoLookupError(
2031 _(b"unknown revision '%s'") % pycompat.bytestr(changeid)
2031 _(b"unknown revision '%s'") % pycompat.bytestr(changeid)
2032 )
2032 )
2033 except error.WdirUnsupported:
2033 except error.WdirUnsupported:
2034 return context.workingctx(self)
2034 return context.workingctx(self)
2035
2035
2036 def __contains__(self, changeid):
2036 def __contains__(self, changeid):
2037 """True if the given changeid exists"""
2037 """True if the given changeid exists"""
2038 try:
2038 try:
2039 self[changeid]
2039 self[changeid]
2040 return True
2040 return True
2041 except error.RepoLookupError:
2041 except error.RepoLookupError:
2042 return False
2042 return False
2043
2043
2044 def __nonzero__(self):
2044 def __nonzero__(self):
2045 return True
2045 return True
2046
2046
2047 __bool__ = __nonzero__
2047 __bool__ = __nonzero__
2048
2048
2049 def __len__(self):
2049 def __len__(self):
2050 # no need to pay the cost of repoview.changelog
2050 # no need to pay the cost of repoview.changelog
2051 unfi = self.unfiltered()
2051 unfi = self.unfiltered()
2052 return len(unfi.changelog)
2052 return len(unfi.changelog)
2053
2053
2054 def __iter__(self):
2054 def __iter__(self):
2055 return iter(self.changelog)
2055 return iter(self.changelog)
2056
2056
2057 def revs(self, expr: bytes, *args):
2057 def revs(self, expr: bytes, *args):
2058 """Find revisions matching a revset.
2058 """Find revisions matching a revset.
2059
2059
2060 The revset is specified as a string ``expr`` that may contain
2060 The revset is specified as a string ``expr`` that may contain
2061 %-formatting to escape certain types. See ``revsetlang.formatspec``.
2061 %-formatting to escape certain types. See ``revsetlang.formatspec``.
2062
2062
2063 Revset aliases from the configuration are not expanded. To expand
2063 Revset aliases from the configuration are not expanded. To expand
2064 user aliases, consider calling ``scmutil.revrange()`` or
2064 user aliases, consider calling ``scmutil.revrange()`` or
2065 ``repo.anyrevs([expr], user=True)``.
2065 ``repo.anyrevs([expr], user=True)``.
2066
2066
2067 Returns a smartset.abstractsmartset, which is a list-like interface
2067 Returns a smartset.abstractsmartset, which is a list-like interface
2068 that contains integer revisions.
2068 that contains integer revisions.
2069 """
2069 """
2070 tree = revsetlang.spectree(expr, *args)
2070 tree = revsetlang.spectree(expr, *args)
2071 return revset.makematcher(tree)(self)
2071 return revset.makematcher(tree)(self)
2072
2072
2073 def set(self, expr: bytes, *args):
2073 def set(self, expr: bytes, *args):
2074 """Find revisions matching a revset and emit changectx instances.
2074 """Find revisions matching a revset and emit changectx instances.
2075
2075
2076 This is a convenience wrapper around ``revs()`` that iterates the
2076 This is a convenience wrapper around ``revs()`` that iterates the
2077 result and is a generator of changectx instances.
2077 result and is a generator of changectx instances.
2078
2078
2079 Revset aliases from the configuration are not expanded. To expand
2079 Revset aliases from the configuration are not expanded. To expand
2080 user aliases, consider calling ``scmutil.revrange()``.
2080 user aliases, consider calling ``scmutil.revrange()``.
2081 """
2081 """
2082 for r in self.revs(expr, *args):
2082 for r in self.revs(expr, *args):
2083 yield self[r]
2083 yield self[r]
2084
2084
2085 def anyrevs(self, specs: bytes, user=False, localalias=None):
2085 def anyrevs(self, specs: bytes, user=False, localalias=None):
2086 """Find revisions matching one of the given revsets.
2086 """Find revisions matching one of the given revsets.
2087
2087
2088 Revset aliases from the configuration are not expanded by default. To
2088 Revset aliases from the configuration are not expanded by default. To
2089 expand user aliases, specify ``user=True``. To provide some local
2089 expand user aliases, specify ``user=True``. To provide some local
2090 definitions overriding user aliases, set ``localalias`` to
2090 definitions overriding user aliases, set ``localalias`` to
2091 ``{name: definitionstring}``.
2091 ``{name: definitionstring}``.
2092 """
2092 """
2093 if specs == [b'null']:
2093 if specs == [b'null']:
2094 return revset.baseset([nullrev])
2094 return revset.baseset([nullrev])
2095 if specs == [b'.']:
2095 if specs == [b'.']:
2096 quick_data = self._quick_access_changeid.get(b'.')
2096 quick_data = self._quick_access_changeid.get(b'.')
2097 if quick_data is not None:
2097 if quick_data is not None:
2098 return revset.baseset([quick_data[0]])
2098 return revset.baseset([quick_data[0]])
2099 if user:
2099 if user:
2100 m = revset.matchany(
2100 m = revset.matchany(
2101 self.ui,
2101 self.ui,
2102 specs,
2102 specs,
2103 lookup=revset.lookupfn(self),
2103 lookup=revset.lookupfn(self),
2104 localalias=localalias,
2104 localalias=localalias,
2105 )
2105 )
2106 else:
2106 else:
2107 m = revset.matchany(None, specs, localalias=localalias)
2107 m = revset.matchany(None, specs, localalias=localalias)
2108 return m(self)
2108 return m(self)
2109
2109
2110 def url(self) -> bytes:
2110 def url(self) -> bytes:
2111 return b'file:' + self.root
2111 return b'file:' + self.root
2112
2112
2113 def hook(self, name, throw=False, **args):
2113 def hook(self, name, throw=False, **args):
2114 """Call a hook, passing this repo instance.
2114 """Call a hook, passing this repo instance.
2115
2115
2116 This a convenience method to aid invoking hooks. Extensions likely
2116 This a convenience method to aid invoking hooks. Extensions likely
2117 won't call this unless they have registered a custom hook or are
2117 won't call this unless they have registered a custom hook or are
2118 replacing code that is expected to call a hook.
2118 replacing code that is expected to call a hook.
2119 """
2119 """
2120 return hook.hook(self.ui, self, name, throw, **args)
2120 return hook.hook(self.ui, self, name, throw, **args)
2121
2121
2122 @filteredpropertycache
2122 @filteredpropertycache
2123 def _tagscache(self):
2123 def _tagscache(self):
2124 """Returns a tagscache object that contains various tags related
2124 """Returns a tagscache object that contains various tags related
2125 caches."""
2125 caches."""
2126
2126
2127 # This simplifies its cache management by having one decorated
2127 # This simplifies its cache management by having one decorated
2128 # function (this one) and the rest simply fetch things from it.
2128 # function (this one) and the rest simply fetch things from it.
2129 class tagscache:
2129 class tagscache:
2130 def __init__(self):
2130 def __init__(self):
2131 # These two define the set of tags for this repository. tags
2131 # These two define the set of tags for this repository. tags
2132 # maps tag name to node; tagtypes maps tag name to 'global' or
2132 # maps tag name to node; tagtypes maps tag name to 'global' or
2133 # 'local'. (Global tags are defined by .hgtags across all
2133 # 'local'. (Global tags are defined by .hgtags across all
2134 # heads, and local tags are defined in .hg/localtags.)
2134 # heads, and local tags are defined in .hg/localtags.)
2135 # They constitute the in-memory cache of tags.
2135 # They constitute the in-memory cache of tags.
2136 self.tags = self.tagtypes = None
2136 self.tags = self.tagtypes = None
2137
2137
2138 self.nodetagscache = self.tagslist = None
2138 self.nodetagscache = self.tagslist = None
2139
2139
2140 cache = tagscache()
2140 cache = tagscache()
2141 cache.tags, cache.tagtypes = self._findtags()
2141 cache.tags, cache.tagtypes = self._findtags()
2142
2142
2143 return cache
2143 return cache
2144
2144
2145 def tags(self):
2145 def tags(self):
2146 '''return a mapping of tag to node'''
2146 '''return a mapping of tag to node'''
2147 t = {}
2147 t = {}
2148 if self.changelog.filteredrevs:
2148 if self.changelog.filteredrevs:
2149 tags, tt = self._findtags()
2149 tags, tt = self._findtags()
2150 else:
2150 else:
2151 tags = self._tagscache.tags
2151 tags = self._tagscache.tags
2152 rev = self.changelog.rev
2152 rev = self.changelog.rev
2153 for k, v in tags.items():
2153 for k, v in tags.items():
2154 try:
2154 try:
2155 # ignore tags to unknown nodes
2155 # ignore tags to unknown nodes
2156 rev(v)
2156 rev(v)
2157 t[k] = v
2157 t[k] = v
2158 except (error.LookupError, ValueError):
2158 except (error.LookupError, ValueError):
2159 pass
2159 pass
2160 return t
2160 return t
2161
2161
2162 def _findtags(self):
2162 def _findtags(self):
2163 """Do the hard work of finding tags. Return a pair of dicts
2163 """Do the hard work of finding tags. Return a pair of dicts
2164 (tags, tagtypes) where tags maps tag name to node, and tagtypes
2164 (tags, tagtypes) where tags maps tag name to node, and tagtypes
2165 maps tag name to a string like \'global\' or \'local\'.
2165 maps tag name to a string like \'global\' or \'local\'.
2166 Subclasses or extensions are free to add their own tags, but
2166 Subclasses or extensions are free to add their own tags, but
2167 should be aware that the returned dicts will be retained for the
2167 should be aware that the returned dicts will be retained for the
2168 duration of the localrepo object."""
2168 duration of the localrepo object."""
2169
2169
2170 # XXX what tagtype should subclasses/extensions use? Currently
2170 # XXX what tagtype should subclasses/extensions use? Currently
2171 # mq and bookmarks add tags, but do not set the tagtype at all.
2171 # mq and bookmarks add tags, but do not set the tagtype at all.
2172 # Should each extension invent its own tag type? Should there
2172 # Should each extension invent its own tag type? Should there
2173 # be one tagtype for all such "virtual" tags? Or is the status
2173 # be one tagtype for all such "virtual" tags? Or is the status
2174 # quo fine?
2174 # quo fine?
2175
2175
2176 # map tag name to (node, hist)
2176 # map tag name to (node, hist)
2177 alltags = tagsmod.findglobaltags(self.ui, self)
2177 alltags = tagsmod.findglobaltags(self.ui, self)
2178 # map tag name to tag type
2178 # map tag name to tag type
2179 tagtypes = {tag: b'global' for tag in alltags}
2179 tagtypes = {tag: b'global' for tag in alltags}
2180
2180
2181 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
2181 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
2182
2182
2183 # Build the return dicts. Have to re-encode tag names because
2183 # Build the return dicts. Have to re-encode tag names because
2184 # the tags module always uses UTF-8 (in order not to lose info
2184 # the tags module always uses UTF-8 (in order not to lose info
2185 # writing to the cache), but the rest of Mercurial wants them in
2185 # writing to the cache), but the rest of Mercurial wants them in
2186 # local encoding.
2186 # local encoding.
2187 tags = {}
2187 tags = {}
2188 for name, (node, hist) in alltags.items():
2188 for name, (node, hist) in alltags.items():
2189 if node != self.nullid:
2189 if node != self.nullid:
2190 tags[encoding.tolocal(name)] = node
2190 tags[encoding.tolocal(name)] = node
2191 tags[b'tip'] = self.changelog.tip()
2191 tags[b'tip'] = self.changelog.tip()
2192 tagtypes = {
2192 tagtypes = {
2193 encoding.tolocal(name): value for (name, value) in tagtypes.items()
2193 encoding.tolocal(name): value for (name, value) in tagtypes.items()
2194 }
2194 }
2195 return (tags, tagtypes)
2195 return (tags, tagtypes)
2196
2196
2197 def tagtype(self, tagname):
2197 def tagtype(self, tagname):
2198 """
2198 """
2199 return the type of the given tag. result can be:
2199 return the type of the given tag. result can be:
2200
2200
2201 'local' : a local tag
2201 'local' : a local tag
2202 'global' : a global tag
2202 'global' : a global tag
2203 None : tag does not exist
2203 None : tag does not exist
2204 """
2204 """
2205
2205
2206 return self._tagscache.tagtypes.get(tagname)
2206 return self._tagscache.tagtypes.get(tagname)
2207
2207
2208 def tagslist(self):
2208 def tagslist(self):
2209 '''return a list of tags ordered by revision'''
2209 '''return a list of tags ordered by revision'''
2210 if not self._tagscache.tagslist:
2210 if not self._tagscache.tagslist:
2211 l = []
2211 l = []
2212 for t, n in self.tags().items():
2212 for t, n in self.tags().items():
2213 l.append((self.changelog.rev(n), t, n))
2213 l.append((self.changelog.rev(n), t, n))
2214 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
2214 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
2215
2215
2216 return self._tagscache.tagslist
2216 return self._tagscache.tagslist
2217
2217
2218 def nodetags(self, node):
2218 def nodetags(self, node):
2219 '''return the tags associated with a node'''
2219 '''return the tags associated with a node'''
2220 if not self._tagscache.nodetagscache:
2220 if not self._tagscache.nodetagscache:
2221 nodetagscache = {}
2221 nodetagscache = {}
2222 for t, n in self._tagscache.tags.items():
2222 for t, n in self._tagscache.tags.items():
2223 nodetagscache.setdefault(n, []).append(t)
2223 nodetagscache.setdefault(n, []).append(t)
2224 for tags in nodetagscache.values():
2224 for tags in nodetagscache.values():
2225 tags.sort()
2225 tags.sort()
2226 self._tagscache.nodetagscache = nodetagscache
2226 self._tagscache.nodetagscache = nodetagscache
2227 return self._tagscache.nodetagscache.get(node, [])
2227 return self._tagscache.nodetagscache.get(node, [])
2228
2228
2229 def nodebookmarks(self, node):
2229 def nodebookmarks(self, node):
2230 """return the list of bookmarks pointing to the specified node"""
2230 """return the list of bookmarks pointing to the specified node"""
2231 return self._bookmarks.names(node)
2231 return self._bookmarks.names(node)
2232
2232
2233 def branchmap(self):
2233 def branchmap(self):
2234 """returns a dictionary {branch: [branchheads]} with branchheads
2234 """returns a dictionary {branch: [branchheads]} with branchheads
2235 ordered by increasing revision number"""
2235 ordered by increasing revision number"""
2236 return self._branchcaches[self]
2236 return self._branchcaches[self]
2237
2237
2238 @unfilteredmethod
2238 @unfilteredmethod
2239 def revbranchcache(self):
2239 def revbranchcache(self):
2240 if not self._revbranchcache:
2240 if not self._revbranchcache:
2241 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
2241 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
2242 return self._revbranchcache
2242 return self._revbranchcache
2243
2243
2244 def register_changeset(self, rev, changelogrevision):
2244 def register_changeset(self, rev, changelogrevision):
2245 self.revbranchcache().setdata(rev, changelogrevision)
2245 self.revbranchcache().setdata(rev, changelogrevision)
2246
2246
2247 def branchtip(self, branch, ignoremissing=False):
2247 def branchtip(self, branch, ignoremissing=False):
2248 """return the tip node for a given branch
2248 """return the tip node for a given branch
2249
2249
2250 If ignoremissing is True, then this method will not raise an error.
2250 If ignoremissing is True, then this method will not raise an error.
2251 This is helpful for callers that only expect None for a missing branch
2251 This is helpful for callers that only expect None for a missing branch
2252 (e.g. namespace).
2252 (e.g. namespace).
2253
2253
2254 """
2254 """
2255 try:
2255 try:
2256 return self.branchmap().branchtip(branch)
2256 return self.branchmap().branchtip(branch)
2257 except KeyError:
2257 except KeyError:
2258 if not ignoremissing:
2258 if not ignoremissing:
2259 raise error.RepoLookupError(_(b"unknown branch '%s'") % branch)
2259 raise error.RepoLookupError(_(b"unknown branch '%s'") % branch)
2260 else:
2260 else:
2261 pass
2261 pass
2262
2262
2263 def lookup(self, key):
2263 def lookup(self, key):
2264 node = scmutil.revsymbol(self, key).node()
2264 node = scmutil.revsymbol(self, key).node()
2265 if node is None:
2265 if node is None:
2266 raise error.RepoLookupError(_(b"unknown revision '%s'") % key)
2266 raise error.RepoLookupError(_(b"unknown revision '%s'") % key)
2267 return node
2267 return node
2268
2268
2269 def lookupbranch(self, key):
2269 def lookupbranch(self, key):
2270 if self.branchmap().hasbranch(key):
2270 if self.branchmap().hasbranch(key):
2271 return key
2271 return key
2272
2272
2273 return scmutil.revsymbol(self, key).branch()
2273 return scmutil.revsymbol(self, key).branch()
2274
2274
2275 def known(self, nodes):
2275 def known(self, nodes):
2276 cl = self.changelog
2276 cl = self.changelog
2277 get_rev = cl.index.get_rev
2277 get_rev = cl.index.get_rev
2278 filtered = cl.filteredrevs
2278 filtered = cl.filteredrevs
2279 result = []
2279 result = []
2280 for n in nodes:
2280 for n in nodes:
2281 r = get_rev(n)
2281 r = get_rev(n)
2282 resp = not (r is None or r in filtered)
2282 resp = not (r is None or r in filtered)
2283 result.append(resp)
2283 result.append(resp)
2284 return result
2284 return result
2285
2285
2286 def local(self):
2286 def local(self):
2287 return self
2287 return self
2288
2288
2289 def publishing(self):
2289 def publishing(self):
2290 # it's safe (and desirable) to trust the publish flag unconditionally
2290 # it's safe (and desirable) to trust the publish flag unconditionally
2291 # so that we don't finalize changes shared between users via ssh or nfs
2291 # so that we don't finalize changes shared between users via ssh or nfs
2292 return self.ui.configbool(b'phases', b'publish', untrusted=True)
2292 return self.ui.configbool(b'phases', b'publish', untrusted=True)
2293
2293
2294 def cancopy(self):
2294 def cancopy(self):
2295 # so statichttprepo's override of local() works
2295 # so statichttprepo's override of local() works
2296 if not self.local():
2296 if not self.local():
2297 return False
2297 return False
2298 if not self.publishing():
2298 if not self.publishing():
2299 return True
2299 return True
2300 # if publishing we can't copy if there is filtered content
2300 # if publishing we can't copy if there is filtered content
2301 return not self.filtered(b'visible').changelog.filteredrevs
2301 return not self.filtered(b'visible').changelog.filteredrevs
2302
2302
2303 def shared(self):
2303 def shared(self):
2304 '''the type of shared repository (None if not shared)'''
2304 '''the type of shared repository (None if not shared)'''
2305 if self.sharedpath != self.path:
2305 if self.sharedpath != self.path:
2306 return b'store'
2306 return b'store'
2307 return None
2307 return None
2308
2308
2309 def wjoin(self, f: bytes, *insidef: bytes) -> bytes:
2309 def wjoin(self, f: bytes, *insidef: bytes) -> bytes:
2310 return self.vfs.reljoin(self.root, f, *insidef)
2310 return self.vfs.reljoin(self.root, f, *insidef)
2311
2311
2312 def setparents(self, p1, p2=None):
2312 def setparents(self, p1, p2=None):
2313 if p2 is None:
2313 if p2 is None:
2314 p2 = self.nullid
2314 p2 = self.nullid
2315 self[None].setparents(p1, p2)
2315 self[None].setparents(p1, p2)
2316 self._quick_access_changeid_invalidate()
2316 self._quick_access_changeid_invalidate()
2317
2317
2318 def filectx(self, path: bytes, changeid=None, fileid=None, changectx=None):
2318 def filectx(self, path: bytes, changeid=None, fileid=None, changectx=None):
2319 """changeid must be a changeset revision, if specified.
2319 """changeid must be a changeset revision, if specified.
2320 fileid can be a file revision or node."""
2320 fileid can be a file revision or node."""
2321 return context.filectx(
2321 return context.filectx(
2322 self, path, changeid, fileid, changectx=changectx
2322 self, path, changeid, fileid, changectx=changectx
2323 )
2323 )
2324
2324
2325 def getcwd(self) -> bytes:
2325 def getcwd(self) -> bytes:
2326 return self.dirstate.getcwd()
2326 return self.dirstate.getcwd()
2327
2327
2328 def pathto(self, f: bytes, cwd: Optional[bytes] = None) -> bytes:
2328 def pathto(self, f: bytes, cwd: Optional[bytes] = None) -> bytes:
2329 return self.dirstate.pathto(f, cwd)
2329 return self.dirstate.pathto(f, cwd)
2330
2330
2331 def _loadfilter(self, filter):
2331 def _loadfilter(self, filter):
2332 if filter not in self._filterpats:
2332 if filter not in self._filterpats:
2333 l = []
2333 l = []
2334 for pat, cmd in self.ui.configitems(filter):
2334 for pat, cmd in self.ui.configitems(filter):
2335 if cmd == b'!':
2335 if cmd == b'!':
2336 continue
2336 continue
2337 mf = matchmod.match(self.root, b'', [pat])
2337 mf = matchmod.match(self.root, b'', [pat])
2338 fn = None
2338 fn = None
2339 params = cmd
2339 params = cmd
2340 for name, filterfn in self._datafilters.items():
2340 for name, filterfn in self._datafilters.items():
2341 if cmd.startswith(name):
2341 if cmd.startswith(name):
2342 fn = filterfn
2342 fn = filterfn
2343 params = cmd[len(name) :].lstrip()
2343 params = cmd[len(name) :].lstrip()
2344 break
2344 break
2345 if not fn:
2345 if not fn:
2346 fn = lambda s, c, **kwargs: procutil.filter(s, c)
2346 fn = lambda s, c, **kwargs: procutil.filter(s, c)
2347 fn.__name__ = 'commandfilter'
2347 fn.__name__ = 'commandfilter'
2348 # Wrap old filters not supporting keyword arguments
2348 # Wrap old filters not supporting keyword arguments
2349 if not pycompat.getargspec(fn)[2]:
2349 if not pycompat.getargspec(fn)[2]:
2350 oldfn = fn
2350 oldfn = fn
2351 fn = lambda s, c, oldfn=oldfn, **kwargs: oldfn(s, c)
2351 fn = lambda s, c, oldfn=oldfn, **kwargs: oldfn(s, c)
2352 fn.__name__ = 'compat-' + oldfn.__name__
2352 fn.__name__ = 'compat-' + oldfn.__name__
2353 l.append((mf, fn, params))
2353 l.append((mf, fn, params))
2354 self._filterpats[filter] = l
2354 self._filterpats[filter] = l
2355 return self._filterpats[filter]
2355 return self._filterpats[filter]
2356
2356
2357 def _filter(self, filterpats, filename, data):
2357 def _filter(self, filterpats, filename, data):
2358 for mf, fn, cmd in filterpats:
2358 for mf, fn, cmd in filterpats:
2359 if mf(filename):
2359 if mf(filename):
2360 self.ui.debug(
2360 self.ui.debug(
2361 b"filtering %s through %s\n"
2361 b"filtering %s through %s\n"
2362 % (filename, cmd or pycompat.sysbytes(fn.__name__))
2362 % (filename, cmd or pycompat.sysbytes(fn.__name__))
2363 )
2363 )
2364 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
2364 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
2365 break
2365 break
2366
2366
2367 return data
2367 return data
2368
2368
2369 @unfilteredpropertycache
2369 @unfilteredpropertycache
2370 def _encodefilterpats(self):
2370 def _encodefilterpats(self):
2371 return self._loadfilter(b'encode')
2371 return self._loadfilter(b'encode')
2372
2372
2373 @unfilteredpropertycache
2373 @unfilteredpropertycache
2374 def _decodefilterpats(self):
2374 def _decodefilterpats(self):
2375 return self._loadfilter(b'decode')
2375 return self._loadfilter(b'decode')
2376
2376
2377 def adddatafilter(self, name, filter):
2377 def adddatafilter(self, name, filter):
2378 self._datafilters[name] = filter
2378 self._datafilters[name] = filter
2379
2379
2380 def wread(self, filename: bytes) -> bytes:
2380 def wread(self, filename: bytes) -> bytes:
2381 if self.wvfs.islink(filename):
2381 if self.wvfs.islink(filename):
2382 data = self.wvfs.readlink(filename)
2382 data = self.wvfs.readlink(filename)
2383 else:
2383 else:
2384 data = self.wvfs.read(filename)
2384 data = self.wvfs.read(filename)
2385 return self._filter(self._encodefilterpats, filename, data)
2385 return self._filter(self._encodefilterpats, filename, data)
2386
2386
2387 def wwrite(
2387 def wwrite(
2388 self,
2388 self,
2389 filename: bytes,
2389 filename: bytes,
2390 data: bytes,
2390 data: bytes,
2391 flags: bytes,
2391 flags: bytes,
2392 backgroundclose=False,
2392 backgroundclose=False,
2393 **kwargs
2393 **kwargs
2394 ) -> int:
2394 ) -> int:
2395 """write ``data`` into ``filename`` in the working directory
2395 """write ``data`` into ``filename`` in the working directory
2396
2396
2397 This returns length of written (maybe decoded) data.
2397 This returns length of written (maybe decoded) data.
2398 """
2398 """
2399 data = self._filter(self._decodefilterpats, filename, data)
2399 data = self._filter(self._decodefilterpats, filename, data)
2400 if b'l' in flags:
2400 if b'l' in flags:
2401 self.wvfs.symlink(data, filename)
2401 self.wvfs.symlink(data, filename)
2402 else:
2402 else:
2403 self.wvfs.write(
2403 self.wvfs.write(
2404 filename, data, backgroundclose=backgroundclose, **kwargs
2404 filename, data, backgroundclose=backgroundclose, **kwargs
2405 )
2405 )
2406 if b'x' in flags:
2406 if b'x' in flags:
2407 self.wvfs.setflags(filename, False, True)
2407 self.wvfs.setflags(filename, False, True)
2408 else:
2408 else:
2409 self.wvfs.setflags(filename, False, False)
2409 self.wvfs.setflags(filename, False, False)
2410 return len(data)
2410 return len(data)
2411
2411
2412 def wwritedata(self, filename: bytes, data: bytes) -> bytes:
2412 def wwritedata(self, filename: bytes, data: bytes) -> bytes:
2413 return self._filter(self._decodefilterpats, filename, data)
2413 return self._filter(self._decodefilterpats, filename, data)
2414
2414
2415 def currenttransaction(self):
2415 def currenttransaction(self):
2416 """return the current transaction or None if non exists"""
2416 """return the current transaction or None if non exists"""
2417 if self._transref:
2417 if self._transref:
2418 tr = self._transref()
2418 tr = self._transref()
2419 else:
2419 else:
2420 tr = None
2420 tr = None
2421
2421
2422 if tr and tr.running():
2422 if tr and tr.running():
2423 return tr
2423 return tr
2424 return None
2424 return None
2425
2425
2426 def transaction(self, desc, report=None):
2426 def transaction(self, desc, report=None):
2427 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
2427 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
2428 b'devel', b'check-locks'
2428 b'devel', b'check-locks'
2429 ):
2429 ):
2430 if self._currentlock(self._lockref) is None:
2430 if self._currentlock(self._lockref) is None:
2431 raise error.ProgrammingError(b'transaction requires locking')
2431 raise error.ProgrammingError(b'transaction requires locking')
2432 tr = self.currenttransaction()
2432 tr = self.currenttransaction()
2433 if tr is not None:
2433 if tr is not None:
2434 return tr.nest(name=desc)
2434 return tr.nest(name=desc)
2435
2435
2436 # abort here if the journal already exists
2436 # abort here if the journal already exists
2437 if self.svfs.exists(b"journal"):
2437 if self.svfs.exists(b"journal"):
2438 raise error.RepoError(
2438 raise error.RepoError(
2439 _(b"abandoned transaction found"),
2439 _(b"abandoned transaction found"),
2440 hint=_(b"run 'hg recover' to clean up transaction"),
2440 hint=_(b"run 'hg recover' to clean up transaction"),
2441 )
2441 )
2442
2442
2443 # At that point your dirstate should be clean:
2443 # At that point your dirstate should be clean:
2444 #
2444 #
2445 # - If you don't have the wlock, why would you still have a dirty
2445 # - If you don't have the wlock, why would you still have a dirty
2446 # dirstate ?
2446 # dirstate ?
2447 #
2447 #
2448 # - If you hold the wlock, you should not be opening a transaction in
2448 # - If you hold the wlock, you should not be opening a transaction in
2449 # the middle of a `distate.changing_*` block. The transaction needs to
2449 # the middle of a `distate.changing_*` block. The transaction needs to
2450 # be open before that and wrap the change-context.
2450 # be open before that and wrap the change-context.
2451 #
2451 #
2452 # - If you are not within a `dirstate.changing_*` context, why is our
2452 # - If you are not within a `dirstate.changing_*` context, why is our
2453 # dirstate dirty?
2453 # dirstate dirty?
2454 if self.dirstate._dirty:
2454 if self.dirstate._dirty:
2455 m = "cannot open a transaction with a dirty dirstate"
2455 m = "cannot open a transaction with a dirty dirstate"
2456 raise error.ProgrammingError(m)
2456 raise error.ProgrammingError(m)
2457
2457
2458 idbase = b"%.40f#%f" % (random.random(), time.time())
2458 idbase = b"%.40f#%f" % (random.random(), time.time())
2459 ha = hex(hashutil.sha1(idbase).digest())
2459 ha = hex(hashutil.sha1(idbase).digest())
2460 txnid = b'TXN:' + ha
2460 txnid = b'TXN:' + ha
2461 self.hook(b'pretxnopen', throw=True, txnname=desc, txnid=txnid)
2461 self.hook(b'pretxnopen', throw=True, txnname=desc, txnid=txnid)
2462
2462
2463 self._writejournal(desc)
2463 self._writejournal(desc)
2464 if report:
2464 if report:
2465 rp = report
2465 rp = report
2466 else:
2466 else:
2467 rp = self.ui.warn
2467 rp = self.ui.warn
2468 vfsmap = self.vfs_map
2468 vfsmap = self.vfs_map
2469 # we must avoid cyclic reference between repo and transaction.
2469 # we must avoid cyclic reference between repo and transaction.
2470 reporef = weakref.ref(self)
2470 reporef = weakref.ref(self)
2471 # Code to track tag movement
2471 # Code to track tag movement
2472 #
2472 #
2473 # Since tags are all handled as file content, it is actually quite hard
2473 # Since tags are all handled as file content, it is actually quite hard
2474 # to track these movement from a code perspective. So we fallback to a
2474 # to track these movement from a code perspective. So we fallback to a
2475 # tracking at the repository level. One could envision to track changes
2475 # tracking at the repository level. One could envision to track changes
2476 # to the '.hgtags' file through changegroup apply but that fails to
2476 # to the '.hgtags' file through changegroup apply but that fails to
2477 # cope with case where transaction expose new heads without changegroup
2477 # cope with case where transaction expose new heads without changegroup
2478 # being involved (eg: phase movement).
2478 # being involved (eg: phase movement).
2479 #
2479 #
2480 # For now, We gate the feature behind a flag since this likely comes
2480 # For now, We gate the feature behind a flag since this likely comes
2481 # with performance impacts. The current code run more often than needed
2481 # with performance impacts. The current code run more often than needed
2482 # and do not use caches as much as it could. The current focus is on
2482 # and do not use caches as much as it could. The current focus is on
2483 # the behavior of the feature so we disable it by default. The flag
2483 # the behavior of the feature so we disable it by default. The flag
2484 # will be removed when we are happy with the performance impact.
2484 # will be removed when we are happy with the performance impact.
2485 #
2485 #
2486 # Once this feature is no longer experimental move the following
2486 # Once this feature is no longer experimental move the following
2487 # documentation to the appropriate help section:
2487 # documentation to the appropriate help section:
2488 #
2488 #
2489 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
2489 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
2490 # tags (new or changed or deleted tags). In addition the details of
2490 # tags (new or changed or deleted tags). In addition the details of
2491 # these changes are made available in a file at:
2491 # these changes are made available in a file at:
2492 # ``REPOROOT/.hg/changes/tags.changes``.
2492 # ``REPOROOT/.hg/changes/tags.changes``.
2493 # Make sure you check for HG_TAG_MOVED before reading that file as it
2493 # Make sure you check for HG_TAG_MOVED before reading that file as it
2494 # might exist from a previous transaction even if no tag were touched
2494 # might exist from a previous transaction even if no tag were touched
2495 # in this one. Changes are recorded in a line base format::
2495 # in this one. Changes are recorded in a line base format::
2496 #
2496 #
2497 # <action> <hex-node> <tag-name>\n
2497 # <action> <hex-node> <tag-name>\n
2498 #
2498 #
2499 # Actions are defined as follow:
2499 # Actions are defined as follow:
2500 # "-R": tag is removed,
2500 # "-R": tag is removed,
2501 # "+A": tag is added,
2501 # "+A": tag is added,
2502 # "-M": tag is moved (old value),
2502 # "-M": tag is moved (old value),
2503 # "+M": tag is moved (new value),
2503 # "+M": tag is moved (new value),
2504 tracktags = lambda x: None
2504 tracktags = lambda x: None
2505 # experimental config: experimental.hook-track-tags
2505 # experimental config: experimental.hook-track-tags
2506 shouldtracktags = self.ui.configbool(
2506 shouldtracktags = self.ui.configbool(
2507 b'experimental', b'hook-track-tags'
2507 b'experimental', b'hook-track-tags'
2508 )
2508 )
2509 if desc != b'strip' and shouldtracktags:
2509 if desc != b'strip' and shouldtracktags:
2510 oldheads = self.changelog.headrevs()
2510 oldheads = self.changelog.headrevs()
2511
2511
2512 def tracktags(tr2):
2512 def tracktags(tr2):
2513 repo = reporef()
2513 repo = reporef()
2514 assert repo is not None # help pytype
2514 assert repo is not None # help pytype
2515 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
2515 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
2516 newheads = repo.changelog.headrevs()
2516 newheads = repo.changelog.headrevs()
2517 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
2517 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
2518 # notes: we compare lists here.
2518 # notes: we compare lists here.
2519 # As we do it only once buiding set would not be cheaper
2519 # As we do it only once buiding set would not be cheaper
2520 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
2520 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
2521 if changes:
2521 if changes:
2522 tr2.hookargs[b'tag_moved'] = b'1'
2522 tr2.hookargs[b'tag_moved'] = b'1'
2523 with repo.vfs(
2523 with repo.vfs(
2524 b'changes/tags.changes', b'w', atomictemp=True
2524 b'changes/tags.changes', b'w', atomictemp=True
2525 ) as changesfile:
2525 ) as changesfile:
2526 # note: we do not register the file to the transaction
2526 # note: we do not register the file to the transaction
2527 # because we needs it to still exist on the transaction
2527 # because we needs it to still exist on the transaction
2528 # is close (for txnclose hooks)
2528 # is close (for txnclose hooks)
2529 tagsmod.writediff(changesfile, changes)
2529 tagsmod.writediff(changesfile, changes)
2530
2530
2531 def validate(tr2):
2531 def validate(tr2):
2532 """will run pre-closing hooks"""
2532 """will run pre-closing hooks"""
2533 # XXX the transaction API is a bit lacking here so we take a hacky
2533 # XXX the transaction API is a bit lacking here so we take a hacky
2534 # path for now
2534 # path for now
2535 #
2535 #
2536 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
2536 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
2537 # dict is copied before these run. In addition we needs the data
2537 # dict is copied before these run. In addition we needs the data
2538 # available to in memory hooks too.
2538 # available to in memory hooks too.
2539 #
2539 #
2540 # Moreover, we also need to make sure this runs before txnclose
2540 # Moreover, we also need to make sure this runs before txnclose
2541 # hooks and there is no "pending" mechanism that would execute
2541 # hooks and there is no "pending" mechanism that would execute
2542 # logic only if hooks are about to run.
2542 # logic only if hooks are about to run.
2543 #
2543 #
2544 # Fixing this limitation of the transaction is also needed to track
2544 # Fixing this limitation of the transaction is also needed to track
2545 # other families of changes (bookmarks, phases, obsolescence).
2545 # other families of changes (bookmarks, phases, obsolescence).
2546 #
2546 #
2547 # This will have to be fixed before we remove the experimental
2547 # This will have to be fixed before we remove the experimental
2548 # gating.
2548 # gating.
2549 tracktags(tr2)
2549 tracktags(tr2)
2550 repo = reporef()
2550 repo = reporef()
2551 assert repo is not None # help pytype
2551 assert repo is not None # help pytype
2552
2552
2553 singleheadopt = (b'experimental', b'single-head-per-branch')
2553 singleheadopt = (b'experimental', b'single-head-per-branch')
2554 singlehead = repo.ui.configbool(*singleheadopt)
2554 singlehead = repo.ui.configbool(*singleheadopt)
2555 if singlehead:
2555 if singlehead:
2556 singleheadsub = repo.ui.configsuboptions(*singleheadopt)[1]
2556 singleheadsub = repo.ui.configsuboptions(*singleheadopt)[1]
2557 accountclosed = singleheadsub.get(
2557 accountclosed = singleheadsub.get(
2558 b"account-closed-heads", False
2558 b"account-closed-heads", False
2559 )
2559 )
2560 if singleheadsub.get(b"public-changes-only", False):
2560 if singleheadsub.get(b"public-changes-only", False):
2561 filtername = b"immutable"
2561 filtername = b"immutable"
2562 else:
2562 else:
2563 filtername = b"visible"
2563 filtername = b"visible"
2564 scmutil.enforcesinglehead(
2564 scmutil.enforcesinglehead(
2565 repo, tr2, desc, accountclosed, filtername
2565 repo, tr2, desc, accountclosed, filtername
2566 )
2566 )
2567 if hook.hashook(repo.ui, b'pretxnclose-bookmark'):
2567 if hook.hashook(repo.ui, b'pretxnclose-bookmark'):
2568 for name, (old, new) in sorted(
2568 for name, (old, new) in sorted(
2569 tr.changes[b'bookmarks'].items()
2569 tr.changes[b'bookmarks'].items()
2570 ):
2570 ):
2571 args = tr.hookargs.copy()
2571 args = tr.hookargs.copy()
2572 args.update(bookmarks.preparehookargs(name, old, new))
2572 args.update(bookmarks.preparehookargs(name, old, new))
2573 repo.hook(
2573 repo.hook(
2574 b'pretxnclose-bookmark',
2574 b'pretxnclose-bookmark',
2575 throw=True,
2575 throw=True,
2576 **pycompat.strkwargs(args)
2576 **pycompat.strkwargs(args)
2577 )
2577 )
2578 if hook.hashook(repo.ui, b'pretxnclose-phase'):
2578 if hook.hashook(repo.ui, b'pretxnclose-phase'):
2579 cl = repo.unfiltered().changelog
2579 cl = repo.unfiltered().changelog
2580 for revs, (old, new) in tr.changes[b'phases']:
2580 for revs, (old, new) in tr.changes[b'phases']:
2581 for rev in revs:
2581 for rev in revs:
2582 args = tr.hookargs.copy()
2582 args = tr.hookargs.copy()
2583 node = hex(cl.node(rev))
2583 node = hex(cl.node(rev))
2584 args.update(phases.preparehookargs(node, old, new))
2584 args.update(phases.preparehookargs(node, old, new))
2585 repo.hook(
2585 repo.hook(
2586 b'pretxnclose-phase',
2586 b'pretxnclose-phase',
2587 throw=True,
2587 throw=True,
2588 **pycompat.strkwargs(args)
2588 **pycompat.strkwargs(args)
2589 )
2589 )
2590
2590
2591 repo.hook(
2591 repo.hook(
2592 b'pretxnclose', throw=True, **pycompat.strkwargs(tr.hookargs)
2592 b'pretxnclose', throw=True, **pycompat.strkwargs(tr.hookargs)
2593 )
2593 )
2594
2594
2595 def releasefn(tr, success):
2595 def releasefn(tr, success):
2596 repo = reporef()
2596 repo = reporef()
2597 if repo is None:
2597 if repo is None:
2598 # If the repo has been GC'd (and this release function is being
2598 # If the repo has been GC'd (and this release function is being
2599 # called from transaction.__del__), there's not much we can do,
2599 # called from transaction.__del__), there's not much we can do,
2600 # so just leave the unfinished transaction there and let the
2600 # so just leave the unfinished transaction there and let the
2601 # user run `hg recover`.
2601 # user run `hg recover`.
2602 return
2602 return
2603 if success:
2603 if success:
2604 # this should be explicitly invoked here, because
2604 # this should be explicitly invoked here, because
2605 # in-memory changes aren't written out at closing
2605 # in-memory changes aren't written out at closing
2606 # transaction, if tr.addfilegenerator (via
2606 # transaction, if tr.addfilegenerator (via
2607 # dirstate.write or so) isn't invoked while
2607 # dirstate.write or so) isn't invoked while
2608 # transaction running
2608 # transaction running
2609 repo.dirstate.write(None)
2609 repo.dirstate.write(None)
2610 else:
2610 else:
2611 # discard all changes (including ones already written
2611 # discard all changes (including ones already written
2612 # out) in this transaction
2612 # out) in this transaction
2613 repo.invalidate(clearfilecache=True)
2613 repo.invalidate(clearfilecache=True)
2614
2614
2615 tr = transaction.transaction(
2615 tr = transaction.transaction(
2616 rp,
2616 rp,
2617 self.svfs,
2617 self.svfs,
2618 vfsmap,
2618 vfsmap,
2619 b"journal",
2619 b"journal",
2620 b"undo",
2620 b"undo",
2621 lambda: None,
2621 lambda: None,
2622 self.store.createmode,
2622 self.store.createmode,
2623 validator=validate,
2623 validator=validate,
2624 releasefn=releasefn,
2624 releasefn=releasefn,
2625 checkambigfiles=_cachedfiles,
2625 checkambigfiles=_cachedfiles,
2626 name=desc,
2626 name=desc,
2627 )
2627 )
2628 for vfs_id, path in self._journalfiles():
2628 for vfs_id, path in self._journalfiles():
2629 tr.add_journal(vfs_id, path)
2629 tr.add_journal(vfs_id, path)
2630 tr.changes[b'origrepolen'] = len(self)
2630 tr.changes[b'origrepolen'] = len(self)
2631 tr.changes[b'obsmarkers'] = set()
2631 tr.changes[b'obsmarkers'] = set()
2632 tr.changes[b'phases'] = []
2632 tr.changes[b'phases'] = []
2633 tr.changes[b'bookmarks'] = {}
2633 tr.changes[b'bookmarks'] = {}
2634
2634
2635 tr.hookargs[b'txnid'] = txnid
2635 tr.hookargs[b'txnid'] = txnid
2636 tr.hookargs[b'txnname'] = desc
2636 tr.hookargs[b'txnname'] = desc
2637 tr.hookargs[b'changes'] = tr.changes
2637 tr.hookargs[b'changes'] = tr.changes
2638 # note: writing the fncache only during finalize mean that the file is
2638 # note: writing the fncache only during finalize mean that the file is
2639 # outdated when running hooks. As fncache is used for streaming clone,
2639 # outdated when running hooks. As fncache is used for streaming clone,
2640 # this is not expected to break anything that happen during the hooks.
2640 # this is not expected to break anything that happen during the hooks.
2641 tr.addfinalize(b'flush-fncache', self.store.write)
2641 tr.addfinalize(b'flush-fncache', self.store.write)
2642
2642
2643 def txnclosehook(tr2):
2643 def txnclosehook(tr2):
2644 """To be run if transaction is successful, will schedule a hook run"""
2644 """To be run if transaction is successful, will schedule a hook run"""
2645 # Don't reference tr2 in hook() so we don't hold a reference.
2645 # Don't reference tr2 in hook() so we don't hold a reference.
2646 # This reduces memory consumption when there are multiple
2646 # This reduces memory consumption when there are multiple
2647 # transactions per lock. This can likely go away if issue5045
2647 # transactions per lock. This can likely go away if issue5045
2648 # fixes the function accumulation.
2648 # fixes the function accumulation.
2649 hookargs = tr2.hookargs
2649 hookargs = tr2.hookargs
2650
2650
2651 def hookfunc(unused_success):
2651 def hookfunc(unused_success):
2652 repo = reporef()
2652 repo = reporef()
2653 assert repo is not None # help pytype
2653 assert repo is not None # help pytype
2654
2654
2655 if hook.hashook(repo.ui, b'txnclose-bookmark'):
2655 if hook.hashook(repo.ui, b'txnclose-bookmark'):
2656 bmchanges = sorted(tr.changes[b'bookmarks'].items())
2656 bmchanges = sorted(tr.changes[b'bookmarks'].items())
2657 for name, (old, new) in bmchanges:
2657 for name, (old, new) in bmchanges:
2658 args = tr.hookargs.copy()
2658 args = tr.hookargs.copy()
2659 args.update(bookmarks.preparehookargs(name, old, new))
2659 args.update(bookmarks.preparehookargs(name, old, new))
2660 repo.hook(
2660 repo.hook(
2661 b'txnclose-bookmark',
2661 b'txnclose-bookmark',
2662 throw=False,
2662 throw=False,
2663 **pycompat.strkwargs(args)
2663 **pycompat.strkwargs(args)
2664 )
2664 )
2665
2665
2666 if hook.hashook(repo.ui, b'txnclose-phase'):
2666 if hook.hashook(repo.ui, b'txnclose-phase'):
2667 cl = repo.unfiltered().changelog
2667 cl = repo.unfiltered().changelog
2668 phasemv = sorted(
2668 phasemv = sorted(
2669 tr.changes[b'phases'], key=lambda r: r[0][0]
2669 tr.changes[b'phases'], key=lambda r: r[0][0]
2670 )
2670 )
2671 for revs, (old, new) in phasemv:
2671 for revs, (old, new) in phasemv:
2672 for rev in revs:
2672 for rev in revs:
2673 args = tr.hookargs.copy()
2673 args = tr.hookargs.copy()
2674 node = hex(cl.node(rev))
2674 node = hex(cl.node(rev))
2675 args.update(phases.preparehookargs(node, old, new))
2675 args.update(phases.preparehookargs(node, old, new))
2676 repo.hook(
2676 repo.hook(
2677 b'txnclose-phase',
2677 b'txnclose-phase',
2678 throw=False,
2678 throw=False,
2679 **pycompat.strkwargs(args)
2679 **pycompat.strkwargs(args)
2680 )
2680 )
2681
2681
2682 repo.hook(
2682 repo.hook(
2683 b'txnclose', throw=False, **pycompat.strkwargs(hookargs)
2683 b'txnclose', throw=False, **pycompat.strkwargs(hookargs)
2684 )
2684 )
2685
2685
2686 repo = reporef()
2686 repo = reporef()
2687 assert repo is not None # help pytype
2687 assert repo is not None # help pytype
2688 repo._afterlock(hookfunc)
2688 repo._afterlock(hookfunc)
2689
2689
2690 tr.addfinalize(b'txnclose-hook', txnclosehook)
2690 tr.addfinalize(b'txnclose-hook', txnclosehook)
2691 # Include a leading "-" to make it happen before the transaction summary
2691 # Include a leading "-" to make it happen before the transaction summary
2692 # reports registered via scmutil.registersummarycallback() whose names
2692 # reports registered via scmutil.registersummarycallback() whose names
2693 # are 00-txnreport etc. That way, the caches will be warm when the
2693 # are 00-txnreport etc. That way, the caches will be warm when the
2694 # callbacks run.
2694 # callbacks run.
2695 tr.addpostclose(b'-warm-cache', self._buildcacheupdater(tr))
2695 tr.addpostclose(b'-warm-cache', self._buildcacheupdater(tr))
2696
2696
2697 def txnaborthook(tr2):
2697 def txnaborthook(tr2):
2698 """To be run if transaction is aborted"""
2698 """To be run if transaction is aborted"""
2699 repo = reporef()
2699 repo = reporef()
2700 assert repo is not None # help pytype
2700 assert repo is not None # help pytype
2701 repo.hook(
2701 repo.hook(
2702 b'txnabort', throw=False, **pycompat.strkwargs(tr2.hookargs)
2702 b'txnabort', throw=False, **pycompat.strkwargs(tr2.hookargs)
2703 )
2703 )
2704
2704
2705 tr.addabort(b'txnabort-hook', txnaborthook)
2705 tr.addabort(b'txnabort-hook', txnaborthook)
2706 # avoid eager cache invalidation. in-memory data should be identical
2706 # avoid eager cache invalidation. in-memory data should be identical
2707 # to stored data if transaction has no error.
2707 # to stored data if transaction has no error.
2708 tr.addpostclose(b'refresh-filecachestats', self._refreshfilecachestats)
2708 tr.addpostclose(b'refresh-filecachestats', self._refreshfilecachestats)
2709 self._transref = weakref.ref(tr)
2709 self._transref = weakref.ref(tr)
2710 scmutil.registersummarycallback(self, tr, desc)
2710 scmutil.registersummarycallback(self, tr, desc)
2711 # This only exist to deal with the need of rollback to have viable
2711 # This only exist to deal with the need of rollback to have viable
2712 # parents at the end of the operation. So backup viable parents at the
2712 # parents at the end of the operation. So backup viable parents at the
2713 # time of this operation.
2713 # time of this operation.
2714 #
2714 #
2715 # We only do it when the `wlock` is taken, otherwise other might be
2715 # We only do it when the `wlock` is taken, otherwise other might be
2716 # altering the dirstate under us.
2716 # altering the dirstate under us.
2717 #
2717 #
2718 # This is really not a great way to do this (first, because we cannot
2718 # This is really not a great way to do this (first, because we cannot
2719 # always do it). There are more viable alternative that exists
2719 # always do it). There are more viable alternative that exists
2720 #
2720 #
2721 # - backing only the working copy parent in a dedicated files and doing
2721 # - backing only the working copy parent in a dedicated files and doing
2722 # a clean "keep-update" to them on `hg rollback`.
2722 # a clean "keep-update" to them on `hg rollback`.
2723 #
2723 #
2724 # - slightly changing the behavior an applying a logic similar to "hg
2724 # - slightly changing the behavior an applying a logic similar to "hg
2725 # strip" to pick a working copy destination on `hg rollback`
2725 # strip" to pick a working copy destination on `hg rollback`
2726 if self.currentwlock() is not None:
2726 if self.currentwlock() is not None:
2727 ds = self.dirstate
2727 ds = self.dirstate
2728 if not self.vfs.exists(b'branch'):
2728 if not self.vfs.exists(b'branch'):
2729 # force a file to be written if None exist
2729 # force a file to be written if None exist
2730 ds.setbranch(b'default', None)
2730 ds.setbranch(b'default', None)
2731
2731
2732 def backup_dirstate(tr):
2732 def backup_dirstate(tr):
2733 for f in ds.all_file_names():
2733 for f in ds.all_file_names():
2734 # hardlink backup is okay because `dirstate` is always
2734 # hardlink backup is okay because `dirstate` is always
2735 # atomically written and possible data file are append only
2735 # atomically written and possible data file are append only
2736 # and resistant to trailing data.
2736 # and resistant to trailing data.
2737 tr.addbackup(f, hardlink=True, location=b'plain')
2737 tr.addbackup(f, hardlink=True, location=b'plain')
2738
2738
2739 tr.addvalidator(b'dirstate-backup', backup_dirstate)
2739 tr.addvalidator(b'dirstate-backup', backup_dirstate)
2740 return tr
2740 return tr
2741
2741
2742 def _journalfiles(self):
2742 def _journalfiles(self):
2743 return (
2743 return (
2744 (self.svfs, b'journal'),
2744 (self.svfs, b'journal'),
2745 (self.vfs, b'journal.desc'),
2745 (self.vfs, b'journal.desc'),
2746 )
2746 )
2747
2747
2748 def undofiles(self):
2748 def undofiles(self):
2749 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
2749 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
2750
2750
2751 @unfilteredmethod
2751 @unfilteredmethod
2752 def _writejournal(self, desc):
2752 def _writejournal(self, desc):
2753 self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc))
2753 self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc))
2754
2754
2755 def recover(self):
2755 def recover(self):
2756 with self.lock():
2756 with self.lock():
2757 if self.svfs.exists(b"journal"):
2757 if self.svfs.exists(b"journal"):
2758 self.ui.status(_(b"rolling back interrupted transaction\n"))
2758 self.ui.status(_(b"rolling back interrupted transaction\n"))
2759 vfsmap = self.vfs_map
2759 vfsmap = self.vfs_map
2760 transaction.rollback(
2760 transaction.rollback(
2761 self.svfs,
2761 self.svfs,
2762 vfsmap,
2762 vfsmap,
2763 b"journal",
2763 b"journal",
2764 self.ui.warn,
2764 self.ui.warn,
2765 checkambigfiles=_cachedfiles,
2765 checkambigfiles=_cachedfiles,
2766 )
2766 )
2767 self.invalidate()
2767 self.invalidate()
2768 return True
2768 return True
2769 else:
2769 else:
2770 self.ui.warn(_(b"no interrupted transaction available\n"))
2770 self.ui.warn(_(b"no interrupted transaction available\n"))
2771 return False
2771 return False
2772
2772
2773 def rollback(self, dryrun=False, force=False):
2773 def rollback(self, dryrun=False, force=False):
2774 wlock = lock = None
2774 wlock = lock = None
2775 try:
2775 try:
2776 wlock = self.wlock()
2776 wlock = self.wlock()
2777 lock = self.lock()
2777 lock = self.lock()
2778 if self.svfs.exists(b"undo"):
2778 if self.svfs.exists(b"undo"):
2779 return self._rollback(dryrun, force)
2779 return self._rollback(dryrun, force)
2780 else:
2780 else:
2781 self.ui.warn(_(b"no rollback information available\n"))
2781 self.ui.warn(_(b"no rollback information available\n"))
2782 return 1
2782 return 1
2783 finally:
2783 finally:
2784 release(lock, wlock)
2784 release(lock, wlock)
2785
2785
2786 @unfilteredmethod # Until we get smarter cache management
2786 @unfilteredmethod # Until we get smarter cache management
2787 def _rollback(self, dryrun, force):
2787 def _rollback(self, dryrun, force):
2788 ui = self.ui
2788 ui = self.ui
2789
2789
2790 parents = self.dirstate.parents()
2790 parents = self.dirstate.parents()
2791 try:
2791 try:
2792 args = self.vfs.read(b'undo.desc').splitlines()
2792 args = self.vfs.read(b'undo.desc').splitlines()
2793 (oldlen, desc, detail) = (int(args[0]), args[1], None)
2793 (oldlen, desc, detail) = (int(args[0]), args[1], None)
2794 if len(args) >= 3:
2794 if len(args) >= 3:
2795 detail = args[2]
2795 detail = args[2]
2796 oldtip = oldlen - 1
2796 oldtip = oldlen - 1
2797
2797
2798 if detail and ui.verbose:
2798 if detail and ui.verbose:
2799 msg = _(
2799 msg = _(
2800 b'repository tip rolled back to revision %d'
2800 b'repository tip rolled back to revision %d'
2801 b' (undo %s: %s)\n'
2801 b' (undo %s: %s)\n'
2802 ) % (oldtip, desc, detail)
2802 ) % (oldtip, desc, detail)
2803 else:
2803 else:
2804 msg = _(
2804 msg = _(
2805 b'repository tip rolled back to revision %d (undo %s)\n'
2805 b'repository tip rolled back to revision %d (undo %s)\n'
2806 ) % (oldtip, desc)
2806 ) % (oldtip, desc)
2807 parentgone = any(self[p].rev() > oldtip for p in parents)
2807 parentgone = any(self[p].rev() > oldtip for p in parents)
2808 except IOError:
2808 except IOError:
2809 msg = _(b'rolling back unknown transaction\n')
2809 msg = _(b'rolling back unknown transaction\n')
2810 desc = None
2810 desc = None
2811 parentgone = True
2811 parentgone = True
2812
2812
2813 if not force and self[b'.'] != self[b'tip'] and desc == b'commit':
2813 if not force and self[b'.'] != self[b'tip'] and desc == b'commit':
2814 raise error.Abort(
2814 raise error.Abort(
2815 _(
2815 _(
2816 b'rollback of last commit while not checked out '
2816 b'rollback of last commit while not checked out '
2817 b'may lose data'
2817 b'may lose data'
2818 ),
2818 ),
2819 hint=_(b'use -f to force'),
2819 hint=_(b'use -f to force'),
2820 )
2820 )
2821
2821
2822 ui.status(msg)
2822 ui.status(msg)
2823 if dryrun:
2823 if dryrun:
2824 return 0
2824 return 0
2825
2825
2826 self.destroying()
2826 self.destroying()
2827 vfsmap = self.vfs_map
2827 vfsmap = self.vfs_map
2828 skip_journal_pattern = None
2828 skip_journal_pattern = None
2829 if not parentgone:
2829 if not parentgone:
2830 skip_journal_pattern = RE_SKIP_DIRSTATE_ROLLBACK
2830 skip_journal_pattern = RE_SKIP_DIRSTATE_ROLLBACK
2831 transaction.rollback(
2831 transaction.rollback(
2832 self.svfs,
2832 self.svfs,
2833 vfsmap,
2833 vfsmap,
2834 b'undo',
2834 b'undo',
2835 ui.warn,
2835 ui.warn,
2836 checkambigfiles=_cachedfiles,
2836 checkambigfiles=_cachedfiles,
2837 skip_journal_pattern=skip_journal_pattern,
2837 skip_journal_pattern=skip_journal_pattern,
2838 )
2838 )
2839 self.invalidate()
2839 self.invalidate()
2840 self.dirstate.invalidate()
2840 self.dirstate.invalidate()
2841
2841
2842 if parentgone:
2842 if parentgone:
2843 # replace this with some explicit parent update in the future.
2843 # replace this with some explicit parent update in the future.
2844 has_node = self.changelog.index.has_node
2844 has_node = self.changelog.index.has_node
2845 if not all(has_node(p) for p in self.dirstate._pl):
2845 if not all(has_node(p) for p in self.dirstate._pl):
2846 # There was no dirstate to backup initially, we need to drop
2846 # There was no dirstate to backup initially, we need to drop
2847 # the existing one.
2847 # the existing one.
2848 with self.dirstate.changing_parents(self):
2848 with self.dirstate.changing_parents(self):
2849 self.dirstate.setparents(self.nullid)
2849 self.dirstate.setparents(self.nullid)
2850 self.dirstate.clear()
2850 self.dirstate.clear()
2851
2851
2852 parents = tuple([p.rev() for p in self[None].parents()])
2852 parents = tuple([p.rev() for p in self[None].parents()])
2853 if len(parents) > 1:
2853 if len(parents) > 1:
2854 ui.status(
2854 ui.status(
2855 _(
2855 _(
2856 b'working directory now based on '
2856 b'working directory now based on '
2857 b'revisions %d and %d\n'
2857 b'revisions %d and %d\n'
2858 )
2858 )
2859 % parents
2859 % parents
2860 )
2860 )
2861 else:
2861 else:
2862 ui.status(
2862 ui.status(
2863 _(b'working directory now based on revision %d\n') % parents
2863 _(b'working directory now based on revision %d\n') % parents
2864 )
2864 )
2865 mergestatemod.mergestate.clean(self)
2865 mergestatemod.mergestate.clean(self)
2866
2866
2867 # TODO: if we know which new heads may result from this rollback, pass
2867 # TODO: if we know which new heads may result from this rollback, pass
2868 # them to destroy(), which will prevent the branchhead cache from being
2868 # them to destroy(), which will prevent the branchhead cache from being
2869 # invalidated.
2869 # invalidated.
2870 self.destroyed()
2870 self.destroyed()
2871 return 0
2871 return 0
2872
2872
2873 def _buildcacheupdater(self, newtransaction):
2873 def _buildcacheupdater(self, newtransaction):
2874 """called during transaction to build the callback updating cache
2874 """called during transaction to build the callback updating cache
2875
2875
2876 Lives on the repository to help extension who might want to augment
2876 Lives on the repository to help extension who might want to augment
2877 this logic. For this purpose, the created transaction is passed to the
2877 this logic. For this purpose, the created transaction is passed to the
2878 method.
2878 method.
2879 """
2879 """
2880 # we must avoid cyclic reference between repo and transaction.
2880 # we must avoid cyclic reference between repo and transaction.
2881 reporef = weakref.ref(self)
2881 reporef = weakref.ref(self)
2882
2882
2883 def updater(tr):
2883 def updater(tr):
2884 repo = reporef()
2884 repo = reporef()
2885 assert repo is not None # help pytype
2885 assert repo is not None # help pytype
2886 repo.updatecaches(tr)
2886 repo.updatecaches(tr)
2887
2887
2888 return updater
2888 return updater
2889
2889
2890 @unfilteredmethod
2890 @unfilteredmethod
2891 def updatecaches(self, tr=None, full=False, caches=None):
2891 def updatecaches(self, tr=None, full=False, caches=None):
2892 """warm appropriate caches
2892 """warm appropriate caches
2893
2893
2894 If this function is called after a transaction closed. The transaction
2894 If this function is called after a transaction closed. The transaction
2895 will be available in the 'tr' argument. This can be used to selectively
2895 will be available in the 'tr' argument. This can be used to selectively
2896 update caches relevant to the changes in that transaction.
2896 update caches relevant to the changes in that transaction.
2897
2897
2898 If 'full' is set, make sure all caches the function knows about have
2898 If 'full' is set, make sure all caches the function knows about have
2899 up-to-date data. Even the ones usually loaded more lazily.
2899 up-to-date data. Even the ones usually loaded more lazily.
2900
2900
2901 The `full` argument can take a special "post-clone" value. In this case
2901 The `full` argument can take a special "post-clone" value. In this case
2902 the cache warming is made after a clone and of the slower cache might
2902 the cache warming is made after a clone and of the slower cache might
2903 be skipped, namely the `.fnodetags` one. This argument is 5.8 specific
2903 be skipped, namely the `.fnodetags` one. This argument is 5.8 specific
2904 as we plan for a cleaner way to deal with this for 5.9.
2904 as we plan for a cleaner way to deal with this for 5.9.
2905 """
2905 """
2906 if tr is not None and tr.hookargs.get(b'source') == b'strip':
2906 if tr is not None and tr.hookargs.get(b'source') == b'strip':
2907 # During strip, many caches are invalid but
2907 # During strip, many caches are invalid but
2908 # later call to `destroyed` will refresh them.
2908 # later call to `destroyed` will refresh them.
2909 return
2909 return
2910
2910
2911 unfi = self.unfiltered()
2911 unfi = self.unfiltered()
2912
2912
2913 if full:
2913 if full:
2914 msg = (
2914 msg = (
2915 "`full` argument for `repo.updatecaches` is deprecated\n"
2915 "`full` argument for `repo.updatecaches` is deprecated\n"
2916 "(use `caches=repository.CACHE_ALL` instead)"
2916 "(use `caches=repository.CACHE_ALL` instead)"
2917 )
2917 )
2918 self.ui.deprecwarn(msg, b"5.9")
2918 self.ui.deprecwarn(msg, b"5.9")
2919 caches = repository.CACHES_ALL
2919 caches = repository.CACHES_ALL
2920 if full == b"post-clone":
2920 if full == b"post-clone":
2921 caches = repository.CACHES_POST_CLONE
2921 caches = repository.CACHES_POST_CLONE
2922 caches = repository.CACHES_ALL
2922 caches = repository.CACHES_ALL
2923 elif caches is None:
2923 elif caches is None:
2924 caches = repository.CACHES_DEFAULT
2924 caches = repository.CACHES_DEFAULT
2925
2925
2926 if repository.CACHE_BRANCHMAP_SERVED in caches:
2926 if repository.CACHE_BRANCHMAP_SERVED in caches:
2927 if tr is None or tr.changes[b'origrepolen'] < len(self):
2927 if tr is None or tr.changes[b'origrepolen'] < len(self):
2928 # accessing the 'served' branchmap should refresh all the others,
2928 # accessing the 'served' branchmap should refresh all the others,
2929 self.ui.debug(b'updating the branch cache\n')
2929 self.ui.debug(b'updating the branch cache\n')
2930 self.filtered(b'served').branchmap()
2930 self.filtered(b'served').branchmap()
2931 self.filtered(b'served.hidden').branchmap()
2931 self.filtered(b'served.hidden').branchmap()
2932 # flush all possibly delayed write.
2932 # flush all possibly delayed write.
2933 self._branchcaches.write_delayed(self)
2933 self._branchcaches.write_delayed(self)
2934
2934
2935 if repository.CACHE_CHANGELOG_CACHE in caches:
2935 if repository.CACHE_CHANGELOG_CACHE in caches:
2936 self.changelog.update_caches(transaction=tr)
2936 self.changelog.update_caches(transaction=tr)
2937
2937
2938 if repository.CACHE_MANIFESTLOG_CACHE in caches:
2938 if repository.CACHE_MANIFESTLOG_CACHE in caches:
2939 self.manifestlog.update_caches(transaction=tr)
2939 self.manifestlog.update_caches(transaction=tr)
2940 for entry in self.store.walk():
2940 for entry in self.store.walk():
2941 if not entry.is_revlog:
2941 if not entry.is_revlog:
2942 continue
2942 continue
2943 if not entry.is_manifestlog:
2943 if not entry.is_manifestlog:
2944 continue
2944 continue
2945 manifestrevlog = entry.get_revlog_instance(self).get_revlog()
2945 manifestrevlog = entry.get_revlog_instance(self).get_revlog()
2946 if manifestrevlog is not None:
2946 if manifestrevlog is not None:
2947 manifestrevlog.update_caches(transaction=tr)
2947 manifestrevlog.update_caches(transaction=tr)
2948
2948
2949 if repository.CACHE_REV_BRANCH in caches:
2949 if repository.CACHE_REV_BRANCH in caches:
2950 rbc = unfi.revbranchcache()
2950 rbc = unfi.revbranchcache()
2951 for r in unfi.changelog:
2951 for r in unfi.changelog:
2952 rbc.branchinfo(r)
2952 rbc.branchinfo(r)
2953 rbc.write()
2953 rbc.write()
2954
2954
2955 if repository.CACHE_FULL_MANIFEST in caches:
2955 if repository.CACHE_FULL_MANIFEST in caches:
2956 # ensure the working copy parents are in the manifestfulltextcache
2956 # ensure the working copy parents are in the manifestfulltextcache
2957 for ctx in self[b'.'].parents():
2957 for ctx in self[b'.'].parents():
2958 ctx.manifest() # accessing the manifest is enough
2958 ctx.manifest() # accessing the manifest is enough
2959
2959
2960 if repository.CACHE_FILE_NODE_TAGS in caches:
2960 if repository.CACHE_FILE_NODE_TAGS in caches:
2961 # accessing fnode cache warms the cache
2961 # accessing fnode cache warms the cache
2962 tagsmod.fnoderevs(self.ui, unfi, unfi.changelog.revs())
2962 tagsmod.fnoderevs(self.ui, unfi, unfi.changelog.revs())
2963
2963
2964 if repository.CACHE_TAGS_DEFAULT in caches:
2964 if repository.CACHE_TAGS_DEFAULT in caches:
2965 # accessing tags warm the cache
2965 # accessing tags warm the cache
2966 self.tags()
2966 self.tags()
2967 if repository.CACHE_TAGS_SERVED in caches:
2967 if repository.CACHE_TAGS_SERVED in caches:
2968 self.filtered(b'served').tags()
2968 self.filtered(b'served').tags()
2969
2969
2970 if repository.CACHE_BRANCHMAP_ALL in caches:
2970 if repository.CACHE_BRANCHMAP_ALL in caches:
2971 # The CACHE_BRANCHMAP_ALL updates lazily-loaded caches immediately,
2971 # The CACHE_BRANCHMAP_ALL updates lazily-loaded caches immediately,
2972 # so we're forcing a write to cause these caches to be warmed up
2972 # so we're forcing a write to cause these caches to be warmed up
2973 # even if they haven't explicitly been requested yet (if they've
2973 # even if they haven't explicitly been requested yet (if they've
2974 # never been used by hg, they won't ever have been written, even if
2974 # never been used by hg, they won't ever have been written, even if
2975 # they're a subset of another kind of cache that *has* been used).
2975 # they're a subset of another kind of cache that *has* been used).
2976 for filt in repoview.filtertable.keys():
2976 for filt in repoview.filtertable.keys():
2977 filtered = self.filtered(filt)
2977 filtered = self.filtered(filt)
2978 filtered.branchmap().write(filtered)
2978 filtered.branchmap().write(filtered)
2979
2979
2980 def invalidatecaches(self):
2980 def invalidatecaches(self):
2981 if '_tagscache' in vars(self):
2981 if '_tagscache' in vars(self):
2982 # can't use delattr on proxy
2982 # can't use delattr on proxy
2983 del self.__dict__['_tagscache']
2983 del self.__dict__['_tagscache']
2984
2984
2985 self._branchcaches.clear()
2985 self._branchcaches.clear()
2986 self.invalidatevolatilesets()
2986 self.invalidatevolatilesets()
2987 self._sparsesignaturecache.clear()
2987 self._sparsesignaturecache.clear()
2988
2988
2989 def invalidatevolatilesets(self):
2989 def invalidatevolatilesets(self):
2990 self.filteredrevcache.clear()
2990 self.filteredrevcache.clear()
2991 obsolete.clearobscaches(self)
2991 obsolete.clearobscaches(self)
2992 self._quick_access_changeid_invalidate()
2992 self._quick_access_changeid_invalidate()
2993
2993
2994 def invalidatedirstate(self):
2994 def invalidatedirstate(self):
2995 """Invalidates the dirstate, causing the next call to dirstate
2995 """Invalidates the dirstate, causing the next call to dirstate
2996 to check if it was modified since the last time it was read,
2996 to check if it was modified since the last time it was read,
2997 rereading it if it has.
2997 rereading it if it has.
2998
2998
2999 This is different to dirstate.invalidate() that it doesn't always
2999 This is different to dirstate.invalidate() that it doesn't always
3000 rereads the dirstate. Use dirstate.invalidate() if you want to
3000 rereads the dirstate. Use dirstate.invalidate() if you want to
3001 explicitly read the dirstate again (i.e. restoring it to a previous
3001 explicitly read the dirstate again (i.e. restoring it to a previous
3002 known good state)."""
3002 known good state)."""
3003 unfi = self.unfiltered()
3003 unfi = self.unfiltered()
3004 if 'dirstate' in unfi.__dict__:
3004 if 'dirstate' in unfi.__dict__:
3005 assert not self.dirstate.is_changing_any
3005 assert not self.dirstate.is_changing_any
3006 del unfi.__dict__['dirstate']
3006 del unfi.__dict__['dirstate']
3007
3007
3008 def invalidate(self, clearfilecache=False):
3008 def invalidate(self, clearfilecache=False):
3009 """Invalidates both store and non-store parts other than dirstate
3009 """Invalidates both store and non-store parts other than dirstate
3010
3010
3011 If a transaction is running, invalidation of store is omitted,
3011 If a transaction is running, invalidation of store is omitted,
3012 because discarding in-memory changes might cause inconsistency
3012 because discarding in-memory changes might cause inconsistency
3013 (e.g. incomplete fncache causes unintentional failure, but
3013 (e.g. incomplete fncache causes unintentional failure, but
3014 redundant one doesn't).
3014 redundant one doesn't).
3015 """
3015 """
3016 unfiltered = self.unfiltered() # all file caches are stored unfiltered
3016 unfiltered = self.unfiltered() # all file caches are stored unfiltered
3017 for k in list(self._filecache.keys()):
3017 for k in list(self._filecache.keys()):
3018 if (
3018 if (
3019 k == b'changelog'
3019 k == b'changelog'
3020 and self.currenttransaction()
3020 and self.currenttransaction()
3021 and self.changelog._delayed
3021 and self.changelog._delayed
3022 ):
3022 ):
3023 # The changelog object may store unwritten revisions. We don't
3023 # The changelog object may store unwritten revisions. We don't
3024 # want to lose them.
3024 # want to lose them.
3025 # TODO: Solve the problem instead of working around it.
3025 # TODO: Solve the problem instead of working around it.
3026 continue
3026 continue
3027
3027
3028 if clearfilecache:
3028 if clearfilecache:
3029 del self._filecache[k]
3029 del self._filecache[k]
3030 try:
3030 try:
3031 # XXX ideally, the key would be a unicode string to match the
3031 # XXX ideally, the key would be a unicode string to match the
3032 # fact it refers to an attribut name. However changing this was
3032 # fact it refers to an attribut name. However changing this was
3033 # a bit a scope creep compared to the series cleaning up
3033 # a bit a scope creep compared to the series cleaning up
3034 # del/set/getattr so we kept thing simple here.
3034 # del/set/getattr so we kept thing simple here.
3035 delattr(unfiltered, pycompat.sysstr(k))
3035 delattr(unfiltered, pycompat.sysstr(k))
3036 except AttributeError:
3036 except AttributeError:
3037 pass
3037 pass
3038 self.invalidatecaches()
3038 self.invalidatecaches()
3039 if not self.currenttransaction():
3039 if not self.currenttransaction():
3040 # TODO: Changing contents of store outside transaction
3040 # TODO: Changing contents of store outside transaction
3041 # causes inconsistency. We should make in-memory store
3041 # causes inconsistency. We should make in-memory store
3042 # changes detectable, and abort if changed.
3042 # changes detectable, and abort if changed.
3043 self.store.invalidatecaches()
3043 self.store.invalidatecaches()
3044
3044
3045 def invalidateall(self):
3045 def invalidateall(self):
3046 """Fully invalidates both store and non-store parts, causing the
3046 """Fully invalidates both store and non-store parts, causing the
3047 subsequent operation to reread any outside changes."""
3047 subsequent operation to reread any outside changes."""
3048 # extension should hook this to invalidate its caches
3048 # extension should hook this to invalidate its caches
3049 self.invalidate()
3049 self.invalidate()
3050 self.invalidatedirstate()
3050 self.invalidatedirstate()
3051
3051
3052 @unfilteredmethod
3052 @unfilteredmethod
3053 def _refreshfilecachestats(self, tr):
3053 def _refreshfilecachestats(self, tr):
3054 """Reload stats of cached files so that they are flagged as valid"""
3054 """Reload stats of cached files so that they are flagged as valid"""
3055 for k, ce in self._filecache.items():
3055 for k, ce in self._filecache.items():
3056 k = pycompat.sysstr(k)
3056 k = pycompat.sysstr(k)
3057 if k == 'dirstate' or k not in self.__dict__:
3057 if k == 'dirstate' or k not in self.__dict__:
3058 continue
3058 continue
3059 ce.refresh()
3059 ce.refresh()
3060
3060
3061 def _lock(
3061 def _lock(
3062 self,
3062 self,
3063 vfs,
3063 vfs,
3064 lockname,
3064 lockname,
3065 wait,
3065 wait,
3066 releasefn,
3066 releasefn,
3067 acquirefn,
3067 acquirefn,
3068 desc,
3068 desc,
3069 ):
3069 ):
3070 timeout = 0
3070 timeout = 0
3071 warntimeout = 0
3071 warntimeout = 0
3072 if wait:
3072 if wait:
3073 timeout = self.ui.configint(b"ui", b"timeout")
3073 timeout = self.ui.configint(b"ui", b"timeout")
3074 warntimeout = self.ui.configint(b"ui", b"timeout.warn")
3074 warntimeout = self.ui.configint(b"ui", b"timeout.warn")
3075 # internal config: ui.signal-safe-lock
3075 # internal config: ui.signal-safe-lock
3076 signalsafe = self.ui.configbool(b'ui', b'signal-safe-lock')
3076 signalsafe = self.ui.configbool(b'ui', b'signal-safe-lock')
3077
3077
3078 l = lockmod.trylock(
3078 l = lockmod.trylock(
3079 self.ui,
3079 self.ui,
3080 vfs,
3080 vfs,
3081 lockname,
3081 lockname,
3082 timeout,
3082 timeout,
3083 warntimeout,
3083 warntimeout,
3084 releasefn=releasefn,
3084 releasefn=releasefn,
3085 acquirefn=acquirefn,
3085 acquirefn=acquirefn,
3086 desc=desc,
3086 desc=desc,
3087 signalsafe=signalsafe,
3087 signalsafe=signalsafe,
3088 )
3088 )
3089 return l
3089 return l
3090
3090
3091 def _afterlock(self, callback):
3091 def _afterlock(self, callback):
3092 """add a callback to be run when the repository is fully unlocked
3092 """add a callback to be run when the repository is fully unlocked
3093
3093
3094 The callback will be executed when the outermost lock is released
3094 The callback will be executed when the outermost lock is released
3095 (with wlock being higher level than 'lock')."""
3095 (with wlock being higher level than 'lock')."""
3096 for ref in (self._wlockref, self._lockref):
3096 for ref in (self._wlockref, self._lockref):
3097 l = ref and ref()
3097 l = ref and ref()
3098 if l and l.held:
3098 if l and l.held:
3099 l.postrelease.append(callback)
3099 l.postrelease.append(callback)
3100 break
3100 break
3101 else: # no lock have been found.
3101 else: # no lock have been found.
3102 callback(True)
3102 callback(True)
3103
3103
3104 def lock(self, wait=True):
3104 def lock(self, wait=True):
3105 """Lock the repository store (.hg/store) and return a weak reference
3105 """Lock the repository store (.hg/store) and return a weak reference
3106 to the lock. Use this before modifying the store (e.g. committing or
3106 to the lock. Use this before modifying the store (e.g. committing or
3107 stripping). If you are opening a transaction, get a lock as well.)
3107 stripping). If you are opening a transaction, get a lock as well.)
3108
3108
3109 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
3109 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
3110 'wlock' first to avoid a dead-lock hazard."""
3110 'wlock' first to avoid a dead-lock hazard."""
3111 l = self._currentlock(self._lockref)
3111 l = self._currentlock(self._lockref)
3112 if l is not None:
3112 if l is not None:
3113 l.lock()
3113 l.lock()
3114 return l
3114 return l
3115
3115
3116 l = self._lock(
3116 l = self._lock(
3117 vfs=self.svfs,
3117 vfs=self.svfs,
3118 lockname=b"lock",
3118 lockname=b"lock",
3119 wait=wait,
3119 wait=wait,
3120 releasefn=None,
3120 releasefn=None,
3121 acquirefn=self.invalidate,
3121 acquirefn=self.invalidate,
3122 desc=_(b'repository %s') % self.origroot,
3122 desc=_(b'repository %s') % self.origroot,
3123 )
3123 )
3124 self._lockref = weakref.ref(l)
3124 self._lockref = weakref.ref(l)
3125 return l
3125 return l
3126
3126
3127 def wlock(self, wait=True):
3127 def wlock(self, wait=True):
3128 """Lock the non-store parts of the repository (everything under
3128 """Lock the non-store parts of the repository (everything under
3129 .hg except .hg/store) and return a weak reference to the lock.
3129 .hg except .hg/store) and return a weak reference to the lock.
3130
3130
3131 Use this before modifying files in .hg.
3131 Use this before modifying files in .hg.
3132
3132
3133 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
3133 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
3134 'wlock' first to avoid a dead-lock hazard."""
3134 'wlock' first to avoid a dead-lock hazard."""
3135 l = self._wlockref() if self._wlockref else None
3135 l = self._wlockref() if self._wlockref else None
3136 if l is not None and l.held:
3136 if l is not None and l.held:
3137 l.lock()
3137 l.lock()
3138 return l
3138 return l
3139
3139
3140 # We do not need to check for non-waiting lock acquisition. Such
3140 # We do not need to check for non-waiting lock acquisition. Such
3141 # acquisition would not cause dead-lock as they would just fail.
3141 # acquisition would not cause dead-lock as they would just fail.
3142 if wait and (
3142 if wait and (
3143 self.ui.configbool(b'devel', b'all-warnings')
3143 self.ui.configbool(b'devel', b'all-warnings')
3144 or self.ui.configbool(b'devel', b'check-locks')
3144 or self.ui.configbool(b'devel', b'check-locks')
3145 ):
3145 ):
3146 if self._currentlock(self._lockref) is not None:
3146 if self._currentlock(self._lockref) is not None:
3147 self.ui.develwarn(b'"wlock" acquired after "lock"')
3147 self.ui.develwarn(b'"wlock" acquired after "lock"')
3148
3148
3149 def unlock():
3149 def unlock():
3150 if self.dirstate.is_changing_any:
3150 if self.dirstate.is_changing_any:
3151 msg = b"wlock release in the middle of a changing parents"
3151 msg = b"wlock release in the middle of a changing parents"
3152 self.ui.develwarn(msg)
3152 self.ui.develwarn(msg)
3153 self.dirstate.invalidate()
3153 self.dirstate.invalidate()
3154 else:
3154 else:
3155 if self.dirstate._dirty:
3155 if self.dirstate._dirty:
3156 msg = b"dirty dirstate on wlock release"
3156 msg = b"dirty dirstate on wlock release"
3157 self.ui.develwarn(msg)
3157 self.ui.develwarn(msg)
3158 self.dirstate.write(None)
3158 self.dirstate.write(None)
3159
3159
3160 unfi = self.unfiltered()
3160 unfi = self.unfiltered()
3161 if 'dirstate' in unfi.__dict__:
3161 if 'dirstate' in unfi.__dict__:
3162 del unfi.__dict__['dirstate']
3162 del unfi.__dict__['dirstate']
3163
3163
3164 l = self._lock(
3164 l = self._lock(
3165 self.vfs,
3165 self.vfs,
3166 b"wlock",
3166 b"wlock",
3167 wait,
3167 wait,
3168 unlock,
3168 unlock,
3169 self.invalidatedirstate,
3169 self.invalidatedirstate,
3170 _(b'working directory of %s') % self.origroot,
3170 _(b'working directory of %s') % self.origroot,
3171 )
3171 )
3172 self._wlockref = weakref.ref(l)
3172 self._wlockref = weakref.ref(l)
3173 return l
3173 return l
3174
3174
3175 def _currentlock(self, lockref):
3175 def _currentlock(self, lockref):
3176 """Returns the lock if it's held, or None if it's not."""
3176 """Returns the lock if it's held, or None if it's not."""
3177 if lockref is None:
3177 if lockref is None:
3178 return None
3178 return None
3179 l = lockref()
3179 l = lockref()
3180 if l is None or not l.held:
3180 if l is None or not l.held:
3181 return None
3181 return None
3182 return l
3182 return l
3183
3183
3184 def currentwlock(self):
3184 def currentwlock(self):
3185 """Returns the wlock if it's held, or None if it's not."""
3185 """Returns the wlock if it's held, or None if it's not."""
3186 return self._currentlock(self._wlockref)
3186 return self._currentlock(self._wlockref)
3187
3187
3188 def currentlock(self):
3188 def currentlock(self):
3189 """Returns the lock if it's held, or None if it's not."""
3189 """Returns the lock if it's held, or None if it's not."""
3190 return self._currentlock(self._lockref)
3190 return self._currentlock(self._lockref)
3191
3191
3192 def checkcommitpatterns(self, wctx, match, status, fail):
3192 def checkcommitpatterns(self, wctx, match, status, fail):
3193 """check for commit arguments that aren't committable"""
3193 """check for commit arguments that aren't committable"""
3194 if match.isexact() or match.prefix():
3194 if match.isexact() or match.prefix():
3195 matched = set(status.modified + status.added + status.removed)
3195 matched = set(status.modified + status.added + status.removed)
3196
3196
3197 for f in match.files():
3197 for f in match.files():
3198 f = self.dirstate.normalize(f)
3198 f = self.dirstate.normalize(f)
3199 if f == b'.' or f in matched or f in wctx.substate:
3199 if f == b'.' or f in matched or f in wctx.substate:
3200 continue
3200 continue
3201 if f in status.deleted:
3201 if f in status.deleted:
3202 fail(f, _(b'file not found!'))
3202 fail(f, _(b'file not found!'))
3203 # Is it a directory that exists or used to exist?
3203 # Is it a directory that exists or used to exist?
3204 if self.wvfs.isdir(f) or wctx.p1().hasdir(f):
3204 if self.wvfs.isdir(f) or wctx.p1().hasdir(f):
3205 d = f + b'/'
3205 d = f + b'/'
3206 for mf in matched:
3206 for mf in matched:
3207 if mf.startswith(d):
3207 if mf.startswith(d):
3208 break
3208 break
3209 else:
3209 else:
3210 fail(f, _(b"no match under directory!"))
3210 fail(f, _(b"no match under directory!"))
3211 elif f not in self.dirstate:
3211 elif f not in self.dirstate:
3212 fail(f, _(b"file not tracked!"))
3212 fail(f, _(b"file not tracked!"))
3213
3213
3214 @unfilteredmethod
3214 @unfilteredmethod
3215 def commit(
3215 def commit(
3216 self,
3216 self,
3217 text=b"",
3217 text=b"",
3218 user=None,
3218 user=None,
3219 date=None,
3219 date=None,
3220 match=None,
3220 match=None,
3221 force=False,
3221 force=False,
3222 editor=None,
3222 editor=None,
3223 extra=None,
3223 extra=None,
3224 ):
3224 ):
3225 """Add a new revision to current repository.
3225 """Add a new revision to current repository.
3226
3226
3227 Revision information is gathered from the working directory,
3227 Revision information is gathered from the working directory,
3228 match can be used to filter the committed files. If editor is
3228 match can be used to filter the committed files. If editor is
3229 supplied, it is called to get a commit message.
3229 supplied, it is called to get a commit message.
3230 """
3230 """
3231 if extra is None:
3231 if extra is None:
3232 extra = {}
3232 extra = {}
3233
3233
3234 def fail(f, msg):
3234 def fail(f, msg):
3235 raise error.InputError(b'%s: %s' % (f, msg))
3235 raise error.InputError(b'%s: %s' % (f, msg))
3236
3236
3237 if not match:
3237 if not match:
3238 match = matchmod.always()
3238 match = matchmod.always()
3239
3239
3240 if not force:
3240 if not force:
3241 match.bad = fail
3241 match.bad = fail
3242
3242
3243 # lock() for recent changelog (see issue4368)
3243 # lock() for recent changelog (see issue4368)
3244 with self.wlock(), self.lock():
3244 with self.wlock(), self.lock():
3245 wctx = self[None]
3245 wctx = self[None]
3246 merge = len(wctx.parents()) > 1
3246 merge = len(wctx.parents()) > 1
3247
3247
3248 if not force and merge and not match.always():
3248 if not force and merge and not match.always():
3249 raise error.Abort(
3249 raise error.Abort(
3250 _(
3250 _(
3251 b'cannot partially commit a merge '
3251 b'cannot partially commit a merge '
3252 b'(do not specify files or patterns)'
3252 b'(do not specify files or patterns)'
3253 )
3253 )
3254 )
3254 )
3255
3255
3256 status = self.status(match=match, clean=force)
3256 status = self.status(match=match, clean=force)
3257 if force:
3257 if force:
3258 status.modified.extend(
3258 status.modified.extend(
3259 status.clean
3259 status.clean
3260 ) # mq may commit clean files
3260 ) # mq may commit clean files
3261
3261
3262 # check subrepos
3262 # check subrepos
3263 subs, commitsubs, newstate = subrepoutil.precommit(
3263 subs, commitsubs, newstate = subrepoutil.precommit(
3264 self.ui, wctx, status, match, force=force
3264 self.ui, wctx, status, match, force=force
3265 )
3265 )
3266
3266
3267 # make sure all explicit patterns are matched
3267 # make sure all explicit patterns are matched
3268 if not force:
3268 if not force:
3269 self.checkcommitpatterns(wctx, match, status, fail)
3269 self.checkcommitpatterns(wctx, match, status, fail)
3270
3270
3271 cctx = context.workingcommitctx(
3271 cctx = context.workingcommitctx(
3272 self, status, text, user, date, extra
3272 self, status, text, user, date, extra
3273 )
3273 )
3274
3274
3275 ms = mergestatemod.mergestate.read(self)
3275 ms = mergestatemod.mergestate.read(self)
3276 mergeutil.checkunresolved(ms)
3276 mergeutil.checkunresolved(ms)
3277
3277
3278 # internal config: ui.allowemptycommit
3278 # internal config: ui.allowemptycommit
3279 if cctx.isempty() and not self.ui.configbool(
3279 if cctx.isempty() and not self.ui.configbool(
3280 b'ui', b'allowemptycommit'
3280 b'ui', b'allowemptycommit'
3281 ):
3281 ):
3282 self.ui.debug(b'nothing to commit, clearing merge state\n')
3282 self.ui.debug(b'nothing to commit, clearing merge state\n')
3283 ms.reset()
3283 ms.reset()
3284 return None
3284 return None
3285
3285
3286 if merge and cctx.deleted():
3286 if merge and cctx.deleted():
3287 raise error.Abort(_(b"cannot commit merge with missing files"))
3287 raise error.Abort(_(b"cannot commit merge with missing files"))
3288
3288
3289 if editor:
3289 if editor:
3290 cctx._text = editor(self, cctx, subs)
3290 cctx._text = editor(self, cctx, subs)
3291 edited = text != cctx._text
3291 edited = text != cctx._text
3292
3292
3293 # Save commit message in case this transaction gets rolled back
3293 # Save commit message in case this transaction gets rolled back
3294 # (e.g. by a pretxncommit hook). Leave the content alone on
3294 # (e.g. by a pretxncommit hook). Leave the content alone on
3295 # the assumption that the user will use the same editor again.
3295 # the assumption that the user will use the same editor again.
3296 msg_path = self.savecommitmessage(cctx._text)
3296 msg_path = self.savecommitmessage(cctx._text)
3297
3297
3298 # commit subs and write new state
3298 # commit subs and write new state
3299 if subs:
3299 if subs:
3300 uipathfn = scmutil.getuipathfn(self)
3300 uipathfn = scmutil.getuipathfn(self)
3301 for s in sorted(commitsubs):
3301 for s in sorted(commitsubs):
3302 sub = wctx.sub(s)
3302 sub = wctx.sub(s)
3303 self.ui.status(
3303 self.ui.status(
3304 _(b'committing subrepository %s\n')
3304 _(b'committing subrepository %s\n')
3305 % uipathfn(subrepoutil.subrelpath(sub))
3305 % uipathfn(subrepoutil.subrelpath(sub))
3306 )
3306 )
3307 sr = sub.commit(cctx._text, user, date)
3307 sr = sub.commit(cctx._text, user, date)
3308 newstate[s] = (newstate[s][0], sr)
3308 newstate[s] = (newstate[s][0], sr)
3309 subrepoutil.writestate(self, newstate)
3309 subrepoutil.writestate(self, newstate)
3310
3310
3311 p1, p2 = self.dirstate.parents()
3311 p1, p2 = self.dirstate.parents()
3312 hookp1, hookp2 = hex(p1), (p2 != self.nullid and hex(p2) or b'')
3312 hookp1, hookp2 = hex(p1), (p2 != self.nullid and hex(p2) or b'')
3313 try:
3313 try:
3314 self.hook(
3314 self.hook(
3315 b"precommit", throw=True, parent1=hookp1, parent2=hookp2
3315 b"precommit", throw=True, parent1=hookp1, parent2=hookp2
3316 )
3316 )
3317 with self.transaction(b'commit'):
3317 with self.transaction(b'commit'):
3318 ret = self.commitctx(cctx, True)
3318 ret = self.commitctx(cctx, True)
3319 # update bookmarks, dirstate and mergestate
3319 # update bookmarks, dirstate and mergestate
3320 bookmarks.update(self, [p1, p2], ret)
3320 bookmarks.update(self, [p1, p2], ret)
3321 cctx.markcommitted(ret)
3321 cctx.markcommitted(ret)
3322 ms.reset()
3322 ms.reset()
3323 except: # re-raises
3323 except: # re-raises
3324 if edited:
3324 if edited:
3325 self.ui.write(
3325 self.ui.write(
3326 _(b'note: commit message saved in %s\n') % msg_path
3326 _(b'note: commit message saved in %s\n') % msg_path
3327 )
3327 )
3328 self.ui.write(
3328 self.ui.write(
3329 _(
3329 _(
3330 b"note: use 'hg commit --logfile "
3330 b"note: use 'hg commit --logfile "
3331 b"%s --edit' to reuse it\n"
3331 b"%s --edit' to reuse it\n"
3332 )
3332 )
3333 % msg_path
3333 % msg_path
3334 )
3334 )
3335 raise
3335 raise
3336
3336
3337 def commithook(unused_success):
3337 def commithook(unused_success):
3338 # hack for command that use a temporary commit (eg: histedit)
3338 # hack for command that use a temporary commit (eg: histedit)
3339 # temporary commit got stripped before hook release
3339 # temporary commit got stripped before hook release
3340 if self.changelog.hasnode(ret):
3340 if self.changelog.hasnode(ret):
3341 self.hook(
3341 self.hook(
3342 b"commit", node=hex(ret), parent1=hookp1, parent2=hookp2
3342 b"commit", node=hex(ret), parent1=hookp1, parent2=hookp2
3343 )
3343 )
3344
3344
3345 self._afterlock(commithook)
3345 self._afterlock(commithook)
3346 return ret
3346 return ret
3347
3347
3348 @unfilteredmethod
3348 @unfilteredmethod
3349 def commitctx(self, ctx, error=False, origctx=None):
3349 def commitctx(self, ctx, error=False, origctx=None):
3350 return commit.commitctx(self, ctx, error=error, origctx=origctx)
3350 return commit.commitctx(self, ctx, error=error, origctx=origctx)
3351
3351
3352 @unfilteredmethod
3352 @unfilteredmethod
3353 def destroying(self):
3353 def destroying(self):
3354 """Inform the repository that nodes are about to be destroyed.
3354 """Inform the repository that nodes are about to be destroyed.
3355 Intended for use by strip and rollback, so there's a common
3355 Intended for use by strip and rollback, so there's a common
3356 place for anything that has to be done before destroying history.
3356 place for anything that has to be done before destroying history.
3357
3357
3358 This is mostly useful for saving state that is in memory and waiting
3358 This is mostly useful for saving state that is in memory and waiting
3359 to be flushed when the current lock is released. Because a call to
3359 to be flushed when the current lock is released. Because a call to
3360 destroyed is imminent, the repo will be invalidated causing those
3360 destroyed is imminent, the repo will be invalidated causing those
3361 changes to stay in memory (waiting for the next unlock), or vanish
3361 changes to stay in memory (waiting for the next unlock), or vanish
3362 completely.
3362 completely.
3363 """
3363 """
3364 # When using the same lock to commit and strip, the phasecache is left
3364 # When using the same lock to commit and strip, the phasecache is left
3365 # dirty after committing. Then when we strip, the repo is invalidated,
3365 # dirty after committing. Then when we strip, the repo is invalidated,
3366 # causing those changes to disappear.
3366 # causing those changes to disappear.
3367 if '_phasecache' in vars(self):
3367 if '_phasecache' in vars(self):
3368 self._phasecache.write()
3368 self._phasecache.write()
3369
3369
3370 @unfilteredmethod
3370 @unfilteredmethod
3371 def destroyed(self):
3371 def destroyed(self):
3372 """Inform the repository that nodes have been destroyed.
3372 """Inform the repository that nodes have been destroyed.
3373 Intended for use by strip and rollback, so there's a common
3373 Intended for use by strip and rollback, so there's a common
3374 place for anything that has to be done after destroying history.
3374 place for anything that has to be done after destroying history.
3375 """
3375 """
3376 # When one tries to:
3376 # When one tries to:
3377 # 1) destroy nodes thus calling this method (e.g. strip)
3377 # 1) destroy nodes thus calling this method (e.g. strip)
3378 # 2) use phasecache somewhere (e.g. commit)
3378 # 2) use phasecache somewhere (e.g. commit)
3379 #
3379 #
3380 # then 2) will fail because the phasecache contains nodes that were
3380 # then 2) will fail because the phasecache contains nodes that were
3381 # removed. We can either remove phasecache from the filecache,
3381 # removed. We can either remove phasecache from the filecache,
3382 # causing it to reload next time it is accessed, or simply filter
3382 # causing it to reload next time it is accessed, or simply filter
3383 # the removed nodes now and write the updated cache.
3383 # the removed nodes now and write the updated cache.
3384 self._phasecache.filterunknown(self)
3384 self._phasecache.filterunknown(self)
3385 self._phasecache.write()
3385 self._phasecache.write()
3386
3386
3387 # refresh all repository caches
3387 # refresh all repository caches
3388 self.updatecaches()
3388 self.updatecaches()
3389
3389
3390 # Ensure the persistent tag cache is updated. Doing it now
3390 # Ensure the persistent tag cache is updated. Doing it now
3391 # means that the tag cache only has to worry about destroyed
3391 # means that the tag cache only has to worry about destroyed
3392 # heads immediately after a strip/rollback. That in turn
3392 # heads immediately after a strip/rollback. That in turn
3393 # guarantees that "cachetip == currenttip" (comparing both rev
3393 # guarantees that "cachetip == currenttip" (comparing both rev
3394 # and node) always means no nodes have been added or destroyed.
3394 # and node) always means no nodes have been added or destroyed.
3395
3395
3396 # XXX this is suboptimal when qrefresh'ing: we strip the current
3396 # XXX this is suboptimal when qrefresh'ing: we strip the current
3397 # head, refresh the tag cache, then immediately add a new head.
3397 # head, refresh the tag cache, then immediately add a new head.
3398 # But I think doing it this way is necessary for the "instant
3398 # But I think doing it this way is necessary for the "instant
3399 # tag cache retrieval" case to work.
3399 # tag cache retrieval" case to work.
3400 self.invalidate()
3400 self.invalidate()
3401
3401
3402 def status(
3402 def status(
3403 self,
3403 self,
3404 node1=b'.',
3404 node1=b'.',
3405 node2=None,
3405 node2=None,
3406 match=None,
3406 match=None,
3407 ignored=False,
3407 ignored=False,
3408 clean=False,
3408 clean=False,
3409 unknown=False,
3409 unknown=False,
3410 listsubrepos=False,
3410 listsubrepos=False,
3411 ):
3411 ):
3412 '''a convenience method that calls node1.status(node2)'''
3412 '''a convenience method that calls node1.status(node2)'''
3413 return self[node1].status(
3413 return self[node1].status(
3414 node2, match, ignored, clean, unknown, listsubrepos
3414 node2, match, ignored, clean, unknown, listsubrepos
3415 )
3415 )
3416
3416
3417 def addpostdsstatus(self, ps):
3417 def addpostdsstatus(self, ps):
3418 """Add a callback to run within the wlock, at the point at which status
3418 """Add a callback to run within the wlock, at the point at which status
3419 fixups happen.
3419 fixups happen.
3420
3420
3421 On status completion, callback(wctx, status) will be called with the
3421 On status completion, callback(wctx, status) will be called with the
3422 wlock held, unless the dirstate has changed from underneath or the wlock
3422 wlock held, unless the dirstate has changed from underneath or the wlock
3423 couldn't be grabbed.
3423 couldn't be grabbed.
3424
3424
3425 Callbacks should not capture and use a cached copy of the dirstate --
3425 Callbacks should not capture and use a cached copy of the dirstate --
3426 it might change in the meanwhile. Instead, they should access the
3426 it might change in the meanwhile. Instead, they should access the
3427 dirstate via wctx.repo().dirstate.
3427 dirstate via wctx.repo().dirstate.
3428
3428
3429 This list is emptied out after each status run -- extensions should
3429 This list is emptied out after each status run -- extensions should
3430 make sure it adds to this list each time dirstate.status is called.
3430 make sure it adds to this list each time dirstate.status is called.
3431 Extensions should also make sure they don't call this for statuses
3431 Extensions should also make sure they don't call this for statuses
3432 that don't involve the dirstate.
3432 that don't involve the dirstate.
3433 """
3433 """
3434
3434
3435 # The list is located here for uniqueness reasons -- it is actually
3435 # The list is located here for uniqueness reasons -- it is actually
3436 # managed by the workingctx, but that isn't unique per-repo.
3436 # managed by the workingctx, but that isn't unique per-repo.
3437 self._postdsstatus.append(ps)
3437 self._postdsstatus.append(ps)
3438
3438
3439 def postdsstatus(self):
3439 def postdsstatus(self):
3440 """Used by workingctx to get the list of post-dirstate-status hooks."""
3440 """Used by workingctx to get the list of post-dirstate-status hooks."""
3441 return self._postdsstatus
3441 return self._postdsstatus
3442
3442
3443 def clearpostdsstatus(self):
3443 def clearpostdsstatus(self):
3444 """Used by workingctx to clear post-dirstate-status hooks."""
3444 """Used by workingctx to clear post-dirstate-status hooks."""
3445 del self._postdsstatus[:]
3445 del self._postdsstatus[:]
3446
3446
3447 def heads(self, start=None):
3447 def heads(self, start=None):
3448 if start is None:
3448 if start is None:
3449 cl = self.changelog
3449 cl = self.changelog
3450 headrevs = reversed(cl.headrevs())
3450 headrevs = reversed(cl.headrevs())
3451 return [cl.node(rev) for rev in headrevs]
3451 return [cl.node(rev) for rev in headrevs]
3452
3452
3453 heads = self.changelog.heads(start)
3453 heads = self.changelog.heads(start)
3454 # sort the output in rev descending order
3454 # sort the output in rev descending order
3455 return sorted(heads, key=self.changelog.rev, reverse=True)
3455 return sorted(heads, key=self.changelog.rev, reverse=True)
3456
3456
3457 def branchheads(self, branch=None, start=None, closed=False):
3457 def branchheads(self, branch=None, start=None, closed=False):
3458 """return a (possibly filtered) list of heads for the given branch
3458 """return a (possibly filtered) list of heads for the given branch
3459
3459
3460 Heads are returned in topological order, from newest to oldest.
3460 Heads are returned in topological order, from newest to oldest.
3461 If branch is None, use the dirstate branch.
3461 If branch is None, use the dirstate branch.
3462 If start is not None, return only heads reachable from start.
3462 If start is not None, return only heads reachable from start.
3463 If closed is True, return heads that are marked as closed as well.
3463 If closed is True, return heads that are marked as closed as well.
3464 """
3464 """
3465 if branch is None:
3465 if branch is None:
3466 branch = self[None].branch()
3466 branch = self[None].branch()
3467 branches = self.branchmap()
3467 branches = self.branchmap()
3468 if not branches.hasbranch(branch):
3468 if not branches.hasbranch(branch):
3469 return []
3469 return []
3470 # the cache returns heads ordered lowest to highest
3470 # the cache returns heads ordered lowest to highest
3471 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
3471 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
3472 if start is not None:
3472 if start is not None:
3473 # filter out the heads that cannot be reached from startrev
3473 # filter out the heads that cannot be reached from startrev
3474 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
3474 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
3475 bheads = [h for h in bheads if h in fbheads]
3475 bheads = [h for h in bheads if h in fbheads]
3476 return bheads
3476 return bheads
3477
3477
3478 def branches(self, nodes):
3478 def branches(self, nodes):
3479 if not nodes:
3479 if not nodes:
3480 nodes = [self.changelog.tip()]
3480 nodes = [self.changelog.tip()]
3481 b = []
3481 b = []
3482 for n in nodes:
3482 for n in nodes:
3483 t = n
3483 t = n
3484 while True:
3484 while True:
3485 p = self.changelog.parents(n)
3485 p = self.changelog.parents(n)
3486 if p[1] != self.nullid or p[0] == self.nullid:
3486 if p[1] != self.nullid or p[0] == self.nullid:
3487 b.append((t, n, p[0], p[1]))
3487 b.append((t, n, p[0], p[1]))
3488 break
3488 break
3489 n = p[0]
3489 n = p[0]
3490 return b
3490 return b
3491
3491
3492 def between(self, pairs):
3492 def between(self, pairs):
3493 r = []
3493 r = []
3494
3494
3495 for top, bottom in pairs:
3495 for top, bottom in pairs:
3496 n, l, i = top, [], 0
3496 n, l, i = top, [], 0
3497 f = 1
3497 f = 1
3498
3498
3499 while n != bottom and n != self.nullid:
3499 while n != bottom and n != self.nullid:
3500 p = self.changelog.parents(n)[0]
3500 p = self.changelog.parents(n)[0]
3501 if i == f:
3501 if i == f:
3502 l.append(n)
3502 l.append(n)
3503 f = f * 2
3503 f = f * 2
3504 n = p
3504 n = p
3505 i += 1
3505 i += 1
3506
3506
3507 r.append(l)
3507 r.append(l)
3508
3508
3509 return r
3509 return r
3510
3510
3511 def checkpush(self, pushop):
3511 def checkpush(self, pushop):
3512 """Extensions can override this function if additional checks have
3512 """Extensions can override this function if additional checks have
3513 to be performed before pushing, or call it if they override push
3513 to be performed before pushing, or call it if they override push
3514 command.
3514 command.
3515 """
3515 """
3516
3516
3517 @unfilteredpropertycache
3517 @unfilteredpropertycache
3518 def prepushoutgoinghooks(self):
3518 def prepushoutgoinghooks(self):
3519 """Return util.hooks consists of a pushop with repo, remote, outgoing
3519 """Return util.hooks consists of a pushop with repo, remote, outgoing
3520 methods, which are called before pushing changesets.
3520 methods, which are called before pushing changesets.
3521 """
3521 """
3522 return util.hooks()
3522 return util.hooks()
3523
3523
3524 def pushkey(self, namespace, key, old, new):
3524 def pushkey(self, namespace, key, old, new):
3525 try:
3525 try:
3526 tr = self.currenttransaction()
3526 tr = self.currenttransaction()
3527 hookargs = {}
3527 hookargs = {}
3528 if tr is not None:
3528 if tr is not None:
3529 hookargs.update(tr.hookargs)
3529 hookargs.update(tr.hookargs)
3530 hookargs = pycompat.strkwargs(hookargs)
3530 hookargs = pycompat.strkwargs(hookargs)
3531 hookargs['namespace'] = namespace
3531 hookargs['namespace'] = namespace
3532 hookargs['key'] = key
3532 hookargs['key'] = key
3533 hookargs['old'] = old
3533 hookargs['old'] = old
3534 hookargs['new'] = new
3534 hookargs['new'] = new
3535 self.hook(b'prepushkey', throw=True, **hookargs)
3535 self.hook(b'prepushkey', throw=True, **hookargs)
3536 except error.HookAbort as exc:
3536 except error.HookAbort as exc:
3537 self.ui.write_err(_(b"pushkey-abort: %s\n") % exc)
3537 self.ui.write_err(_(b"pushkey-abort: %s\n") % exc)
3538 if exc.hint:
3538 if exc.hint:
3539 self.ui.write_err(_(b"(%s)\n") % exc.hint)
3539 self.ui.write_err(_(b"(%s)\n") % exc.hint)
3540 return False
3540 return False
3541 self.ui.debug(b'pushing key for "%s:%s"\n' % (namespace, key))
3541 self.ui.debug(b'pushing key for "%s:%s"\n' % (namespace, key))
3542 ret = pushkey.push(self, namespace, key, old, new)
3542 ret = pushkey.push(self, namespace, key, old, new)
3543
3543
3544 def runhook(unused_success):
3544 def runhook(unused_success):
3545 self.hook(
3545 self.hook(
3546 b'pushkey',
3546 b'pushkey',
3547 namespace=namespace,
3547 namespace=namespace,
3548 key=key,
3548 key=key,
3549 old=old,
3549 old=old,
3550 new=new,
3550 new=new,
3551 ret=ret,
3551 ret=ret,
3552 )
3552 )
3553
3553
3554 self._afterlock(runhook)
3554 self._afterlock(runhook)
3555 return ret
3555 return ret
3556
3556
3557 def listkeys(self, namespace):
3557 def listkeys(self, namespace):
3558 self.hook(b'prelistkeys', throw=True, namespace=namespace)
3558 self.hook(b'prelistkeys', throw=True, namespace=namespace)
3559 self.ui.debug(b'listing keys for "%s"\n' % namespace)
3559 self.ui.debug(b'listing keys for "%s"\n' % namespace)
3560 values = pushkey.list(self, namespace)
3560 values = pushkey.list(self, namespace)
3561 self.hook(b'listkeys', namespace=namespace, values=values)
3561 self.hook(b'listkeys', namespace=namespace, values=values)
3562 return values
3562 return values
3563
3563
3564 def debugwireargs(self, one, two, three=None, four=None, five=None):
3564 def debugwireargs(self, one, two, three=None, four=None, five=None):
3565 '''used to test argument passing over the wire'''
3565 '''used to test argument passing over the wire'''
3566 return b"%s %s %s %s %s" % (
3566 return b"%s %s %s %s %s" % (
3567 one,
3567 one,
3568 two,
3568 two,
3569 pycompat.bytestr(three),
3569 pycompat.bytestr(three),
3570 pycompat.bytestr(four),
3570 pycompat.bytestr(four),
3571 pycompat.bytestr(five),
3571 pycompat.bytestr(five),
3572 )
3572 )
3573
3573
3574 def savecommitmessage(self, text):
3574 def savecommitmessage(self, text):
3575 fp = self.vfs(b'last-message.txt', b'wb')
3575 fp = self.vfs(b'last-message.txt', b'wb')
3576 try:
3576 try:
3577 fp.write(text)
3577 fp.write(text)
3578 finally:
3578 finally:
3579 fp.close()
3579 fp.close()
3580 return self.pathto(fp.name[len(self.root) + 1 :])
3580 return self.pathto(fp.name[len(self.root) + 1 :])
3581
3581
3582 def register_wanted_sidedata(self, category):
3582 def register_wanted_sidedata(self, category):
3583 if repository.REPO_FEATURE_SIDE_DATA not in self.features:
3583 if repository.REPO_FEATURE_SIDE_DATA not in self.features:
3584 # Only revlogv2 repos can want sidedata.
3584 # Only revlogv2 repos can want sidedata.
3585 return
3585 return
3586 self._wanted_sidedata.add(pycompat.bytestr(category))
3586 self._wanted_sidedata.add(pycompat.bytestr(category))
3587
3587
3588 def register_sidedata_computer(
3588 def register_sidedata_computer(
3589 self, kind, category, keys, computer, flags, replace=False
3589 self, kind, category, keys, computer, flags, replace=False
3590 ):
3590 ):
3591 if kind not in revlogconst.ALL_KINDS:
3591 if kind not in revlogconst.ALL_KINDS:
3592 msg = _(b"unexpected revlog kind '%s'.")
3592 msg = _(b"unexpected revlog kind '%s'.")
3593 raise error.ProgrammingError(msg % kind)
3593 raise error.ProgrammingError(msg % kind)
3594 category = pycompat.bytestr(category)
3594 category = pycompat.bytestr(category)
3595 already_registered = category in self._sidedata_computers.get(kind, [])
3595 already_registered = category in self._sidedata_computers.get(kind, [])
3596 if already_registered and not replace:
3596 if already_registered and not replace:
3597 msg = _(
3597 msg = _(
3598 b"cannot register a sidedata computer twice for category '%s'."
3598 b"cannot register a sidedata computer twice for category '%s'."
3599 )
3599 )
3600 raise error.ProgrammingError(msg % category)
3600 raise error.ProgrammingError(msg % category)
3601 if replace and not already_registered:
3601 if replace and not already_registered:
3602 msg = _(
3602 msg = _(
3603 b"cannot replace a sidedata computer that isn't registered "
3603 b"cannot replace a sidedata computer that isn't registered "
3604 b"for category '%s'."
3604 b"for category '%s'."
3605 )
3605 )
3606 raise error.ProgrammingError(msg % category)
3606 raise error.ProgrammingError(msg % category)
3607 self._sidedata_computers.setdefault(kind, {})
3607 self._sidedata_computers.setdefault(kind, {})
3608 self._sidedata_computers[kind][category] = (keys, computer, flags)
3608 self._sidedata_computers[kind][category] = (keys, computer, flags)
3609
3609
3610
3610
3611 def undoname(fn: bytes) -> bytes:
3611 def undoname(fn: bytes) -> bytes:
3612 base, name = os.path.split(fn)
3612 base, name = os.path.split(fn)
3613 assert name.startswith(b'journal')
3613 assert name.startswith(b'journal')
3614 return os.path.join(base, name.replace(b'journal', b'undo', 1))
3614 return os.path.join(base, name.replace(b'journal', b'undo', 1))
3615
3615
3616
3616
3617 def instance(ui, path: bytes, create, intents=None, createopts=None):
3617 def instance(ui, path: bytes, create, intents=None, createopts=None):
3618 # prevent cyclic import localrepo -> upgrade -> localrepo
3618 # prevent cyclic import localrepo -> upgrade -> localrepo
3619 from . import upgrade
3619 from . import upgrade
3620
3620
3621 localpath = urlutil.urllocalpath(path)
3621 localpath = urlutil.urllocalpath(path)
3622 if create:
3622 if create:
3623 createrepository(ui, localpath, createopts=createopts)
3623 createrepository(ui, localpath, createopts=createopts)
3624
3624
3625 def repo_maker():
3625 def repo_maker():
3626 return makelocalrepository(ui, localpath, intents=intents)
3626 return makelocalrepository(ui, localpath, intents=intents)
3627
3627
3628 repo = repo_maker()
3628 repo = repo_maker()
3629 repo = upgrade.may_auto_upgrade(repo, repo_maker)
3629 repo = upgrade.may_auto_upgrade(repo, repo_maker)
3630 return repo
3630 return repo
3631
3631
3632
3632
3633 def islocal(path: bytes) -> bool:
3633 def islocal(path: bytes) -> bool:
3634 return True
3634 return True
3635
3635
3636
3636
3637 def defaultcreateopts(ui, createopts=None):
3637 def defaultcreateopts(ui, createopts=None):
3638 """Populate the default creation options for a repository.
3638 """Populate the default creation options for a repository.
3639
3639
3640 A dictionary of explicitly requested creation options can be passed
3640 A dictionary of explicitly requested creation options can be passed
3641 in. Missing keys will be populated.
3641 in. Missing keys will be populated.
3642 """
3642 """
3643 createopts = dict(createopts or {})
3643 createopts = dict(createopts or {})
3644
3644
3645 if b'backend' not in createopts:
3645 if b'backend' not in createopts:
3646 # experimental config: storage.new-repo-backend
3646 # experimental config: storage.new-repo-backend
3647 createopts[b'backend'] = ui.config(b'storage', b'new-repo-backend')
3647 createopts[b'backend'] = ui.config(b'storage', b'new-repo-backend')
3648
3648
3649 return createopts
3649 return createopts
3650
3650
3651
3651
3652 def clone_requirements(ui, createopts, srcrepo):
3652 def clone_requirements(ui, createopts, srcrepo):
3653 """clone the requirements of a local repo for a local clone
3653 """clone the requirements of a local repo for a local clone
3654
3654
3655 The store requirements are unchanged while the working copy requirements
3655 The store requirements are unchanged while the working copy requirements
3656 depends on the configuration
3656 depends on the configuration
3657 """
3657 """
3658 target_requirements = set()
3658 target_requirements = set()
3659 if not srcrepo.requirements:
3659 if not srcrepo.requirements:
3660 # this is a legacy revlog "v0" repository, we cannot do anything fancy
3660 # this is a legacy revlog "v0" repository, we cannot do anything fancy
3661 # with it.
3661 # with it.
3662 return target_requirements
3662 return target_requirements
3663 createopts = defaultcreateopts(ui, createopts=createopts)
3663 createopts = defaultcreateopts(ui, createopts=createopts)
3664 for r in newreporequirements(ui, createopts):
3664 for r in newreporequirements(ui, createopts):
3665 if r in requirementsmod.WORKING_DIR_REQUIREMENTS:
3665 if r in requirementsmod.WORKING_DIR_REQUIREMENTS:
3666 target_requirements.add(r)
3666 target_requirements.add(r)
3667
3667
3668 for r in srcrepo.requirements:
3668 for r in srcrepo.requirements:
3669 if r not in requirementsmod.WORKING_DIR_REQUIREMENTS:
3669 if r not in requirementsmod.WORKING_DIR_REQUIREMENTS:
3670 target_requirements.add(r)
3670 target_requirements.add(r)
3671 return target_requirements
3671 return target_requirements
3672
3672
3673
3673
3674 def newreporequirements(ui, createopts):
3674 def newreporequirements(ui, createopts):
3675 """Determine the set of requirements for a new local repository.
3675 """Determine the set of requirements for a new local repository.
3676
3676
3677 Extensions can wrap this function to specify custom requirements for
3677 Extensions can wrap this function to specify custom requirements for
3678 new repositories.
3678 new repositories.
3679 """
3679 """
3680
3680
3681 if b'backend' not in createopts:
3681 if b'backend' not in createopts:
3682 raise error.ProgrammingError(
3682 raise error.ProgrammingError(
3683 b'backend key not present in createopts; '
3683 b'backend key not present in createopts; '
3684 b'was defaultcreateopts() called?'
3684 b'was defaultcreateopts() called?'
3685 )
3685 )
3686
3686
3687 if createopts[b'backend'] != b'revlogv1':
3687 if createopts[b'backend'] != b'revlogv1':
3688 raise error.Abort(
3688 raise error.Abort(
3689 _(
3689 _(
3690 b'unable to determine repository requirements for '
3690 b'unable to determine repository requirements for '
3691 b'storage backend: %s'
3691 b'storage backend: %s'
3692 )
3692 )
3693 % createopts[b'backend']
3693 % createopts[b'backend']
3694 )
3694 )
3695
3695
3696 requirements = {requirementsmod.REVLOGV1_REQUIREMENT}
3696 requirements = {requirementsmod.REVLOGV1_REQUIREMENT}
3697 if ui.configbool(b'format', b'usestore'):
3697 if ui.configbool(b'format', b'usestore'):
3698 requirements.add(requirementsmod.STORE_REQUIREMENT)
3698 requirements.add(requirementsmod.STORE_REQUIREMENT)
3699 if ui.configbool(b'format', b'usefncache'):
3699 if ui.configbool(b'format', b'usefncache'):
3700 requirements.add(requirementsmod.FNCACHE_REQUIREMENT)
3700 requirements.add(requirementsmod.FNCACHE_REQUIREMENT)
3701 if ui.configbool(b'format', b'dotencode'):
3701 if ui.configbool(b'format', b'dotencode'):
3702 requirements.add(requirementsmod.DOTENCODE_REQUIREMENT)
3702 requirements.add(requirementsmod.DOTENCODE_REQUIREMENT)
3703
3703
3704 compengines = ui.configlist(b'format', b'revlog-compression')
3704 compengines = ui.configlist(b'format', b'revlog-compression')
3705 for compengine in compengines:
3705 for compengine in compengines:
3706 if compengine in util.compengines:
3706 if compengine in util.compengines:
3707 engine = util.compengines[compengine]
3707 engine = util.compengines[compengine]
3708 if engine.available() and engine.revlogheader():
3708 if engine.available() and engine.revlogheader():
3709 break
3709 break
3710 else:
3710 else:
3711 raise error.Abort(
3711 raise error.Abort(
3712 _(
3712 _(
3713 b'compression engines %s defined by '
3713 b'compression engines %s defined by '
3714 b'format.revlog-compression not available'
3714 b'format.revlog-compression not available'
3715 )
3715 )
3716 % b', '.join(b'"%s"' % e for e in compengines),
3716 % b', '.join(b'"%s"' % e for e in compengines),
3717 hint=_(
3717 hint=_(
3718 b'run "hg debuginstall" to list available '
3718 b'run "hg debuginstall" to list available '
3719 b'compression engines'
3719 b'compression engines'
3720 ),
3720 ),
3721 )
3721 )
3722
3722
3723 # zlib is the historical default and doesn't need an explicit requirement.
3723 # zlib is the historical default and doesn't need an explicit requirement.
3724 if compengine == b'zstd':
3724 if compengine == b'zstd':
3725 requirements.add(b'revlog-compression-zstd')
3725 requirements.add(b'revlog-compression-zstd')
3726 elif compengine != b'zlib':
3726 elif compengine != b'zlib':
3727 requirements.add(b'exp-compression-%s' % compengine)
3727 requirements.add(b'exp-compression-%s' % compengine)
3728
3728
3729 if scmutil.gdinitconfig(ui):
3729 if scmutil.gdinitconfig(ui):
3730 requirements.add(requirementsmod.GENERALDELTA_REQUIREMENT)
3730 requirements.add(requirementsmod.GENERALDELTA_REQUIREMENT)
3731 if ui.configbool(b'format', b'sparse-revlog'):
3731 if ui.configbool(b'format', b'sparse-revlog'):
3732 requirements.add(requirementsmod.SPARSEREVLOG_REQUIREMENT)
3732 requirements.add(requirementsmod.SPARSEREVLOG_REQUIREMENT)
3733
3733
3734 # experimental config: format.use-dirstate-v2
3734 # experimental config: format.use-dirstate-v2
3735 # Keep this logic in sync with `has_dirstate_v2()` in `tests/hghave.py`
3735 # Keep this logic in sync with `has_dirstate_v2()` in `tests/hghave.py`
3736 if ui.configbool(b'format', b'use-dirstate-v2'):
3736 if ui.configbool(b'format', b'use-dirstate-v2'):
3737 requirements.add(requirementsmod.DIRSTATE_V2_REQUIREMENT)
3737 requirements.add(requirementsmod.DIRSTATE_V2_REQUIREMENT)
3738
3738
3739 # experimental config: format.exp-use-copies-side-data-changeset
3739 # experimental config: format.exp-use-copies-side-data-changeset
3740 if ui.configbool(b'format', b'exp-use-copies-side-data-changeset'):
3740 if ui.configbool(b'format', b'exp-use-copies-side-data-changeset'):
3741 requirements.add(requirementsmod.CHANGELOGV2_REQUIREMENT)
3741 requirements.add(requirementsmod.CHANGELOGV2_REQUIREMENT)
3742 requirements.add(requirementsmod.COPIESSDC_REQUIREMENT)
3742 requirements.add(requirementsmod.COPIESSDC_REQUIREMENT)
3743 if ui.configbool(b'experimental', b'treemanifest'):
3743 if ui.configbool(b'experimental', b'treemanifest'):
3744 requirements.add(requirementsmod.TREEMANIFEST_REQUIREMENT)
3744 requirements.add(requirementsmod.TREEMANIFEST_REQUIREMENT)
3745
3745
3746 changelogv2 = ui.config(b'format', b'exp-use-changelog-v2')
3746 changelogv2 = ui.config(b'format', b'exp-use-changelog-v2')
3747 if changelogv2 == b'enable-unstable-format-and-corrupt-my-data':
3747 if changelogv2 == b'enable-unstable-format-and-corrupt-my-data':
3748 requirements.add(requirementsmod.CHANGELOGV2_REQUIREMENT)
3748 requirements.add(requirementsmod.CHANGELOGV2_REQUIREMENT)
3749
3749
3750 revlogv2 = ui.config(b'experimental', b'revlogv2')
3750 revlogv2 = ui.config(b'experimental', b'revlogv2')
3751 if revlogv2 == b'enable-unstable-format-and-corrupt-my-data':
3751 if revlogv2 == b'enable-unstable-format-and-corrupt-my-data':
3752 requirements.discard(requirementsmod.REVLOGV1_REQUIREMENT)
3752 requirements.discard(requirementsmod.REVLOGV1_REQUIREMENT)
3753 requirements.add(requirementsmod.REVLOGV2_REQUIREMENT)
3753 requirements.add(requirementsmod.REVLOGV2_REQUIREMENT)
3754 # experimental config: format.internal-phase
3754 # experimental config: format.internal-phase
3755 if ui.configbool(b'format', b'use-internal-phase'):
3755 if ui.configbool(b'format', b'use-internal-phase'):
3756 requirements.add(requirementsmod.INTERNAL_PHASE_REQUIREMENT)
3756 requirements.add(requirementsmod.INTERNAL_PHASE_REQUIREMENT)
3757
3757
3758 # experimental config: format.exp-archived-phase
3758 # experimental config: format.exp-archived-phase
3759 if ui.configbool(b'format', b'exp-archived-phase'):
3759 if ui.configbool(b'format', b'exp-archived-phase'):
3760 requirements.add(requirementsmod.ARCHIVED_PHASE_REQUIREMENT)
3760 requirements.add(requirementsmod.ARCHIVED_PHASE_REQUIREMENT)
3761
3761
3762 if createopts.get(b'narrowfiles'):
3762 if createopts.get(b'narrowfiles'):
3763 requirements.add(requirementsmod.NARROW_REQUIREMENT)
3763 requirements.add(requirementsmod.NARROW_REQUIREMENT)
3764
3764
3765 if createopts.get(b'lfs'):
3765 if createopts.get(b'lfs'):
3766 requirements.add(b'lfs')
3766 requirements.add(b'lfs')
3767
3767
3768 if ui.configbool(b'format', b'bookmarks-in-store'):
3768 if ui.configbool(b'format', b'bookmarks-in-store'):
3769 requirements.add(requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT)
3769 requirements.add(requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT)
3770
3770
3771 # The feature is disabled unless a fast implementation is available.
3771 # The feature is disabled unless a fast implementation is available.
3772 persistent_nodemap_default = policy.importrust('revlog') is not None
3772 persistent_nodemap_default = policy.importrust('revlog') is not None
3773 if ui.configbool(
3773 if ui.configbool(
3774 b'format', b'use-persistent-nodemap', persistent_nodemap_default
3774 b'format', b'use-persistent-nodemap', persistent_nodemap_default
3775 ):
3775 ):
3776 requirements.add(requirementsmod.NODEMAP_REQUIREMENT)
3776 requirements.add(requirementsmod.NODEMAP_REQUIREMENT)
3777
3777
3778 # if share-safe is enabled, let's create the new repository with the new
3778 # if share-safe is enabled, let's create the new repository with the new
3779 # requirement
3779 # requirement
3780 if ui.configbool(b'format', b'use-share-safe'):
3780 if ui.configbool(b'format', b'use-share-safe'):
3781 requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
3781 requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
3782
3782
3783 # if we are creating a share-repoΒΉ we have to handle requirement
3783 # if we are creating a share-repoΒΉ we have to handle requirement
3784 # differently.
3784 # differently.
3785 #
3785 #
3786 # [1] (i.e. reusing the store from another repository, just having a
3786 # [1] (i.e. reusing the store from another repository, just having a
3787 # working copy)
3787 # working copy)
3788 if b'sharedrepo' in createopts:
3788 if b'sharedrepo' in createopts:
3789 source_requirements = set(createopts[b'sharedrepo'].requirements)
3789 source_requirements = set(createopts[b'sharedrepo'].requirements)
3790
3790
3791 if requirementsmod.SHARESAFE_REQUIREMENT not in source_requirements:
3791 if requirementsmod.SHARESAFE_REQUIREMENT not in source_requirements:
3792 # share to an old school repository, we have to copy the
3792 # share to an old school repository, we have to copy the
3793 # requirements and hope for the best.
3793 # requirements and hope for the best.
3794 requirements = source_requirements
3794 requirements = source_requirements
3795 else:
3795 else:
3796 # We have control on the working copy only, so "copy" the non
3796 # We have control on the working copy only, so "copy" the non
3797 # working copy part over, ignoring previous logic.
3797 # working copy part over, ignoring previous logic.
3798 to_drop = set()
3798 to_drop = set()
3799 for req in requirements:
3799 for req in requirements:
3800 if req in requirementsmod.WORKING_DIR_REQUIREMENTS:
3800 if req in requirementsmod.WORKING_DIR_REQUIREMENTS:
3801 continue
3801 continue
3802 if req in source_requirements:
3802 if req in source_requirements:
3803 continue
3803 continue
3804 to_drop.add(req)
3804 to_drop.add(req)
3805 requirements -= to_drop
3805 requirements -= to_drop
3806 requirements |= source_requirements
3806 requirements |= source_requirements
3807
3807
3808 if createopts.get(b'sharedrelative'):
3808 if createopts.get(b'sharedrelative'):
3809 requirements.add(requirementsmod.RELATIVE_SHARED_REQUIREMENT)
3809 requirements.add(requirementsmod.RELATIVE_SHARED_REQUIREMENT)
3810 else:
3810 else:
3811 requirements.add(requirementsmod.SHARED_REQUIREMENT)
3811 requirements.add(requirementsmod.SHARED_REQUIREMENT)
3812
3812
3813 if ui.configbool(b'format', b'use-dirstate-tracked-hint'):
3813 if ui.configbool(b'format', b'use-dirstate-tracked-hint'):
3814 version = ui.configint(b'format', b'use-dirstate-tracked-hint.version')
3814 version = ui.configint(b'format', b'use-dirstate-tracked-hint.version')
3815 msg = _(b"ignoring unknown tracked key version: %d\n")
3815 msg = _(b"ignoring unknown tracked key version: %d\n")
3816 hint = _(
3816 hint = _(
3817 b"see `hg help config.format.use-dirstate-tracked-hint-version"
3817 b"see `hg help config.format.use-dirstate-tracked-hint-version"
3818 )
3818 )
3819 if version != 1:
3819 if version != 1:
3820 ui.warn(msg % version, hint=hint)
3820 ui.warn(msg % version, hint=hint)
3821 else:
3821 else:
3822 requirements.add(requirementsmod.DIRSTATE_TRACKED_HINT_V1)
3822 requirements.add(requirementsmod.DIRSTATE_TRACKED_HINT_V1)
3823
3823
3824 return requirements
3824 return requirements
3825
3825
3826
3826
3827 def checkrequirementscompat(ui, requirements):
3827 def checkrequirementscompat(ui, requirements):
3828 """Checks compatibility of repository requirements enabled and disabled.
3828 """Checks compatibility of repository requirements enabled and disabled.
3829
3829
3830 Returns a set of requirements which needs to be dropped because dependend
3830 Returns a set of requirements which needs to be dropped because dependend
3831 requirements are not enabled. Also warns users about it"""
3831 requirements are not enabled. Also warns users about it"""
3832
3832
3833 dropped = set()
3833 dropped = set()
3834
3834
3835 if requirementsmod.STORE_REQUIREMENT not in requirements:
3835 if requirementsmod.STORE_REQUIREMENT not in requirements:
3836 if requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT in requirements:
3836 if requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT in requirements:
3837 ui.warn(
3837 ui.warn(
3838 _(
3838 _(
3839 b'ignoring enabled \'format.bookmarks-in-store\' config '
3839 b'ignoring enabled \'format.bookmarks-in-store\' config '
3840 b'beacuse it is incompatible with disabled '
3840 b'beacuse it is incompatible with disabled '
3841 b'\'format.usestore\' config\n'
3841 b'\'format.usestore\' config\n'
3842 )
3842 )
3843 )
3843 )
3844 dropped.add(requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT)
3844 dropped.add(requirementsmod.BOOKMARKS_IN_STORE_REQUIREMENT)
3845
3845
3846 if (
3846 if (
3847 requirementsmod.SHARED_REQUIREMENT in requirements
3847 requirementsmod.SHARED_REQUIREMENT in requirements
3848 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
3848 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
3849 ):
3849 ):
3850 raise error.Abort(
3850 raise error.Abort(
3851 _(
3851 _(
3852 b"cannot create shared repository as source was created"
3852 b"cannot create shared repository as source was created"
3853 b" with 'format.usestore' config disabled"
3853 b" with 'format.usestore' config disabled"
3854 )
3854 )
3855 )
3855 )
3856
3856
3857 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
3857 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
3858 if ui.hasconfig(b'format', b'use-share-safe'):
3858 if ui.hasconfig(b'format', b'use-share-safe'):
3859 msg = _(
3859 msg = _(
3860 b"ignoring enabled 'format.use-share-safe' config because "
3860 b"ignoring enabled 'format.use-share-safe' config because "
3861 b"it is incompatible with disabled 'format.usestore'"
3861 b"it is incompatible with disabled 'format.usestore'"
3862 b" config\n"
3862 b" config\n"
3863 )
3863 )
3864 ui.warn(msg)
3864 ui.warn(msg)
3865 dropped.add(requirementsmod.SHARESAFE_REQUIREMENT)
3865 dropped.add(requirementsmod.SHARESAFE_REQUIREMENT)
3866
3866
3867 return dropped
3867 return dropped
3868
3868
3869
3869
3870 def filterknowncreateopts(ui, createopts):
3870 def filterknowncreateopts(ui, createopts):
3871 """Filters a dict of repo creation options against options that are known.
3871 """Filters a dict of repo creation options against options that are known.
3872
3872
3873 Receives a dict of repo creation options and returns a dict of those
3873 Receives a dict of repo creation options and returns a dict of those
3874 options that we don't know how to handle.
3874 options that we don't know how to handle.
3875
3875
3876 This function is called as part of repository creation. If the
3876 This function is called as part of repository creation. If the
3877 returned dict contains any items, repository creation will not
3877 returned dict contains any items, repository creation will not
3878 be allowed, as it means there was a request to create a repository
3878 be allowed, as it means there was a request to create a repository
3879 with options not recognized by loaded code.
3879 with options not recognized by loaded code.
3880
3880
3881 Extensions can wrap this function to filter out creation options
3881 Extensions can wrap this function to filter out creation options
3882 they know how to handle.
3882 they know how to handle.
3883 """
3883 """
3884 known = {
3884 known = {
3885 b'backend',
3885 b'backend',
3886 b'lfs',
3886 b'lfs',
3887 b'narrowfiles',
3887 b'narrowfiles',
3888 b'sharedrepo',
3888 b'sharedrepo',
3889 b'sharedrelative',
3889 b'sharedrelative',
3890 b'shareditems',
3890 b'shareditems',
3891 b'shallowfilestore',
3891 b'shallowfilestore',
3892 }
3892 }
3893
3893
3894 return {k: v for k, v in createopts.items() if k not in known}
3894 return {k: v for k, v in createopts.items() if k not in known}
3895
3895
3896
3896
3897 def createrepository(ui, path: bytes, createopts=None, requirements=None):
3897 def createrepository(ui, path: bytes, createopts=None, requirements=None):
3898 """Create a new repository in a vfs.
3898 """Create a new repository in a vfs.
3899
3899
3900 ``path`` path to the new repo's working directory.
3900 ``path`` path to the new repo's working directory.
3901 ``createopts`` options for the new repository.
3901 ``createopts`` options for the new repository.
3902 ``requirement`` predefined set of requirements.
3902 ``requirement`` predefined set of requirements.
3903 (incompatible with ``createopts``)
3903 (incompatible with ``createopts``)
3904
3904
3905 The following keys for ``createopts`` are recognized:
3905 The following keys for ``createopts`` are recognized:
3906
3906
3907 backend
3907 backend
3908 The storage backend to use.
3908 The storage backend to use.
3909 lfs
3909 lfs
3910 Repository will be created with ``lfs`` requirement. The lfs extension
3910 Repository will be created with ``lfs`` requirement. The lfs extension
3911 will automatically be loaded when the repository is accessed.
3911 will automatically be loaded when the repository is accessed.
3912 narrowfiles
3912 narrowfiles
3913 Set up repository to support narrow file storage.
3913 Set up repository to support narrow file storage.
3914 sharedrepo
3914 sharedrepo
3915 Repository object from which storage should be shared.
3915 Repository object from which storage should be shared.
3916 sharedrelative
3916 sharedrelative
3917 Boolean indicating if the path to the shared repo should be
3917 Boolean indicating if the path to the shared repo should be
3918 stored as relative. By default, the pointer to the "parent" repo
3918 stored as relative. By default, the pointer to the "parent" repo
3919 is stored as an absolute path.
3919 is stored as an absolute path.
3920 shareditems
3920 shareditems
3921 Set of items to share to the new repository (in addition to storage).
3921 Set of items to share to the new repository (in addition to storage).
3922 shallowfilestore
3922 shallowfilestore
3923 Indicates that storage for files should be shallow (not all ancestor
3923 Indicates that storage for files should be shallow (not all ancestor
3924 revisions are known).
3924 revisions are known).
3925 """
3925 """
3926
3926
3927 if requirements is not None:
3927 if requirements is not None:
3928 if createopts is not None:
3928 if createopts is not None:
3929 msg = b'cannot specify both createopts and requirements'
3929 msg = b'cannot specify both createopts and requirements'
3930 raise error.ProgrammingError(msg)
3930 raise error.ProgrammingError(msg)
3931 createopts = {}
3931 createopts = {}
3932 else:
3932 else:
3933 createopts = defaultcreateopts(ui, createopts=createopts)
3933 createopts = defaultcreateopts(ui, createopts=createopts)
3934
3934
3935 unknownopts = filterknowncreateopts(ui, createopts)
3935 unknownopts = filterknowncreateopts(ui, createopts)
3936
3936
3937 if not isinstance(unknownopts, dict):
3937 if not isinstance(unknownopts, dict):
3938 raise error.ProgrammingError(
3938 raise error.ProgrammingError(
3939 b'filterknowncreateopts() did not return a dict'
3939 b'filterknowncreateopts() did not return a dict'
3940 )
3940 )
3941
3941
3942 if unknownopts:
3942 if unknownopts:
3943 raise error.Abort(
3943 raise error.Abort(
3944 _(
3944 _(
3945 b'unable to create repository because of unknown '
3945 b'unable to create repository because of unknown '
3946 b'creation option: %s'
3946 b'creation option: %s'
3947 )
3947 )
3948 % b', '.join(sorted(unknownopts)),
3948 % b', '.join(sorted(unknownopts)),
3949 hint=_(b'is a required extension not loaded?'),
3949 hint=_(b'is a required extension not loaded?'),
3950 )
3950 )
3951
3951
3952 requirements = newreporequirements(ui, createopts=createopts)
3952 requirements = newreporequirements(ui, createopts=createopts)
3953 requirements -= checkrequirementscompat(ui, requirements)
3953 requirements -= checkrequirementscompat(ui, requirements)
3954
3954
3955 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
3955 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
3956
3956
3957 hgvfs = vfsmod.vfs(wdirvfs.join(b'.hg'))
3957 hgvfs = vfsmod.vfs(wdirvfs.join(b'.hg'))
3958 if hgvfs.exists():
3958 if hgvfs.exists():
3959 raise error.RepoError(_(b'repository %s already exists') % path)
3959 raise error.RepoError(_(b'repository %s already exists') % path)
3960
3960
3961 if b'sharedrepo' in createopts:
3961 if b'sharedrepo' in createopts:
3962 sharedpath = createopts[b'sharedrepo'].sharedpath
3962 sharedpath = createopts[b'sharedrepo'].sharedpath
3963
3963
3964 if createopts.get(b'sharedrelative'):
3964 if createopts.get(b'sharedrelative'):
3965 try:
3965 try:
3966 sharedpath = os.path.relpath(sharedpath, hgvfs.base)
3966 sharedpath = os.path.relpath(sharedpath, hgvfs.base)
3967 sharedpath = util.pconvert(sharedpath)
3967 sharedpath = util.pconvert(sharedpath)
3968 except (IOError, ValueError) as e:
3968 except (IOError, ValueError) as e:
3969 # ValueError is raised on Windows if the drive letters differ
3969 # ValueError is raised on Windows if the drive letters differ
3970 # on each path.
3970 # on each path.
3971 raise error.Abort(
3971 raise error.Abort(
3972 _(b'cannot calculate relative path'),
3972 _(b'cannot calculate relative path'),
3973 hint=stringutil.forcebytestr(e),
3973 hint=stringutil.forcebytestr(e),
3974 )
3974 )
3975
3975
3976 if not wdirvfs.exists():
3976 if not wdirvfs.exists():
3977 wdirvfs.makedirs()
3977 wdirvfs.makedirs()
3978
3978
3979 hgvfs.makedir(notindexed=True)
3979 hgvfs.makedir(notindexed=True)
3980 if b'sharedrepo' not in createopts:
3980 if b'sharedrepo' not in createopts:
3981 hgvfs.mkdir(b'cache')
3981 hgvfs.mkdir(b'cache')
3982 hgvfs.mkdir(b'wcache')
3982 hgvfs.mkdir(b'wcache')
3983
3983
3984 has_store = requirementsmod.STORE_REQUIREMENT in requirements
3984 has_store = requirementsmod.STORE_REQUIREMENT in requirements
3985 if has_store and b'sharedrepo' not in createopts:
3985 if has_store and b'sharedrepo' not in createopts:
3986 hgvfs.mkdir(b'store')
3986 hgvfs.mkdir(b'store')
3987
3987
3988 # We create an invalid changelog outside the store so very old
3988 # We create an invalid changelog outside the store so very old
3989 # Mercurial versions (which didn't know about the requirements
3989 # Mercurial versions (which didn't know about the requirements
3990 # file) encounter an error on reading the changelog. This
3990 # file) encounter an error on reading the changelog. This
3991 # effectively locks out old clients and prevents them from
3991 # effectively locks out old clients and prevents them from
3992 # mucking with a repo in an unknown format.
3992 # mucking with a repo in an unknown format.
3993 #
3993 #
3994 # The revlog header has version 65535, which won't be recognized by
3994 # The revlog header has version 65535, which won't be recognized by
3995 # such old clients.
3995 # such old clients.
3996 hgvfs.append(
3996 hgvfs.append(
3997 b'00changelog.i',
3997 b'00changelog.i',
3998 b'\0\0\xFF\xFF dummy changelog to prevent using the old repo '
3998 b'\0\0\xFF\xFF dummy changelog to prevent using the old repo '
3999 b'layout',
3999 b'layout',
4000 )
4000 )
4001
4001
4002 # Filter the requirements into working copy and store ones
4002 # Filter the requirements into working copy and store ones
4003 wcreq, storereq = scmutil.filterrequirements(requirements)
4003 wcreq, storereq = scmutil.filterrequirements(requirements)
4004 # write working copy ones
4004 # write working copy ones
4005 scmutil.writerequires(hgvfs, wcreq)
4005 scmutil.writerequires(hgvfs, wcreq)
4006 # If there are store requirements and the current repository
4006 # If there are store requirements and the current repository
4007 # is not a shared one, write stored requirements
4007 # is not a shared one, write stored requirements
4008 # For new shared repository, we don't need to write the store
4008 # For new shared repository, we don't need to write the store
4009 # requirements as they are already present in store requires
4009 # requirements as they are already present in store requires
4010 if storereq and b'sharedrepo' not in createopts:
4010 if storereq and b'sharedrepo' not in createopts:
4011 storevfs = vfsmod.vfs(hgvfs.join(b'store'), cacheaudited=True)
4011 storevfs = vfsmod.vfs(hgvfs.join(b'store'), cacheaudited=True)
4012 scmutil.writerequires(storevfs, storereq)
4012 scmutil.writerequires(storevfs, storereq)
4013
4013
4014 # Write out file telling readers where to find the shared store.
4014 # Write out file telling readers where to find the shared store.
4015 if b'sharedrepo' in createopts:
4015 if b'sharedrepo' in createopts:
4016 hgvfs.write(b'sharedpath', sharedpath)
4016 hgvfs.write(b'sharedpath', sharedpath)
4017
4017
4018 if createopts.get(b'shareditems'):
4018 if createopts.get(b'shareditems'):
4019 shared = b'\n'.join(sorted(createopts[b'shareditems'])) + b'\n'
4019 shared = b'\n'.join(sorted(createopts[b'shareditems'])) + b'\n'
4020 hgvfs.write(b'shared', shared)
4020 hgvfs.write(b'shared', shared)
4021
4021
4022
4022
4023 def poisonrepository(repo):
4023 def poisonrepository(repo):
4024 """Poison a repository instance so it can no longer be used."""
4024 """Poison a repository instance so it can no longer be used."""
4025 # Perform any cleanup on the instance.
4025 # Perform any cleanup on the instance.
4026 repo.close()
4026 repo.close()
4027
4027
4028 # Our strategy is to replace the type of the object with one that
4028 # Our strategy is to replace the type of the object with one that
4029 # has all attribute lookups result in error.
4029 # has all attribute lookups result in error.
4030 #
4030 #
4031 # But we have to allow the close() method because some constructors
4031 # But we have to allow the close() method because some constructors
4032 # of repos call close() on repo references.
4032 # of repos call close() on repo references.
4033 class poisonedrepository:
4033 class poisonedrepository:
4034 def __getattribute__(self, item):
4034 def __getattribute__(self, item):
4035 if item == 'close':
4035 if item == 'close':
4036 return object.__getattribute__(self, item)
4036 return object.__getattribute__(self, item)
4037
4037
4038 raise error.ProgrammingError(
4038 raise error.ProgrammingError(
4039 b'repo instances should not be used after unshare'
4039 b'repo instances should not be used after unshare'
4040 )
4040 )
4041
4041
4042 def close(self):
4042 def close(self):
4043 pass
4043 pass
4044
4044
4045 # We may have a repoview, which intercepts __setattr__. So be sure
4045 # We may have a repoview, which intercepts __setattr__. So be sure
4046 # we operate at the lowest level possible.
4046 # we operate at the lowest level possible.
4047 object.__setattr__(repo, '__class__', poisonedrepository)
4047 object.__setattr__(repo, '__class__', poisonedrepository)
@@ -1,3721 +1,3719 b''
1 # revlog.py - storage back-end for mercurial
1 # revlog.py - storage back-end for mercurial
2 # coding: utf8
2 # coding: utf8
3 #
3 #
4 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
4 # Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9 """Storage back-end for Mercurial.
9 """Storage back-end for Mercurial.
10
10
11 This provides efficient delta storage with O(1) retrieve and append
11 This provides efficient delta storage with O(1) retrieve and append
12 and O(changes) merge between branches.
12 and O(changes) merge between branches.
13 """
13 """
14
14
15
15
16 import binascii
16 import binascii
17 import collections
17 import collections
18 import contextlib
18 import contextlib
19 import io
19 import io
20 import os
20 import os
21 import struct
21 import struct
22 import weakref
22 import weakref
23 import zlib
23 import zlib
24
24
25 # import stuff from node for others to import from revlog
25 # import stuff from node for others to import from revlog
26 from .node import (
26 from .node import (
27 bin,
27 bin,
28 hex,
28 hex,
29 nullrev,
29 nullrev,
30 sha1nodeconstants,
30 sha1nodeconstants,
31 short,
31 short,
32 wdirrev,
32 wdirrev,
33 )
33 )
34 from .i18n import _
34 from .i18n import _
35 from .revlogutils.constants import (
35 from .revlogutils.constants import (
36 ALL_KINDS,
36 ALL_KINDS,
37 CHANGELOGV2,
37 CHANGELOGV2,
38 COMP_MODE_DEFAULT,
38 COMP_MODE_DEFAULT,
39 COMP_MODE_INLINE,
39 COMP_MODE_INLINE,
40 COMP_MODE_PLAIN,
40 COMP_MODE_PLAIN,
41 DELTA_BASE_REUSE_NO,
41 DELTA_BASE_REUSE_NO,
42 DELTA_BASE_REUSE_TRY,
42 DELTA_BASE_REUSE_TRY,
43 ENTRY_RANK,
43 ENTRY_RANK,
44 FEATURES_BY_VERSION,
44 FEATURES_BY_VERSION,
45 FLAG_GENERALDELTA,
45 FLAG_GENERALDELTA,
46 FLAG_INLINE_DATA,
46 FLAG_INLINE_DATA,
47 INDEX_HEADER,
47 INDEX_HEADER,
48 KIND_CHANGELOG,
48 KIND_CHANGELOG,
49 KIND_FILELOG,
49 KIND_FILELOG,
50 RANK_UNKNOWN,
50 RANK_UNKNOWN,
51 REVLOGV0,
51 REVLOGV0,
52 REVLOGV1,
52 REVLOGV1,
53 REVLOGV1_FLAGS,
53 REVLOGV1_FLAGS,
54 REVLOGV2,
54 REVLOGV2,
55 REVLOGV2_FLAGS,
55 REVLOGV2_FLAGS,
56 REVLOG_DEFAULT_FLAGS,
56 REVLOG_DEFAULT_FLAGS,
57 REVLOG_DEFAULT_FORMAT,
57 REVLOG_DEFAULT_FORMAT,
58 REVLOG_DEFAULT_VERSION,
58 REVLOG_DEFAULT_VERSION,
59 SUPPORTED_FLAGS,
59 SUPPORTED_FLAGS,
60 )
60 )
61 from .revlogutils.flagutil import (
61 from .revlogutils.flagutil import (
62 REVIDX_DEFAULT_FLAGS,
62 REVIDX_DEFAULT_FLAGS,
63 REVIDX_ELLIPSIS,
63 REVIDX_ELLIPSIS,
64 REVIDX_EXTSTORED,
64 REVIDX_EXTSTORED,
65 REVIDX_FLAGS_ORDER,
65 REVIDX_FLAGS_ORDER,
66 REVIDX_HASCOPIESINFO,
66 REVIDX_HASCOPIESINFO,
67 REVIDX_ISCENSORED,
67 REVIDX_ISCENSORED,
68 REVIDX_RAWTEXT_CHANGING_FLAGS,
68 REVIDX_RAWTEXT_CHANGING_FLAGS,
69 )
69 )
70 from .thirdparty import attr
70 from .thirdparty import attr
71 from . import (
71 from . import (
72 ancestor,
72 ancestor,
73 dagop,
73 dagop,
74 error,
74 error,
75 mdiff,
75 mdiff,
76 policy,
76 policy,
77 pycompat,
77 pycompat,
78 revlogutils,
78 revlogutils,
79 templatefilters,
79 templatefilters,
80 util,
80 util,
81 )
81 )
82 from .interfaces import (
82 from .interfaces import (
83 repository,
83 repository,
84 util as interfaceutil,
84 util as interfaceutil,
85 )
85 )
86 from .revlogutils import (
86 from .revlogutils import (
87 deltas as deltautil,
87 deltas as deltautil,
88 docket as docketutil,
88 docket as docketutil,
89 flagutil,
89 flagutil,
90 nodemap as nodemaputil,
90 nodemap as nodemaputil,
91 randomaccessfile,
91 randomaccessfile,
92 revlogv0,
92 revlogv0,
93 rewrite,
93 rewrite,
94 sidedata as sidedatautil,
94 sidedata as sidedatautil,
95 )
95 )
96 from .utils import (
96 from .utils import (
97 storageutil,
97 storageutil,
98 stringutil,
98 stringutil,
99 )
99 )
100
100
101 # blanked usage of all the name to prevent pyflakes constraints
101 # blanked usage of all the name to prevent pyflakes constraints
102 # We need these name available in the module for extensions.
102 # We need these name available in the module for extensions.
103
103
104 REVLOGV0
104 REVLOGV0
105 REVLOGV1
105 REVLOGV1
106 REVLOGV2
106 REVLOGV2
107 CHANGELOGV2
107 CHANGELOGV2
108 FLAG_INLINE_DATA
108 FLAG_INLINE_DATA
109 FLAG_GENERALDELTA
109 FLAG_GENERALDELTA
110 REVLOG_DEFAULT_FLAGS
110 REVLOG_DEFAULT_FLAGS
111 REVLOG_DEFAULT_FORMAT
111 REVLOG_DEFAULT_FORMAT
112 REVLOG_DEFAULT_VERSION
112 REVLOG_DEFAULT_VERSION
113 REVLOGV1_FLAGS
113 REVLOGV1_FLAGS
114 REVLOGV2_FLAGS
114 REVLOGV2_FLAGS
115 REVIDX_ISCENSORED
115 REVIDX_ISCENSORED
116 REVIDX_ELLIPSIS
116 REVIDX_ELLIPSIS
117 REVIDX_HASCOPIESINFO
117 REVIDX_HASCOPIESINFO
118 REVIDX_EXTSTORED
118 REVIDX_EXTSTORED
119 REVIDX_DEFAULT_FLAGS
119 REVIDX_DEFAULT_FLAGS
120 REVIDX_FLAGS_ORDER
120 REVIDX_FLAGS_ORDER
121 REVIDX_RAWTEXT_CHANGING_FLAGS
121 REVIDX_RAWTEXT_CHANGING_FLAGS
122
122
123 parsers = policy.importmod('parsers')
123 parsers = policy.importmod('parsers')
124 rustancestor = policy.importrust('ancestor')
124 rustancestor = policy.importrust('ancestor')
125 rustdagop = policy.importrust('dagop')
125 rustdagop = policy.importrust('dagop')
126 rustrevlog = policy.importrust('revlog')
126 rustrevlog = policy.importrust('revlog')
127
127
128 # Aliased for performance.
128 # Aliased for performance.
129 _zlibdecompress = zlib.decompress
129 _zlibdecompress = zlib.decompress
130
130
131 # max size of inline data embedded into a revlog
131 # max size of inline data embedded into a revlog
132 _maxinline = 131072
132 _maxinline = 131072
133
133
134 # Flag processors for REVIDX_ELLIPSIS.
134 # Flag processors for REVIDX_ELLIPSIS.
135 def ellipsisreadprocessor(rl, text):
135 def ellipsisreadprocessor(rl, text):
136 return text, False
136 return text, False
137
137
138
138
139 def ellipsiswriteprocessor(rl, text):
139 def ellipsiswriteprocessor(rl, text):
140 return text, False
140 return text, False
141
141
142
142
143 def ellipsisrawprocessor(rl, text):
143 def ellipsisrawprocessor(rl, text):
144 return False
144 return False
145
145
146
146
147 ellipsisprocessor = (
147 ellipsisprocessor = (
148 ellipsisreadprocessor,
148 ellipsisreadprocessor,
149 ellipsiswriteprocessor,
149 ellipsiswriteprocessor,
150 ellipsisrawprocessor,
150 ellipsisrawprocessor,
151 )
151 )
152
152
153
153
154 def _verify_revision(rl, skipflags, state, node):
154 def _verify_revision(rl, skipflags, state, node):
155 """Verify the integrity of the given revlog ``node`` while providing a hook
155 """Verify the integrity of the given revlog ``node`` while providing a hook
156 point for extensions to influence the operation."""
156 point for extensions to influence the operation."""
157 if skipflags:
157 if skipflags:
158 state[b'skipread'].add(node)
158 state[b'skipread'].add(node)
159 else:
159 else:
160 # Side-effect: read content and verify hash.
160 # Side-effect: read content and verify hash.
161 rl.revision(node)
161 rl.revision(node)
162
162
163
163
164 # True if a fast implementation for persistent-nodemap is available
164 # True if a fast implementation for persistent-nodemap is available
165 #
165 #
166 # We also consider we have a "fast" implementation in "pure" python because
166 # We also consider we have a "fast" implementation in "pure" python because
167 # people using pure don't really have performance consideration (and a
167 # people using pure don't really have performance consideration (and a
168 # wheelbarrow of other slowness source)
168 # wheelbarrow of other slowness source)
169 HAS_FAST_PERSISTENT_NODEMAP = rustrevlog is not None or hasattr(
169 HAS_FAST_PERSISTENT_NODEMAP = rustrevlog is not None or hasattr(
170 parsers, 'BaseIndexObject'
170 parsers, 'BaseIndexObject'
171 )
171 )
172
172
173
173
174 @interfaceutil.implementer(repository.irevisiondelta)
174 @interfaceutil.implementer(repository.irevisiondelta)
175 @attr.s(slots=True)
175 @attr.s(slots=True)
176 class revlogrevisiondelta:
176 class revlogrevisiondelta:
177 node = attr.ib()
177 node = attr.ib()
178 p1node = attr.ib()
178 p1node = attr.ib()
179 p2node = attr.ib()
179 p2node = attr.ib()
180 basenode = attr.ib()
180 basenode = attr.ib()
181 flags = attr.ib()
181 flags = attr.ib()
182 baserevisionsize = attr.ib()
182 baserevisionsize = attr.ib()
183 revision = attr.ib()
183 revision = attr.ib()
184 delta = attr.ib()
184 delta = attr.ib()
185 sidedata = attr.ib()
185 sidedata = attr.ib()
186 protocol_flags = attr.ib()
186 protocol_flags = attr.ib()
187 linknode = attr.ib(default=None)
187 linknode = attr.ib(default=None)
188
188
189
189
190 @interfaceutil.implementer(repository.iverifyproblem)
190 @interfaceutil.implementer(repository.iverifyproblem)
191 @attr.s(frozen=True)
191 @attr.s(frozen=True)
192 class revlogproblem:
192 class revlogproblem:
193 warning = attr.ib(default=None)
193 warning = attr.ib(default=None)
194 error = attr.ib(default=None)
194 error = attr.ib(default=None)
195 node = attr.ib(default=None)
195 node = attr.ib(default=None)
196
196
197
197
198 def parse_index_v1(data, inline):
198 def parse_index_v1(data, inline):
199 # call the C implementation to parse the index data
199 # call the C implementation to parse the index data
200 index, cache = parsers.parse_index2(data, inline)
200 index, cache = parsers.parse_index2(data, inline)
201 return index, cache
201 return index, cache
202
202
203
203
204 def parse_index_v2(data, inline):
204 def parse_index_v2(data, inline):
205 # call the C implementation to parse the index data
205 # call the C implementation to parse the index data
206 index, cache = parsers.parse_index2(data, inline, format=REVLOGV2)
206 index, cache = parsers.parse_index2(data, inline, format=REVLOGV2)
207 return index, cache
207 return index, cache
208
208
209
209
210 def parse_index_cl_v2(data, inline):
210 def parse_index_cl_v2(data, inline):
211 # call the C implementation to parse the index data
211 # call the C implementation to parse the index data
212 index, cache = parsers.parse_index2(data, inline, format=CHANGELOGV2)
212 index, cache = parsers.parse_index2(data, inline, format=CHANGELOGV2)
213 return index, cache
213 return index, cache
214
214
215
215
216 if hasattr(parsers, 'parse_index_devel_nodemap'):
216 if hasattr(parsers, 'parse_index_devel_nodemap'):
217
217
218 def parse_index_v1_nodemap(data, inline):
218 def parse_index_v1_nodemap(data, inline):
219 index, cache = parsers.parse_index_devel_nodemap(data, inline)
219 index, cache = parsers.parse_index_devel_nodemap(data, inline)
220 return index, cache
220 return index, cache
221
221
222
222
223 else:
223 else:
224 parse_index_v1_nodemap = None
224 parse_index_v1_nodemap = None
225
225
226
226
227 def parse_index_v1_mixed(data, inline):
227 def parse_index_v1_mixed(data, inline):
228 index, cache = parse_index_v1(data, inline)
228 index, cache = parse_index_v1(data, inline)
229 return rustrevlog.MixedIndex(index), cache
229 return rustrevlog.MixedIndex(index), cache
230
230
231
231
232 # corresponds to uncompressed length of indexformatng (2 gigs, 4-byte
232 # corresponds to uncompressed length of indexformatng (2 gigs, 4-byte
233 # signed integer)
233 # signed integer)
234 _maxentrysize = 0x7FFFFFFF
234 _maxentrysize = 0x7FFFFFFF
235
235
236 FILE_TOO_SHORT_MSG = _(
236 FILE_TOO_SHORT_MSG = _(
237 b'cannot read from revlog %s;'
237 b'cannot read from revlog %s;'
238 b' expected %d bytes from offset %d, data size is %d'
238 b' expected %d bytes from offset %d, data size is %d'
239 )
239 )
240
240
241 hexdigits = b'0123456789abcdefABCDEF'
241 hexdigits = b'0123456789abcdefABCDEF'
242
242
243
243
244 class _Config:
244 class _Config:
245 def copy(self):
245 def copy(self):
246 return self.__class__(**self.__dict__)
246 return self.__class__(**self.__dict__)
247
247
248
248
249 @attr.s()
249 @attr.s()
250 class FeatureConfig(_Config):
250 class FeatureConfig(_Config):
251 """Hold configuration values about the available revlog features"""
251 """Hold configuration values about the available revlog features"""
252
252
253 # the default compression engine
253 # the default compression engine
254 compression_engine = attr.ib(default=b'zlib')
254 compression_engine = attr.ib(default=b'zlib')
255 # compression engines options
255 # compression engines options
256 compression_engine_options = attr.ib(default=attr.Factory(dict))
256 compression_engine_options = attr.ib(default=attr.Factory(dict))
257
257
258 # can we use censor on this revlog
258 # can we use censor on this revlog
259 censorable = attr.ib(default=False)
259 censorable = attr.ib(default=False)
260 # does this revlog use the "side data" feature
260 # does this revlog use the "side data" feature
261 has_side_data = attr.ib(default=False)
261 has_side_data = attr.ib(default=False)
262 # might remove rank configuration once the computation has no impact
262 # might remove rank configuration once the computation has no impact
263 compute_rank = attr.ib(default=False)
263 compute_rank = attr.ib(default=False)
264 # parent order is supposed to be semantically irrelevant, so we
264 # parent order is supposed to be semantically irrelevant, so we
265 # normally resort parents to ensure that the first parent is non-null,
265 # normally resort parents to ensure that the first parent is non-null,
266 # if there is a non-null parent at all.
266 # if there is a non-null parent at all.
267 # filelog abuses the parent order as flag to mark some instances of
267 # filelog abuses the parent order as flag to mark some instances of
268 # meta-encoded files, so allow it to disable this behavior.
268 # meta-encoded files, so allow it to disable this behavior.
269 canonical_parent_order = attr.ib(default=False)
269 canonical_parent_order = attr.ib(default=False)
270 # can ellipsis commit be used
270 # can ellipsis commit be used
271 enable_ellipsis = attr.ib(default=False)
271 enable_ellipsis = attr.ib(default=False)
272
272
273 def copy(self):
273 def copy(self):
274 new = super().copy()
274 new = super().copy()
275 new.compression_engine_options = self.compression_engine_options.copy()
275 new.compression_engine_options = self.compression_engine_options.copy()
276 return new
276 return new
277
277
278
278
279 @attr.s()
279 @attr.s()
280 class DataConfig(_Config):
280 class DataConfig(_Config):
281 """Hold configuration value about how the revlog data are read"""
281 """Hold configuration value about how the revlog data are read"""
282
282
283 # should we try to open the "pending" version of the revlog
283 # should we try to open the "pending" version of the revlog
284 try_pending = attr.ib(default=False)
284 try_pending = attr.ib(default=False)
285 # should we try to open the "splitted" version of the revlog
285 # should we try to open the "splitted" version of the revlog
286 try_split = attr.ib(default=False)
286 try_split = attr.ib(default=False)
287 # When True, indexfile should be opened with checkambig=True at writing,
287 # When True, indexfile should be opened with checkambig=True at writing,
288 # to avoid file stat ambiguity.
288 # to avoid file stat ambiguity.
289 check_ambig = attr.ib(default=False)
289 check_ambig = attr.ib(default=False)
290
290
291 # If true, use mmap instead of reading to deal with large index
291 # If true, use mmap instead of reading to deal with large index
292 mmap_large_index = attr.ib(default=False)
292 mmap_large_index = attr.ib(default=False)
293 # how much data is large
293 # how much data is large
294 mmap_index_threshold = attr.ib(default=None)
294 mmap_index_threshold = attr.ib(default=None)
295 # How much data to read and cache into the raw revlog data cache.
295 # How much data to read and cache into the raw revlog data cache.
296 chunk_cache_size = attr.ib(default=65536)
296 chunk_cache_size = attr.ib(default=65536)
297
297
298 # Allow sparse reading of the revlog data
298 # Allow sparse reading of the revlog data
299 with_sparse_read = attr.ib(default=False)
299 with_sparse_read = attr.ib(default=False)
300 # minimal density of a sparse read chunk
300 # minimal density of a sparse read chunk
301 sr_density_threshold = attr.ib(default=0.50)
301 sr_density_threshold = attr.ib(default=0.50)
302 # minimal size of data we skip when performing sparse read
302 # minimal size of data we skip when performing sparse read
303 sr_min_gap_size = attr.ib(default=262144)
303 sr_min_gap_size = attr.ib(default=262144)
304
304
305 # are delta encoded against arbitrary bases.
305 # are delta encoded against arbitrary bases.
306 generaldelta = attr.ib(default=False)
306 generaldelta = attr.ib(default=False)
307
307
308
308
309 @attr.s()
309 @attr.s()
310 class DeltaConfig(_Config):
310 class DeltaConfig(_Config):
311 """Hold configuration value about how new delta are computed
311 """Hold configuration value about how new delta are computed
312
312
313 Some attributes are duplicated from DataConfig to help havign each object
313 Some attributes are duplicated from DataConfig to help havign each object
314 self contained.
314 self contained.
315 """
315 """
316
316
317 # can delta be encoded against arbitrary bases.
317 # can delta be encoded against arbitrary bases.
318 general_delta = attr.ib(default=False)
318 general_delta = attr.ib(default=False)
319 # Allow sparse writing of the revlog data
319 # Allow sparse writing of the revlog data
320 sparse_revlog = attr.ib(default=False)
320 sparse_revlog = attr.ib(default=False)
321 # maximum length of a delta chain
321 # maximum length of a delta chain
322 max_chain_len = attr.ib(default=None)
322 max_chain_len = attr.ib(default=None)
323 # Maximum distance between delta chain base start and end
323 # Maximum distance between delta chain base start and end
324 max_deltachain_span = attr.ib(default=-1)
324 max_deltachain_span = attr.ib(default=-1)
325 # If `upper_bound_comp` is not None, this is the expected maximal gain from
325 # If `upper_bound_comp` is not None, this is the expected maximal gain from
326 # compression for the data content.
326 # compression for the data content.
327 upper_bound_comp = attr.ib(default=None)
327 upper_bound_comp = attr.ib(default=None)
328 # Should we try a delta against both parent
328 # Should we try a delta against both parent
329 delta_both_parents = attr.ib(default=True)
329 delta_both_parents = attr.ib(default=True)
330 # Test delta base candidate group by chunk of this maximal size.
330 # Test delta base candidate group by chunk of this maximal size.
331 candidate_group_chunk_size = attr.ib(default=0)
331 candidate_group_chunk_size = attr.ib(default=0)
332 # Should we display debug information about delta computation
332 # Should we display debug information about delta computation
333 debug_delta = attr.ib(default=False)
333 debug_delta = attr.ib(default=False)
334 # trust incoming delta by default
334 # trust incoming delta by default
335 lazy_delta = attr.ib(default=True)
335 lazy_delta = attr.ib(default=True)
336 # trust the base of incoming delta by default
336 # trust the base of incoming delta by default
337 lazy_delta_base = attr.ib(default=False)
337 lazy_delta_base = attr.ib(default=False)
338
338
339
339
340 class revlog:
340 class revlog:
341 """
341 """
342 the underlying revision storage object
342 the underlying revision storage object
343
343
344 A revlog consists of two parts, an index and the revision data.
344 A revlog consists of two parts, an index and the revision data.
345
345
346 The index is a file with a fixed record size containing
346 The index is a file with a fixed record size containing
347 information on each revision, including its nodeid (hash), the
347 information on each revision, including its nodeid (hash), the
348 nodeids of its parents, the position and offset of its data within
348 nodeids of its parents, the position and offset of its data within
349 the data file, and the revision it's based on. Finally, each entry
349 the data file, and the revision it's based on. Finally, each entry
350 contains a linkrev entry that can serve as a pointer to external
350 contains a linkrev entry that can serve as a pointer to external
351 data.
351 data.
352
352
353 The revision data itself is a linear collection of data chunks.
353 The revision data itself is a linear collection of data chunks.
354 Each chunk represents a revision and is usually represented as a
354 Each chunk represents a revision and is usually represented as a
355 delta against the previous chunk. To bound lookup time, runs of
355 delta against the previous chunk. To bound lookup time, runs of
356 deltas are limited to about 2 times the length of the original
356 deltas are limited to about 2 times the length of the original
357 version data. This makes retrieval of a version proportional to
357 version data. This makes retrieval of a version proportional to
358 its size, or O(1) relative to the number of revisions.
358 its size, or O(1) relative to the number of revisions.
359
359
360 Both pieces of the revlog are written to in an append-only
360 Both pieces of the revlog are written to in an append-only
361 fashion, which means we never need to rewrite a file to insert or
361 fashion, which means we never need to rewrite a file to insert or
362 remove data, and can use some simple techniques to avoid the need
362 remove data, and can use some simple techniques to avoid the need
363 for locking while reading.
363 for locking while reading.
364
364
365 If checkambig, indexfile is opened with checkambig=True at
365 If checkambig, indexfile is opened with checkambig=True at
366 writing, to avoid file stat ambiguity.
366 writing, to avoid file stat ambiguity.
367
367
368 If mmaplargeindex is True, and an mmapindexthreshold is set, the
368 If mmaplargeindex is True, and an mmapindexthreshold is set, the
369 index will be mmapped rather than read if it is larger than the
369 index will be mmapped rather than read if it is larger than the
370 configured threshold.
370 configured threshold.
371
371
372 If censorable is True, the revlog can have censored revisions.
372 If censorable is True, the revlog can have censored revisions.
373
373
374 If `upperboundcomp` is not None, this is the expected maximal gain from
374 If `upperboundcomp` is not None, this is the expected maximal gain from
375 compression for the data content.
375 compression for the data content.
376
376
377 `concurrencychecker` is an optional function that receives 3 arguments: a
377 `concurrencychecker` is an optional function that receives 3 arguments: a
378 file handle, a filename, and an expected position. It should check whether
378 file handle, a filename, and an expected position. It should check whether
379 the current position in the file handle is valid, and log/warn/fail (by
379 the current position in the file handle is valid, and log/warn/fail (by
380 raising).
380 raising).
381
381
382 See mercurial/revlogutils/contants.py for details about the content of an
382 See mercurial/revlogutils/contants.py for details about the content of an
383 index entry.
383 index entry.
384 """
384 """
385
385
386 _flagserrorclass = error.RevlogError
386 _flagserrorclass = error.RevlogError
387
387
388 @staticmethod
388 @staticmethod
389 def is_inline_index(header_bytes):
389 def is_inline_index(header_bytes):
390 """Determine if a revlog is inline from the initial bytes of the index"""
390 """Determine if a revlog is inline from the initial bytes of the index"""
391 header = INDEX_HEADER.unpack(header_bytes)[0]
391 header = INDEX_HEADER.unpack(header_bytes)[0]
392
392
393 _format_flags = header & ~0xFFFF
393 _format_flags = header & ~0xFFFF
394 _format_version = header & 0xFFFF
394 _format_version = header & 0xFFFF
395
395
396 features = FEATURES_BY_VERSION[_format_version]
396 features = FEATURES_BY_VERSION[_format_version]
397 return features[b'inline'](_format_flags)
397 return features[b'inline'](_format_flags)
398
398
399 def __init__(
399 def __init__(
400 self,
400 self,
401 opener,
401 opener,
402 target,
402 target,
403 radix,
403 radix,
404 postfix=None, # only exist for `tmpcensored` now
404 postfix=None, # only exist for `tmpcensored` now
405 checkambig=False,
405 checkambig=False,
406 mmaplargeindex=False,
406 mmaplargeindex=False,
407 censorable=False,
407 censorable=False,
408 upperboundcomp=None,
408 upperboundcomp=None,
409 persistentnodemap=False,
409 persistentnodemap=False,
410 concurrencychecker=None,
410 concurrencychecker=None,
411 trypending=False,
411 trypending=False,
412 try_split=False,
412 try_split=False,
413 canonical_parent_order=True,
413 canonical_parent_order=True,
414 ):
414 ):
415 """
415 """
416 create a revlog object
416 create a revlog object
417
417
418 opener is a function that abstracts the file opening operation
418 opener is a function that abstracts the file opening operation
419 and can be used to implement COW semantics or the like.
419 and can be used to implement COW semantics or the like.
420
420
421 `target`: a (KIND, ID) tuple that identify the content stored in
421 `target`: a (KIND, ID) tuple that identify the content stored in
422 this revlog. It help the rest of the code to understand what the revlog
422 this revlog. It help the rest of the code to understand what the revlog
423 is about without having to resort to heuristic and index filename
423 is about without having to resort to heuristic and index filename
424 analysis. Note: that this must be reliably be set by normal code, but
424 analysis. Note: that this must be reliably be set by normal code, but
425 that test, debug, or performance measurement code might not set this to
425 that test, debug, or performance measurement code might not set this to
426 accurate value.
426 accurate value.
427 """
427 """
428 self.upperboundcomp = upperboundcomp
428 self.upperboundcomp = upperboundcomp
429
429
430 self.radix = radix
430 self.radix = radix
431
431
432 self._docket_file = None
432 self._docket_file = None
433 self._indexfile = None
433 self._indexfile = None
434 self._datafile = None
434 self._datafile = None
435 self._sidedatafile = None
435 self._sidedatafile = None
436 self._nodemap_file = None
436 self._nodemap_file = None
437 self.postfix = postfix
437 self.postfix = postfix
438 self._trypending = trypending
438 self._trypending = trypending
439 self._try_split = try_split
439 self._try_split = try_split
440 self.opener = opener
440 self.opener = opener
441 if persistentnodemap:
441 if persistentnodemap:
442 self._nodemap_file = nodemaputil.get_nodemap_file(self)
442 self._nodemap_file = nodemaputil.get_nodemap_file(self)
443
443
444 assert target[0] in ALL_KINDS
444 assert target[0] in ALL_KINDS
445 assert len(target) == 2
445 assert len(target) == 2
446 self.target = target
446 self.target = target
447 if b'feature-config' in self.opener.options:
447 if b'feature-config' in self.opener.options:
448 self.feature_config = self.opener.options[b'feature-config'].copy()
448 self.feature_config = self.opener.options[b'feature-config'].copy()
449 else:
449 else:
450 self.feature_config = FeatureConfig()
450 self.feature_config = FeatureConfig()
451 self.feature_config.censorable = censorable
451 self.feature_config.censorable = censorable
452 self.feature_config.canonical_parent_order = canonical_parent_order
452 self.feature_config.canonical_parent_order = canonical_parent_order
453 if b'data-config' in self.opener.options:
453 if b'data-config' in self.opener.options:
454 self.data_config = self.opener.options[b'data-config'].copy()
454 self.data_config = self.opener.options[b'data-config'].copy()
455 else:
455 else:
456 self.data_config = DataConfig()
456 self.data_config = DataConfig()
457 self.data_config.check_ambig = checkambig
457 self.data_config.check_ambig = checkambig
458 self.data_config.mmap_large_index = mmaplargeindex
458 self.data_config.mmap_large_index = mmaplargeindex
459 if b'delta-config' in self.opener.options:
459 if b'delta-config' in self.opener.options:
460 self.delta_config = self.opener.options[b'delta-config'].copy()
460 self.delta_config = self.opener.options[b'delta-config'].copy()
461 else:
461 else:
462 self.delta_config = DeltaConfig()
462 self.delta_config = DeltaConfig()
463
463
464 # 3-tuple of (node, rev, text) for a raw revision.
464 # 3-tuple of (node, rev, text) for a raw revision.
465 self._revisioncache = None
465 self._revisioncache = None
466 # Maps rev to chain base rev.
466 # Maps rev to chain base rev.
467 self._chainbasecache = util.lrucachedict(100)
467 self._chainbasecache = util.lrucachedict(100)
468 # 2-tuple of (offset, data) of raw data from the revlog at an offset.
468 # 2-tuple of (offset, data) of raw data from the revlog at an offset.
469 self._chunkcache = (0, b'')
469 self._chunkcache = (0, b'')
470
470
471 self.index = None
471 self.index = None
472 self._docket = None
472 self._docket = None
473 self._nodemap_docket = None
473 self._nodemap_docket = None
474 # Mapping of partial identifiers to full nodes.
474 # Mapping of partial identifiers to full nodes.
475 self._pcache = {}
475 self._pcache = {}
476
476
477 # other optionnals features
477 # other optionnals features
478
478
479 # Make copy of flag processors so each revlog instance can support
479 # Make copy of flag processors so each revlog instance can support
480 # custom flags.
480 # custom flags.
481 self._flagprocessors = dict(flagutil.flagprocessors)
481 self._flagprocessors = dict(flagutil.flagprocessors)
482
482
483 # 3-tuple of file handles being used for active writing.
483 # 3-tuple of file handles being used for active writing.
484 self._writinghandles = None
484 self._writinghandles = None
485 # prevent nesting of addgroup
485 # prevent nesting of addgroup
486 self._adding_group = None
486 self._adding_group = None
487
487
488 self._loadindex()
488 self._loadindex()
489
489
490 self._concurrencychecker = concurrencychecker
490 self._concurrencychecker = concurrencychecker
491
491
492 @property
492 @property
493 def _generaldelta(self):
493 def _generaldelta(self):
494 """temporary compatibility proxy"""
494 """temporary compatibility proxy"""
495 return self.delta_config.general_delta
495 return self.delta_config.general_delta
496
496
497 @property
497 @property
498 def _checkambig(self):
498 def _checkambig(self):
499 """temporary compatibility proxy"""
499 """temporary compatibility proxy"""
500 return self.data_config.check_ambig
500 return self.data_config.check_ambig
501
501
502 @property
502 @property
503 def _mmaplargeindex(self):
503 def _mmaplargeindex(self):
504 """temporary compatibility proxy"""
504 """temporary compatibility proxy"""
505 return self.data_config.mmap_large_index
505 return self.data_config.mmap_large_index
506
506
507 @property
507 @property
508 def _censorable(self):
508 def _censorable(self):
509 """temporary compatibility proxy"""
509 """temporary compatibility proxy"""
510 return self.feature_config.censorable
510 return self.feature_config.censorable
511
511
512 @property
512 @property
513 def _chunkcachesize(self):
513 def _chunkcachesize(self):
514 """temporary compatibility proxy"""
514 """temporary compatibility proxy"""
515 return self.data_config.chunk_cache_size
515 return self.data_config.chunk_cache_size
516
516
517 @property
517 @property
518 def _maxchainlen(self):
518 def _maxchainlen(self):
519 """temporary compatibility proxy"""
519 """temporary compatibility proxy"""
520 return self.delta_config.max_chain_len
520 return self.delta_config.max_chain_len
521
521
522 @property
522 @property
523 def _deltabothparents(self):
523 def _deltabothparents(self):
524 """temporary compatibility proxy"""
524 """temporary compatibility proxy"""
525 return self.delta_config.delta_both_parents
525 return self.delta_config.delta_both_parents
526
526
527 @property
527 @property
528 def _candidate_group_chunk_size(self):
528 def _candidate_group_chunk_size(self):
529 """temporary compatibility proxy"""
529 """temporary compatibility proxy"""
530 return self.delta_config.candidate_group_chunk_size
530 return self.delta_config.candidate_group_chunk_size
531
531
532 @property
532 @property
533 def _debug_delta(self):
533 def _debug_delta(self):
534 """temporary compatibility proxy"""
534 """temporary compatibility proxy"""
535 return self.delta_config.debug_delta
535 return self.delta_config.debug_delta
536
536
537 @property
537 @property
538 def _compengine(self):
538 def _compengine(self):
539 """temporary compatibility proxy"""
539 """temporary compatibility proxy"""
540 return self.feature_config.compression_engine
540 return self.feature_config.compression_engine
541
541
542 @property
542 @property
543 def _compengineopts(self):
543 def _compengineopts(self):
544 """temporary compatibility proxy"""
544 """temporary compatibility proxy"""
545 return self.feature_config.compression_engine_options
545 return self.feature_config.compression_engine_options
546
546
547 @property
547 @property
548 def _maxdeltachainspan(self):
548 def _maxdeltachainspan(self):
549 """temporary compatibility proxy"""
549 """temporary compatibility proxy"""
550 return self.delta_config.max_deltachain_span
550 return self.delta_config.max_deltachain_span
551
551
552 @property
552 @property
553 def _withsparseread(self):
553 def _withsparseread(self):
554 """temporary compatibility proxy"""
554 """temporary compatibility proxy"""
555 return self.data_config.with_sparse_read
555 return self.data_config.with_sparse_read
556
556
557 @property
557 @property
558 def _sparserevlog(self):
558 def _sparserevlog(self):
559 """temporary compatibility proxy"""
559 """temporary compatibility proxy"""
560 return self.delta_config.sparse_revlog
560 return self.delta_config.sparse_revlog
561
561
562 @property
562 @property
563 def hassidedata(self):
563 def hassidedata(self):
564 """temporary compatibility proxy"""
564 """temporary compatibility proxy"""
565 return self.feature_config.has_side_data
565 return self.feature_config.has_side_data
566
566
567 @property
567 @property
568 def _srdensitythreshold(self):
568 def _srdensitythreshold(self):
569 """temporary compatibility proxy"""
569 """temporary compatibility proxy"""
570 return self.data_config.sr_density_threshold
570 return self.data_config.sr_density_threshold
571
571
572 @property
572 @property
573 def _srmingapsize(self):
573 def _srmingapsize(self):
574 """temporary compatibility proxy"""
574 """temporary compatibility proxy"""
575 return self.data_config.sr_min_gap_size
575 return self.data_config.sr_min_gap_size
576
576
577 @property
577 @property
578 def _compute_rank(self):
578 def _compute_rank(self):
579 """temporary compatibility proxy"""
579 """temporary compatibility proxy"""
580 return self.feature_config.compute_rank
580 return self.feature_config.compute_rank
581
581
582 @property
582 @property
583 def canonical_parent_order(self):
583 def canonical_parent_order(self):
584 """temporary compatibility proxy"""
584 """temporary compatibility proxy"""
585 return self.feature_config.canonical_parent_order
585 return self.feature_config.canonical_parent_order
586
586
587 @property
587 @property
588 def _lazydelta(self):
588 def _lazydelta(self):
589 """temporary compatibility proxy"""
589 """temporary compatibility proxy"""
590 return self.delta_config.lazy_delta
590 return self.delta_config.lazy_delta
591
591
592 @property
592 @property
593 def _lazydeltabase(self):
593 def _lazydeltabase(self):
594 """temporary compatibility proxy"""
594 """temporary compatibility proxy"""
595 return self.delta_config.lazy_delta_base
595 return self.delta_config.lazy_delta_base
596
596
597 def _init_opts(self):
597 def _init_opts(self):
598 """process options (from above/config) to setup associated default revlog mode
598 """process options (from above/config) to setup associated default revlog mode
599
599
600 These values might be affected when actually reading on disk information.
600 These values might be affected when actually reading on disk information.
601
601
602 The relevant values are returned for use in _loadindex().
602 The relevant values are returned for use in _loadindex().
603
603
604 * newversionflags:
604 * newversionflags:
605 version header to use if we need to create a new revlog
605 version header to use if we need to create a new revlog
606
606
607 * mmapindexthreshold:
607 * mmapindexthreshold:
608 minimal index size for start to use mmap
608 minimal index size for start to use mmap
609
609
610 * force_nodemap:
610 * force_nodemap:
611 force the usage of a "development" version of the nodemap code
611 force the usage of a "development" version of the nodemap code
612 """
612 """
613 opts = self.opener.options
613 opts = self.opener.options
614
614
615 if b'changelogv2' in opts and self.revlog_kind == KIND_CHANGELOG:
615 if b'changelogv2' in opts and self.revlog_kind == KIND_CHANGELOG:
616 new_header = CHANGELOGV2
616 new_header = CHANGELOGV2
617 compute_rank = opts.get(b'changelogv2.compute-rank', True)
617 compute_rank = opts.get(b'changelogv2.compute-rank', True)
618 self.feature_config.compute_rank = compute_rank
618 self.feature_config.compute_rank = compute_rank
619 elif b'revlogv2' in opts:
619 elif b'revlogv2' in opts:
620 new_header = REVLOGV2
620 new_header = REVLOGV2
621 elif b'revlogv1' in opts:
621 elif b'revlogv1' in opts:
622 new_header = REVLOGV1 | FLAG_INLINE_DATA
622 new_header = REVLOGV1 | FLAG_INLINE_DATA
623 if b'generaldelta' in opts:
623 if b'generaldelta' in opts:
624 new_header |= FLAG_GENERALDELTA
624 new_header |= FLAG_GENERALDELTA
625 elif b'revlogv0' in self.opener.options:
625 elif b'revlogv0' in self.opener.options:
626 new_header = REVLOGV0
626 new_header = REVLOGV0
627 else:
627 else:
628 new_header = REVLOG_DEFAULT_VERSION
628 new_header = REVLOG_DEFAULT_VERSION
629
629
630 mmapindexthreshold = None
630 mmapindexthreshold = None
631 if self._mmaplargeindex:
631 if self._mmaplargeindex:
632 mmapindexthreshold = self.data_config.mmap_index_threshold
632 mmapindexthreshold = self.data_config.mmap_index_threshold
633 if b'sparse-revlog' in opts:
634 self.delta_config.sparse_revlog = bool(opts[b'sparse-revlog'])
635 if self.delta_config.sparse_revlog:
633 if self.delta_config.sparse_revlog:
636 # sparse-revlog forces sparse-read
634 # sparse-revlog forces sparse-read
637 self.data_config.with_sparse_read = True
635 self.data_config.with_sparse_read = True
638 elif b'with-sparse-read' in opts:
636 elif b'with-sparse-read' in opts:
639 self.data_config.with_sparse_read = bool(opts[b'with-sparse-read'])
637 self.data_config.with_sparse_read = bool(opts[b'with-sparse-read'])
640 if b'sparse-read-density-threshold' in opts:
638 if b'sparse-read-density-threshold' in opts:
641 self.data_config.sr_density_threshold = opts[
639 self.data_config.sr_density_threshold = opts[
642 b'sparse-read-density-threshold'
640 b'sparse-read-density-threshold'
643 ]
641 ]
644 if b'sparse-read-min-gap-size' in opts:
642 if b'sparse-read-min-gap-size' in opts:
645 self.data_config.sr_min_gap_size = opts[b'sparse-read-min-gap-size']
643 self.data_config.sr_min_gap_size = opts[b'sparse-read-min-gap-size']
646 if self.feature_config.enable_ellipsis:
644 if self.feature_config.enable_ellipsis:
647 self._flagprocessors[REVIDX_ELLIPSIS] = ellipsisprocessor
645 self._flagprocessors[REVIDX_ELLIPSIS] = ellipsisprocessor
648
646
649 # revlog v0 doesn't have flag processors
647 # revlog v0 doesn't have flag processors
650 for flag, processor in opts.get(b'flagprocessors', {}).items():
648 for flag, processor in opts.get(b'flagprocessors', {}).items():
651 flagutil.insertflagprocessor(flag, processor, self._flagprocessors)
649 flagutil.insertflagprocessor(flag, processor, self._flagprocessors)
652
650
653 chunk_cache_size = self.data_config.chunk_cache_size
651 chunk_cache_size = self.data_config.chunk_cache_size
654 if chunk_cache_size <= 0:
652 if chunk_cache_size <= 0:
655 raise error.RevlogError(
653 raise error.RevlogError(
656 _(b'revlog chunk cache size %r is not greater than 0')
654 _(b'revlog chunk cache size %r is not greater than 0')
657 % chunk_cache_size
655 % chunk_cache_size
658 )
656 )
659 elif chunk_cache_size & (chunk_cache_size - 1):
657 elif chunk_cache_size & (chunk_cache_size - 1):
660 raise error.RevlogError(
658 raise error.RevlogError(
661 _(b'revlog chunk cache size %r is not a power of 2')
659 _(b'revlog chunk cache size %r is not a power of 2')
662 % chunk_cache_size
660 % chunk_cache_size
663 )
661 )
664 force_nodemap = opts.get(b'devel-force-nodemap', False)
662 force_nodemap = opts.get(b'devel-force-nodemap', False)
665 return new_header, mmapindexthreshold, force_nodemap
663 return new_header, mmapindexthreshold, force_nodemap
666
664
667 def _get_data(self, filepath, mmap_threshold, size=None):
665 def _get_data(self, filepath, mmap_threshold, size=None):
668 """return a file content with or without mmap
666 """return a file content with or without mmap
669
667
670 If the file is missing return the empty string"""
668 If the file is missing return the empty string"""
671 try:
669 try:
672 with self.opener(filepath) as fp:
670 with self.opener(filepath) as fp:
673 if mmap_threshold is not None:
671 if mmap_threshold is not None:
674 file_size = self.opener.fstat(fp).st_size
672 file_size = self.opener.fstat(fp).st_size
675 if file_size >= mmap_threshold:
673 if file_size >= mmap_threshold:
676 if size is not None:
674 if size is not None:
677 # avoid potentiel mmap crash
675 # avoid potentiel mmap crash
678 size = min(file_size, size)
676 size = min(file_size, size)
679 # TODO: should .close() to release resources without
677 # TODO: should .close() to release resources without
680 # relying on Python GC
678 # relying on Python GC
681 if size is None:
679 if size is None:
682 return util.buffer(util.mmapread(fp))
680 return util.buffer(util.mmapread(fp))
683 else:
681 else:
684 return util.buffer(util.mmapread(fp, size))
682 return util.buffer(util.mmapread(fp, size))
685 if size is None:
683 if size is None:
686 return fp.read()
684 return fp.read()
687 else:
685 else:
688 return fp.read(size)
686 return fp.read(size)
689 except FileNotFoundError:
687 except FileNotFoundError:
690 return b''
688 return b''
691
689
692 def get_streams(self, max_linkrev, force_inline=False):
690 def get_streams(self, max_linkrev, force_inline=False):
693 """return a list of streams that represent this revlog
691 """return a list of streams that represent this revlog
694
692
695 This is used by stream-clone to do bytes to bytes copies of a repository.
693 This is used by stream-clone to do bytes to bytes copies of a repository.
696
694
697 This streams data for all revisions that refer to a changelog revision up
695 This streams data for all revisions that refer to a changelog revision up
698 to `max_linkrev`.
696 to `max_linkrev`.
699
697
700 If `force_inline` is set, it enforces that the stream will represent an inline revlog.
698 If `force_inline` is set, it enforces that the stream will represent an inline revlog.
701
699
702 It returns is a list of three-tuple:
700 It returns is a list of three-tuple:
703
701
704 [
702 [
705 (filename, bytes_stream, stream_size),
703 (filename, bytes_stream, stream_size),
706 …
704 …
707 ]
705 ]
708 """
706 """
709 n = len(self)
707 n = len(self)
710 index = self.index
708 index = self.index
711 while n > 0:
709 while n > 0:
712 linkrev = index[n - 1][4]
710 linkrev = index[n - 1][4]
713 if linkrev < max_linkrev:
711 if linkrev < max_linkrev:
714 break
712 break
715 # note: this loop will rarely go through multiple iterations, since
713 # note: this loop will rarely go through multiple iterations, since
716 # it only traverses commits created during the current streaming
714 # it only traverses commits created during the current streaming
717 # pull operation.
715 # pull operation.
718 #
716 #
719 # If this become a problem, using a binary search should cap the
717 # If this become a problem, using a binary search should cap the
720 # runtime of this.
718 # runtime of this.
721 n = n - 1
719 n = n - 1
722 if n == 0:
720 if n == 0:
723 # no data to send
721 # no data to send
724 return []
722 return []
725 index_size = n * index.entry_size
723 index_size = n * index.entry_size
726 data_size = self.end(n - 1)
724 data_size = self.end(n - 1)
727
725
728 # XXX we might have been split (or stripped) since the object
726 # XXX we might have been split (or stripped) since the object
729 # initialization, We need to close this race too, but having a way to
727 # initialization, We need to close this race too, but having a way to
730 # pre-open the file we feed to the revlog and never closing them before
728 # pre-open the file we feed to the revlog and never closing them before
731 # we are done streaming.
729 # we are done streaming.
732
730
733 if self._inline:
731 if self._inline:
734
732
735 def get_stream():
733 def get_stream():
736 with self._indexfp() as fp:
734 with self._indexfp() as fp:
737 yield None
735 yield None
738 size = index_size + data_size
736 size = index_size + data_size
739 if size <= 65536:
737 if size <= 65536:
740 yield fp.read(size)
738 yield fp.read(size)
741 else:
739 else:
742 yield from util.filechunkiter(fp, limit=size)
740 yield from util.filechunkiter(fp, limit=size)
743
741
744 inline_stream = get_stream()
742 inline_stream = get_stream()
745 next(inline_stream)
743 next(inline_stream)
746 return [
744 return [
747 (self._indexfile, inline_stream, index_size + data_size),
745 (self._indexfile, inline_stream, index_size + data_size),
748 ]
746 ]
749 elif force_inline:
747 elif force_inline:
750
748
751 def get_stream():
749 def get_stream():
752 with self.reading():
750 with self.reading():
753 yield None
751 yield None
754
752
755 for rev in range(n):
753 for rev in range(n):
756 idx = self.index.entry_binary(rev)
754 idx = self.index.entry_binary(rev)
757 if rev == 0 and self._docket is None:
755 if rev == 0 and self._docket is None:
758 # re-inject the inline flag
756 # re-inject the inline flag
759 header = self._format_flags
757 header = self._format_flags
760 header |= self._format_version
758 header |= self._format_version
761 header |= FLAG_INLINE_DATA
759 header |= FLAG_INLINE_DATA
762 header = self.index.pack_header(header)
760 header = self.index.pack_header(header)
763 idx = header + idx
761 idx = header + idx
764 yield idx
762 yield idx
765 yield self._getsegmentforrevs(rev, rev)[1]
763 yield self._getsegmentforrevs(rev, rev)[1]
766
764
767 inline_stream = get_stream()
765 inline_stream = get_stream()
768 next(inline_stream)
766 next(inline_stream)
769 return [
767 return [
770 (self._indexfile, inline_stream, index_size + data_size),
768 (self._indexfile, inline_stream, index_size + data_size),
771 ]
769 ]
772 else:
770 else:
773
771
774 def get_index_stream():
772 def get_index_stream():
775 with self._indexfp() as fp:
773 with self._indexfp() as fp:
776 yield None
774 yield None
777 if index_size <= 65536:
775 if index_size <= 65536:
778 yield fp.read(index_size)
776 yield fp.read(index_size)
779 else:
777 else:
780 yield from util.filechunkiter(fp, limit=index_size)
778 yield from util.filechunkiter(fp, limit=index_size)
781
779
782 def get_data_stream():
780 def get_data_stream():
783 with self._datafp() as fp:
781 with self._datafp() as fp:
784 yield None
782 yield None
785 if data_size <= 65536:
783 if data_size <= 65536:
786 yield fp.read(data_size)
784 yield fp.read(data_size)
787 else:
785 else:
788 yield from util.filechunkiter(fp, limit=data_size)
786 yield from util.filechunkiter(fp, limit=data_size)
789
787
790 index_stream = get_index_stream()
788 index_stream = get_index_stream()
791 next(index_stream)
789 next(index_stream)
792 data_stream = get_data_stream()
790 data_stream = get_data_stream()
793 next(data_stream)
791 next(data_stream)
794 return [
792 return [
795 (self._datafile, data_stream, data_size),
793 (self._datafile, data_stream, data_size),
796 (self._indexfile, index_stream, index_size),
794 (self._indexfile, index_stream, index_size),
797 ]
795 ]
798
796
799 def _loadindex(self, docket=None):
797 def _loadindex(self, docket=None):
800
798
801 new_header, mmapindexthreshold, force_nodemap = self._init_opts()
799 new_header, mmapindexthreshold, force_nodemap = self._init_opts()
802
800
803 if self.postfix is not None:
801 if self.postfix is not None:
804 entry_point = b'%s.i.%s' % (self.radix, self.postfix)
802 entry_point = b'%s.i.%s' % (self.radix, self.postfix)
805 elif self._trypending and self.opener.exists(b'%s.i.a' % self.radix):
803 elif self._trypending and self.opener.exists(b'%s.i.a' % self.radix):
806 entry_point = b'%s.i.a' % self.radix
804 entry_point = b'%s.i.a' % self.radix
807 elif self._try_split and self.opener.exists(self._split_index_file):
805 elif self._try_split and self.opener.exists(self._split_index_file):
808 entry_point = self._split_index_file
806 entry_point = self._split_index_file
809 else:
807 else:
810 entry_point = b'%s.i' % self.radix
808 entry_point = b'%s.i' % self.radix
811
809
812 if docket is not None:
810 if docket is not None:
813 self._docket = docket
811 self._docket = docket
814 self._docket_file = entry_point
812 self._docket_file = entry_point
815 else:
813 else:
816 self._initempty = True
814 self._initempty = True
817 entry_data = self._get_data(entry_point, mmapindexthreshold)
815 entry_data = self._get_data(entry_point, mmapindexthreshold)
818 if len(entry_data) > 0:
816 if len(entry_data) > 0:
819 header = INDEX_HEADER.unpack(entry_data[:4])[0]
817 header = INDEX_HEADER.unpack(entry_data[:4])[0]
820 self._initempty = False
818 self._initempty = False
821 else:
819 else:
822 header = new_header
820 header = new_header
823
821
824 self._format_flags = header & ~0xFFFF
822 self._format_flags = header & ~0xFFFF
825 self._format_version = header & 0xFFFF
823 self._format_version = header & 0xFFFF
826
824
827 supported_flags = SUPPORTED_FLAGS.get(self._format_version)
825 supported_flags = SUPPORTED_FLAGS.get(self._format_version)
828 if supported_flags is None:
826 if supported_flags is None:
829 msg = _(b'unknown version (%d) in revlog %s')
827 msg = _(b'unknown version (%d) in revlog %s')
830 msg %= (self._format_version, self.display_id)
828 msg %= (self._format_version, self.display_id)
831 raise error.RevlogError(msg)
829 raise error.RevlogError(msg)
832 elif self._format_flags & ~supported_flags:
830 elif self._format_flags & ~supported_flags:
833 msg = _(b'unknown flags (%#04x) in version %d revlog %s')
831 msg = _(b'unknown flags (%#04x) in version %d revlog %s')
834 display_flag = self._format_flags >> 16
832 display_flag = self._format_flags >> 16
835 msg %= (display_flag, self._format_version, self.display_id)
833 msg %= (display_flag, self._format_version, self.display_id)
836 raise error.RevlogError(msg)
834 raise error.RevlogError(msg)
837
835
838 features = FEATURES_BY_VERSION[self._format_version]
836 features = FEATURES_BY_VERSION[self._format_version]
839 self._inline = features[b'inline'](self._format_flags)
837 self._inline = features[b'inline'](self._format_flags)
840 self.delta_config.general_delta = features[b'generaldelta'](
838 self.delta_config.general_delta = features[b'generaldelta'](
841 self._format_flags
839 self._format_flags
842 )
840 )
843 self.feature_config.has_side_data = features[b'sidedata']
841 self.feature_config.has_side_data = features[b'sidedata']
844
842
845 if not features[b'docket']:
843 if not features[b'docket']:
846 self._indexfile = entry_point
844 self._indexfile = entry_point
847 index_data = entry_data
845 index_data = entry_data
848 else:
846 else:
849 self._docket_file = entry_point
847 self._docket_file = entry_point
850 if self._initempty:
848 if self._initempty:
851 self._docket = docketutil.default_docket(self, header)
849 self._docket = docketutil.default_docket(self, header)
852 else:
850 else:
853 self._docket = docketutil.parse_docket(
851 self._docket = docketutil.parse_docket(
854 self, entry_data, use_pending=self._trypending
852 self, entry_data, use_pending=self._trypending
855 )
853 )
856
854
857 if self._docket is not None:
855 if self._docket is not None:
858 self._indexfile = self._docket.index_filepath()
856 self._indexfile = self._docket.index_filepath()
859 index_data = b''
857 index_data = b''
860 index_size = self._docket.index_end
858 index_size = self._docket.index_end
861 if index_size > 0:
859 if index_size > 0:
862 index_data = self._get_data(
860 index_data = self._get_data(
863 self._indexfile, mmapindexthreshold, size=index_size
861 self._indexfile, mmapindexthreshold, size=index_size
864 )
862 )
865 if len(index_data) < index_size:
863 if len(index_data) < index_size:
866 msg = _(b'too few index data for %s: got %d, expected %d')
864 msg = _(b'too few index data for %s: got %d, expected %d')
867 msg %= (self.display_id, len(index_data), index_size)
865 msg %= (self.display_id, len(index_data), index_size)
868 raise error.RevlogError(msg)
866 raise error.RevlogError(msg)
869
867
870 self._inline = False
868 self._inline = False
871 # generaldelta implied by version 2 revlogs.
869 # generaldelta implied by version 2 revlogs.
872 self.delta_config.general_delta = True
870 self.delta_config.general_delta = True
873 # the logic for persistent nodemap will be dealt with within the
871 # the logic for persistent nodemap will be dealt with within the
874 # main docket, so disable it for now.
872 # main docket, so disable it for now.
875 self._nodemap_file = None
873 self._nodemap_file = None
876
874
877 if self._docket is not None:
875 if self._docket is not None:
878 self._datafile = self._docket.data_filepath()
876 self._datafile = self._docket.data_filepath()
879 self._sidedatafile = self._docket.sidedata_filepath()
877 self._sidedatafile = self._docket.sidedata_filepath()
880 elif self.postfix is None:
878 elif self.postfix is None:
881 self._datafile = b'%s.d' % self.radix
879 self._datafile = b'%s.d' % self.radix
882 else:
880 else:
883 self._datafile = b'%s.d.%s' % (self.radix, self.postfix)
881 self._datafile = b'%s.d.%s' % (self.radix, self.postfix)
884
882
885 self.nodeconstants = sha1nodeconstants
883 self.nodeconstants = sha1nodeconstants
886 self.nullid = self.nodeconstants.nullid
884 self.nullid = self.nodeconstants.nullid
887
885
888 # sparse-revlog can't be on without general-delta (issue6056)
886 # sparse-revlog can't be on without general-delta (issue6056)
889 if not self._generaldelta:
887 if not self._generaldelta:
890 self.delta_config.sparse_revlog = False
888 self.delta_config.sparse_revlog = False
891
889
892 self._storedeltachains = True
890 self._storedeltachains = True
893
891
894 devel_nodemap = (
892 devel_nodemap = (
895 self._nodemap_file
893 self._nodemap_file
896 and force_nodemap
894 and force_nodemap
897 and parse_index_v1_nodemap is not None
895 and parse_index_v1_nodemap is not None
898 )
896 )
899
897
900 use_rust_index = False
898 use_rust_index = False
901 if rustrevlog is not None:
899 if rustrevlog is not None:
902 if self._nodemap_file is not None:
900 if self._nodemap_file is not None:
903 use_rust_index = True
901 use_rust_index = True
904 else:
902 else:
905 use_rust_index = self.opener.options.get(b'rust.index')
903 use_rust_index = self.opener.options.get(b'rust.index')
906
904
907 self._parse_index = parse_index_v1
905 self._parse_index = parse_index_v1
908 if self._format_version == REVLOGV0:
906 if self._format_version == REVLOGV0:
909 self._parse_index = revlogv0.parse_index_v0
907 self._parse_index = revlogv0.parse_index_v0
910 elif self._format_version == REVLOGV2:
908 elif self._format_version == REVLOGV2:
911 self._parse_index = parse_index_v2
909 self._parse_index = parse_index_v2
912 elif self._format_version == CHANGELOGV2:
910 elif self._format_version == CHANGELOGV2:
913 self._parse_index = parse_index_cl_v2
911 self._parse_index = parse_index_cl_v2
914 elif devel_nodemap:
912 elif devel_nodemap:
915 self._parse_index = parse_index_v1_nodemap
913 self._parse_index = parse_index_v1_nodemap
916 elif use_rust_index:
914 elif use_rust_index:
917 self._parse_index = parse_index_v1_mixed
915 self._parse_index = parse_index_v1_mixed
918 try:
916 try:
919 d = self._parse_index(index_data, self._inline)
917 d = self._parse_index(index_data, self._inline)
920 index, chunkcache = d
918 index, chunkcache = d
921 use_nodemap = (
919 use_nodemap = (
922 not self._inline
920 not self._inline
923 and self._nodemap_file is not None
921 and self._nodemap_file is not None
924 and hasattr(index, 'update_nodemap_data')
922 and hasattr(index, 'update_nodemap_data')
925 )
923 )
926 if use_nodemap:
924 if use_nodemap:
927 nodemap_data = nodemaputil.persisted_data(self)
925 nodemap_data = nodemaputil.persisted_data(self)
928 if nodemap_data is not None:
926 if nodemap_data is not None:
929 docket = nodemap_data[0]
927 docket = nodemap_data[0]
930 if (
928 if (
931 len(d[0]) > docket.tip_rev
929 len(d[0]) > docket.tip_rev
932 and d[0][docket.tip_rev][7] == docket.tip_node
930 and d[0][docket.tip_rev][7] == docket.tip_node
933 ):
931 ):
934 # no changelog tampering
932 # no changelog tampering
935 self._nodemap_docket = docket
933 self._nodemap_docket = docket
936 index.update_nodemap_data(*nodemap_data)
934 index.update_nodemap_data(*nodemap_data)
937 except (ValueError, IndexError):
935 except (ValueError, IndexError):
938 raise error.RevlogError(
936 raise error.RevlogError(
939 _(b"index %s is corrupted") % self.display_id
937 _(b"index %s is corrupted") % self.display_id
940 )
938 )
941 self.index = index
939 self.index = index
942 self._segmentfile = randomaccessfile.randomaccessfile(
940 self._segmentfile = randomaccessfile.randomaccessfile(
943 self.opener,
941 self.opener,
944 (self._indexfile if self._inline else self._datafile),
942 (self._indexfile if self._inline else self._datafile),
945 self._chunkcachesize,
943 self._chunkcachesize,
946 chunkcache,
944 chunkcache,
947 )
945 )
948 self._segmentfile_sidedata = randomaccessfile.randomaccessfile(
946 self._segmentfile_sidedata = randomaccessfile.randomaccessfile(
949 self.opener,
947 self.opener,
950 self._sidedatafile,
948 self._sidedatafile,
951 self._chunkcachesize,
949 self._chunkcachesize,
952 )
950 )
953 # revnum -> (chain-length, sum-delta-length)
951 # revnum -> (chain-length, sum-delta-length)
954 self._chaininfocache = util.lrucachedict(500)
952 self._chaininfocache = util.lrucachedict(500)
955 # revlog header -> revlog compressor
953 # revlog header -> revlog compressor
956 self._decompressors = {}
954 self._decompressors = {}
957
955
958 def get_revlog(self):
956 def get_revlog(self):
959 """simple function to mirror API of other not-really-revlog API"""
957 """simple function to mirror API of other not-really-revlog API"""
960 return self
958 return self
961
959
962 @util.propertycache
960 @util.propertycache
963 def revlog_kind(self):
961 def revlog_kind(self):
964 return self.target[0]
962 return self.target[0]
965
963
966 @util.propertycache
964 @util.propertycache
967 def display_id(self):
965 def display_id(self):
968 """The public facing "ID" of the revlog that we use in message"""
966 """The public facing "ID" of the revlog that we use in message"""
969 if self.revlog_kind == KIND_FILELOG:
967 if self.revlog_kind == KIND_FILELOG:
970 # Reference the file without the "data/" prefix, so it is familiar
968 # Reference the file without the "data/" prefix, so it is familiar
971 # to the user.
969 # to the user.
972 return self.target[1]
970 return self.target[1]
973 else:
971 else:
974 return self.radix
972 return self.radix
975
973
976 def _get_decompressor(self, t):
974 def _get_decompressor(self, t):
977 try:
975 try:
978 compressor = self._decompressors[t]
976 compressor = self._decompressors[t]
979 except KeyError:
977 except KeyError:
980 try:
978 try:
981 engine = util.compengines.forrevlogheader(t)
979 engine = util.compengines.forrevlogheader(t)
982 compressor = engine.revlogcompressor(self._compengineopts)
980 compressor = engine.revlogcompressor(self._compengineopts)
983 self._decompressors[t] = compressor
981 self._decompressors[t] = compressor
984 except KeyError:
982 except KeyError:
985 raise error.RevlogError(
983 raise error.RevlogError(
986 _(b'unknown compression type %s') % binascii.hexlify(t)
984 _(b'unknown compression type %s') % binascii.hexlify(t)
987 )
985 )
988 return compressor
986 return compressor
989
987
990 @util.propertycache
988 @util.propertycache
991 def _compressor(self):
989 def _compressor(self):
992 engine = util.compengines[self._compengine]
990 engine = util.compengines[self._compengine]
993 return engine.revlogcompressor(self._compengineopts)
991 return engine.revlogcompressor(self._compengineopts)
994
992
995 @util.propertycache
993 @util.propertycache
996 def _decompressor(self):
994 def _decompressor(self):
997 """the default decompressor"""
995 """the default decompressor"""
998 if self._docket is None:
996 if self._docket is None:
999 return None
997 return None
1000 t = self._docket.default_compression_header
998 t = self._docket.default_compression_header
1001 c = self._get_decompressor(t)
999 c = self._get_decompressor(t)
1002 return c.decompress
1000 return c.decompress
1003
1001
1004 def _indexfp(self):
1002 def _indexfp(self):
1005 """file object for the revlog's index file"""
1003 """file object for the revlog's index file"""
1006 return self.opener(self._indexfile, mode=b"r")
1004 return self.opener(self._indexfile, mode=b"r")
1007
1005
1008 def __index_write_fp(self):
1006 def __index_write_fp(self):
1009 # You should not use this directly and use `_writing` instead
1007 # You should not use this directly and use `_writing` instead
1010 try:
1008 try:
1011 f = self.opener(
1009 f = self.opener(
1012 self._indexfile, mode=b"r+", checkambig=self._checkambig
1010 self._indexfile, mode=b"r+", checkambig=self._checkambig
1013 )
1011 )
1014 if self._docket is None:
1012 if self._docket is None:
1015 f.seek(0, os.SEEK_END)
1013 f.seek(0, os.SEEK_END)
1016 else:
1014 else:
1017 f.seek(self._docket.index_end, os.SEEK_SET)
1015 f.seek(self._docket.index_end, os.SEEK_SET)
1018 return f
1016 return f
1019 except FileNotFoundError:
1017 except FileNotFoundError:
1020 return self.opener(
1018 return self.opener(
1021 self._indexfile, mode=b"w+", checkambig=self._checkambig
1019 self._indexfile, mode=b"w+", checkambig=self._checkambig
1022 )
1020 )
1023
1021
1024 def __index_new_fp(self):
1022 def __index_new_fp(self):
1025 # You should not use this unless you are upgrading from inline revlog
1023 # You should not use this unless you are upgrading from inline revlog
1026 return self.opener(
1024 return self.opener(
1027 self._indexfile,
1025 self._indexfile,
1028 mode=b"w",
1026 mode=b"w",
1029 checkambig=self._checkambig,
1027 checkambig=self._checkambig,
1030 atomictemp=True,
1028 atomictemp=True,
1031 )
1029 )
1032
1030
1033 def _datafp(self, mode=b'r'):
1031 def _datafp(self, mode=b'r'):
1034 """file object for the revlog's data file"""
1032 """file object for the revlog's data file"""
1035 return self.opener(self._datafile, mode=mode)
1033 return self.opener(self._datafile, mode=mode)
1036
1034
1037 @contextlib.contextmanager
1035 @contextlib.contextmanager
1038 def _sidedatareadfp(self):
1036 def _sidedatareadfp(self):
1039 """file object suitable to read sidedata"""
1037 """file object suitable to read sidedata"""
1040 if self._writinghandles:
1038 if self._writinghandles:
1041 yield self._writinghandles[2]
1039 yield self._writinghandles[2]
1042 else:
1040 else:
1043 with self.opener(self._sidedatafile) as fp:
1041 with self.opener(self._sidedatafile) as fp:
1044 yield fp
1042 yield fp
1045
1043
1046 def tiprev(self):
1044 def tiprev(self):
1047 return len(self.index) - 1
1045 return len(self.index) - 1
1048
1046
1049 def tip(self):
1047 def tip(self):
1050 return self.node(self.tiprev())
1048 return self.node(self.tiprev())
1051
1049
1052 def __contains__(self, rev):
1050 def __contains__(self, rev):
1053 return 0 <= rev < len(self)
1051 return 0 <= rev < len(self)
1054
1052
1055 def __len__(self):
1053 def __len__(self):
1056 return len(self.index)
1054 return len(self.index)
1057
1055
1058 def __iter__(self):
1056 def __iter__(self):
1059 return iter(range(len(self)))
1057 return iter(range(len(self)))
1060
1058
1061 def revs(self, start=0, stop=None):
1059 def revs(self, start=0, stop=None):
1062 """iterate over all rev in this revlog (from start to stop)"""
1060 """iterate over all rev in this revlog (from start to stop)"""
1063 return storageutil.iterrevs(len(self), start=start, stop=stop)
1061 return storageutil.iterrevs(len(self), start=start, stop=stop)
1064
1062
1065 def hasnode(self, node):
1063 def hasnode(self, node):
1066 try:
1064 try:
1067 self.rev(node)
1065 self.rev(node)
1068 return True
1066 return True
1069 except KeyError:
1067 except KeyError:
1070 return False
1068 return False
1071
1069
1072 def _candelta(self, baserev, rev):
1070 def _candelta(self, baserev, rev):
1073 """whether two revisions (baserev, rev) can be delta-ed or not"""
1071 """whether two revisions (baserev, rev) can be delta-ed or not"""
1074 # Disable delta if either rev requires a content-changing flag
1072 # Disable delta if either rev requires a content-changing flag
1075 # processor (ex. LFS). This is because such flag processor can alter
1073 # processor (ex. LFS). This is because such flag processor can alter
1076 # the rawtext content that the delta will be based on, and two clients
1074 # the rawtext content that the delta will be based on, and two clients
1077 # could have a same revlog node with different flags (i.e. different
1075 # could have a same revlog node with different flags (i.e. different
1078 # rawtext contents) and the delta could be incompatible.
1076 # rawtext contents) and the delta could be incompatible.
1079 if (self.flags(baserev) & REVIDX_RAWTEXT_CHANGING_FLAGS) or (
1077 if (self.flags(baserev) & REVIDX_RAWTEXT_CHANGING_FLAGS) or (
1080 self.flags(rev) & REVIDX_RAWTEXT_CHANGING_FLAGS
1078 self.flags(rev) & REVIDX_RAWTEXT_CHANGING_FLAGS
1081 ):
1079 ):
1082 return False
1080 return False
1083 return True
1081 return True
1084
1082
1085 def update_caches(self, transaction):
1083 def update_caches(self, transaction):
1086 """update on disk cache
1084 """update on disk cache
1087
1085
1088 If a transaction is passed, the update may be delayed to transaction
1086 If a transaction is passed, the update may be delayed to transaction
1089 commit."""
1087 commit."""
1090 if self._nodemap_file is not None:
1088 if self._nodemap_file is not None:
1091 if transaction is None:
1089 if transaction is None:
1092 nodemaputil.update_persistent_nodemap(self)
1090 nodemaputil.update_persistent_nodemap(self)
1093 else:
1091 else:
1094 nodemaputil.setup_persistent_nodemap(transaction, self)
1092 nodemaputil.setup_persistent_nodemap(transaction, self)
1095
1093
1096 def clearcaches(self):
1094 def clearcaches(self):
1097 """Clear in-memory caches"""
1095 """Clear in-memory caches"""
1098 self._revisioncache = None
1096 self._revisioncache = None
1099 self._chainbasecache.clear()
1097 self._chainbasecache.clear()
1100 self._segmentfile.clear_cache()
1098 self._segmentfile.clear_cache()
1101 self._segmentfile_sidedata.clear_cache()
1099 self._segmentfile_sidedata.clear_cache()
1102 self._pcache = {}
1100 self._pcache = {}
1103 self._nodemap_docket = None
1101 self._nodemap_docket = None
1104 self.index.clearcaches()
1102 self.index.clearcaches()
1105 # The python code is the one responsible for validating the docket, we
1103 # The python code is the one responsible for validating the docket, we
1106 # end up having to refresh it here.
1104 # end up having to refresh it here.
1107 use_nodemap = (
1105 use_nodemap = (
1108 not self._inline
1106 not self._inline
1109 and self._nodemap_file is not None
1107 and self._nodemap_file is not None
1110 and hasattr(self.index, 'update_nodemap_data')
1108 and hasattr(self.index, 'update_nodemap_data')
1111 )
1109 )
1112 if use_nodemap:
1110 if use_nodemap:
1113 nodemap_data = nodemaputil.persisted_data(self)
1111 nodemap_data = nodemaputil.persisted_data(self)
1114 if nodemap_data is not None:
1112 if nodemap_data is not None:
1115 self._nodemap_docket = nodemap_data[0]
1113 self._nodemap_docket = nodemap_data[0]
1116 self.index.update_nodemap_data(*nodemap_data)
1114 self.index.update_nodemap_data(*nodemap_data)
1117
1115
1118 def rev(self, node):
1116 def rev(self, node):
1119 """return the revision number associated with a <nodeid>"""
1117 """return the revision number associated with a <nodeid>"""
1120 try:
1118 try:
1121 return self.index.rev(node)
1119 return self.index.rev(node)
1122 except TypeError:
1120 except TypeError:
1123 raise
1121 raise
1124 except error.RevlogError:
1122 except error.RevlogError:
1125 # parsers.c radix tree lookup failed
1123 # parsers.c radix tree lookup failed
1126 if (
1124 if (
1127 node == self.nodeconstants.wdirid
1125 node == self.nodeconstants.wdirid
1128 or node in self.nodeconstants.wdirfilenodeids
1126 or node in self.nodeconstants.wdirfilenodeids
1129 ):
1127 ):
1130 raise error.WdirUnsupported
1128 raise error.WdirUnsupported
1131 raise error.LookupError(node, self.display_id, _(b'no node'))
1129 raise error.LookupError(node, self.display_id, _(b'no node'))
1132
1130
1133 # Accessors for index entries.
1131 # Accessors for index entries.
1134
1132
1135 # First tuple entry is 8 bytes. First 6 bytes are offset. Last 2 bytes
1133 # First tuple entry is 8 bytes. First 6 bytes are offset. Last 2 bytes
1136 # are flags.
1134 # are flags.
1137 def start(self, rev):
1135 def start(self, rev):
1138 return int(self.index[rev][0] >> 16)
1136 return int(self.index[rev][0] >> 16)
1139
1137
1140 def sidedata_cut_off(self, rev):
1138 def sidedata_cut_off(self, rev):
1141 sd_cut_off = self.index[rev][8]
1139 sd_cut_off = self.index[rev][8]
1142 if sd_cut_off != 0:
1140 if sd_cut_off != 0:
1143 return sd_cut_off
1141 return sd_cut_off
1144 # This is some annoying dance, because entries without sidedata
1142 # This is some annoying dance, because entries without sidedata
1145 # currently use 0 as their ofsset. (instead of previous-offset +
1143 # currently use 0 as their ofsset. (instead of previous-offset +
1146 # previous-size)
1144 # previous-size)
1147 #
1145 #
1148 # We should reconsider this sidedata β†’ 0 sidata_offset policy.
1146 # We should reconsider this sidedata β†’ 0 sidata_offset policy.
1149 # In the meantime, we need this.
1147 # In the meantime, we need this.
1150 while 0 <= rev:
1148 while 0 <= rev:
1151 e = self.index[rev]
1149 e = self.index[rev]
1152 if e[9] != 0:
1150 if e[9] != 0:
1153 return e[8] + e[9]
1151 return e[8] + e[9]
1154 rev -= 1
1152 rev -= 1
1155 return 0
1153 return 0
1156
1154
1157 def flags(self, rev):
1155 def flags(self, rev):
1158 return self.index[rev][0] & 0xFFFF
1156 return self.index[rev][0] & 0xFFFF
1159
1157
1160 def length(self, rev):
1158 def length(self, rev):
1161 return self.index[rev][1]
1159 return self.index[rev][1]
1162
1160
1163 def sidedata_length(self, rev):
1161 def sidedata_length(self, rev):
1164 if not self.hassidedata:
1162 if not self.hassidedata:
1165 return 0
1163 return 0
1166 return self.index[rev][9]
1164 return self.index[rev][9]
1167
1165
1168 def rawsize(self, rev):
1166 def rawsize(self, rev):
1169 """return the length of the uncompressed text for a given revision"""
1167 """return the length of the uncompressed text for a given revision"""
1170 l = self.index[rev][2]
1168 l = self.index[rev][2]
1171 if l >= 0:
1169 if l >= 0:
1172 return l
1170 return l
1173
1171
1174 t = self.rawdata(rev)
1172 t = self.rawdata(rev)
1175 return len(t)
1173 return len(t)
1176
1174
1177 def size(self, rev):
1175 def size(self, rev):
1178 """length of non-raw text (processed by a "read" flag processor)"""
1176 """length of non-raw text (processed by a "read" flag processor)"""
1179 # fast path: if no "read" flag processor could change the content,
1177 # fast path: if no "read" flag processor could change the content,
1180 # size is rawsize. note: ELLIPSIS is known to not change the content.
1178 # size is rawsize. note: ELLIPSIS is known to not change the content.
1181 flags = self.flags(rev)
1179 flags = self.flags(rev)
1182 if flags & (flagutil.REVIDX_KNOWN_FLAGS ^ REVIDX_ELLIPSIS) == 0:
1180 if flags & (flagutil.REVIDX_KNOWN_FLAGS ^ REVIDX_ELLIPSIS) == 0:
1183 return self.rawsize(rev)
1181 return self.rawsize(rev)
1184
1182
1185 return len(self.revision(rev))
1183 return len(self.revision(rev))
1186
1184
1187 def fast_rank(self, rev):
1185 def fast_rank(self, rev):
1188 """Return the rank of a revision if already known, or None otherwise.
1186 """Return the rank of a revision if already known, or None otherwise.
1189
1187
1190 The rank of a revision is the size of the sub-graph it defines as a
1188 The rank of a revision is the size of the sub-graph it defines as a
1191 head. Equivalently, the rank of a revision `r` is the size of the set
1189 head. Equivalently, the rank of a revision `r` is the size of the set
1192 `ancestors(r)`, `r` included.
1190 `ancestors(r)`, `r` included.
1193
1191
1194 This method returns the rank retrieved from the revlog in constant
1192 This method returns the rank retrieved from the revlog in constant
1195 time. It makes no attempt at computing unknown values for versions of
1193 time. It makes no attempt at computing unknown values for versions of
1196 the revlog which do not persist the rank.
1194 the revlog which do not persist the rank.
1197 """
1195 """
1198 rank = self.index[rev][ENTRY_RANK]
1196 rank = self.index[rev][ENTRY_RANK]
1199 if self._format_version != CHANGELOGV2 or rank == RANK_UNKNOWN:
1197 if self._format_version != CHANGELOGV2 or rank == RANK_UNKNOWN:
1200 return None
1198 return None
1201 if rev == nullrev:
1199 if rev == nullrev:
1202 return 0 # convention
1200 return 0 # convention
1203 return rank
1201 return rank
1204
1202
1205 def chainbase(self, rev):
1203 def chainbase(self, rev):
1206 base = self._chainbasecache.get(rev)
1204 base = self._chainbasecache.get(rev)
1207 if base is not None:
1205 if base is not None:
1208 return base
1206 return base
1209
1207
1210 index = self.index
1208 index = self.index
1211 iterrev = rev
1209 iterrev = rev
1212 base = index[iterrev][3]
1210 base = index[iterrev][3]
1213 while base != iterrev:
1211 while base != iterrev:
1214 iterrev = base
1212 iterrev = base
1215 base = index[iterrev][3]
1213 base = index[iterrev][3]
1216
1214
1217 self._chainbasecache[rev] = base
1215 self._chainbasecache[rev] = base
1218 return base
1216 return base
1219
1217
1220 def linkrev(self, rev):
1218 def linkrev(self, rev):
1221 return self.index[rev][4]
1219 return self.index[rev][4]
1222
1220
1223 def parentrevs(self, rev):
1221 def parentrevs(self, rev):
1224 try:
1222 try:
1225 entry = self.index[rev]
1223 entry = self.index[rev]
1226 except IndexError:
1224 except IndexError:
1227 if rev == wdirrev:
1225 if rev == wdirrev:
1228 raise error.WdirUnsupported
1226 raise error.WdirUnsupported
1229 raise
1227 raise
1230
1228
1231 if self.canonical_parent_order and entry[5] == nullrev:
1229 if self.canonical_parent_order and entry[5] == nullrev:
1232 return entry[6], entry[5]
1230 return entry[6], entry[5]
1233 else:
1231 else:
1234 return entry[5], entry[6]
1232 return entry[5], entry[6]
1235
1233
1236 # fast parentrevs(rev) where rev isn't filtered
1234 # fast parentrevs(rev) where rev isn't filtered
1237 _uncheckedparentrevs = parentrevs
1235 _uncheckedparentrevs = parentrevs
1238
1236
1239 def node(self, rev):
1237 def node(self, rev):
1240 try:
1238 try:
1241 return self.index[rev][7]
1239 return self.index[rev][7]
1242 except IndexError:
1240 except IndexError:
1243 if rev == wdirrev:
1241 if rev == wdirrev:
1244 raise error.WdirUnsupported
1242 raise error.WdirUnsupported
1245 raise
1243 raise
1246
1244
1247 # Derived from index values.
1245 # Derived from index values.
1248
1246
1249 def end(self, rev):
1247 def end(self, rev):
1250 return self.start(rev) + self.length(rev)
1248 return self.start(rev) + self.length(rev)
1251
1249
1252 def parents(self, node):
1250 def parents(self, node):
1253 i = self.index
1251 i = self.index
1254 d = i[self.rev(node)]
1252 d = i[self.rev(node)]
1255 # inline node() to avoid function call overhead
1253 # inline node() to avoid function call overhead
1256 if self.canonical_parent_order and d[5] == self.nullid:
1254 if self.canonical_parent_order and d[5] == self.nullid:
1257 return i[d[6]][7], i[d[5]][7]
1255 return i[d[6]][7], i[d[5]][7]
1258 else:
1256 else:
1259 return i[d[5]][7], i[d[6]][7]
1257 return i[d[5]][7], i[d[6]][7]
1260
1258
1261 def chainlen(self, rev):
1259 def chainlen(self, rev):
1262 return self._chaininfo(rev)[0]
1260 return self._chaininfo(rev)[0]
1263
1261
1264 def _chaininfo(self, rev):
1262 def _chaininfo(self, rev):
1265 chaininfocache = self._chaininfocache
1263 chaininfocache = self._chaininfocache
1266 if rev in chaininfocache:
1264 if rev in chaininfocache:
1267 return chaininfocache[rev]
1265 return chaininfocache[rev]
1268 index = self.index
1266 index = self.index
1269 generaldelta = self._generaldelta
1267 generaldelta = self._generaldelta
1270 iterrev = rev
1268 iterrev = rev
1271 e = index[iterrev]
1269 e = index[iterrev]
1272 clen = 0
1270 clen = 0
1273 compresseddeltalen = 0
1271 compresseddeltalen = 0
1274 while iterrev != e[3]:
1272 while iterrev != e[3]:
1275 clen += 1
1273 clen += 1
1276 compresseddeltalen += e[1]
1274 compresseddeltalen += e[1]
1277 if generaldelta:
1275 if generaldelta:
1278 iterrev = e[3]
1276 iterrev = e[3]
1279 else:
1277 else:
1280 iterrev -= 1
1278 iterrev -= 1
1281 if iterrev in chaininfocache:
1279 if iterrev in chaininfocache:
1282 t = chaininfocache[iterrev]
1280 t = chaininfocache[iterrev]
1283 clen += t[0]
1281 clen += t[0]
1284 compresseddeltalen += t[1]
1282 compresseddeltalen += t[1]
1285 break
1283 break
1286 e = index[iterrev]
1284 e = index[iterrev]
1287 else:
1285 else:
1288 # Add text length of base since decompressing that also takes
1286 # Add text length of base since decompressing that also takes
1289 # work. For cache hits the length is already included.
1287 # work. For cache hits the length is already included.
1290 compresseddeltalen += e[1]
1288 compresseddeltalen += e[1]
1291 r = (clen, compresseddeltalen)
1289 r = (clen, compresseddeltalen)
1292 chaininfocache[rev] = r
1290 chaininfocache[rev] = r
1293 return r
1291 return r
1294
1292
1295 def _deltachain(self, rev, stoprev=None):
1293 def _deltachain(self, rev, stoprev=None):
1296 """Obtain the delta chain for a revision.
1294 """Obtain the delta chain for a revision.
1297
1295
1298 ``stoprev`` specifies a revision to stop at. If not specified, we
1296 ``stoprev`` specifies a revision to stop at. If not specified, we
1299 stop at the base of the chain.
1297 stop at the base of the chain.
1300
1298
1301 Returns a 2-tuple of (chain, stopped) where ``chain`` is a list of
1299 Returns a 2-tuple of (chain, stopped) where ``chain`` is a list of
1302 revs in ascending order and ``stopped`` is a bool indicating whether
1300 revs in ascending order and ``stopped`` is a bool indicating whether
1303 ``stoprev`` was hit.
1301 ``stoprev`` was hit.
1304 """
1302 """
1305 # Try C implementation.
1303 # Try C implementation.
1306 try:
1304 try:
1307 return self.index.deltachain(rev, stoprev, self._generaldelta)
1305 return self.index.deltachain(rev, stoprev, self._generaldelta)
1308 except AttributeError:
1306 except AttributeError:
1309 pass
1307 pass
1310
1308
1311 chain = []
1309 chain = []
1312
1310
1313 # Alias to prevent attribute lookup in tight loop.
1311 # Alias to prevent attribute lookup in tight loop.
1314 index = self.index
1312 index = self.index
1315 generaldelta = self._generaldelta
1313 generaldelta = self._generaldelta
1316
1314
1317 iterrev = rev
1315 iterrev = rev
1318 e = index[iterrev]
1316 e = index[iterrev]
1319 while iterrev != e[3] and iterrev != stoprev:
1317 while iterrev != e[3] and iterrev != stoprev:
1320 chain.append(iterrev)
1318 chain.append(iterrev)
1321 if generaldelta:
1319 if generaldelta:
1322 iterrev = e[3]
1320 iterrev = e[3]
1323 else:
1321 else:
1324 iterrev -= 1
1322 iterrev -= 1
1325 e = index[iterrev]
1323 e = index[iterrev]
1326
1324
1327 if iterrev == stoprev:
1325 if iterrev == stoprev:
1328 stopped = True
1326 stopped = True
1329 else:
1327 else:
1330 chain.append(iterrev)
1328 chain.append(iterrev)
1331 stopped = False
1329 stopped = False
1332
1330
1333 chain.reverse()
1331 chain.reverse()
1334 return chain, stopped
1332 return chain, stopped
1335
1333
1336 def ancestors(self, revs, stoprev=0, inclusive=False):
1334 def ancestors(self, revs, stoprev=0, inclusive=False):
1337 """Generate the ancestors of 'revs' in reverse revision order.
1335 """Generate the ancestors of 'revs' in reverse revision order.
1338 Does not generate revs lower than stoprev.
1336 Does not generate revs lower than stoprev.
1339
1337
1340 See the documentation for ancestor.lazyancestors for more details."""
1338 See the documentation for ancestor.lazyancestors for more details."""
1341
1339
1342 # first, make sure start revisions aren't filtered
1340 # first, make sure start revisions aren't filtered
1343 revs = list(revs)
1341 revs = list(revs)
1344 checkrev = self.node
1342 checkrev = self.node
1345 for r in revs:
1343 for r in revs:
1346 checkrev(r)
1344 checkrev(r)
1347 # and we're sure ancestors aren't filtered as well
1345 # and we're sure ancestors aren't filtered as well
1348
1346
1349 if rustancestor is not None and self.index.rust_ext_compat:
1347 if rustancestor is not None and self.index.rust_ext_compat:
1350 lazyancestors = rustancestor.LazyAncestors
1348 lazyancestors = rustancestor.LazyAncestors
1351 arg = self.index
1349 arg = self.index
1352 else:
1350 else:
1353 lazyancestors = ancestor.lazyancestors
1351 lazyancestors = ancestor.lazyancestors
1354 arg = self._uncheckedparentrevs
1352 arg = self._uncheckedparentrevs
1355 return lazyancestors(arg, revs, stoprev=stoprev, inclusive=inclusive)
1353 return lazyancestors(arg, revs, stoprev=stoprev, inclusive=inclusive)
1356
1354
1357 def descendants(self, revs):
1355 def descendants(self, revs):
1358 return dagop.descendantrevs(revs, self.revs, self.parentrevs)
1356 return dagop.descendantrevs(revs, self.revs, self.parentrevs)
1359
1357
1360 def findcommonmissing(self, common=None, heads=None):
1358 def findcommonmissing(self, common=None, heads=None):
1361 """Return a tuple of the ancestors of common and the ancestors of heads
1359 """Return a tuple of the ancestors of common and the ancestors of heads
1362 that are not ancestors of common. In revset terminology, we return the
1360 that are not ancestors of common. In revset terminology, we return the
1363 tuple:
1361 tuple:
1364
1362
1365 ::common, (::heads) - (::common)
1363 ::common, (::heads) - (::common)
1366
1364
1367 The list is sorted by revision number, meaning it is
1365 The list is sorted by revision number, meaning it is
1368 topologically sorted.
1366 topologically sorted.
1369
1367
1370 'heads' and 'common' are both lists of node IDs. If heads is
1368 'heads' and 'common' are both lists of node IDs. If heads is
1371 not supplied, uses all of the revlog's heads. If common is not
1369 not supplied, uses all of the revlog's heads. If common is not
1372 supplied, uses nullid."""
1370 supplied, uses nullid."""
1373 if common is None:
1371 if common is None:
1374 common = [self.nullid]
1372 common = [self.nullid]
1375 if heads is None:
1373 if heads is None:
1376 heads = self.heads()
1374 heads = self.heads()
1377
1375
1378 common = [self.rev(n) for n in common]
1376 common = [self.rev(n) for n in common]
1379 heads = [self.rev(n) for n in heads]
1377 heads = [self.rev(n) for n in heads]
1380
1378
1381 # we want the ancestors, but inclusive
1379 # we want the ancestors, but inclusive
1382 class lazyset:
1380 class lazyset:
1383 def __init__(self, lazyvalues):
1381 def __init__(self, lazyvalues):
1384 self.addedvalues = set()
1382 self.addedvalues = set()
1385 self.lazyvalues = lazyvalues
1383 self.lazyvalues = lazyvalues
1386
1384
1387 def __contains__(self, value):
1385 def __contains__(self, value):
1388 return value in self.addedvalues or value in self.lazyvalues
1386 return value in self.addedvalues or value in self.lazyvalues
1389
1387
1390 def __iter__(self):
1388 def __iter__(self):
1391 added = self.addedvalues
1389 added = self.addedvalues
1392 for r in added:
1390 for r in added:
1393 yield r
1391 yield r
1394 for r in self.lazyvalues:
1392 for r in self.lazyvalues:
1395 if not r in added:
1393 if not r in added:
1396 yield r
1394 yield r
1397
1395
1398 def add(self, value):
1396 def add(self, value):
1399 self.addedvalues.add(value)
1397 self.addedvalues.add(value)
1400
1398
1401 def update(self, values):
1399 def update(self, values):
1402 self.addedvalues.update(values)
1400 self.addedvalues.update(values)
1403
1401
1404 has = lazyset(self.ancestors(common))
1402 has = lazyset(self.ancestors(common))
1405 has.add(nullrev)
1403 has.add(nullrev)
1406 has.update(common)
1404 has.update(common)
1407
1405
1408 # take all ancestors from heads that aren't in has
1406 # take all ancestors from heads that aren't in has
1409 missing = set()
1407 missing = set()
1410 visit = collections.deque(r for r in heads if r not in has)
1408 visit = collections.deque(r for r in heads if r not in has)
1411 while visit:
1409 while visit:
1412 r = visit.popleft()
1410 r = visit.popleft()
1413 if r in missing:
1411 if r in missing:
1414 continue
1412 continue
1415 else:
1413 else:
1416 missing.add(r)
1414 missing.add(r)
1417 for p in self.parentrevs(r):
1415 for p in self.parentrevs(r):
1418 if p not in has:
1416 if p not in has:
1419 visit.append(p)
1417 visit.append(p)
1420 missing = list(missing)
1418 missing = list(missing)
1421 missing.sort()
1419 missing.sort()
1422 return has, [self.node(miss) for miss in missing]
1420 return has, [self.node(miss) for miss in missing]
1423
1421
1424 def incrementalmissingrevs(self, common=None):
1422 def incrementalmissingrevs(self, common=None):
1425 """Return an object that can be used to incrementally compute the
1423 """Return an object that can be used to incrementally compute the
1426 revision numbers of the ancestors of arbitrary sets that are not
1424 revision numbers of the ancestors of arbitrary sets that are not
1427 ancestors of common. This is an ancestor.incrementalmissingancestors
1425 ancestors of common. This is an ancestor.incrementalmissingancestors
1428 object.
1426 object.
1429
1427
1430 'common' is a list of revision numbers. If common is not supplied, uses
1428 'common' is a list of revision numbers. If common is not supplied, uses
1431 nullrev.
1429 nullrev.
1432 """
1430 """
1433 if common is None:
1431 if common is None:
1434 common = [nullrev]
1432 common = [nullrev]
1435
1433
1436 if rustancestor is not None and self.index.rust_ext_compat:
1434 if rustancestor is not None and self.index.rust_ext_compat:
1437 return rustancestor.MissingAncestors(self.index, common)
1435 return rustancestor.MissingAncestors(self.index, common)
1438 return ancestor.incrementalmissingancestors(self.parentrevs, common)
1436 return ancestor.incrementalmissingancestors(self.parentrevs, common)
1439
1437
1440 def findmissingrevs(self, common=None, heads=None):
1438 def findmissingrevs(self, common=None, heads=None):
1441 """Return the revision numbers of the ancestors of heads that
1439 """Return the revision numbers of the ancestors of heads that
1442 are not ancestors of common.
1440 are not ancestors of common.
1443
1441
1444 More specifically, return a list of revision numbers corresponding to
1442 More specifically, return a list of revision numbers corresponding to
1445 nodes N such that every N satisfies the following constraints:
1443 nodes N such that every N satisfies the following constraints:
1446
1444
1447 1. N is an ancestor of some node in 'heads'
1445 1. N is an ancestor of some node in 'heads'
1448 2. N is not an ancestor of any node in 'common'
1446 2. N is not an ancestor of any node in 'common'
1449
1447
1450 The list is sorted by revision number, meaning it is
1448 The list is sorted by revision number, meaning it is
1451 topologically sorted.
1449 topologically sorted.
1452
1450
1453 'heads' and 'common' are both lists of revision numbers. If heads is
1451 'heads' and 'common' are both lists of revision numbers. If heads is
1454 not supplied, uses all of the revlog's heads. If common is not
1452 not supplied, uses all of the revlog's heads. If common is not
1455 supplied, uses nullid."""
1453 supplied, uses nullid."""
1456 if common is None:
1454 if common is None:
1457 common = [nullrev]
1455 common = [nullrev]
1458 if heads is None:
1456 if heads is None:
1459 heads = self.headrevs()
1457 heads = self.headrevs()
1460
1458
1461 inc = self.incrementalmissingrevs(common=common)
1459 inc = self.incrementalmissingrevs(common=common)
1462 return inc.missingancestors(heads)
1460 return inc.missingancestors(heads)
1463
1461
1464 def findmissing(self, common=None, heads=None):
1462 def findmissing(self, common=None, heads=None):
1465 """Return the ancestors of heads that are not ancestors of common.
1463 """Return the ancestors of heads that are not ancestors of common.
1466
1464
1467 More specifically, return a list of nodes N such that every N
1465 More specifically, return a list of nodes N such that every N
1468 satisfies the following constraints:
1466 satisfies the following constraints:
1469
1467
1470 1. N is an ancestor of some node in 'heads'
1468 1. N is an ancestor of some node in 'heads'
1471 2. N is not an ancestor of any node in 'common'
1469 2. N is not an ancestor of any node in 'common'
1472
1470
1473 The list is sorted by revision number, meaning it is
1471 The list is sorted by revision number, meaning it is
1474 topologically sorted.
1472 topologically sorted.
1475
1473
1476 'heads' and 'common' are both lists of node IDs. If heads is
1474 'heads' and 'common' are both lists of node IDs. If heads is
1477 not supplied, uses all of the revlog's heads. If common is not
1475 not supplied, uses all of the revlog's heads. If common is not
1478 supplied, uses nullid."""
1476 supplied, uses nullid."""
1479 if common is None:
1477 if common is None:
1480 common = [self.nullid]
1478 common = [self.nullid]
1481 if heads is None:
1479 if heads is None:
1482 heads = self.heads()
1480 heads = self.heads()
1483
1481
1484 common = [self.rev(n) for n in common]
1482 common = [self.rev(n) for n in common]
1485 heads = [self.rev(n) for n in heads]
1483 heads = [self.rev(n) for n in heads]
1486
1484
1487 inc = self.incrementalmissingrevs(common=common)
1485 inc = self.incrementalmissingrevs(common=common)
1488 return [self.node(r) for r in inc.missingancestors(heads)]
1486 return [self.node(r) for r in inc.missingancestors(heads)]
1489
1487
1490 def nodesbetween(self, roots=None, heads=None):
1488 def nodesbetween(self, roots=None, heads=None):
1491 """Return a topological path from 'roots' to 'heads'.
1489 """Return a topological path from 'roots' to 'heads'.
1492
1490
1493 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
1491 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
1494 topologically sorted list of all nodes N that satisfy both of
1492 topologically sorted list of all nodes N that satisfy both of
1495 these constraints:
1493 these constraints:
1496
1494
1497 1. N is a descendant of some node in 'roots'
1495 1. N is a descendant of some node in 'roots'
1498 2. N is an ancestor of some node in 'heads'
1496 2. N is an ancestor of some node in 'heads'
1499
1497
1500 Every node is considered to be both a descendant and an ancestor
1498 Every node is considered to be both a descendant and an ancestor
1501 of itself, so every reachable node in 'roots' and 'heads' will be
1499 of itself, so every reachable node in 'roots' and 'heads' will be
1502 included in 'nodes'.
1500 included in 'nodes'.
1503
1501
1504 'outroots' is the list of reachable nodes in 'roots', i.e., the
1502 'outroots' is the list of reachable nodes in 'roots', i.e., the
1505 subset of 'roots' that is returned in 'nodes'. Likewise,
1503 subset of 'roots' that is returned in 'nodes'. Likewise,
1506 'outheads' is the subset of 'heads' that is also in 'nodes'.
1504 'outheads' is the subset of 'heads' that is also in 'nodes'.
1507
1505
1508 'roots' and 'heads' are both lists of node IDs. If 'roots' is
1506 'roots' and 'heads' are both lists of node IDs. If 'roots' is
1509 unspecified, uses nullid as the only root. If 'heads' is
1507 unspecified, uses nullid as the only root. If 'heads' is
1510 unspecified, uses list of all of the revlog's heads."""
1508 unspecified, uses list of all of the revlog's heads."""
1511 nonodes = ([], [], [])
1509 nonodes = ([], [], [])
1512 if roots is not None:
1510 if roots is not None:
1513 roots = list(roots)
1511 roots = list(roots)
1514 if not roots:
1512 if not roots:
1515 return nonodes
1513 return nonodes
1516 lowestrev = min([self.rev(n) for n in roots])
1514 lowestrev = min([self.rev(n) for n in roots])
1517 else:
1515 else:
1518 roots = [self.nullid] # Everybody's a descendant of nullid
1516 roots = [self.nullid] # Everybody's a descendant of nullid
1519 lowestrev = nullrev
1517 lowestrev = nullrev
1520 if (lowestrev == nullrev) and (heads is None):
1518 if (lowestrev == nullrev) and (heads is None):
1521 # We want _all_ the nodes!
1519 # We want _all_ the nodes!
1522 return (
1520 return (
1523 [self.node(r) for r in self],
1521 [self.node(r) for r in self],
1524 [self.nullid],
1522 [self.nullid],
1525 list(self.heads()),
1523 list(self.heads()),
1526 )
1524 )
1527 if heads is None:
1525 if heads is None:
1528 # All nodes are ancestors, so the latest ancestor is the last
1526 # All nodes are ancestors, so the latest ancestor is the last
1529 # node.
1527 # node.
1530 highestrev = len(self) - 1
1528 highestrev = len(self) - 1
1531 # Set ancestors to None to signal that every node is an ancestor.
1529 # Set ancestors to None to signal that every node is an ancestor.
1532 ancestors = None
1530 ancestors = None
1533 # Set heads to an empty dictionary for later discovery of heads
1531 # Set heads to an empty dictionary for later discovery of heads
1534 heads = {}
1532 heads = {}
1535 else:
1533 else:
1536 heads = list(heads)
1534 heads = list(heads)
1537 if not heads:
1535 if not heads:
1538 return nonodes
1536 return nonodes
1539 ancestors = set()
1537 ancestors = set()
1540 # Turn heads into a dictionary so we can remove 'fake' heads.
1538 # Turn heads into a dictionary so we can remove 'fake' heads.
1541 # Also, later we will be using it to filter out the heads we can't
1539 # Also, later we will be using it to filter out the heads we can't
1542 # find from roots.
1540 # find from roots.
1543 heads = dict.fromkeys(heads, False)
1541 heads = dict.fromkeys(heads, False)
1544 # Start at the top and keep marking parents until we're done.
1542 # Start at the top and keep marking parents until we're done.
1545 nodestotag = set(heads)
1543 nodestotag = set(heads)
1546 # Remember where the top was so we can use it as a limit later.
1544 # Remember where the top was so we can use it as a limit later.
1547 highestrev = max([self.rev(n) for n in nodestotag])
1545 highestrev = max([self.rev(n) for n in nodestotag])
1548 while nodestotag:
1546 while nodestotag:
1549 # grab a node to tag
1547 # grab a node to tag
1550 n = nodestotag.pop()
1548 n = nodestotag.pop()
1551 # Never tag nullid
1549 # Never tag nullid
1552 if n == self.nullid:
1550 if n == self.nullid:
1553 continue
1551 continue
1554 # A node's revision number represents its place in a
1552 # A node's revision number represents its place in a
1555 # topologically sorted list of nodes.
1553 # topologically sorted list of nodes.
1556 r = self.rev(n)
1554 r = self.rev(n)
1557 if r >= lowestrev:
1555 if r >= lowestrev:
1558 if n not in ancestors:
1556 if n not in ancestors:
1559 # If we are possibly a descendant of one of the roots
1557 # If we are possibly a descendant of one of the roots
1560 # and we haven't already been marked as an ancestor
1558 # and we haven't already been marked as an ancestor
1561 ancestors.add(n) # Mark as ancestor
1559 ancestors.add(n) # Mark as ancestor
1562 # Add non-nullid parents to list of nodes to tag.
1560 # Add non-nullid parents to list of nodes to tag.
1563 nodestotag.update(
1561 nodestotag.update(
1564 [p for p in self.parents(n) if p != self.nullid]
1562 [p for p in self.parents(n) if p != self.nullid]
1565 )
1563 )
1566 elif n in heads: # We've seen it before, is it a fake head?
1564 elif n in heads: # We've seen it before, is it a fake head?
1567 # So it is, real heads should not be the ancestors of
1565 # So it is, real heads should not be the ancestors of
1568 # any other heads.
1566 # any other heads.
1569 heads.pop(n)
1567 heads.pop(n)
1570 if not ancestors:
1568 if not ancestors:
1571 return nonodes
1569 return nonodes
1572 # Now that we have our set of ancestors, we want to remove any
1570 # Now that we have our set of ancestors, we want to remove any
1573 # roots that are not ancestors.
1571 # roots that are not ancestors.
1574
1572
1575 # If one of the roots was nullid, everything is included anyway.
1573 # If one of the roots was nullid, everything is included anyway.
1576 if lowestrev > nullrev:
1574 if lowestrev > nullrev:
1577 # But, since we weren't, let's recompute the lowest rev to not
1575 # But, since we weren't, let's recompute the lowest rev to not
1578 # include roots that aren't ancestors.
1576 # include roots that aren't ancestors.
1579
1577
1580 # Filter out roots that aren't ancestors of heads
1578 # Filter out roots that aren't ancestors of heads
1581 roots = [root for root in roots if root in ancestors]
1579 roots = [root for root in roots if root in ancestors]
1582 # Recompute the lowest revision
1580 # Recompute the lowest revision
1583 if roots:
1581 if roots:
1584 lowestrev = min([self.rev(root) for root in roots])
1582 lowestrev = min([self.rev(root) for root in roots])
1585 else:
1583 else:
1586 # No more roots? Return empty list
1584 # No more roots? Return empty list
1587 return nonodes
1585 return nonodes
1588 else:
1586 else:
1589 # We are descending from nullid, and don't need to care about
1587 # We are descending from nullid, and don't need to care about
1590 # any other roots.
1588 # any other roots.
1591 lowestrev = nullrev
1589 lowestrev = nullrev
1592 roots = [self.nullid]
1590 roots = [self.nullid]
1593 # Transform our roots list into a set.
1591 # Transform our roots list into a set.
1594 descendants = set(roots)
1592 descendants = set(roots)
1595 # Also, keep the original roots so we can filter out roots that aren't
1593 # Also, keep the original roots so we can filter out roots that aren't
1596 # 'real' roots (i.e. are descended from other roots).
1594 # 'real' roots (i.e. are descended from other roots).
1597 roots = descendants.copy()
1595 roots = descendants.copy()
1598 # Our topologically sorted list of output nodes.
1596 # Our topologically sorted list of output nodes.
1599 orderedout = []
1597 orderedout = []
1600 # Don't start at nullid since we don't want nullid in our output list,
1598 # Don't start at nullid since we don't want nullid in our output list,
1601 # and if nullid shows up in descendants, empty parents will look like
1599 # and if nullid shows up in descendants, empty parents will look like
1602 # they're descendants.
1600 # they're descendants.
1603 for r in self.revs(start=max(lowestrev, 0), stop=highestrev + 1):
1601 for r in self.revs(start=max(lowestrev, 0), stop=highestrev + 1):
1604 n = self.node(r)
1602 n = self.node(r)
1605 isdescendant = False
1603 isdescendant = False
1606 if lowestrev == nullrev: # Everybody is a descendant of nullid
1604 if lowestrev == nullrev: # Everybody is a descendant of nullid
1607 isdescendant = True
1605 isdescendant = True
1608 elif n in descendants:
1606 elif n in descendants:
1609 # n is already a descendant
1607 # n is already a descendant
1610 isdescendant = True
1608 isdescendant = True
1611 # This check only needs to be done here because all the roots
1609 # This check only needs to be done here because all the roots
1612 # will start being marked is descendants before the loop.
1610 # will start being marked is descendants before the loop.
1613 if n in roots:
1611 if n in roots:
1614 # If n was a root, check if it's a 'real' root.
1612 # If n was a root, check if it's a 'real' root.
1615 p = tuple(self.parents(n))
1613 p = tuple(self.parents(n))
1616 # If any of its parents are descendants, it's not a root.
1614 # If any of its parents are descendants, it's not a root.
1617 if (p[0] in descendants) or (p[1] in descendants):
1615 if (p[0] in descendants) or (p[1] in descendants):
1618 roots.remove(n)
1616 roots.remove(n)
1619 else:
1617 else:
1620 p = tuple(self.parents(n))
1618 p = tuple(self.parents(n))
1621 # A node is a descendant if either of its parents are
1619 # A node is a descendant if either of its parents are
1622 # descendants. (We seeded the dependents list with the roots
1620 # descendants. (We seeded the dependents list with the roots
1623 # up there, remember?)
1621 # up there, remember?)
1624 if (p[0] in descendants) or (p[1] in descendants):
1622 if (p[0] in descendants) or (p[1] in descendants):
1625 descendants.add(n)
1623 descendants.add(n)
1626 isdescendant = True
1624 isdescendant = True
1627 if isdescendant and ((ancestors is None) or (n in ancestors)):
1625 if isdescendant and ((ancestors is None) or (n in ancestors)):
1628 # Only include nodes that are both descendants and ancestors.
1626 # Only include nodes that are both descendants and ancestors.
1629 orderedout.append(n)
1627 orderedout.append(n)
1630 if (ancestors is not None) and (n in heads):
1628 if (ancestors is not None) and (n in heads):
1631 # We're trying to figure out which heads are reachable
1629 # We're trying to figure out which heads are reachable
1632 # from roots.
1630 # from roots.
1633 # Mark this head as having been reached
1631 # Mark this head as having been reached
1634 heads[n] = True
1632 heads[n] = True
1635 elif ancestors is None:
1633 elif ancestors is None:
1636 # Otherwise, we're trying to discover the heads.
1634 # Otherwise, we're trying to discover the heads.
1637 # Assume this is a head because if it isn't, the next step
1635 # Assume this is a head because if it isn't, the next step
1638 # will eventually remove it.
1636 # will eventually remove it.
1639 heads[n] = True
1637 heads[n] = True
1640 # But, obviously its parents aren't.
1638 # But, obviously its parents aren't.
1641 for p in self.parents(n):
1639 for p in self.parents(n):
1642 heads.pop(p, None)
1640 heads.pop(p, None)
1643 heads = [head for head, flag in heads.items() if flag]
1641 heads = [head for head, flag in heads.items() if flag]
1644 roots = list(roots)
1642 roots = list(roots)
1645 assert orderedout
1643 assert orderedout
1646 assert roots
1644 assert roots
1647 assert heads
1645 assert heads
1648 return (orderedout, roots, heads)
1646 return (orderedout, roots, heads)
1649
1647
1650 def headrevs(self, revs=None):
1648 def headrevs(self, revs=None):
1651 if revs is None:
1649 if revs is None:
1652 try:
1650 try:
1653 return self.index.headrevs()
1651 return self.index.headrevs()
1654 except AttributeError:
1652 except AttributeError:
1655 return self._headrevs()
1653 return self._headrevs()
1656 if rustdagop is not None and self.index.rust_ext_compat:
1654 if rustdagop is not None and self.index.rust_ext_compat:
1657 return rustdagop.headrevs(self.index, revs)
1655 return rustdagop.headrevs(self.index, revs)
1658 return dagop.headrevs(revs, self._uncheckedparentrevs)
1656 return dagop.headrevs(revs, self._uncheckedparentrevs)
1659
1657
1660 def computephases(self, roots):
1658 def computephases(self, roots):
1661 return self.index.computephasesmapsets(roots)
1659 return self.index.computephasesmapsets(roots)
1662
1660
1663 def _headrevs(self):
1661 def _headrevs(self):
1664 count = len(self)
1662 count = len(self)
1665 if not count:
1663 if not count:
1666 return [nullrev]
1664 return [nullrev]
1667 # we won't iter over filtered rev so nobody is a head at start
1665 # we won't iter over filtered rev so nobody is a head at start
1668 ishead = [0] * (count + 1)
1666 ishead = [0] * (count + 1)
1669 index = self.index
1667 index = self.index
1670 for r in self:
1668 for r in self:
1671 ishead[r] = 1 # I may be an head
1669 ishead[r] = 1 # I may be an head
1672 e = index[r]
1670 e = index[r]
1673 ishead[e[5]] = ishead[e[6]] = 0 # my parent are not
1671 ishead[e[5]] = ishead[e[6]] = 0 # my parent are not
1674 return [r for r, val in enumerate(ishead) if val]
1672 return [r for r, val in enumerate(ishead) if val]
1675
1673
1676 def heads(self, start=None, stop=None):
1674 def heads(self, start=None, stop=None):
1677 """return the list of all nodes that have no children
1675 """return the list of all nodes that have no children
1678
1676
1679 if start is specified, only heads that are descendants of
1677 if start is specified, only heads that are descendants of
1680 start will be returned
1678 start will be returned
1681 if stop is specified, it will consider all the revs from stop
1679 if stop is specified, it will consider all the revs from stop
1682 as if they had no children
1680 as if they had no children
1683 """
1681 """
1684 if start is None and stop is None:
1682 if start is None and stop is None:
1685 if not len(self):
1683 if not len(self):
1686 return [self.nullid]
1684 return [self.nullid]
1687 return [self.node(r) for r in self.headrevs()]
1685 return [self.node(r) for r in self.headrevs()]
1688
1686
1689 if start is None:
1687 if start is None:
1690 start = nullrev
1688 start = nullrev
1691 else:
1689 else:
1692 start = self.rev(start)
1690 start = self.rev(start)
1693
1691
1694 stoprevs = {self.rev(n) for n in stop or []}
1692 stoprevs = {self.rev(n) for n in stop or []}
1695
1693
1696 revs = dagop.headrevssubset(
1694 revs = dagop.headrevssubset(
1697 self.revs, self.parentrevs, startrev=start, stoprevs=stoprevs
1695 self.revs, self.parentrevs, startrev=start, stoprevs=stoprevs
1698 )
1696 )
1699
1697
1700 return [self.node(rev) for rev in revs]
1698 return [self.node(rev) for rev in revs]
1701
1699
1702 def children(self, node):
1700 def children(self, node):
1703 """find the children of a given node"""
1701 """find the children of a given node"""
1704 c = []
1702 c = []
1705 p = self.rev(node)
1703 p = self.rev(node)
1706 for r in self.revs(start=p + 1):
1704 for r in self.revs(start=p + 1):
1707 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
1705 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
1708 if prevs:
1706 if prevs:
1709 for pr in prevs:
1707 for pr in prevs:
1710 if pr == p:
1708 if pr == p:
1711 c.append(self.node(r))
1709 c.append(self.node(r))
1712 elif p == nullrev:
1710 elif p == nullrev:
1713 c.append(self.node(r))
1711 c.append(self.node(r))
1714 return c
1712 return c
1715
1713
1716 def commonancestorsheads(self, a, b):
1714 def commonancestorsheads(self, a, b):
1717 """calculate all the heads of the common ancestors of nodes a and b"""
1715 """calculate all the heads of the common ancestors of nodes a and b"""
1718 a, b = self.rev(a), self.rev(b)
1716 a, b = self.rev(a), self.rev(b)
1719 ancs = self._commonancestorsheads(a, b)
1717 ancs = self._commonancestorsheads(a, b)
1720 return pycompat.maplist(self.node, ancs)
1718 return pycompat.maplist(self.node, ancs)
1721
1719
1722 def _commonancestorsheads(self, *revs):
1720 def _commonancestorsheads(self, *revs):
1723 """calculate all the heads of the common ancestors of revs"""
1721 """calculate all the heads of the common ancestors of revs"""
1724 try:
1722 try:
1725 ancs = self.index.commonancestorsheads(*revs)
1723 ancs = self.index.commonancestorsheads(*revs)
1726 except (AttributeError, OverflowError): # C implementation failed
1724 except (AttributeError, OverflowError): # C implementation failed
1727 ancs = ancestor.commonancestorsheads(self.parentrevs, *revs)
1725 ancs = ancestor.commonancestorsheads(self.parentrevs, *revs)
1728 return ancs
1726 return ancs
1729
1727
1730 def isancestor(self, a, b):
1728 def isancestor(self, a, b):
1731 """return True if node a is an ancestor of node b
1729 """return True if node a is an ancestor of node b
1732
1730
1733 A revision is considered an ancestor of itself."""
1731 A revision is considered an ancestor of itself."""
1734 a, b = self.rev(a), self.rev(b)
1732 a, b = self.rev(a), self.rev(b)
1735 return self.isancestorrev(a, b)
1733 return self.isancestorrev(a, b)
1736
1734
1737 def isancestorrev(self, a, b):
1735 def isancestorrev(self, a, b):
1738 """return True if revision a is an ancestor of revision b
1736 """return True if revision a is an ancestor of revision b
1739
1737
1740 A revision is considered an ancestor of itself.
1738 A revision is considered an ancestor of itself.
1741
1739
1742 The implementation of this is trivial but the use of
1740 The implementation of this is trivial but the use of
1743 reachableroots is not."""
1741 reachableroots is not."""
1744 if a == nullrev:
1742 if a == nullrev:
1745 return True
1743 return True
1746 elif a == b:
1744 elif a == b:
1747 return True
1745 return True
1748 elif a > b:
1746 elif a > b:
1749 return False
1747 return False
1750 return bool(self.reachableroots(a, [b], [a], includepath=False))
1748 return bool(self.reachableroots(a, [b], [a], includepath=False))
1751
1749
1752 def reachableroots(self, minroot, heads, roots, includepath=False):
1750 def reachableroots(self, minroot, heads, roots, includepath=False):
1753 """return (heads(::(<roots> and <roots>::<heads>)))
1751 """return (heads(::(<roots> and <roots>::<heads>)))
1754
1752
1755 If includepath is True, return (<roots>::<heads>)."""
1753 If includepath is True, return (<roots>::<heads>)."""
1756 try:
1754 try:
1757 return self.index.reachableroots2(
1755 return self.index.reachableroots2(
1758 minroot, heads, roots, includepath
1756 minroot, heads, roots, includepath
1759 )
1757 )
1760 except AttributeError:
1758 except AttributeError:
1761 return dagop._reachablerootspure(
1759 return dagop._reachablerootspure(
1762 self.parentrevs, minroot, roots, heads, includepath
1760 self.parentrevs, minroot, roots, heads, includepath
1763 )
1761 )
1764
1762
1765 def ancestor(self, a, b):
1763 def ancestor(self, a, b):
1766 """calculate the "best" common ancestor of nodes a and b"""
1764 """calculate the "best" common ancestor of nodes a and b"""
1767
1765
1768 a, b = self.rev(a), self.rev(b)
1766 a, b = self.rev(a), self.rev(b)
1769 try:
1767 try:
1770 ancs = self.index.ancestors(a, b)
1768 ancs = self.index.ancestors(a, b)
1771 except (AttributeError, OverflowError):
1769 except (AttributeError, OverflowError):
1772 ancs = ancestor.ancestors(self.parentrevs, a, b)
1770 ancs = ancestor.ancestors(self.parentrevs, a, b)
1773 if ancs:
1771 if ancs:
1774 # choose a consistent winner when there's a tie
1772 # choose a consistent winner when there's a tie
1775 return min(map(self.node, ancs))
1773 return min(map(self.node, ancs))
1776 return self.nullid
1774 return self.nullid
1777
1775
1778 def _match(self, id):
1776 def _match(self, id):
1779 if isinstance(id, int):
1777 if isinstance(id, int):
1780 # rev
1778 # rev
1781 return self.node(id)
1779 return self.node(id)
1782 if len(id) == self.nodeconstants.nodelen:
1780 if len(id) == self.nodeconstants.nodelen:
1783 # possibly a binary node
1781 # possibly a binary node
1784 # odds of a binary node being all hex in ASCII are 1 in 10**25
1782 # odds of a binary node being all hex in ASCII are 1 in 10**25
1785 try:
1783 try:
1786 node = id
1784 node = id
1787 self.rev(node) # quick search the index
1785 self.rev(node) # quick search the index
1788 return node
1786 return node
1789 except error.LookupError:
1787 except error.LookupError:
1790 pass # may be partial hex id
1788 pass # may be partial hex id
1791 try:
1789 try:
1792 # str(rev)
1790 # str(rev)
1793 rev = int(id)
1791 rev = int(id)
1794 if b"%d" % rev != id:
1792 if b"%d" % rev != id:
1795 raise ValueError
1793 raise ValueError
1796 if rev < 0:
1794 if rev < 0:
1797 rev = len(self) + rev
1795 rev = len(self) + rev
1798 if rev < 0 or rev >= len(self):
1796 if rev < 0 or rev >= len(self):
1799 raise ValueError
1797 raise ValueError
1800 return self.node(rev)
1798 return self.node(rev)
1801 except (ValueError, OverflowError):
1799 except (ValueError, OverflowError):
1802 pass
1800 pass
1803 if len(id) == 2 * self.nodeconstants.nodelen:
1801 if len(id) == 2 * self.nodeconstants.nodelen:
1804 try:
1802 try:
1805 # a full hex nodeid?
1803 # a full hex nodeid?
1806 node = bin(id)
1804 node = bin(id)
1807 self.rev(node)
1805 self.rev(node)
1808 return node
1806 return node
1809 except (binascii.Error, error.LookupError):
1807 except (binascii.Error, error.LookupError):
1810 pass
1808 pass
1811
1809
1812 def _partialmatch(self, id):
1810 def _partialmatch(self, id):
1813 # we don't care wdirfilenodeids as they should be always full hash
1811 # we don't care wdirfilenodeids as they should be always full hash
1814 maybewdir = self.nodeconstants.wdirhex.startswith(id)
1812 maybewdir = self.nodeconstants.wdirhex.startswith(id)
1815 ambiguous = False
1813 ambiguous = False
1816 try:
1814 try:
1817 partial = self.index.partialmatch(id)
1815 partial = self.index.partialmatch(id)
1818 if partial and self.hasnode(partial):
1816 if partial and self.hasnode(partial):
1819 if maybewdir:
1817 if maybewdir:
1820 # single 'ff...' match in radix tree, ambiguous with wdir
1818 # single 'ff...' match in radix tree, ambiguous with wdir
1821 ambiguous = True
1819 ambiguous = True
1822 else:
1820 else:
1823 return partial
1821 return partial
1824 elif maybewdir:
1822 elif maybewdir:
1825 # no 'ff...' match in radix tree, wdir identified
1823 # no 'ff...' match in radix tree, wdir identified
1826 raise error.WdirUnsupported
1824 raise error.WdirUnsupported
1827 else:
1825 else:
1828 return None
1826 return None
1829 except error.RevlogError:
1827 except error.RevlogError:
1830 # parsers.c radix tree lookup gave multiple matches
1828 # parsers.c radix tree lookup gave multiple matches
1831 # fast path: for unfiltered changelog, radix tree is accurate
1829 # fast path: for unfiltered changelog, radix tree is accurate
1832 if not getattr(self, 'filteredrevs', None):
1830 if not getattr(self, 'filteredrevs', None):
1833 ambiguous = True
1831 ambiguous = True
1834 # fall through to slow path that filters hidden revisions
1832 # fall through to slow path that filters hidden revisions
1835 except (AttributeError, ValueError):
1833 except (AttributeError, ValueError):
1836 # we are pure python, or key is not hex
1834 # we are pure python, or key is not hex
1837 pass
1835 pass
1838 if ambiguous:
1836 if ambiguous:
1839 raise error.AmbiguousPrefixLookupError(
1837 raise error.AmbiguousPrefixLookupError(
1840 id, self.display_id, _(b'ambiguous identifier')
1838 id, self.display_id, _(b'ambiguous identifier')
1841 )
1839 )
1842
1840
1843 if id in self._pcache:
1841 if id in self._pcache:
1844 return self._pcache[id]
1842 return self._pcache[id]
1845
1843
1846 if len(id) <= 40:
1844 if len(id) <= 40:
1847 # hex(node)[:...]
1845 # hex(node)[:...]
1848 l = len(id) // 2 * 2 # grab an even number of digits
1846 l = len(id) // 2 * 2 # grab an even number of digits
1849 try:
1847 try:
1850 # we're dropping the last digit, so let's check that it's hex,
1848 # we're dropping the last digit, so let's check that it's hex,
1851 # to avoid the expensive computation below if it's not
1849 # to avoid the expensive computation below if it's not
1852 if len(id) % 2 > 0:
1850 if len(id) % 2 > 0:
1853 if not (id[-1] in hexdigits):
1851 if not (id[-1] in hexdigits):
1854 return None
1852 return None
1855 prefix = bin(id[:l])
1853 prefix = bin(id[:l])
1856 except binascii.Error:
1854 except binascii.Error:
1857 pass
1855 pass
1858 else:
1856 else:
1859 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
1857 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
1860 nl = [
1858 nl = [
1861 n for n in nl if hex(n).startswith(id) and self.hasnode(n)
1859 n for n in nl if hex(n).startswith(id) and self.hasnode(n)
1862 ]
1860 ]
1863 if self.nodeconstants.nullhex.startswith(id):
1861 if self.nodeconstants.nullhex.startswith(id):
1864 nl.append(self.nullid)
1862 nl.append(self.nullid)
1865 if len(nl) > 0:
1863 if len(nl) > 0:
1866 if len(nl) == 1 and not maybewdir:
1864 if len(nl) == 1 and not maybewdir:
1867 self._pcache[id] = nl[0]
1865 self._pcache[id] = nl[0]
1868 return nl[0]
1866 return nl[0]
1869 raise error.AmbiguousPrefixLookupError(
1867 raise error.AmbiguousPrefixLookupError(
1870 id, self.display_id, _(b'ambiguous identifier')
1868 id, self.display_id, _(b'ambiguous identifier')
1871 )
1869 )
1872 if maybewdir:
1870 if maybewdir:
1873 raise error.WdirUnsupported
1871 raise error.WdirUnsupported
1874 return None
1872 return None
1875
1873
1876 def lookup(self, id):
1874 def lookup(self, id):
1877 """locate a node based on:
1875 """locate a node based on:
1878 - revision number or str(revision number)
1876 - revision number or str(revision number)
1879 - nodeid or subset of hex nodeid
1877 - nodeid or subset of hex nodeid
1880 """
1878 """
1881 n = self._match(id)
1879 n = self._match(id)
1882 if n is not None:
1880 if n is not None:
1883 return n
1881 return n
1884 n = self._partialmatch(id)
1882 n = self._partialmatch(id)
1885 if n:
1883 if n:
1886 return n
1884 return n
1887
1885
1888 raise error.LookupError(id, self.display_id, _(b'no match found'))
1886 raise error.LookupError(id, self.display_id, _(b'no match found'))
1889
1887
1890 def shortest(self, node, minlength=1):
1888 def shortest(self, node, minlength=1):
1891 """Find the shortest unambiguous prefix that matches node."""
1889 """Find the shortest unambiguous prefix that matches node."""
1892
1890
1893 def isvalid(prefix):
1891 def isvalid(prefix):
1894 try:
1892 try:
1895 matchednode = self._partialmatch(prefix)
1893 matchednode = self._partialmatch(prefix)
1896 except error.AmbiguousPrefixLookupError:
1894 except error.AmbiguousPrefixLookupError:
1897 return False
1895 return False
1898 except error.WdirUnsupported:
1896 except error.WdirUnsupported:
1899 # single 'ff...' match
1897 # single 'ff...' match
1900 return True
1898 return True
1901 if matchednode is None:
1899 if matchednode is None:
1902 raise error.LookupError(node, self.display_id, _(b'no node'))
1900 raise error.LookupError(node, self.display_id, _(b'no node'))
1903 return True
1901 return True
1904
1902
1905 def maybewdir(prefix):
1903 def maybewdir(prefix):
1906 return all(c == b'f' for c in pycompat.iterbytestr(prefix))
1904 return all(c == b'f' for c in pycompat.iterbytestr(prefix))
1907
1905
1908 hexnode = hex(node)
1906 hexnode = hex(node)
1909
1907
1910 def disambiguate(hexnode, minlength):
1908 def disambiguate(hexnode, minlength):
1911 """Disambiguate against wdirid."""
1909 """Disambiguate against wdirid."""
1912 for length in range(minlength, len(hexnode) + 1):
1910 for length in range(minlength, len(hexnode) + 1):
1913 prefix = hexnode[:length]
1911 prefix = hexnode[:length]
1914 if not maybewdir(prefix):
1912 if not maybewdir(prefix):
1915 return prefix
1913 return prefix
1916
1914
1917 if not getattr(self, 'filteredrevs', None):
1915 if not getattr(self, 'filteredrevs', None):
1918 try:
1916 try:
1919 length = max(self.index.shortest(node), minlength)
1917 length = max(self.index.shortest(node), minlength)
1920 return disambiguate(hexnode, length)
1918 return disambiguate(hexnode, length)
1921 except error.RevlogError:
1919 except error.RevlogError:
1922 if node != self.nodeconstants.wdirid:
1920 if node != self.nodeconstants.wdirid:
1923 raise error.LookupError(
1921 raise error.LookupError(
1924 node, self.display_id, _(b'no node')
1922 node, self.display_id, _(b'no node')
1925 )
1923 )
1926 except AttributeError:
1924 except AttributeError:
1927 # Fall through to pure code
1925 # Fall through to pure code
1928 pass
1926 pass
1929
1927
1930 if node == self.nodeconstants.wdirid:
1928 if node == self.nodeconstants.wdirid:
1931 for length in range(minlength, len(hexnode) + 1):
1929 for length in range(minlength, len(hexnode) + 1):
1932 prefix = hexnode[:length]
1930 prefix = hexnode[:length]
1933 if isvalid(prefix):
1931 if isvalid(prefix):
1934 return prefix
1932 return prefix
1935
1933
1936 for length in range(minlength, len(hexnode) + 1):
1934 for length in range(minlength, len(hexnode) + 1):
1937 prefix = hexnode[:length]
1935 prefix = hexnode[:length]
1938 if isvalid(prefix):
1936 if isvalid(prefix):
1939 return disambiguate(hexnode, length)
1937 return disambiguate(hexnode, length)
1940
1938
1941 def cmp(self, node, text):
1939 def cmp(self, node, text):
1942 """compare text with a given file revision
1940 """compare text with a given file revision
1943
1941
1944 returns True if text is different than what is stored.
1942 returns True if text is different than what is stored.
1945 """
1943 """
1946 p1, p2 = self.parents(node)
1944 p1, p2 = self.parents(node)
1947 return storageutil.hashrevisionsha1(text, p1, p2) != node
1945 return storageutil.hashrevisionsha1(text, p1, p2) != node
1948
1946
1949 def _getsegmentforrevs(self, startrev, endrev):
1947 def _getsegmentforrevs(self, startrev, endrev):
1950 """Obtain a segment of raw data corresponding to a range of revisions.
1948 """Obtain a segment of raw data corresponding to a range of revisions.
1951
1949
1952 Accepts the start and end revisions and an optional already-open
1950 Accepts the start and end revisions and an optional already-open
1953 file handle to be used for reading. If the file handle is read, its
1951 file handle to be used for reading. If the file handle is read, its
1954 seek position will not be preserved.
1952 seek position will not be preserved.
1955
1953
1956 Requests for data may be satisfied by a cache.
1954 Requests for data may be satisfied by a cache.
1957
1955
1958 Returns a 2-tuple of (offset, data) for the requested range of
1956 Returns a 2-tuple of (offset, data) for the requested range of
1959 revisions. Offset is the integer offset from the beginning of the
1957 revisions. Offset is the integer offset from the beginning of the
1960 revlog and data is a str or buffer of the raw byte data.
1958 revlog and data is a str or buffer of the raw byte data.
1961
1959
1962 Callers will need to call ``self.start(rev)`` and ``self.length(rev)``
1960 Callers will need to call ``self.start(rev)`` and ``self.length(rev)``
1963 to determine where each revision's data begins and ends.
1961 to determine where each revision's data begins and ends.
1964 """
1962 """
1965 # Inlined self.start(startrev) & self.end(endrev) for perf reasons
1963 # Inlined self.start(startrev) & self.end(endrev) for perf reasons
1966 # (functions are expensive).
1964 # (functions are expensive).
1967 index = self.index
1965 index = self.index
1968 istart = index[startrev]
1966 istart = index[startrev]
1969 start = int(istart[0] >> 16)
1967 start = int(istart[0] >> 16)
1970 if startrev == endrev:
1968 if startrev == endrev:
1971 end = start + istart[1]
1969 end = start + istart[1]
1972 else:
1970 else:
1973 iend = index[endrev]
1971 iend = index[endrev]
1974 end = int(iend[0] >> 16) + iend[1]
1972 end = int(iend[0] >> 16) + iend[1]
1975
1973
1976 if self._inline:
1974 if self._inline:
1977 start += (startrev + 1) * self.index.entry_size
1975 start += (startrev + 1) * self.index.entry_size
1978 end += (endrev + 1) * self.index.entry_size
1976 end += (endrev + 1) * self.index.entry_size
1979 length = end - start
1977 length = end - start
1980
1978
1981 return start, self._segmentfile.read_chunk(start, length)
1979 return start, self._segmentfile.read_chunk(start, length)
1982
1980
1983 def _chunk(self, rev):
1981 def _chunk(self, rev):
1984 """Obtain a single decompressed chunk for a revision.
1982 """Obtain a single decompressed chunk for a revision.
1985
1983
1986 Accepts an integer revision and an optional already-open file handle
1984 Accepts an integer revision and an optional already-open file handle
1987 to be used for reading. If used, the seek position of the file will not
1985 to be used for reading. If used, the seek position of the file will not
1988 be preserved.
1986 be preserved.
1989
1987
1990 Returns a str holding uncompressed data for the requested revision.
1988 Returns a str holding uncompressed data for the requested revision.
1991 """
1989 """
1992 compression_mode = self.index[rev][10]
1990 compression_mode = self.index[rev][10]
1993 data = self._getsegmentforrevs(rev, rev)[1]
1991 data = self._getsegmentforrevs(rev, rev)[1]
1994 if compression_mode == COMP_MODE_PLAIN:
1992 if compression_mode == COMP_MODE_PLAIN:
1995 return data
1993 return data
1996 elif compression_mode == COMP_MODE_DEFAULT:
1994 elif compression_mode == COMP_MODE_DEFAULT:
1997 return self._decompressor(data)
1995 return self._decompressor(data)
1998 elif compression_mode == COMP_MODE_INLINE:
1996 elif compression_mode == COMP_MODE_INLINE:
1999 return self.decompress(data)
1997 return self.decompress(data)
2000 else:
1998 else:
2001 msg = b'unknown compression mode %d'
1999 msg = b'unknown compression mode %d'
2002 msg %= compression_mode
2000 msg %= compression_mode
2003 raise error.RevlogError(msg)
2001 raise error.RevlogError(msg)
2004
2002
2005 def _chunks(self, revs, targetsize=None):
2003 def _chunks(self, revs, targetsize=None):
2006 """Obtain decompressed chunks for the specified revisions.
2004 """Obtain decompressed chunks for the specified revisions.
2007
2005
2008 Accepts an iterable of numeric revisions that are assumed to be in
2006 Accepts an iterable of numeric revisions that are assumed to be in
2009 ascending order. Also accepts an optional already-open file handle
2007 ascending order. Also accepts an optional already-open file handle
2010 to be used for reading. If used, the seek position of the file will
2008 to be used for reading. If used, the seek position of the file will
2011 not be preserved.
2009 not be preserved.
2012
2010
2013 This function is similar to calling ``self._chunk()`` multiple times,
2011 This function is similar to calling ``self._chunk()`` multiple times,
2014 but is faster.
2012 but is faster.
2015
2013
2016 Returns a list with decompressed data for each requested revision.
2014 Returns a list with decompressed data for each requested revision.
2017 """
2015 """
2018 if not revs:
2016 if not revs:
2019 return []
2017 return []
2020 start = self.start
2018 start = self.start
2021 length = self.length
2019 length = self.length
2022 inline = self._inline
2020 inline = self._inline
2023 iosize = self.index.entry_size
2021 iosize = self.index.entry_size
2024 buffer = util.buffer
2022 buffer = util.buffer
2025
2023
2026 l = []
2024 l = []
2027 ladd = l.append
2025 ladd = l.append
2028
2026
2029 if not self._withsparseread:
2027 if not self._withsparseread:
2030 slicedchunks = (revs,)
2028 slicedchunks = (revs,)
2031 else:
2029 else:
2032 slicedchunks = deltautil.slicechunk(
2030 slicedchunks = deltautil.slicechunk(
2033 self, revs, targetsize=targetsize
2031 self, revs, targetsize=targetsize
2034 )
2032 )
2035
2033
2036 for revschunk in slicedchunks:
2034 for revschunk in slicedchunks:
2037 firstrev = revschunk[0]
2035 firstrev = revschunk[0]
2038 # Skip trailing revisions with empty diff
2036 # Skip trailing revisions with empty diff
2039 for lastrev in revschunk[::-1]:
2037 for lastrev in revschunk[::-1]:
2040 if length(lastrev) != 0:
2038 if length(lastrev) != 0:
2041 break
2039 break
2042
2040
2043 try:
2041 try:
2044 offset, data = self._getsegmentforrevs(firstrev, lastrev)
2042 offset, data = self._getsegmentforrevs(firstrev, lastrev)
2045 except OverflowError:
2043 except OverflowError:
2046 # issue4215 - we can't cache a run of chunks greater than
2044 # issue4215 - we can't cache a run of chunks greater than
2047 # 2G on Windows
2045 # 2G on Windows
2048 return [self._chunk(rev) for rev in revschunk]
2046 return [self._chunk(rev) for rev in revschunk]
2049
2047
2050 decomp = self.decompress
2048 decomp = self.decompress
2051 # self._decompressor might be None, but will not be used in that case
2049 # self._decompressor might be None, but will not be used in that case
2052 def_decomp = self._decompressor
2050 def_decomp = self._decompressor
2053 for rev in revschunk:
2051 for rev in revschunk:
2054 chunkstart = start(rev)
2052 chunkstart = start(rev)
2055 if inline:
2053 if inline:
2056 chunkstart += (rev + 1) * iosize
2054 chunkstart += (rev + 1) * iosize
2057 chunklength = length(rev)
2055 chunklength = length(rev)
2058 comp_mode = self.index[rev][10]
2056 comp_mode = self.index[rev][10]
2059 c = buffer(data, chunkstart - offset, chunklength)
2057 c = buffer(data, chunkstart - offset, chunklength)
2060 if comp_mode == COMP_MODE_PLAIN:
2058 if comp_mode == COMP_MODE_PLAIN:
2061 ladd(c)
2059 ladd(c)
2062 elif comp_mode == COMP_MODE_INLINE:
2060 elif comp_mode == COMP_MODE_INLINE:
2063 ladd(decomp(c))
2061 ladd(decomp(c))
2064 elif comp_mode == COMP_MODE_DEFAULT:
2062 elif comp_mode == COMP_MODE_DEFAULT:
2065 ladd(def_decomp(c))
2063 ladd(def_decomp(c))
2066 else:
2064 else:
2067 msg = b'unknown compression mode %d'
2065 msg = b'unknown compression mode %d'
2068 msg %= comp_mode
2066 msg %= comp_mode
2069 raise error.RevlogError(msg)
2067 raise error.RevlogError(msg)
2070
2068
2071 return l
2069 return l
2072
2070
2073 def deltaparent(self, rev):
2071 def deltaparent(self, rev):
2074 """return deltaparent of the given revision"""
2072 """return deltaparent of the given revision"""
2075 base = self.index[rev][3]
2073 base = self.index[rev][3]
2076 if base == rev:
2074 if base == rev:
2077 return nullrev
2075 return nullrev
2078 elif self._generaldelta:
2076 elif self._generaldelta:
2079 return base
2077 return base
2080 else:
2078 else:
2081 return rev - 1
2079 return rev - 1
2082
2080
2083 def issnapshot(self, rev):
2081 def issnapshot(self, rev):
2084 """tells whether rev is a snapshot"""
2082 """tells whether rev is a snapshot"""
2085 if not self._sparserevlog:
2083 if not self._sparserevlog:
2086 return self.deltaparent(rev) == nullrev
2084 return self.deltaparent(rev) == nullrev
2087 elif hasattr(self.index, 'issnapshot'):
2085 elif hasattr(self.index, 'issnapshot'):
2088 # directly assign the method to cache the testing and access
2086 # directly assign the method to cache the testing and access
2089 self.issnapshot = self.index.issnapshot
2087 self.issnapshot = self.index.issnapshot
2090 return self.issnapshot(rev)
2088 return self.issnapshot(rev)
2091 if rev == nullrev:
2089 if rev == nullrev:
2092 return True
2090 return True
2093 entry = self.index[rev]
2091 entry = self.index[rev]
2094 base = entry[3]
2092 base = entry[3]
2095 if base == rev:
2093 if base == rev:
2096 return True
2094 return True
2097 if base == nullrev:
2095 if base == nullrev:
2098 return True
2096 return True
2099 p1 = entry[5]
2097 p1 = entry[5]
2100 while self.length(p1) == 0:
2098 while self.length(p1) == 0:
2101 b = self.deltaparent(p1)
2099 b = self.deltaparent(p1)
2102 if b == p1:
2100 if b == p1:
2103 break
2101 break
2104 p1 = b
2102 p1 = b
2105 p2 = entry[6]
2103 p2 = entry[6]
2106 while self.length(p2) == 0:
2104 while self.length(p2) == 0:
2107 b = self.deltaparent(p2)
2105 b = self.deltaparent(p2)
2108 if b == p2:
2106 if b == p2:
2109 break
2107 break
2110 p2 = b
2108 p2 = b
2111 if base == p1 or base == p2:
2109 if base == p1 or base == p2:
2112 return False
2110 return False
2113 return self.issnapshot(base)
2111 return self.issnapshot(base)
2114
2112
2115 def snapshotdepth(self, rev):
2113 def snapshotdepth(self, rev):
2116 """number of snapshot in the chain before this one"""
2114 """number of snapshot in the chain before this one"""
2117 if not self.issnapshot(rev):
2115 if not self.issnapshot(rev):
2118 raise error.ProgrammingError(b'revision %d not a snapshot')
2116 raise error.ProgrammingError(b'revision %d not a snapshot')
2119 return len(self._deltachain(rev)[0]) - 1
2117 return len(self._deltachain(rev)[0]) - 1
2120
2118
2121 def revdiff(self, rev1, rev2):
2119 def revdiff(self, rev1, rev2):
2122 """return or calculate a delta between two revisions
2120 """return or calculate a delta between two revisions
2123
2121
2124 The delta calculated is in binary form and is intended to be written to
2122 The delta calculated is in binary form and is intended to be written to
2125 revlog data directly. So this function needs raw revision data.
2123 revlog data directly. So this function needs raw revision data.
2126 """
2124 """
2127 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
2125 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
2128 return bytes(self._chunk(rev2))
2126 return bytes(self._chunk(rev2))
2129
2127
2130 return mdiff.textdiff(self.rawdata(rev1), self.rawdata(rev2))
2128 return mdiff.textdiff(self.rawdata(rev1), self.rawdata(rev2))
2131
2129
2132 def revision(self, nodeorrev):
2130 def revision(self, nodeorrev):
2133 """return an uncompressed revision of a given node or revision
2131 """return an uncompressed revision of a given node or revision
2134 number.
2132 number.
2135 """
2133 """
2136 return self._revisiondata(nodeorrev)
2134 return self._revisiondata(nodeorrev)
2137
2135
2138 def sidedata(self, nodeorrev):
2136 def sidedata(self, nodeorrev):
2139 """a map of extra data related to the changeset but not part of the hash
2137 """a map of extra data related to the changeset but not part of the hash
2140
2138
2141 This function currently return a dictionary. However, more advanced
2139 This function currently return a dictionary. However, more advanced
2142 mapping object will likely be used in the future for a more
2140 mapping object will likely be used in the future for a more
2143 efficient/lazy code.
2141 efficient/lazy code.
2144 """
2142 """
2145 # deal with <nodeorrev> argument type
2143 # deal with <nodeorrev> argument type
2146 if isinstance(nodeorrev, int):
2144 if isinstance(nodeorrev, int):
2147 rev = nodeorrev
2145 rev = nodeorrev
2148 else:
2146 else:
2149 rev = self.rev(nodeorrev)
2147 rev = self.rev(nodeorrev)
2150 return self._sidedata(rev)
2148 return self._sidedata(rev)
2151
2149
2152 def _revisiondata(self, nodeorrev, raw=False):
2150 def _revisiondata(self, nodeorrev, raw=False):
2153 # deal with <nodeorrev> argument type
2151 # deal with <nodeorrev> argument type
2154 if isinstance(nodeorrev, int):
2152 if isinstance(nodeorrev, int):
2155 rev = nodeorrev
2153 rev = nodeorrev
2156 node = self.node(rev)
2154 node = self.node(rev)
2157 else:
2155 else:
2158 node = nodeorrev
2156 node = nodeorrev
2159 rev = None
2157 rev = None
2160
2158
2161 # fast path the special `nullid` rev
2159 # fast path the special `nullid` rev
2162 if node == self.nullid:
2160 if node == self.nullid:
2163 return b""
2161 return b""
2164
2162
2165 # ``rawtext`` is the text as stored inside the revlog. Might be the
2163 # ``rawtext`` is the text as stored inside the revlog. Might be the
2166 # revision or might need to be processed to retrieve the revision.
2164 # revision or might need to be processed to retrieve the revision.
2167 rev, rawtext, validated = self._rawtext(node, rev)
2165 rev, rawtext, validated = self._rawtext(node, rev)
2168
2166
2169 if raw and validated:
2167 if raw and validated:
2170 # if we don't want to process the raw text and that raw
2168 # if we don't want to process the raw text and that raw
2171 # text is cached, we can exit early.
2169 # text is cached, we can exit early.
2172 return rawtext
2170 return rawtext
2173 if rev is None:
2171 if rev is None:
2174 rev = self.rev(node)
2172 rev = self.rev(node)
2175 # the revlog's flag for this revision
2173 # the revlog's flag for this revision
2176 # (usually alter its state or content)
2174 # (usually alter its state or content)
2177 flags = self.flags(rev)
2175 flags = self.flags(rev)
2178
2176
2179 if validated and flags == REVIDX_DEFAULT_FLAGS:
2177 if validated and flags == REVIDX_DEFAULT_FLAGS:
2180 # no extra flags set, no flag processor runs, text = rawtext
2178 # no extra flags set, no flag processor runs, text = rawtext
2181 return rawtext
2179 return rawtext
2182
2180
2183 if raw:
2181 if raw:
2184 validatehash = flagutil.processflagsraw(self, rawtext, flags)
2182 validatehash = flagutil.processflagsraw(self, rawtext, flags)
2185 text = rawtext
2183 text = rawtext
2186 else:
2184 else:
2187 r = flagutil.processflagsread(self, rawtext, flags)
2185 r = flagutil.processflagsread(self, rawtext, flags)
2188 text, validatehash = r
2186 text, validatehash = r
2189 if validatehash:
2187 if validatehash:
2190 self.checkhash(text, node, rev=rev)
2188 self.checkhash(text, node, rev=rev)
2191 if not validated:
2189 if not validated:
2192 self._revisioncache = (node, rev, rawtext)
2190 self._revisioncache = (node, rev, rawtext)
2193
2191
2194 return text
2192 return text
2195
2193
2196 def _rawtext(self, node, rev):
2194 def _rawtext(self, node, rev):
2197 """return the possibly unvalidated rawtext for a revision
2195 """return the possibly unvalidated rawtext for a revision
2198
2196
2199 returns (rev, rawtext, validated)
2197 returns (rev, rawtext, validated)
2200 """
2198 """
2201
2199
2202 # revision in the cache (could be useful to apply delta)
2200 # revision in the cache (could be useful to apply delta)
2203 cachedrev = None
2201 cachedrev = None
2204 # An intermediate text to apply deltas to
2202 # An intermediate text to apply deltas to
2205 basetext = None
2203 basetext = None
2206
2204
2207 # Check if we have the entry in cache
2205 # Check if we have the entry in cache
2208 # The cache entry looks like (node, rev, rawtext)
2206 # The cache entry looks like (node, rev, rawtext)
2209 if self._revisioncache:
2207 if self._revisioncache:
2210 if self._revisioncache[0] == node:
2208 if self._revisioncache[0] == node:
2211 return (rev, self._revisioncache[2], True)
2209 return (rev, self._revisioncache[2], True)
2212 cachedrev = self._revisioncache[1]
2210 cachedrev = self._revisioncache[1]
2213
2211
2214 if rev is None:
2212 if rev is None:
2215 rev = self.rev(node)
2213 rev = self.rev(node)
2216
2214
2217 chain, stopped = self._deltachain(rev, stoprev=cachedrev)
2215 chain, stopped = self._deltachain(rev, stoprev=cachedrev)
2218 if stopped:
2216 if stopped:
2219 basetext = self._revisioncache[2]
2217 basetext = self._revisioncache[2]
2220
2218
2221 # drop cache to save memory, the caller is expected to
2219 # drop cache to save memory, the caller is expected to
2222 # update self._revisioncache after validating the text
2220 # update self._revisioncache after validating the text
2223 self._revisioncache = None
2221 self._revisioncache = None
2224
2222
2225 targetsize = None
2223 targetsize = None
2226 rawsize = self.index[rev][2]
2224 rawsize = self.index[rev][2]
2227 if 0 <= rawsize:
2225 if 0 <= rawsize:
2228 targetsize = 4 * rawsize
2226 targetsize = 4 * rawsize
2229
2227
2230 bins = self._chunks(chain, targetsize=targetsize)
2228 bins = self._chunks(chain, targetsize=targetsize)
2231 if basetext is None:
2229 if basetext is None:
2232 basetext = bytes(bins[0])
2230 basetext = bytes(bins[0])
2233 bins = bins[1:]
2231 bins = bins[1:]
2234
2232
2235 rawtext = mdiff.patches(basetext, bins)
2233 rawtext = mdiff.patches(basetext, bins)
2236 del basetext # let us have a chance to free memory early
2234 del basetext # let us have a chance to free memory early
2237 return (rev, rawtext, False)
2235 return (rev, rawtext, False)
2238
2236
2239 def _sidedata(self, rev):
2237 def _sidedata(self, rev):
2240 """Return the sidedata for a given revision number."""
2238 """Return the sidedata for a given revision number."""
2241 index_entry = self.index[rev]
2239 index_entry = self.index[rev]
2242 sidedata_offset = index_entry[8]
2240 sidedata_offset = index_entry[8]
2243 sidedata_size = index_entry[9]
2241 sidedata_size = index_entry[9]
2244
2242
2245 if self._inline:
2243 if self._inline:
2246 sidedata_offset += self.index.entry_size * (1 + rev)
2244 sidedata_offset += self.index.entry_size * (1 + rev)
2247 if sidedata_size == 0:
2245 if sidedata_size == 0:
2248 return {}
2246 return {}
2249
2247
2250 if self._docket.sidedata_end < sidedata_offset + sidedata_size:
2248 if self._docket.sidedata_end < sidedata_offset + sidedata_size:
2251 filename = self._sidedatafile
2249 filename = self._sidedatafile
2252 end = self._docket.sidedata_end
2250 end = self._docket.sidedata_end
2253 offset = sidedata_offset
2251 offset = sidedata_offset
2254 length = sidedata_size
2252 length = sidedata_size
2255 m = FILE_TOO_SHORT_MSG % (filename, length, offset, end)
2253 m = FILE_TOO_SHORT_MSG % (filename, length, offset, end)
2256 raise error.RevlogError(m)
2254 raise error.RevlogError(m)
2257
2255
2258 comp_segment = self._segmentfile_sidedata.read_chunk(
2256 comp_segment = self._segmentfile_sidedata.read_chunk(
2259 sidedata_offset, sidedata_size
2257 sidedata_offset, sidedata_size
2260 )
2258 )
2261
2259
2262 comp = self.index[rev][11]
2260 comp = self.index[rev][11]
2263 if comp == COMP_MODE_PLAIN:
2261 if comp == COMP_MODE_PLAIN:
2264 segment = comp_segment
2262 segment = comp_segment
2265 elif comp == COMP_MODE_DEFAULT:
2263 elif comp == COMP_MODE_DEFAULT:
2266 segment = self._decompressor(comp_segment)
2264 segment = self._decompressor(comp_segment)
2267 elif comp == COMP_MODE_INLINE:
2265 elif comp == COMP_MODE_INLINE:
2268 segment = self.decompress(comp_segment)
2266 segment = self.decompress(comp_segment)
2269 else:
2267 else:
2270 msg = b'unknown compression mode %d'
2268 msg = b'unknown compression mode %d'
2271 msg %= comp
2269 msg %= comp
2272 raise error.RevlogError(msg)
2270 raise error.RevlogError(msg)
2273
2271
2274 sidedata = sidedatautil.deserialize_sidedata(segment)
2272 sidedata = sidedatautil.deserialize_sidedata(segment)
2275 return sidedata
2273 return sidedata
2276
2274
2277 def rawdata(self, nodeorrev):
2275 def rawdata(self, nodeorrev):
2278 """return an uncompressed raw data of a given node or revision number."""
2276 """return an uncompressed raw data of a given node or revision number."""
2279 return self._revisiondata(nodeorrev, raw=True)
2277 return self._revisiondata(nodeorrev, raw=True)
2280
2278
2281 def hash(self, text, p1, p2):
2279 def hash(self, text, p1, p2):
2282 """Compute a node hash.
2280 """Compute a node hash.
2283
2281
2284 Available as a function so that subclasses can replace the hash
2282 Available as a function so that subclasses can replace the hash
2285 as needed.
2283 as needed.
2286 """
2284 """
2287 return storageutil.hashrevisionsha1(text, p1, p2)
2285 return storageutil.hashrevisionsha1(text, p1, p2)
2288
2286
2289 def checkhash(self, text, node, p1=None, p2=None, rev=None):
2287 def checkhash(self, text, node, p1=None, p2=None, rev=None):
2290 """Check node hash integrity.
2288 """Check node hash integrity.
2291
2289
2292 Available as a function so that subclasses can extend hash mismatch
2290 Available as a function so that subclasses can extend hash mismatch
2293 behaviors as needed.
2291 behaviors as needed.
2294 """
2292 """
2295 try:
2293 try:
2296 if p1 is None and p2 is None:
2294 if p1 is None and p2 is None:
2297 p1, p2 = self.parents(node)
2295 p1, p2 = self.parents(node)
2298 if node != self.hash(text, p1, p2):
2296 if node != self.hash(text, p1, p2):
2299 # Clear the revision cache on hash failure. The revision cache
2297 # Clear the revision cache on hash failure. The revision cache
2300 # only stores the raw revision and clearing the cache does have
2298 # only stores the raw revision and clearing the cache does have
2301 # the side-effect that we won't have a cache hit when the raw
2299 # the side-effect that we won't have a cache hit when the raw
2302 # revision data is accessed. But this case should be rare and
2300 # revision data is accessed. But this case should be rare and
2303 # it is extra work to teach the cache about the hash
2301 # it is extra work to teach the cache about the hash
2304 # verification state.
2302 # verification state.
2305 if self._revisioncache and self._revisioncache[0] == node:
2303 if self._revisioncache and self._revisioncache[0] == node:
2306 self._revisioncache = None
2304 self._revisioncache = None
2307
2305
2308 revornode = rev
2306 revornode = rev
2309 if revornode is None:
2307 if revornode is None:
2310 revornode = templatefilters.short(hex(node))
2308 revornode = templatefilters.short(hex(node))
2311 raise error.RevlogError(
2309 raise error.RevlogError(
2312 _(b"integrity check failed on %s:%s")
2310 _(b"integrity check failed on %s:%s")
2313 % (self.display_id, pycompat.bytestr(revornode))
2311 % (self.display_id, pycompat.bytestr(revornode))
2314 )
2312 )
2315 except error.RevlogError:
2313 except error.RevlogError:
2316 if self._censorable and storageutil.iscensoredtext(text):
2314 if self._censorable and storageutil.iscensoredtext(text):
2317 raise error.CensoredNodeError(self.display_id, node, text)
2315 raise error.CensoredNodeError(self.display_id, node, text)
2318 raise
2316 raise
2319
2317
2320 @property
2318 @property
2321 def _split_index_file(self):
2319 def _split_index_file(self):
2322 """the path where to expect the index of an ongoing splitting operation
2320 """the path where to expect the index of an ongoing splitting operation
2323
2321
2324 The file will only exist if a splitting operation is in progress, but
2322 The file will only exist if a splitting operation is in progress, but
2325 it is always expected at the same location."""
2323 it is always expected at the same location."""
2326 parts = self.radix.split(b'/')
2324 parts = self.radix.split(b'/')
2327 if len(parts) > 1:
2325 if len(parts) > 1:
2328 # adds a '-s' prefix to the ``data/` or `meta/` base
2326 # adds a '-s' prefix to the ``data/` or `meta/` base
2329 head = parts[0] + b'-s'
2327 head = parts[0] + b'-s'
2330 mids = parts[1:-1]
2328 mids = parts[1:-1]
2331 tail = parts[-1] + b'.i'
2329 tail = parts[-1] + b'.i'
2332 pieces = [head] + mids + [tail]
2330 pieces = [head] + mids + [tail]
2333 return b'/'.join(pieces)
2331 return b'/'.join(pieces)
2334 else:
2332 else:
2335 # the revlog is stored at the root of the store (changelog or
2333 # the revlog is stored at the root of the store (changelog or
2336 # manifest), no risk of collision.
2334 # manifest), no risk of collision.
2337 return self.radix + b'.i.s'
2335 return self.radix + b'.i.s'
2338
2336
2339 def _enforceinlinesize(self, tr, side_write=True):
2337 def _enforceinlinesize(self, tr, side_write=True):
2340 """Check if the revlog is too big for inline and convert if so.
2338 """Check if the revlog is too big for inline and convert if so.
2341
2339
2342 This should be called after revisions are added to the revlog. If the
2340 This should be called after revisions are added to the revlog. If the
2343 revlog has grown too large to be an inline revlog, it will convert it
2341 revlog has grown too large to be an inline revlog, it will convert it
2344 to use multiple index and data files.
2342 to use multiple index and data files.
2345 """
2343 """
2346 tiprev = len(self) - 1
2344 tiprev = len(self) - 1
2347 total_size = self.start(tiprev) + self.length(tiprev)
2345 total_size = self.start(tiprev) + self.length(tiprev)
2348 if not self._inline or total_size < _maxinline:
2346 if not self._inline or total_size < _maxinline:
2349 return
2347 return
2350
2348
2351 troffset = tr.findoffset(self._indexfile)
2349 troffset = tr.findoffset(self._indexfile)
2352 if troffset is None:
2350 if troffset is None:
2353 raise error.RevlogError(
2351 raise error.RevlogError(
2354 _(b"%s not found in the transaction") % self._indexfile
2352 _(b"%s not found in the transaction") % self._indexfile
2355 )
2353 )
2356 if troffset:
2354 if troffset:
2357 tr.addbackup(self._indexfile, for_offset=True)
2355 tr.addbackup(self._indexfile, for_offset=True)
2358 tr.add(self._datafile, 0)
2356 tr.add(self._datafile, 0)
2359
2357
2360 existing_handles = False
2358 existing_handles = False
2361 if self._writinghandles is not None:
2359 if self._writinghandles is not None:
2362 existing_handles = True
2360 existing_handles = True
2363 fp = self._writinghandles[0]
2361 fp = self._writinghandles[0]
2364 fp.flush()
2362 fp.flush()
2365 fp.close()
2363 fp.close()
2366 # We can't use the cached file handle after close(). So prevent
2364 # We can't use the cached file handle after close(). So prevent
2367 # its usage.
2365 # its usage.
2368 self._writinghandles = None
2366 self._writinghandles = None
2369 self._segmentfile.writing_handle = None
2367 self._segmentfile.writing_handle = None
2370 # No need to deal with sidedata writing handle as it is only
2368 # No need to deal with sidedata writing handle as it is only
2371 # relevant with revlog-v2 which is never inline, not reaching
2369 # relevant with revlog-v2 which is never inline, not reaching
2372 # this code
2370 # this code
2373 if side_write:
2371 if side_write:
2374 old_index_file_path = self._indexfile
2372 old_index_file_path = self._indexfile
2375 new_index_file_path = self._split_index_file
2373 new_index_file_path = self._split_index_file
2376 opener = self.opener
2374 opener = self.opener
2377 weak_self = weakref.ref(self)
2375 weak_self = weakref.ref(self)
2378
2376
2379 # the "split" index replace the real index when the transaction is finalized
2377 # the "split" index replace the real index when the transaction is finalized
2380 def finalize_callback(tr):
2378 def finalize_callback(tr):
2381 opener.rename(
2379 opener.rename(
2382 new_index_file_path,
2380 new_index_file_path,
2383 old_index_file_path,
2381 old_index_file_path,
2384 checkambig=True,
2382 checkambig=True,
2385 )
2383 )
2386 maybe_self = weak_self()
2384 maybe_self = weak_self()
2387 if maybe_self is not None:
2385 if maybe_self is not None:
2388 maybe_self._indexfile = old_index_file_path
2386 maybe_self._indexfile = old_index_file_path
2389
2387
2390 def abort_callback(tr):
2388 def abort_callback(tr):
2391 maybe_self = weak_self()
2389 maybe_self = weak_self()
2392 if maybe_self is not None:
2390 if maybe_self is not None:
2393 maybe_self._indexfile = old_index_file_path
2391 maybe_self._indexfile = old_index_file_path
2394
2392
2395 tr.registertmp(new_index_file_path)
2393 tr.registertmp(new_index_file_path)
2396 if self.target[1] is not None:
2394 if self.target[1] is not None:
2397 callback_id = b'000-revlog-split-%d-%s' % self.target
2395 callback_id = b'000-revlog-split-%d-%s' % self.target
2398 else:
2396 else:
2399 callback_id = b'000-revlog-split-%d' % self.target[0]
2397 callback_id = b'000-revlog-split-%d' % self.target[0]
2400 tr.addfinalize(callback_id, finalize_callback)
2398 tr.addfinalize(callback_id, finalize_callback)
2401 tr.addabort(callback_id, abort_callback)
2399 tr.addabort(callback_id, abort_callback)
2402
2400
2403 new_dfh = self._datafp(b'w+')
2401 new_dfh = self._datafp(b'w+')
2404 new_dfh.truncate(0) # drop any potentially existing data
2402 new_dfh.truncate(0) # drop any potentially existing data
2405 try:
2403 try:
2406 with self.reading():
2404 with self.reading():
2407 for r in self:
2405 for r in self:
2408 new_dfh.write(self._getsegmentforrevs(r, r)[1])
2406 new_dfh.write(self._getsegmentforrevs(r, r)[1])
2409 new_dfh.flush()
2407 new_dfh.flush()
2410
2408
2411 if side_write:
2409 if side_write:
2412 self._indexfile = new_index_file_path
2410 self._indexfile = new_index_file_path
2413 with self.__index_new_fp() as fp:
2411 with self.__index_new_fp() as fp:
2414 self._format_flags &= ~FLAG_INLINE_DATA
2412 self._format_flags &= ~FLAG_INLINE_DATA
2415 self._inline = False
2413 self._inline = False
2416 for i in self:
2414 for i in self:
2417 e = self.index.entry_binary(i)
2415 e = self.index.entry_binary(i)
2418 if i == 0 and self._docket is None:
2416 if i == 0 and self._docket is None:
2419 header = self._format_flags | self._format_version
2417 header = self._format_flags | self._format_version
2420 header = self.index.pack_header(header)
2418 header = self.index.pack_header(header)
2421 e = header + e
2419 e = header + e
2422 fp.write(e)
2420 fp.write(e)
2423 if self._docket is not None:
2421 if self._docket is not None:
2424 self._docket.index_end = fp.tell()
2422 self._docket.index_end = fp.tell()
2425
2423
2426 # If we don't use side-write, the temp file replace the real
2424 # If we don't use side-write, the temp file replace the real
2427 # index when we exit the context manager
2425 # index when we exit the context manager
2428
2426
2429 nodemaputil.setup_persistent_nodemap(tr, self)
2427 nodemaputil.setup_persistent_nodemap(tr, self)
2430 self._segmentfile = randomaccessfile.randomaccessfile(
2428 self._segmentfile = randomaccessfile.randomaccessfile(
2431 self.opener,
2429 self.opener,
2432 self._datafile,
2430 self._datafile,
2433 self._chunkcachesize,
2431 self._chunkcachesize,
2434 )
2432 )
2435
2433
2436 if existing_handles:
2434 if existing_handles:
2437 # switched from inline to conventional reopen the index
2435 # switched from inline to conventional reopen the index
2438 ifh = self.__index_write_fp()
2436 ifh = self.__index_write_fp()
2439 self._writinghandles = (ifh, new_dfh, None)
2437 self._writinghandles = (ifh, new_dfh, None)
2440 self._segmentfile.writing_handle = new_dfh
2438 self._segmentfile.writing_handle = new_dfh
2441 new_dfh = None
2439 new_dfh = None
2442 # No need to deal with sidedata writing handle as it is only
2440 # No need to deal with sidedata writing handle as it is only
2443 # relevant with revlog-v2 which is never inline, not reaching
2441 # relevant with revlog-v2 which is never inline, not reaching
2444 # this code
2442 # this code
2445 finally:
2443 finally:
2446 if new_dfh is not None:
2444 if new_dfh is not None:
2447 new_dfh.close()
2445 new_dfh.close()
2448
2446
2449 def _nodeduplicatecallback(self, transaction, node):
2447 def _nodeduplicatecallback(self, transaction, node):
2450 """called when trying to add a node already stored."""
2448 """called when trying to add a node already stored."""
2451
2449
2452 @contextlib.contextmanager
2450 @contextlib.contextmanager
2453 def reading(self):
2451 def reading(self):
2454 """Context manager that keeps data and sidedata files open for reading"""
2452 """Context manager that keeps data and sidedata files open for reading"""
2455 if len(self.index) == 0:
2453 if len(self.index) == 0:
2456 yield # nothing to be read
2454 yield # nothing to be read
2457 else:
2455 else:
2458 with self._segmentfile.reading():
2456 with self._segmentfile.reading():
2459 with self._segmentfile_sidedata.reading():
2457 with self._segmentfile_sidedata.reading():
2460 yield
2458 yield
2461
2459
2462 @contextlib.contextmanager
2460 @contextlib.contextmanager
2463 def _writing(self, transaction):
2461 def _writing(self, transaction):
2464 if self._trypending:
2462 if self._trypending:
2465 msg = b'try to write in a `trypending` revlog: %s'
2463 msg = b'try to write in a `trypending` revlog: %s'
2466 msg %= self.display_id
2464 msg %= self.display_id
2467 raise error.ProgrammingError(msg)
2465 raise error.ProgrammingError(msg)
2468 if self._writinghandles is not None:
2466 if self._writinghandles is not None:
2469 yield
2467 yield
2470 else:
2468 else:
2471 ifh = dfh = sdfh = None
2469 ifh = dfh = sdfh = None
2472 try:
2470 try:
2473 r = len(self)
2471 r = len(self)
2474 # opening the data file.
2472 # opening the data file.
2475 dsize = 0
2473 dsize = 0
2476 if r:
2474 if r:
2477 dsize = self.end(r - 1)
2475 dsize = self.end(r - 1)
2478 dfh = None
2476 dfh = None
2479 if not self._inline:
2477 if not self._inline:
2480 try:
2478 try:
2481 dfh = self._datafp(b"r+")
2479 dfh = self._datafp(b"r+")
2482 if self._docket is None:
2480 if self._docket is None:
2483 dfh.seek(0, os.SEEK_END)
2481 dfh.seek(0, os.SEEK_END)
2484 else:
2482 else:
2485 dfh.seek(self._docket.data_end, os.SEEK_SET)
2483 dfh.seek(self._docket.data_end, os.SEEK_SET)
2486 except FileNotFoundError:
2484 except FileNotFoundError:
2487 dfh = self._datafp(b"w+")
2485 dfh = self._datafp(b"w+")
2488 transaction.add(self._datafile, dsize)
2486 transaction.add(self._datafile, dsize)
2489 if self._sidedatafile is not None:
2487 if self._sidedatafile is not None:
2490 # revlog-v2 does not inline, help Pytype
2488 # revlog-v2 does not inline, help Pytype
2491 assert dfh is not None
2489 assert dfh is not None
2492 try:
2490 try:
2493 sdfh = self.opener(self._sidedatafile, mode=b"r+")
2491 sdfh = self.opener(self._sidedatafile, mode=b"r+")
2494 dfh.seek(self._docket.sidedata_end, os.SEEK_SET)
2492 dfh.seek(self._docket.sidedata_end, os.SEEK_SET)
2495 except FileNotFoundError:
2493 except FileNotFoundError:
2496 sdfh = self.opener(self._sidedatafile, mode=b"w+")
2494 sdfh = self.opener(self._sidedatafile, mode=b"w+")
2497 transaction.add(
2495 transaction.add(
2498 self._sidedatafile, self._docket.sidedata_end
2496 self._sidedatafile, self._docket.sidedata_end
2499 )
2497 )
2500
2498
2501 # opening the index file.
2499 # opening the index file.
2502 isize = r * self.index.entry_size
2500 isize = r * self.index.entry_size
2503 ifh = self.__index_write_fp()
2501 ifh = self.__index_write_fp()
2504 if self._inline:
2502 if self._inline:
2505 transaction.add(self._indexfile, dsize + isize)
2503 transaction.add(self._indexfile, dsize + isize)
2506 else:
2504 else:
2507 transaction.add(self._indexfile, isize)
2505 transaction.add(self._indexfile, isize)
2508 # exposing all file handle for writing.
2506 # exposing all file handle for writing.
2509 self._writinghandles = (ifh, dfh, sdfh)
2507 self._writinghandles = (ifh, dfh, sdfh)
2510 self._segmentfile.writing_handle = ifh if self._inline else dfh
2508 self._segmentfile.writing_handle = ifh if self._inline else dfh
2511 self._segmentfile_sidedata.writing_handle = sdfh
2509 self._segmentfile_sidedata.writing_handle = sdfh
2512 yield
2510 yield
2513 if self._docket is not None:
2511 if self._docket is not None:
2514 self._write_docket(transaction)
2512 self._write_docket(transaction)
2515 finally:
2513 finally:
2516 self._writinghandles = None
2514 self._writinghandles = None
2517 self._segmentfile.writing_handle = None
2515 self._segmentfile.writing_handle = None
2518 self._segmentfile_sidedata.writing_handle = None
2516 self._segmentfile_sidedata.writing_handle = None
2519 if dfh is not None:
2517 if dfh is not None:
2520 dfh.close()
2518 dfh.close()
2521 if sdfh is not None:
2519 if sdfh is not None:
2522 sdfh.close()
2520 sdfh.close()
2523 # closing the index file last to avoid exposing referent to
2521 # closing the index file last to avoid exposing referent to
2524 # potential unflushed data content.
2522 # potential unflushed data content.
2525 if ifh is not None:
2523 if ifh is not None:
2526 ifh.close()
2524 ifh.close()
2527
2525
2528 def _write_docket(self, transaction):
2526 def _write_docket(self, transaction):
2529 """write the current docket on disk
2527 """write the current docket on disk
2530
2528
2531 Exist as a method to help changelog to implement transaction logic
2529 Exist as a method to help changelog to implement transaction logic
2532
2530
2533 We could also imagine using the same transaction logic for all revlog
2531 We could also imagine using the same transaction logic for all revlog
2534 since docket are cheap."""
2532 since docket are cheap."""
2535 self._docket.write(transaction)
2533 self._docket.write(transaction)
2536
2534
2537 def addrevision(
2535 def addrevision(
2538 self,
2536 self,
2539 text,
2537 text,
2540 transaction,
2538 transaction,
2541 link,
2539 link,
2542 p1,
2540 p1,
2543 p2,
2541 p2,
2544 cachedelta=None,
2542 cachedelta=None,
2545 node=None,
2543 node=None,
2546 flags=REVIDX_DEFAULT_FLAGS,
2544 flags=REVIDX_DEFAULT_FLAGS,
2547 deltacomputer=None,
2545 deltacomputer=None,
2548 sidedata=None,
2546 sidedata=None,
2549 ):
2547 ):
2550 """add a revision to the log
2548 """add a revision to the log
2551
2549
2552 text - the revision data to add
2550 text - the revision data to add
2553 transaction - the transaction object used for rollback
2551 transaction - the transaction object used for rollback
2554 link - the linkrev data to add
2552 link - the linkrev data to add
2555 p1, p2 - the parent nodeids of the revision
2553 p1, p2 - the parent nodeids of the revision
2556 cachedelta - an optional precomputed delta
2554 cachedelta - an optional precomputed delta
2557 node - nodeid of revision; typically node is not specified, and it is
2555 node - nodeid of revision; typically node is not specified, and it is
2558 computed by default as hash(text, p1, p2), however subclasses might
2556 computed by default as hash(text, p1, p2), however subclasses might
2559 use different hashing method (and override checkhash() in such case)
2557 use different hashing method (and override checkhash() in such case)
2560 flags - the known flags to set on the revision
2558 flags - the known flags to set on the revision
2561 deltacomputer - an optional deltacomputer instance shared between
2559 deltacomputer - an optional deltacomputer instance shared between
2562 multiple calls
2560 multiple calls
2563 """
2561 """
2564 if link == nullrev:
2562 if link == nullrev:
2565 raise error.RevlogError(
2563 raise error.RevlogError(
2566 _(b"attempted to add linkrev -1 to %s") % self.display_id
2564 _(b"attempted to add linkrev -1 to %s") % self.display_id
2567 )
2565 )
2568
2566
2569 if sidedata is None:
2567 if sidedata is None:
2570 sidedata = {}
2568 sidedata = {}
2571 elif sidedata and not self.hassidedata:
2569 elif sidedata and not self.hassidedata:
2572 raise error.ProgrammingError(
2570 raise error.ProgrammingError(
2573 _(b"trying to add sidedata to a revlog who don't support them")
2571 _(b"trying to add sidedata to a revlog who don't support them")
2574 )
2572 )
2575
2573
2576 if flags:
2574 if flags:
2577 node = node or self.hash(text, p1, p2)
2575 node = node or self.hash(text, p1, p2)
2578
2576
2579 rawtext, validatehash = flagutil.processflagswrite(self, text, flags)
2577 rawtext, validatehash = flagutil.processflagswrite(self, text, flags)
2580
2578
2581 # If the flag processor modifies the revision data, ignore any provided
2579 # If the flag processor modifies the revision data, ignore any provided
2582 # cachedelta.
2580 # cachedelta.
2583 if rawtext != text:
2581 if rawtext != text:
2584 cachedelta = None
2582 cachedelta = None
2585
2583
2586 if len(rawtext) > _maxentrysize:
2584 if len(rawtext) > _maxentrysize:
2587 raise error.RevlogError(
2585 raise error.RevlogError(
2588 _(
2586 _(
2589 b"%s: size of %d bytes exceeds maximum revlog storage of 2GiB"
2587 b"%s: size of %d bytes exceeds maximum revlog storage of 2GiB"
2590 )
2588 )
2591 % (self.display_id, len(rawtext))
2589 % (self.display_id, len(rawtext))
2592 )
2590 )
2593
2591
2594 node = node or self.hash(rawtext, p1, p2)
2592 node = node or self.hash(rawtext, p1, p2)
2595 rev = self.index.get_rev(node)
2593 rev = self.index.get_rev(node)
2596 if rev is not None:
2594 if rev is not None:
2597 return rev
2595 return rev
2598
2596
2599 if validatehash:
2597 if validatehash:
2600 self.checkhash(rawtext, node, p1=p1, p2=p2)
2598 self.checkhash(rawtext, node, p1=p1, p2=p2)
2601
2599
2602 return self.addrawrevision(
2600 return self.addrawrevision(
2603 rawtext,
2601 rawtext,
2604 transaction,
2602 transaction,
2605 link,
2603 link,
2606 p1,
2604 p1,
2607 p2,
2605 p2,
2608 node,
2606 node,
2609 flags,
2607 flags,
2610 cachedelta=cachedelta,
2608 cachedelta=cachedelta,
2611 deltacomputer=deltacomputer,
2609 deltacomputer=deltacomputer,
2612 sidedata=sidedata,
2610 sidedata=sidedata,
2613 )
2611 )
2614
2612
2615 def addrawrevision(
2613 def addrawrevision(
2616 self,
2614 self,
2617 rawtext,
2615 rawtext,
2618 transaction,
2616 transaction,
2619 link,
2617 link,
2620 p1,
2618 p1,
2621 p2,
2619 p2,
2622 node,
2620 node,
2623 flags,
2621 flags,
2624 cachedelta=None,
2622 cachedelta=None,
2625 deltacomputer=None,
2623 deltacomputer=None,
2626 sidedata=None,
2624 sidedata=None,
2627 ):
2625 ):
2628 """add a raw revision with known flags, node and parents
2626 """add a raw revision with known flags, node and parents
2629 useful when reusing a revision not stored in this revlog (ex: received
2627 useful when reusing a revision not stored in this revlog (ex: received
2630 over wire, or read from an external bundle).
2628 over wire, or read from an external bundle).
2631 """
2629 """
2632 with self._writing(transaction):
2630 with self._writing(transaction):
2633 return self._addrevision(
2631 return self._addrevision(
2634 node,
2632 node,
2635 rawtext,
2633 rawtext,
2636 transaction,
2634 transaction,
2637 link,
2635 link,
2638 p1,
2636 p1,
2639 p2,
2637 p2,
2640 flags,
2638 flags,
2641 cachedelta,
2639 cachedelta,
2642 deltacomputer=deltacomputer,
2640 deltacomputer=deltacomputer,
2643 sidedata=sidedata,
2641 sidedata=sidedata,
2644 )
2642 )
2645
2643
2646 def compress(self, data):
2644 def compress(self, data):
2647 """Generate a possibly-compressed representation of data."""
2645 """Generate a possibly-compressed representation of data."""
2648 if not data:
2646 if not data:
2649 return b'', data
2647 return b'', data
2650
2648
2651 compressed = self._compressor.compress(data)
2649 compressed = self._compressor.compress(data)
2652
2650
2653 if compressed:
2651 if compressed:
2654 # The revlog compressor added the header in the returned data.
2652 # The revlog compressor added the header in the returned data.
2655 return b'', compressed
2653 return b'', compressed
2656
2654
2657 if data[0:1] == b'\0':
2655 if data[0:1] == b'\0':
2658 return b'', data
2656 return b'', data
2659 return b'u', data
2657 return b'u', data
2660
2658
2661 def decompress(self, data):
2659 def decompress(self, data):
2662 """Decompress a revlog chunk.
2660 """Decompress a revlog chunk.
2663
2661
2664 The chunk is expected to begin with a header identifying the
2662 The chunk is expected to begin with a header identifying the
2665 format type so it can be routed to an appropriate decompressor.
2663 format type so it can be routed to an appropriate decompressor.
2666 """
2664 """
2667 if not data:
2665 if not data:
2668 return data
2666 return data
2669
2667
2670 # Revlogs are read much more frequently than they are written and many
2668 # Revlogs are read much more frequently than they are written and many
2671 # chunks only take microseconds to decompress, so performance is
2669 # chunks only take microseconds to decompress, so performance is
2672 # important here.
2670 # important here.
2673 #
2671 #
2674 # We can make a few assumptions about revlogs:
2672 # We can make a few assumptions about revlogs:
2675 #
2673 #
2676 # 1) the majority of chunks will be compressed (as opposed to inline
2674 # 1) the majority of chunks will be compressed (as opposed to inline
2677 # raw data).
2675 # raw data).
2678 # 2) decompressing *any* data will likely by at least 10x slower than
2676 # 2) decompressing *any* data will likely by at least 10x slower than
2679 # returning raw inline data.
2677 # returning raw inline data.
2680 # 3) we want to prioritize common and officially supported compression
2678 # 3) we want to prioritize common and officially supported compression
2681 # engines
2679 # engines
2682 #
2680 #
2683 # It follows that we want to optimize for "decompress compressed data
2681 # It follows that we want to optimize for "decompress compressed data
2684 # when encoded with common and officially supported compression engines"
2682 # when encoded with common and officially supported compression engines"
2685 # case over "raw data" and "data encoded by less common or non-official
2683 # case over "raw data" and "data encoded by less common or non-official
2686 # compression engines." That is why we have the inline lookup first
2684 # compression engines." That is why we have the inline lookup first
2687 # followed by the compengines lookup.
2685 # followed by the compengines lookup.
2688 #
2686 #
2689 # According to `hg perfrevlogchunks`, this is ~0.5% faster for zlib
2687 # According to `hg perfrevlogchunks`, this is ~0.5% faster for zlib
2690 # compressed chunks. And this matters for changelog and manifest reads.
2688 # compressed chunks. And this matters for changelog and manifest reads.
2691 t = data[0:1]
2689 t = data[0:1]
2692
2690
2693 if t == b'x':
2691 if t == b'x':
2694 try:
2692 try:
2695 return _zlibdecompress(data)
2693 return _zlibdecompress(data)
2696 except zlib.error as e:
2694 except zlib.error as e:
2697 raise error.RevlogError(
2695 raise error.RevlogError(
2698 _(b'revlog decompress error: %s')
2696 _(b'revlog decompress error: %s')
2699 % stringutil.forcebytestr(e)
2697 % stringutil.forcebytestr(e)
2700 )
2698 )
2701 # '\0' is more common than 'u' so it goes first.
2699 # '\0' is more common than 'u' so it goes first.
2702 elif t == b'\0':
2700 elif t == b'\0':
2703 return data
2701 return data
2704 elif t == b'u':
2702 elif t == b'u':
2705 return util.buffer(data, 1)
2703 return util.buffer(data, 1)
2706
2704
2707 compressor = self._get_decompressor(t)
2705 compressor = self._get_decompressor(t)
2708
2706
2709 return compressor.decompress(data)
2707 return compressor.decompress(data)
2710
2708
2711 def _addrevision(
2709 def _addrevision(
2712 self,
2710 self,
2713 node,
2711 node,
2714 rawtext,
2712 rawtext,
2715 transaction,
2713 transaction,
2716 link,
2714 link,
2717 p1,
2715 p1,
2718 p2,
2716 p2,
2719 flags,
2717 flags,
2720 cachedelta,
2718 cachedelta,
2721 alwayscache=False,
2719 alwayscache=False,
2722 deltacomputer=None,
2720 deltacomputer=None,
2723 sidedata=None,
2721 sidedata=None,
2724 ):
2722 ):
2725 """internal function to add revisions to the log
2723 """internal function to add revisions to the log
2726
2724
2727 see addrevision for argument descriptions.
2725 see addrevision for argument descriptions.
2728
2726
2729 note: "addrevision" takes non-raw text, "_addrevision" takes raw text.
2727 note: "addrevision" takes non-raw text, "_addrevision" takes raw text.
2730
2728
2731 if "deltacomputer" is not provided or None, a defaultdeltacomputer will
2729 if "deltacomputer" is not provided or None, a defaultdeltacomputer will
2732 be used.
2730 be used.
2733
2731
2734 invariants:
2732 invariants:
2735 - rawtext is optional (can be None); if not set, cachedelta must be set.
2733 - rawtext is optional (can be None); if not set, cachedelta must be set.
2736 if both are set, they must correspond to each other.
2734 if both are set, they must correspond to each other.
2737 """
2735 """
2738 if node == self.nullid:
2736 if node == self.nullid:
2739 raise error.RevlogError(
2737 raise error.RevlogError(
2740 _(b"%s: attempt to add null revision") % self.display_id
2738 _(b"%s: attempt to add null revision") % self.display_id
2741 )
2739 )
2742 if (
2740 if (
2743 node == self.nodeconstants.wdirid
2741 node == self.nodeconstants.wdirid
2744 or node in self.nodeconstants.wdirfilenodeids
2742 or node in self.nodeconstants.wdirfilenodeids
2745 ):
2743 ):
2746 raise error.RevlogError(
2744 raise error.RevlogError(
2747 _(b"%s: attempt to add wdir revision") % self.display_id
2745 _(b"%s: attempt to add wdir revision") % self.display_id
2748 )
2746 )
2749 if self._writinghandles is None:
2747 if self._writinghandles is None:
2750 msg = b'adding revision outside `revlog._writing` context'
2748 msg = b'adding revision outside `revlog._writing` context'
2751 raise error.ProgrammingError(msg)
2749 raise error.ProgrammingError(msg)
2752
2750
2753 btext = [rawtext]
2751 btext = [rawtext]
2754
2752
2755 curr = len(self)
2753 curr = len(self)
2756 prev = curr - 1
2754 prev = curr - 1
2757
2755
2758 offset = self._get_data_offset(prev)
2756 offset = self._get_data_offset(prev)
2759
2757
2760 if self._concurrencychecker:
2758 if self._concurrencychecker:
2761 ifh, dfh, sdfh = self._writinghandles
2759 ifh, dfh, sdfh = self._writinghandles
2762 # XXX no checking for the sidedata file
2760 # XXX no checking for the sidedata file
2763 if self._inline:
2761 if self._inline:
2764 # offset is "as if" it were in the .d file, so we need to add on
2762 # offset is "as if" it were in the .d file, so we need to add on
2765 # the size of the entry metadata.
2763 # the size of the entry metadata.
2766 self._concurrencychecker(
2764 self._concurrencychecker(
2767 ifh, self._indexfile, offset + curr * self.index.entry_size
2765 ifh, self._indexfile, offset + curr * self.index.entry_size
2768 )
2766 )
2769 else:
2767 else:
2770 # Entries in the .i are a consistent size.
2768 # Entries in the .i are a consistent size.
2771 self._concurrencychecker(
2769 self._concurrencychecker(
2772 ifh, self._indexfile, curr * self.index.entry_size
2770 ifh, self._indexfile, curr * self.index.entry_size
2773 )
2771 )
2774 self._concurrencychecker(dfh, self._datafile, offset)
2772 self._concurrencychecker(dfh, self._datafile, offset)
2775
2773
2776 p1r, p2r = self.rev(p1), self.rev(p2)
2774 p1r, p2r = self.rev(p1), self.rev(p2)
2777
2775
2778 # full versions are inserted when the needed deltas
2776 # full versions are inserted when the needed deltas
2779 # become comparable to the uncompressed text
2777 # become comparable to the uncompressed text
2780 if rawtext is None:
2778 if rawtext is None:
2781 # need rawtext size, before changed by flag processors, which is
2779 # need rawtext size, before changed by flag processors, which is
2782 # the non-raw size. use revlog explicitly to avoid filelog's extra
2780 # the non-raw size. use revlog explicitly to avoid filelog's extra
2783 # logic that might remove metadata size.
2781 # logic that might remove metadata size.
2784 textlen = mdiff.patchedsize(
2782 textlen = mdiff.patchedsize(
2785 revlog.size(self, cachedelta[0]), cachedelta[1]
2783 revlog.size(self, cachedelta[0]), cachedelta[1]
2786 )
2784 )
2787 else:
2785 else:
2788 textlen = len(rawtext)
2786 textlen = len(rawtext)
2789
2787
2790 if deltacomputer is None:
2788 if deltacomputer is None:
2791 write_debug = None
2789 write_debug = None
2792 if self._debug_delta:
2790 if self._debug_delta:
2793 write_debug = transaction._report
2791 write_debug = transaction._report
2794 deltacomputer = deltautil.deltacomputer(
2792 deltacomputer = deltautil.deltacomputer(
2795 self, write_debug=write_debug
2793 self, write_debug=write_debug
2796 )
2794 )
2797
2795
2798 if cachedelta is not None and len(cachedelta) == 2:
2796 if cachedelta is not None and len(cachedelta) == 2:
2799 # If the cached delta has no information about how it should be
2797 # If the cached delta has no information about how it should be
2800 # reused, add the default reuse instruction according to the
2798 # reused, add the default reuse instruction according to the
2801 # revlog's configuration.
2799 # revlog's configuration.
2802 if self._generaldelta and self._lazydeltabase:
2800 if self._generaldelta and self._lazydeltabase:
2803 delta_base_reuse = DELTA_BASE_REUSE_TRY
2801 delta_base_reuse = DELTA_BASE_REUSE_TRY
2804 else:
2802 else:
2805 delta_base_reuse = DELTA_BASE_REUSE_NO
2803 delta_base_reuse = DELTA_BASE_REUSE_NO
2806 cachedelta = (cachedelta[0], cachedelta[1], delta_base_reuse)
2804 cachedelta = (cachedelta[0], cachedelta[1], delta_base_reuse)
2807
2805
2808 revinfo = revlogutils.revisioninfo(
2806 revinfo = revlogutils.revisioninfo(
2809 node,
2807 node,
2810 p1,
2808 p1,
2811 p2,
2809 p2,
2812 btext,
2810 btext,
2813 textlen,
2811 textlen,
2814 cachedelta,
2812 cachedelta,
2815 flags,
2813 flags,
2816 )
2814 )
2817
2815
2818 deltainfo = deltacomputer.finddeltainfo(revinfo)
2816 deltainfo = deltacomputer.finddeltainfo(revinfo)
2819
2817
2820 compression_mode = COMP_MODE_INLINE
2818 compression_mode = COMP_MODE_INLINE
2821 if self._docket is not None:
2819 if self._docket is not None:
2822 default_comp = self._docket.default_compression_header
2820 default_comp = self._docket.default_compression_header
2823 r = deltautil.delta_compression(default_comp, deltainfo)
2821 r = deltautil.delta_compression(default_comp, deltainfo)
2824 compression_mode, deltainfo = r
2822 compression_mode, deltainfo = r
2825
2823
2826 sidedata_compression_mode = COMP_MODE_INLINE
2824 sidedata_compression_mode = COMP_MODE_INLINE
2827 if sidedata and self.hassidedata:
2825 if sidedata and self.hassidedata:
2828 sidedata_compression_mode = COMP_MODE_PLAIN
2826 sidedata_compression_mode = COMP_MODE_PLAIN
2829 serialized_sidedata = sidedatautil.serialize_sidedata(sidedata)
2827 serialized_sidedata = sidedatautil.serialize_sidedata(sidedata)
2830 sidedata_offset = self._docket.sidedata_end
2828 sidedata_offset = self._docket.sidedata_end
2831 h, comp_sidedata = self.compress(serialized_sidedata)
2829 h, comp_sidedata = self.compress(serialized_sidedata)
2832 if (
2830 if (
2833 h != b'u'
2831 h != b'u'
2834 and comp_sidedata[0:1] != b'\0'
2832 and comp_sidedata[0:1] != b'\0'
2835 and len(comp_sidedata) < len(serialized_sidedata)
2833 and len(comp_sidedata) < len(serialized_sidedata)
2836 ):
2834 ):
2837 assert not h
2835 assert not h
2838 if (
2836 if (
2839 comp_sidedata[0:1]
2837 comp_sidedata[0:1]
2840 == self._docket.default_compression_header
2838 == self._docket.default_compression_header
2841 ):
2839 ):
2842 sidedata_compression_mode = COMP_MODE_DEFAULT
2840 sidedata_compression_mode = COMP_MODE_DEFAULT
2843 serialized_sidedata = comp_sidedata
2841 serialized_sidedata = comp_sidedata
2844 else:
2842 else:
2845 sidedata_compression_mode = COMP_MODE_INLINE
2843 sidedata_compression_mode = COMP_MODE_INLINE
2846 serialized_sidedata = comp_sidedata
2844 serialized_sidedata = comp_sidedata
2847 else:
2845 else:
2848 serialized_sidedata = b""
2846 serialized_sidedata = b""
2849 # Don't store the offset if the sidedata is empty, that way
2847 # Don't store the offset if the sidedata is empty, that way
2850 # we can easily detect empty sidedata and they will be no different
2848 # we can easily detect empty sidedata and they will be no different
2851 # than ones we manually add.
2849 # than ones we manually add.
2852 sidedata_offset = 0
2850 sidedata_offset = 0
2853
2851
2854 rank = RANK_UNKNOWN
2852 rank = RANK_UNKNOWN
2855 if self._compute_rank:
2853 if self._compute_rank:
2856 if (p1r, p2r) == (nullrev, nullrev):
2854 if (p1r, p2r) == (nullrev, nullrev):
2857 rank = 1
2855 rank = 1
2858 elif p1r != nullrev and p2r == nullrev:
2856 elif p1r != nullrev and p2r == nullrev:
2859 rank = 1 + self.fast_rank(p1r)
2857 rank = 1 + self.fast_rank(p1r)
2860 elif p1r == nullrev and p2r != nullrev:
2858 elif p1r == nullrev and p2r != nullrev:
2861 rank = 1 + self.fast_rank(p2r)
2859 rank = 1 + self.fast_rank(p2r)
2862 else: # merge node
2860 else: # merge node
2863 if rustdagop is not None and self.index.rust_ext_compat:
2861 if rustdagop is not None and self.index.rust_ext_compat:
2864 rank = rustdagop.rank(self.index, p1r, p2r)
2862 rank = rustdagop.rank(self.index, p1r, p2r)
2865 else:
2863 else:
2866 pmin, pmax = sorted((p1r, p2r))
2864 pmin, pmax = sorted((p1r, p2r))
2867 rank = 1 + self.fast_rank(pmax)
2865 rank = 1 + self.fast_rank(pmax)
2868 rank += sum(1 for _ in self.findmissingrevs([pmax], [pmin]))
2866 rank += sum(1 for _ in self.findmissingrevs([pmax], [pmin]))
2869
2867
2870 e = revlogutils.entry(
2868 e = revlogutils.entry(
2871 flags=flags,
2869 flags=flags,
2872 data_offset=offset,
2870 data_offset=offset,
2873 data_compressed_length=deltainfo.deltalen,
2871 data_compressed_length=deltainfo.deltalen,
2874 data_uncompressed_length=textlen,
2872 data_uncompressed_length=textlen,
2875 data_compression_mode=compression_mode,
2873 data_compression_mode=compression_mode,
2876 data_delta_base=deltainfo.base,
2874 data_delta_base=deltainfo.base,
2877 link_rev=link,
2875 link_rev=link,
2878 parent_rev_1=p1r,
2876 parent_rev_1=p1r,
2879 parent_rev_2=p2r,
2877 parent_rev_2=p2r,
2880 node_id=node,
2878 node_id=node,
2881 sidedata_offset=sidedata_offset,
2879 sidedata_offset=sidedata_offset,
2882 sidedata_compressed_length=len(serialized_sidedata),
2880 sidedata_compressed_length=len(serialized_sidedata),
2883 sidedata_compression_mode=sidedata_compression_mode,
2881 sidedata_compression_mode=sidedata_compression_mode,
2884 rank=rank,
2882 rank=rank,
2885 )
2883 )
2886
2884
2887 self.index.append(e)
2885 self.index.append(e)
2888 entry = self.index.entry_binary(curr)
2886 entry = self.index.entry_binary(curr)
2889 if curr == 0 and self._docket is None:
2887 if curr == 0 and self._docket is None:
2890 header = self._format_flags | self._format_version
2888 header = self._format_flags | self._format_version
2891 header = self.index.pack_header(header)
2889 header = self.index.pack_header(header)
2892 entry = header + entry
2890 entry = header + entry
2893 self._writeentry(
2891 self._writeentry(
2894 transaction,
2892 transaction,
2895 entry,
2893 entry,
2896 deltainfo.data,
2894 deltainfo.data,
2897 link,
2895 link,
2898 offset,
2896 offset,
2899 serialized_sidedata,
2897 serialized_sidedata,
2900 sidedata_offset,
2898 sidedata_offset,
2901 )
2899 )
2902
2900
2903 rawtext = btext[0]
2901 rawtext = btext[0]
2904
2902
2905 if alwayscache and rawtext is None:
2903 if alwayscache and rawtext is None:
2906 rawtext = deltacomputer.buildtext(revinfo)
2904 rawtext = deltacomputer.buildtext(revinfo)
2907
2905
2908 if type(rawtext) == bytes: # only accept immutable objects
2906 if type(rawtext) == bytes: # only accept immutable objects
2909 self._revisioncache = (node, curr, rawtext)
2907 self._revisioncache = (node, curr, rawtext)
2910 self._chainbasecache[curr] = deltainfo.chainbase
2908 self._chainbasecache[curr] = deltainfo.chainbase
2911 return curr
2909 return curr
2912
2910
2913 def _get_data_offset(self, prev):
2911 def _get_data_offset(self, prev):
2914 """Returns the current offset in the (in-transaction) data file.
2912 """Returns the current offset in the (in-transaction) data file.
2915 Versions < 2 of the revlog can get this 0(1), revlog v2 needs a docket
2913 Versions < 2 of the revlog can get this 0(1), revlog v2 needs a docket
2916 file to store that information: since sidedata can be rewritten to the
2914 file to store that information: since sidedata can be rewritten to the
2917 end of the data file within a transaction, you can have cases where, for
2915 end of the data file within a transaction, you can have cases where, for
2918 example, rev `n` does not have sidedata while rev `n - 1` does, leading
2916 example, rev `n` does not have sidedata while rev `n - 1` does, leading
2919 to `n - 1`'s sidedata being written after `n`'s data.
2917 to `n - 1`'s sidedata being written after `n`'s data.
2920
2918
2921 TODO cache this in a docket file before getting out of experimental."""
2919 TODO cache this in a docket file before getting out of experimental."""
2922 if self._docket is None:
2920 if self._docket is None:
2923 return self.end(prev)
2921 return self.end(prev)
2924 else:
2922 else:
2925 return self._docket.data_end
2923 return self._docket.data_end
2926
2924
2927 def _writeentry(
2925 def _writeentry(
2928 self, transaction, entry, data, link, offset, sidedata, sidedata_offset
2926 self, transaction, entry, data, link, offset, sidedata, sidedata_offset
2929 ):
2927 ):
2930 # Files opened in a+ mode have inconsistent behavior on various
2928 # Files opened in a+ mode have inconsistent behavior on various
2931 # platforms. Windows requires that a file positioning call be made
2929 # platforms. Windows requires that a file positioning call be made
2932 # when the file handle transitions between reads and writes. See
2930 # when the file handle transitions between reads and writes. See
2933 # 3686fa2b8eee and the mixedfilemodewrapper in windows.py. On other
2931 # 3686fa2b8eee and the mixedfilemodewrapper in windows.py. On other
2934 # platforms, Python or the platform itself can be buggy. Some versions
2932 # platforms, Python or the platform itself can be buggy. Some versions
2935 # of Solaris have been observed to not append at the end of the file
2933 # of Solaris have been observed to not append at the end of the file
2936 # if the file was seeked to before the end. See issue4943 for more.
2934 # if the file was seeked to before the end. See issue4943 for more.
2937 #
2935 #
2938 # We work around this issue by inserting a seek() before writing.
2936 # We work around this issue by inserting a seek() before writing.
2939 # Note: This is likely not necessary on Python 3. However, because
2937 # Note: This is likely not necessary on Python 3. However, because
2940 # the file handle is reused for reads and may be seeked there, we need
2938 # the file handle is reused for reads and may be seeked there, we need
2941 # to be careful before changing this.
2939 # to be careful before changing this.
2942 if self._writinghandles is None:
2940 if self._writinghandles is None:
2943 msg = b'adding revision outside `revlog._writing` context'
2941 msg = b'adding revision outside `revlog._writing` context'
2944 raise error.ProgrammingError(msg)
2942 raise error.ProgrammingError(msg)
2945 ifh, dfh, sdfh = self._writinghandles
2943 ifh, dfh, sdfh = self._writinghandles
2946 if self._docket is None:
2944 if self._docket is None:
2947 ifh.seek(0, os.SEEK_END)
2945 ifh.seek(0, os.SEEK_END)
2948 else:
2946 else:
2949 ifh.seek(self._docket.index_end, os.SEEK_SET)
2947 ifh.seek(self._docket.index_end, os.SEEK_SET)
2950 if dfh:
2948 if dfh:
2951 if self._docket is None:
2949 if self._docket is None:
2952 dfh.seek(0, os.SEEK_END)
2950 dfh.seek(0, os.SEEK_END)
2953 else:
2951 else:
2954 dfh.seek(self._docket.data_end, os.SEEK_SET)
2952 dfh.seek(self._docket.data_end, os.SEEK_SET)
2955 if sdfh:
2953 if sdfh:
2956 sdfh.seek(self._docket.sidedata_end, os.SEEK_SET)
2954 sdfh.seek(self._docket.sidedata_end, os.SEEK_SET)
2957
2955
2958 curr = len(self) - 1
2956 curr = len(self) - 1
2959 if not self._inline:
2957 if not self._inline:
2960 transaction.add(self._datafile, offset)
2958 transaction.add(self._datafile, offset)
2961 if self._sidedatafile:
2959 if self._sidedatafile:
2962 transaction.add(self._sidedatafile, sidedata_offset)
2960 transaction.add(self._sidedatafile, sidedata_offset)
2963 transaction.add(self._indexfile, curr * len(entry))
2961 transaction.add(self._indexfile, curr * len(entry))
2964 if data[0]:
2962 if data[0]:
2965 dfh.write(data[0])
2963 dfh.write(data[0])
2966 dfh.write(data[1])
2964 dfh.write(data[1])
2967 if sidedata:
2965 if sidedata:
2968 sdfh.write(sidedata)
2966 sdfh.write(sidedata)
2969 ifh.write(entry)
2967 ifh.write(entry)
2970 else:
2968 else:
2971 offset += curr * self.index.entry_size
2969 offset += curr * self.index.entry_size
2972 transaction.add(self._indexfile, offset)
2970 transaction.add(self._indexfile, offset)
2973 ifh.write(entry)
2971 ifh.write(entry)
2974 ifh.write(data[0])
2972 ifh.write(data[0])
2975 ifh.write(data[1])
2973 ifh.write(data[1])
2976 assert not sidedata
2974 assert not sidedata
2977 self._enforceinlinesize(transaction)
2975 self._enforceinlinesize(transaction)
2978 if self._docket is not None:
2976 if self._docket is not None:
2979 # revlog-v2 always has 3 writing handles, help Pytype
2977 # revlog-v2 always has 3 writing handles, help Pytype
2980 wh1 = self._writinghandles[0]
2978 wh1 = self._writinghandles[0]
2981 wh2 = self._writinghandles[1]
2979 wh2 = self._writinghandles[1]
2982 wh3 = self._writinghandles[2]
2980 wh3 = self._writinghandles[2]
2983 assert wh1 is not None
2981 assert wh1 is not None
2984 assert wh2 is not None
2982 assert wh2 is not None
2985 assert wh3 is not None
2983 assert wh3 is not None
2986 self._docket.index_end = wh1.tell()
2984 self._docket.index_end = wh1.tell()
2987 self._docket.data_end = wh2.tell()
2985 self._docket.data_end = wh2.tell()
2988 self._docket.sidedata_end = wh3.tell()
2986 self._docket.sidedata_end = wh3.tell()
2989
2987
2990 nodemaputil.setup_persistent_nodemap(transaction, self)
2988 nodemaputil.setup_persistent_nodemap(transaction, self)
2991
2989
2992 def addgroup(
2990 def addgroup(
2993 self,
2991 self,
2994 deltas,
2992 deltas,
2995 linkmapper,
2993 linkmapper,
2996 transaction,
2994 transaction,
2997 alwayscache=False,
2995 alwayscache=False,
2998 addrevisioncb=None,
2996 addrevisioncb=None,
2999 duplicaterevisioncb=None,
2997 duplicaterevisioncb=None,
3000 debug_info=None,
2998 debug_info=None,
3001 delta_base_reuse_policy=None,
2999 delta_base_reuse_policy=None,
3002 ):
3000 ):
3003 """
3001 """
3004 add a delta group
3002 add a delta group
3005
3003
3006 given a set of deltas, add them to the revision log. the
3004 given a set of deltas, add them to the revision log. the
3007 first delta is against its parent, which should be in our
3005 first delta is against its parent, which should be in our
3008 log, the rest are against the previous delta.
3006 log, the rest are against the previous delta.
3009
3007
3010 If ``addrevisioncb`` is defined, it will be called with arguments of
3008 If ``addrevisioncb`` is defined, it will be called with arguments of
3011 this revlog and the node that was added.
3009 this revlog and the node that was added.
3012 """
3010 """
3013
3011
3014 if self._adding_group:
3012 if self._adding_group:
3015 raise error.ProgrammingError(b'cannot nest addgroup() calls')
3013 raise error.ProgrammingError(b'cannot nest addgroup() calls')
3016
3014
3017 # read the default delta-base reuse policy from revlog config if the
3015 # read the default delta-base reuse policy from revlog config if the
3018 # group did not specify one.
3016 # group did not specify one.
3019 if delta_base_reuse_policy is None:
3017 if delta_base_reuse_policy is None:
3020 if self._generaldelta and self._lazydeltabase:
3018 if self._generaldelta and self._lazydeltabase:
3021 delta_base_reuse_policy = DELTA_BASE_REUSE_TRY
3019 delta_base_reuse_policy = DELTA_BASE_REUSE_TRY
3022 else:
3020 else:
3023 delta_base_reuse_policy = DELTA_BASE_REUSE_NO
3021 delta_base_reuse_policy = DELTA_BASE_REUSE_NO
3024
3022
3025 self._adding_group = True
3023 self._adding_group = True
3026 empty = True
3024 empty = True
3027 try:
3025 try:
3028 with self._writing(transaction):
3026 with self._writing(transaction):
3029 write_debug = None
3027 write_debug = None
3030 if self._debug_delta:
3028 if self._debug_delta:
3031 write_debug = transaction._report
3029 write_debug = transaction._report
3032 deltacomputer = deltautil.deltacomputer(
3030 deltacomputer = deltautil.deltacomputer(
3033 self,
3031 self,
3034 write_debug=write_debug,
3032 write_debug=write_debug,
3035 debug_info=debug_info,
3033 debug_info=debug_info,
3036 )
3034 )
3037 # loop through our set of deltas
3035 # loop through our set of deltas
3038 for data in deltas:
3036 for data in deltas:
3039 (
3037 (
3040 node,
3038 node,
3041 p1,
3039 p1,
3042 p2,
3040 p2,
3043 linknode,
3041 linknode,
3044 deltabase,
3042 deltabase,
3045 delta,
3043 delta,
3046 flags,
3044 flags,
3047 sidedata,
3045 sidedata,
3048 ) = data
3046 ) = data
3049 link = linkmapper(linknode)
3047 link = linkmapper(linknode)
3050 flags = flags or REVIDX_DEFAULT_FLAGS
3048 flags = flags or REVIDX_DEFAULT_FLAGS
3051
3049
3052 rev = self.index.get_rev(node)
3050 rev = self.index.get_rev(node)
3053 if rev is not None:
3051 if rev is not None:
3054 # this can happen if two branches make the same change
3052 # this can happen if two branches make the same change
3055 self._nodeduplicatecallback(transaction, rev)
3053 self._nodeduplicatecallback(transaction, rev)
3056 if duplicaterevisioncb:
3054 if duplicaterevisioncb:
3057 duplicaterevisioncb(self, rev)
3055 duplicaterevisioncb(self, rev)
3058 empty = False
3056 empty = False
3059 continue
3057 continue
3060
3058
3061 for p in (p1, p2):
3059 for p in (p1, p2):
3062 if not self.index.has_node(p):
3060 if not self.index.has_node(p):
3063 raise error.LookupError(
3061 raise error.LookupError(
3064 p, self.radix, _(b'unknown parent')
3062 p, self.radix, _(b'unknown parent')
3065 )
3063 )
3066
3064
3067 if not self.index.has_node(deltabase):
3065 if not self.index.has_node(deltabase):
3068 raise error.LookupError(
3066 raise error.LookupError(
3069 deltabase, self.display_id, _(b'unknown delta base')
3067 deltabase, self.display_id, _(b'unknown delta base')
3070 )
3068 )
3071
3069
3072 baserev = self.rev(deltabase)
3070 baserev = self.rev(deltabase)
3073
3071
3074 if baserev != nullrev and self.iscensored(baserev):
3072 if baserev != nullrev and self.iscensored(baserev):
3075 # if base is censored, delta must be full replacement in a
3073 # if base is censored, delta must be full replacement in a
3076 # single patch operation
3074 # single patch operation
3077 hlen = struct.calcsize(b">lll")
3075 hlen = struct.calcsize(b">lll")
3078 oldlen = self.rawsize(baserev)
3076 oldlen = self.rawsize(baserev)
3079 newlen = len(delta) - hlen
3077 newlen = len(delta) - hlen
3080 if delta[:hlen] != mdiff.replacediffheader(
3078 if delta[:hlen] != mdiff.replacediffheader(
3081 oldlen, newlen
3079 oldlen, newlen
3082 ):
3080 ):
3083 raise error.CensoredBaseError(
3081 raise error.CensoredBaseError(
3084 self.display_id, self.node(baserev)
3082 self.display_id, self.node(baserev)
3085 )
3083 )
3086
3084
3087 if not flags and self._peek_iscensored(baserev, delta):
3085 if not flags and self._peek_iscensored(baserev, delta):
3088 flags |= REVIDX_ISCENSORED
3086 flags |= REVIDX_ISCENSORED
3089
3087
3090 # We assume consumers of addrevisioncb will want to retrieve
3088 # We assume consumers of addrevisioncb will want to retrieve
3091 # the added revision, which will require a call to
3089 # the added revision, which will require a call to
3092 # revision(). revision() will fast path if there is a cache
3090 # revision(). revision() will fast path if there is a cache
3093 # hit. So, we tell _addrevision() to always cache in this case.
3091 # hit. So, we tell _addrevision() to always cache in this case.
3094 # We're only using addgroup() in the context of changegroup
3092 # We're only using addgroup() in the context of changegroup
3095 # generation so the revision data can always be handled as raw
3093 # generation so the revision data can always be handled as raw
3096 # by the flagprocessor.
3094 # by the flagprocessor.
3097 rev = self._addrevision(
3095 rev = self._addrevision(
3098 node,
3096 node,
3099 None,
3097 None,
3100 transaction,
3098 transaction,
3101 link,
3099 link,
3102 p1,
3100 p1,
3103 p2,
3101 p2,
3104 flags,
3102 flags,
3105 (baserev, delta, delta_base_reuse_policy),
3103 (baserev, delta, delta_base_reuse_policy),
3106 alwayscache=alwayscache,
3104 alwayscache=alwayscache,
3107 deltacomputer=deltacomputer,
3105 deltacomputer=deltacomputer,
3108 sidedata=sidedata,
3106 sidedata=sidedata,
3109 )
3107 )
3110
3108
3111 if addrevisioncb:
3109 if addrevisioncb:
3112 addrevisioncb(self, rev)
3110 addrevisioncb(self, rev)
3113 empty = False
3111 empty = False
3114 finally:
3112 finally:
3115 self._adding_group = False
3113 self._adding_group = False
3116 return not empty
3114 return not empty
3117
3115
3118 def iscensored(self, rev):
3116 def iscensored(self, rev):
3119 """Check if a file revision is censored."""
3117 """Check if a file revision is censored."""
3120 if not self._censorable:
3118 if not self._censorable:
3121 return False
3119 return False
3122
3120
3123 return self.flags(rev) & REVIDX_ISCENSORED
3121 return self.flags(rev) & REVIDX_ISCENSORED
3124
3122
3125 def _peek_iscensored(self, baserev, delta):
3123 def _peek_iscensored(self, baserev, delta):
3126 """Quickly check if a delta produces a censored revision."""
3124 """Quickly check if a delta produces a censored revision."""
3127 if not self._censorable:
3125 if not self._censorable:
3128 return False
3126 return False
3129
3127
3130 return storageutil.deltaiscensored(delta, baserev, self.rawsize)
3128 return storageutil.deltaiscensored(delta, baserev, self.rawsize)
3131
3129
3132 def getstrippoint(self, minlink):
3130 def getstrippoint(self, minlink):
3133 """find the minimum rev that must be stripped to strip the linkrev
3131 """find the minimum rev that must be stripped to strip the linkrev
3134
3132
3135 Returns a tuple containing the minimum rev and a set of all revs that
3133 Returns a tuple containing the minimum rev and a set of all revs that
3136 have linkrevs that will be broken by this strip.
3134 have linkrevs that will be broken by this strip.
3137 """
3135 """
3138 return storageutil.resolvestripinfo(
3136 return storageutil.resolvestripinfo(
3139 minlink,
3137 minlink,
3140 len(self) - 1,
3138 len(self) - 1,
3141 self.headrevs(),
3139 self.headrevs(),
3142 self.linkrev,
3140 self.linkrev,
3143 self.parentrevs,
3141 self.parentrevs,
3144 )
3142 )
3145
3143
3146 def strip(self, minlink, transaction):
3144 def strip(self, minlink, transaction):
3147 """truncate the revlog on the first revision with a linkrev >= minlink
3145 """truncate the revlog on the first revision with a linkrev >= minlink
3148
3146
3149 This function is called when we're stripping revision minlink and
3147 This function is called when we're stripping revision minlink and
3150 its descendants from the repository.
3148 its descendants from the repository.
3151
3149
3152 We have to remove all revisions with linkrev >= minlink, because
3150 We have to remove all revisions with linkrev >= minlink, because
3153 the equivalent changelog revisions will be renumbered after the
3151 the equivalent changelog revisions will be renumbered after the
3154 strip.
3152 strip.
3155
3153
3156 So we truncate the revlog on the first of these revisions, and
3154 So we truncate the revlog on the first of these revisions, and
3157 trust that the caller has saved the revisions that shouldn't be
3155 trust that the caller has saved the revisions that shouldn't be
3158 removed and that it'll re-add them after this truncation.
3156 removed and that it'll re-add them after this truncation.
3159 """
3157 """
3160 if len(self) == 0:
3158 if len(self) == 0:
3161 return
3159 return
3162
3160
3163 rev, _ = self.getstrippoint(minlink)
3161 rev, _ = self.getstrippoint(minlink)
3164 if rev == len(self):
3162 if rev == len(self):
3165 return
3163 return
3166
3164
3167 # first truncate the files on disk
3165 # first truncate the files on disk
3168 data_end = self.start(rev)
3166 data_end = self.start(rev)
3169 if not self._inline:
3167 if not self._inline:
3170 transaction.add(self._datafile, data_end)
3168 transaction.add(self._datafile, data_end)
3171 end = rev * self.index.entry_size
3169 end = rev * self.index.entry_size
3172 else:
3170 else:
3173 end = data_end + (rev * self.index.entry_size)
3171 end = data_end + (rev * self.index.entry_size)
3174
3172
3175 if self._sidedatafile:
3173 if self._sidedatafile:
3176 sidedata_end = self.sidedata_cut_off(rev)
3174 sidedata_end = self.sidedata_cut_off(rev)
3177 transaction.add(self._sidedatafile, sidedata_end)
3175 transaction.add(self._sidedatafile, sidedata_end)
3178
3176
3179 transaction.add(self._indexfile, end)
3177 transaction.add(self._indexfile, end)
3180 if self._docket is not None:
3178 if self._docket is not None:
3181 # XXX we could, leverage the docket while stripping. However it is
3179 # XXX we could, leverage the docket while stripping. However it is
3182 # not powerfull enough at the time of this comment
3180 # not powerfull enough at the time of this comment
3183 self._docket.index_end = end
3181 self._docket.index_end = end
3184 self._docket.data_end = data_end
3182 self._docket.data_end = data_end
3185 self._docket.sidedata_end = sidedata_end
3183 self._docket.sidedata_end = sidedata_end
3186 self._docket.write(transaction, stripping=True)
3184 self._docket.write(transaction, stripping=True)
3187
3185
3188 # then reset internal state in memory to forget those revisions
3186 # then reset internal state in memory to forget those revisions
3189 self._revisioncache = None
3187 self._revisioncache = None
3190 self._chaininfocache = util.lrucachedict(500)
3188 self._chaininfocache = util.lrucachedict(500)
3191 self._segmentfile.clear_cache()
3189 self._segmentfile.clear_cache()
3192 self._segmentfile_sidedata.clear_cache()
3190 self._segmentfile_sidedata.clear_cache()
3193
3191
3194 del self.index[rev:-1]
3192 del self.index[rev:-1]
3195
3193
3196 def checksize(self):
3194 def checksize(self):
3197 """Check size of index and data files
3195 """Check size of index and data files
3198
3196
3199 return a (dd, di) tuple.
3197 return a (dd, di) tuple.
3200 - dd: extra bytes for the "data" file
3198 - dd: extra bytes for the "data" file
3201 - di: extra bytes for the "index" file
3199 - di: extra bytes for the "index" file
3202
3200
3203 A healthy revlog will return (0, 0).
3201 A healthy revlog will return (0, 0).
3204 """
3202 """
3205 expected = 0
3203 expected = 0
3206 if len(self):
3204 if len(self):
3207 expected = max(0, self.end(len(self) - 1))
3205 expected = max(0, self.end(len(self) - 1))
3208
3206
3209 try:
3207 try:
3210 with self._datafp() as f:
3208 with self._datafp() as f:
3211 f.seek(0, io.SEEK_END)
3209 f.seek(0, io.SEEK_END)
3212 actual = f.tell()
3210 actual = f.tell()
3213 dd = actual - expected
3211 dd = actual - expected
3214 except FileNotFoundError:
3212 except FileNotFoundError:
3215 dd = 0
3213 dd = 0
3216
3214
3217 try:
3215 try:
3218 f = self.opener(self._indexfile)
3216 f = self.opener(self._indexfile)
3219 f.seek(0, io.SEEK_END)
3217 f.seek(0, io.SEEK_END)
3220 actual = f.tell()
3218 actual = f.tell()
3221 f.close()
3219 f.close()
3222 s = self.index.entry_size
3220 s = self.index.entry_size
3223 i = max(0, actual // s)
3221 i = max(0, actual // s)
3224 di = actual - (i * s)
3222 di = actual - (i * s)
3225 if self._inline:
3223 if self._inline:
3226 databytes = 0
3224 databytes = 0
3227 for r in self:
3225 for r in self:
3228 databytes += max(0, self.length(r))
3226 databytes += max(0, self.length(r))
3229 dd = 0
3227 dd = 0
3230 di = actual - len(self) * s - databytes
3228 di = actual - len(self) * s - databytes
3231 except FileNotFoundError:
3229 except FileNotFoundError:
3232 di = 0
3230 di = 0
3233
3231
3234 return (dd, di)
3232 return (dd, di)
3235
3233
3236 def files(self):
3234 def files(self):
3237 res = [self._indexfile]
3235 res = [self._indexfile]
3238 if self._docket_file is None:
3236 if self._docket_file is None:
3239 if not self._inline:
3237 if not self._inline:
3240 res.append(self._datafile)
3238 res.append(self._datafile)
3241 else:
3239 else:
3242 res.append(self._docket_file)
3240 res.append(self._docket_file)
3243 res.extend(self._docket.old_index_filepaths(include_empty=False))
3241 res.extend(self._docket.old_index_filepaths(include_empty=False))
3244 if self._docket.data_end:
3242 if self._docket.data_end:
3245 res.append(self._datafile)
3243 res.append(self._datafile)
3246 res.extend(self._docket.old_data_filepaths(include_empty=False))
3244 res.extend(self._docket.old_data_filepaths(include_empty=False))
3247 if self._docket.sidedata_end:
3245 if self._docket.sidedata_end:
3248 res.append(self._sidedatafile)
3246 res.append(self._sidedatafile)
3249 res.extend(self._docket.old_sidedata_filepaths(include_empty=False))
3247 res.extend(self._docket.old_sidedata_filepaths(include_empty=False))
3250 return res
3248 return res
3251
3249
3252 def emitrevisions(
3250 def emitrevisions(
3253 self,
3251 self,
3254 nodes,
3252 nodes,
3255 nodesorder=None,
3253 nodesorder=None,
3256 revisiondata=False,
3254 revisiondata=False,
3257 assumehaveparentrevisions=False,
3255 assumehaveparentrevisions=False,
3258 deltamode=repository.CG_DELTAMODE_STD,
3256 deltamode=repository.CG_DELTAMODE_STD,
3259 sidedata_helpers=None,
3257 sidedata_helpers=None,
3260 debug_info=None,
3258 debug_info=None,
3261 ):
3259 ):
3262 if nodesorder not in (b'nodes', b'storage', b'linear', None):
3260 if nodesorder not in (b'nodes', b'storage', b'linear', None):
3263 raise error.ProgrammingError(
3261 raise error.ProgrammingError(
3264 b'unhandled value for nodesorder: %s' % nodesorder
3262 b'unhandled value for nodesorder: %s' % nodesorder
3265 )
3263 )
3266
3264
3267 if nodesorder is None and not self._generaldelta:
3265 if nodesorder is None and not self._generaldelta:
3268 nodesorder = b'storage'
3266 nodesorder = b'storage'
3269
3267
3270 if (
3268 if (
3271 not self._storedeltachains
3269 not self._storedeltachains
3272 and deltamode != repository.CG_DELTAMODE_PREV
3270 and deltamode != repository.CG_DELTAMODE_PREV
3273 ):
3271 ):
3274 deltamode = repository.CG_DELTAMODE_FULL
3272 deltamode = repository.CG_DELTAMODE_FULL
3275
3273
3276 return storageutil.emitrevisions(
3274 return storageutil.emitrevisions(
3277 self,
3275 self,
3278 nodes,
3276 nodes,
3279 nodesorder,
3277 nodesorder,
3280 revlogrevisiondelta,
3278 revlogrevisiondelta,
3281 deltaparentfn=self.deltaparent,
3279 deltaparentfn=self.deltaparent,
3282 candeltafn=self._candelta,
3280 candeltafn=self._candelta,
3283 rawsizefn=self.rawsize,
3281 rawsizefn=self.rawsize,
3284 revdifffn=self.revdiff,
3282 revdifffn=self.revdiff,
3285 flagsfn=self.flags,
3283 flagsfn=self.flags,
3286 deltamode=deltamode,
3284 deltamode=deltamode,
3287 revisiondata=revisiondata,
3285 revisiondata=revisiondata,
3288 assumehaveparentrevisions=assumehaveparentrevisions,
3286 assumehaveparentrevisions=assumehaveparentrevisions,
3289 sidedata_helpers=sidedata_helpers,
3287 sidedata_helpers=sidedata_helpers,
3290 debug_info=debug_info,
3288 debug_info=debug_info,
3291 )
3289 )
3292
3290
3293 DELTAREUSEALWAYS = b'always'
3291 DELTAREUSEALWAYS = b'always'
3294 DELTAREUSESAMEREVS = b'samerevs'
3292 DELTAREUSESAMEREVS = b'samerevs'
3295 DELTAREUSENEVER = b'never'
3293 DELTAREUSENEVER = b'never'
3296
3294
3297 DELTAREUSEFULLADD = b'fulladd'
3295 DELTAREUSEFULLADD = b'fulladd'
3298
3296
3299 DELTAREUSEALL = {b'always', b'samerevs', b'never', b'fulladd'}
3297 DELTAREUSEALL = {b'always', b'samerevs', b'never', b'fulladd'}
3300
3298
3301 def clone(
3299 def clone(
3302 self,
3300 self,
3303 tr,
3301 tr,
3304 destrevlog,
3302 destrevlog,
3305 addrevisioncb=None,
3303 addrevisioncb=None,
3306 deltareuse=DELTAREUSESAMEREVS,
3304 deltareuse=DELTAREUSESAMEREVS,
3307 forcedeltabothparents=None,
3305 forcedeltabothparents=None,
3308 sidedata_helpers=None,
3306 sidedata_helpers=None,
3309 ):
3307 ):
3310 """Copy this revlog to another, possibly with format changes.
3308 """Copy this revlog to another, possibly with format changes.
3311
3309
3312 The destination revlog will contain the same revisions and nodes.
3310 The destination revlog will contain the same revisions and nodes.
3313 However, it may not be bit-for-bit identical due to e.g. delta encoding
3311 However, it may not be bit-for-bit identical due to e.g. delta encoding
3314 differences.
3312 differences.
3315
3313
3316 The ``deltareuse`` argument control how deltas from the existing revlog
3314 The ``deltareuse`` argument control how deltas from the existing revlog
3317 are preserved in the destination revlog. The argument can have the
3315 are preserved in the destination revlog. The argument can have the
3318 following values:
3316 following values:
3319
3317
3320 DELTAREUSEALWAYS
3318 DELTAREUSEALWAYS
3321 Deltas will always be reused (if possible), even if the destination
3319 Deltas will always be reused (if possible), even if the destination
3322 revlog would not select the same revisions for the delta. This is the
3320 revlog would not select the same revisions for the delta. This is the
3323 fastest mode of operation.
3321 fastest mode of operation.
3324 DELTAREUSESAMEREVS
3322 DELTAREUSESAMEREVS
3325 Deltas will be reused if the destination revlog would pick the same
3323 Deltas will be reused if the destination revlog would pick the same
3326 revisions for the delta. This mode strikes a balance between speed
3324 revisions for the delta. This mode strikes a balance between speed
3327 and optimization.
3325 and optimization.
3328 DELTAREUSENEVER
3326 DELTAREUSENEVER
3329 Deltas will never be reused. This is the slowest mode of execution.
3327 Deltas will never be reused. This is the slowest mode of execution.
3330 This mode can be used to recompute deltas (e.g. if the diff/delta
3328 This mode can be used to recompute deltas (e.g. if the diff/delta
3331 algorithm changes).
3329 algorithm changes).
3332 DELTAREUSEFULLADD
3330 DELTAREUSEFULLADD
3333 Revision will be re-added as if their were new content. This is
3331 Revision will be re-added as if their were new content. This is
3334 slower than DELTAREUSEALWAYS but allow more mechanism to kicks in.
3332 slower than DELTAREUSEALWAYS but allow more mechanism to kicks in.
3335 eg: large file detection and handling.
3333 eg: large file detection and handling.
3336
3334
3337 Delta computation can be slow, so the choice of delta reuse policy can
3335 Delta computation can be slow, so the choice of delta reuse policy can
3338 significantly affect run time.
3336 significantly affect run time.
3339
3337
3340 The default policy (``DELTAREUSESAMEREVS``) strikes a balance between
3338 The default policy (``DELTAREUSESAMEREVS``) strikes a balance between
3341 two extremes. Deltas will be reused if they are appropriate. But if the
3339 two extremes. Deltas will be reused if they are appropriate. But if the
3342 delta could choose a better revision, it will do so. This means if you
3340 delta could choose a better revision, it will do so. This means if you
3343 are converting a non-generaldelta revlog to a generaldelta revlog,
3341 are converting a non-generaldelta revlog to a generaldelta revlog,
3344 deltas will be recomputed if the delta's parent isn't a parent of the
3342 deltas will be recomputed if the delta's parent isn't a parent of the
3345 revision.
3343 revision.
3346
3344
3347 In addition to the delta policy, the ``forcedeltabothparents``
3345 In addition to the delta policy, the ``forcedeltabothparents``
3348 argument controls whether to force compute deltas against both parents
3346 argument controls whether to force compute deltas against both parents
3349 for merges. By default, the current default is used.
3347 for merges. By default, the current default is used.
3350
3348
3351 See `revlogutil.sidedata.get_sidedata_helpers` for the doc on
3349 See `revlogutil.sidedata.get_sidedata_helpers` for the doc on
3352 `sidedata_helpers`.
3350 `sidedata_helpers`.
3353 """
3351 """
3354 if deltareuse not in self.DELTAREUSEALL:
3352 if deltareuse not in self.DELTAREUSEALL:
3355 raise ValueError(
3353 raise ValueError(
3356 _(b'value for deltareuse invalid: %s') % deltareuse
3354 _(b'value for deltareuse invalid: %s') % deltareuse
3357 )
3355 )
3358
3356
3359 if len(destrevlog):
3357 if len(destrevlog):
3360 raise ValueError(_(b'destination revlog is not empty'))
3358 raise ValueError(_(b'destination revlog is not empty'))
3361
3359
3362 if getattr(self, 'filteredrevs', None):
3360 if getattr(self, 'filteredrevs', None):
3363 raise ValueError(_(b'source revlog has filtered revisions'))
3361 raise ValueError(_(b'source revlog has filtered revisions'))
3364 if getattr(destrevlog, 'filteredrevs', None):
3362 if getattr(destrevlog, 'filteredrevs', None):
3365 raise ValueError(_(b'destination revlog has filtered revisions'))
3363 raise ValueError(_(b'destination revlog has filtered revisions'))
3366
3364
3367 # lazydelta and lazydeltabase controls whether to reuse a cached delta,
3365 # lazydelta and lazydeltabase controls whether to reuse a cached delta,
3368 # if possible.
3366 # if possible.
3369 old_delta_config = destrevlog.delta_config
3367 old_delta_config = destrevlog.delta_config
3370 destrevlog.delta_config = destrevlog.delta_config.copy()
3368 destrevlog.delta_config = destrevlog.delta_config.copy()
3371
3369
3372 try:
3370 try:
3373 if deltareuse == self.DELTAREUSEALWAYS:
3371 if deltareuse == self.DELTAREUSEALWAYS:
3374 destrevlog.delta_config.lazy_delta_base = True
3372 destrevlog.delta_config.lazy_delta_base = True
3375 destrevlog.delta_config.lazy_delta = True
3373 destrevlog.delta_config.lazy_delta = True
3376 elif deltareuse == self.DELTAREUSESAMEREVS:
3374 elif deltareuse == self.DELTAREUSESAMEREVS:
3377 destrevlog.delta_config.lazy_delta_base = False
3375 destrevlog.delta_config.lazy_delta_base = False
3378 destrevlog.delta_config.lazy_delta = True
3376 destrevlog.delta_config.lazy_delta = True
3379 elif deltareuse == self.DELTAREUSENEVER:
3377 elif deltareuse == self.DELTAREUSENEVER:
3380 destrevlog.delta_config.lazy_delta_base = False
3378 destrevlog.delta_config.lazy_delta_base = False
3381 destrevlog.delta_config.lazy_delta = False
3379 destrevlog.delta_config.lazy_delta = False
3382
3380
3383 delta_both_parents = (
3381 delta_both_parents = (
3384 forcedeltabothparents or old_delta_config.delta_both_parents
3382 forcedeltabothparents or old_delta_config.delta_both_parents
3385 )
3383 )
3386 destrevlog.delta_config.delta_both_parents = delta_both_parents
3384 destrevlog.delta_config.delta_both_parents = delta_both_parents
3387
3385
3388 with self.reading():
3386 with self.reading():
3389 self._clone(
3387 self._clone(
3390 tr,
3388 tr,
3391 destrevlog,
3389 destrevlog,
3392 addrevisioncb,
3390 addrevisioncb,
3393 deltareuse,
3391 deltareuse,
3394 forcedeltabothparents,
3392 forcedeltabothparents,
3395 sidedata_helpers,
3393 sidedata_helpers,
3396 )
3394 )
3397
3395
3398 finally:
3396 finally:
3399 destrevlog.delta_config = old_delta_config
3397 destrevlog.delta_config = old_delta_config
3400
3398
3401 def _clone(
3399 def _clone(
3402 self,
3400 self,
3403 tr,
3401 tr,
3404 destrevlog,
3402 destrevlog,
3405 addrevisioncb,
3403 addrevisioncb,
3406 deltareuse,
3404 deltareuse,
3407 forcedeltabothparents,
3405 forcedeltabothparents,
3408 sidedata_helpers,
3406 sidedata_helpers,
3409 ):
3407 ):
3410 """perform the core duty of `revlog.clone` after parameter processing"""
3408 """perform the core duty of `revlog.clone` after parameter processing"""
3411 write_debug = None
3409 write_debug = None
3412 if self._debug_delta:
3410 if self._debug_delta:
3413 write_debug = tr._report
3411 write_debug = tr._report
3414 deltacomputer = deltautil.deltacomputer(
3412 deltacomputer = deltautil.deltacomputer(
3415 destrevlog,
3413 destrevlog,
3416 write_debug=write_debug,
3414 write_debug=write_debug,
3417 )
3415 )
3418 index = self.index
3416 index = self.index
3419 for rev in self:
3417 for rev in self:
3420 entry = index[rev]
3418 entry = index[rev]
3421
3419
3422 # Some classes override linkrev to take filtered revs into
3420 # Some classes override linkrev to take filtered revs into
3423 # account. Use raw entry from index.
3421 # account. Use raw entry from index.
3424 flags = entry[0] & 0xFFFF
3422 flags = entry[0] & 0xFFFF
3425 linkrev = entry[4]
3423 linkrev = entry[4]
3426 p1 = index[entry[5]][7]
3424 p1 = index[entry[5]][7]
3427 p2 = index[entry[6]][7]
3425 p2 = index[entry[6]][7]
3428 node = entry[7]
3426 node = entry[7]
3429
3427
3430 # (Possibly) reuse the delta from the revlog if allowed and
3428 # (Possibly) reuse the delta from the revlog if allowed and
3431 # the revlog chunk is a delta.
3429 # the revlog chunk is a delta.
3432 cachedelta = None
3430 cachedelta = None
3433 rawtext = None
3431 rawtext = None
3434 if deltareuse == self.DELTAREUSEFULLADD:
3432 if deltareuse == self.DELTAREUSEFULLADD:
3435 text = self._revisiondata(rev)
3433 text = self._revisiondata(rev)
3436 sidedata = self.sidedata(rev)
3434 sidedata = self.sidedata(rev)
3437
3435
3438 if sidedata_helpers is not None:
3436 if sidedata_helpers is not None:
3439 (sidedata, new_flags) = sidedatautil.run_sidedata_helpers(
3437 (sidedata, new_flags) = sidedatautil.run_sidedata_helpers(
3440 self, sidedata_helpers, sidedata, rev
3438 self, sidedata_helpers, sidedata, rev
3441 )
3439 )
3442 flags = flags | new_flags[0] & ~new_flags[1]
3440 flags = flags | new_flags[0] & ~new_flags[1]
3443
3441
3444 destrevlog.addrevision(
3442 destrevlog.addrevision(
3445 text,
3443 text,
3446 tr,
3444 tr,
3447 linkrev,
3445 linkrev,
3448 p1,
3446 p1,
3449 p2,
3447 p2,
3450 cachedelta=cachedelta,
3448 cachedelta=cachedelta,
3451 node=node,
3449 node=node,
3452 flags=flags,
3450 flags=flags,
3453 deltacomputer=deltacomputer,
3451 deltacomputer=deltacomputer,
3454 sidedata=sidedata,
3452 sidedata=sidedata,
3455 )
3453 )
3456 else:
3454 else:
3457 if destrevlog._lazydelta:
3455 if destrevlog._lazydelta:
3458 dp = self.deltaparent(rev)
3456 dp = self.deltaparent(rev)
3459 if dp != nullrev:
3457 if dp != nullrev:
3460 cachedelta = (dp, bytes(self._chunk(rev)))
3458 cachedelta = (dp, bytes(self._chunk(rev)))
3461
3459
3462 sidedata = None
3460 sidedata = None
3463 if not cachedelta:
3461 if not cachedelta:
3464 rawtext = self._revisiondata(rev)
3462 rawtext = self._revisiondata(rev)
3465 sidedata = self.sidedata(rev)
3463 sidedata = self.sidedata(rev)
3466 if sidedata is None:
3464 if sidedata is None:
3467 sidedata = self.sidedata(rev)
3465 sidedata = self.sidedata(rev)
3468
3466
3469 if sidedata_helpers is not None:
3467 if sidedata_helpers is not None:
3470 (sidedata, new_flags) = sidedatautil.run_sidedata_helpers(
3468 (sidedata, new_flags) = sidedatautil.run_sidedata_helpers(
3471 self, sidedata_helpers, sidedata, rev
3469 self, sidedata_helpers, sidedata, rev
3472 )
3470 )
3473 flags = flags | new_flags[0] & ~new_flags[1]
3471 flags = flags | new_flags[0] & ~new_flags[1]
3474
3472
3475 with destrevlog._writing(tr):
3473 with destrevlog._writing(tr):
3476 destrevlog._addrevision(
3474 destrevlog._addrevision(
3477 node,
3475 node,
3478 rawtext,
3476 rawtext,
3479 tr,
3477 tr,
3480 linkrev,
3478 linkrev,
3481 p1,
3479 p1,
3482 p2,
3480 p2,
3483 flags,
3481 flags,
3484 cachedelta,
3482 cachedelta,
3485 deltacomputer=deltacomputer,
3483 deltacomputer=deltacomputer,
3486 sidedata=sidedata,
3484 sidedata=sidedata,
3487 )
3485 )
3488
3486
3489 if addrevisioncb:
3487 if addrevisioncb:
3490 addrevisioncb(self, rev, node)
3488 addrevisioncb(self, rev, node)
3491
3489
3492 def censorrevision(self, tr, censornode, tombstone=b''):
3490 def censorrevision(self, tr, censornode, tombstone=b''):
3493 if self._format_version == REVLOGV0:
3491 if self._format_version == REVLOGV0:
3494 raise error.RevlogError(
3492 raise error.RevlogError(
3495 _(b'cannot censor with version %d revlogs')
3493 _(b'cannot censor with version %d revlogs')
3496 % self._format_version
3494 % self._format_version
3497 )
3495 )
3498 elif self._format_version == REVLOGV1:
3496 elif self._format_version == REVLOGV1:
3499 rewrite.v1_censor(self, tr, censornode, tombstone)
3497 rewrite.v1_censor(self, tr, censornode, tombstone)
3500 else:
3498 else:
3501 rewrite.v2_censor(self, tr, censornode, tombstone)
3499 rewrite.v2_censor(self, tr, censornode, tombstone)
3502
3500
3503 def verifyintegrity(self, state):
3501 def verifyintegrity(self, state):
3504 """Verifies the integrity of the revlog.
3502 """Verifies the integrity of the revlog.
3505
3503
3506 Yields ``revlogproblem`` instances describing problems that are
3504 Yields ``revlogproblem`` instances describing problems that are
3507 found.
3505 found.
3508 """
3506 """
3509 dd, di = self.checksize()
3507 dd, di = self.checksize()
3510 if dd:
3508 if dd:
3511 yield revlogproblem(error=_(b'data length off by %d bytes') % dd)
3509 yield revlogproblem(error=_(b'data length off by %d bytes') % dd)
3512 if di:
3510 if di:
3513 yield revlogproblem(error=_(b'index contains %d extra bytes') % di)
3511 yield revlogproblem(error=_(b'index contains %d extra bytes') % di)
3514
3512
3515 version = self._format_version
3513 version = self._format_version
3516
3514
3517 # The verifier tells us what version revlog we should be.
3515 # The verifier tells us what version revlog we should be.
3518 if version != state[b'expectedversion']:
3516 if version != state[b'expectedversion']:
3519 yield revlogproblem(
3517 yield revlogproblem(
3520 warning=_(b"warning: '%s' uses revlog format %d; expected %d")
3518 warning=_(b"warning: '%s' uses revlog format %d; expected %d")
3521 % (self.display_id, version, state[b'expectedversion'])
3519 % (self.display_id, version, state[b'expectedversion'])
3522 )
3520 )
3523
3521
3524 state[b'skipread'] = set()
3522 state[b'skipread'] = set()
3525 state[b'safe_renamed'] = set()
3523 state[b'safe_renamed'] = set()
3526
3524
3527 for rev in self:
3525 for rev in self:
3528 node = self.node(rev)
3526 node = self.node(rev)
3529
3527
3530 # Verify contents. 4 cases to care about:
3528 # Verify contents. 4 cases to care about:
3531 #
3529 #
3532 # common: the most common case
3530 # common: the most common case
3533 # rename: with a rename
3531 # rename: with a rename
3534 # meta: file content starts with b'\1\n', the metadata
3532 # meta: file content starts with b'\1\n', the metadata
3535 # header defined in filelog.py, but without a rename
3533 # header defined in filelog.py, but without a rename
3536 # ext: content stored externally
3534 # ext: content stored externally
3537 #
3535 #
3538 # More formally, their differences are shown below:
3536 # More formally, their differences are shown below:
3539 #
3537 #
3540 # | common | rename | meta | ext
3538 # | common | rename | meta | ext
3541 # -------------------------------------------------------
3539 # -------------------------------------------------------
3542 # flags() | 0 | 0 | 0 | not 0
3540 # flags() | 0 | 0 | 0 | not 0
3543 # renamed() | False | True | False | ?
3541 # renamed() | False | True | False | ?
3544 # rawtext[0:2]=='\1\n'| False | True | True | ?
3542 # rawtext[0:2]=='\1\n'| False | True | True | ?
3545 #
3543 #
3546 # "rawtext" means the raw text stored in revlog data, which
3544 # "rawtext" means the raw text stored in revlog data, which
3547 # could be retrieved by "rawdata(rev)". "text"
3545 # could be retrieved by "rawdata(rev)". "text"
3548 # mentioned below is "revision(rev)".
3546 # mentioned below is "revision(rev)".
3549 #
3547 #
3550 # There are 3 different lengths stored physically:
3548 # There are 3 different lengths stored physically:
3551 # 1. L1: rawsize, stored in revlog index
3549 # 1. L1: rawsize, stored in revlog index
3552 # 2. L2: len(rawtext), stored in revlog data
3550 # 2. L2: len(rawtext), stored in revlog data
3553 # 3. L3: len(text), stored in revlog data if flags==0, or
3551 # 3. L3: len(text), stored in revlog data if flags==0, or
3554 # possibly somewhere else if flags!=0
3552 # possibly somewhere else if flags!=0
3555 #
3553 #
3556 # L1 should be equal to L2. L3 could be different from them.
3554 # L1 should be equal to L2. L3 could be different from them.
3557 # "text" may or may not affect commit hash depending on flag
3555 # "text" may or may not affect commit hash depending on flag
3558 # processors (see flagutil.addflagprocessor).
3556 # processors (see flagutil.addflagprocessor).
3559 #
3557 #
3560 # | common | rename | meta | ext
3558 # | common | rename | meta | ext
3561 # -------------------------------------------------
3559 # -------------------------------------------------
3562 # rawsize() | L1 | L1 | L1 | L1
3560 # rawsize() | L1 | L1 | L1 | L1
3563 # size() | L1 | L2-LM | L1(*) | L1 (?)
3561 # size() | L1 | L2-LM | L1(*) | L1 (?)
3564 # len(rawtext) | L2 | L2 | L2 | L2
3562 # len(rawtext) | L2 | L2 | L2 | L2
3565 # len(text) | L2 | L2 | L2 | L3
3563 # len(text) | L2 | L2 | L2 | L3
3566 # len(read()) | L2 | L2-LM | L2-LM | L3 (?)
3564 # len(read()) | L2 | L2-LM | L2-LM | L3 (?)
3567 #
3565 #
3568 # LM: length of metadata, depending on rawtext
3566 # LM: length of metadata, depending on rawtext
3569 # (*): not ideal, see comment in filelog.size
3567 # (*): not ideal, see comment in filelog.size
3570 # (?): could be "- len(meta)" if the resolved content has
3568 # (?): could be "- len(meta)" if the resolved content has
3571 # rename metadata
3569 # rename metadata
3572 #
3570 #
3573 # Checks needed to be done:
3571 # Checks needed to be done:
3574 # 1. length check: L1 == L2, in all cases.
3572 # 1. length check: L1 == L2, in all cases.
3575 # 2. hash check: depending on flag processor, we may need to
3573 # 2. hash check: depending on flag processor, we may need to
3576 # use either "text" (external), or "rawtext" (in revlog).
3574 # use either "text" (external), or "rawtext" (in revlog).
3577
3575
3578 try:
3576 try:
3579 skipflags = state.get(b'skipflags', 0)
3577 skipflags = state.get(b'skipflags', 0)
3580 if skipflags:
3578 if skipflags:
3581 skipflags &= self.flags(rev)
3579 skipflags &= self.flags(rev)
3582
3580
3583 _verify_revision(self, skipflags, state, node)
3581 _verify_revision(self, skipflags, state, node)
3584
3582
3585 l1 = self.rawsize(rev)
3583 l1 = self.rawsize(rev)
3586 l2 = len(self.rawdata(node))
3584 l2 = len(self.rawdata(node))
3587
3585
3588 if l1 != l2:
3586 if l1 != l2:
3589 yield revlogproblem(
3587 yield revlogproblem(
3590 error=_(b'unpacked size is %d, %d expected') % (l2, l1),
3588 error=_(b'unpacked size is %d, %d expected') % (l2, l1),
3591 node=node,
3589 node=node,
3592 )
3590 )
3593
3591
3594 except error.CensoredNodeError:
3592 except error.CensoredNodeError:
3595 if state[b'erroroncensored']:
3593 if state[b'erroroncensored']:
3596 yield revlogproblem(
3594 yield revlogproblem(
3597 error=_(b'censored file data'), node=node
3595 error=_(b'censored file data'), node=node
3598 )
3596 )
3599 state[b'skipread'].add(node)
3597 state[b'skipread'].add(node)
3600 except Exception as e:
3598 except Exception as e:
3601 yield revlogproblem(
3599 yield revlogproblem(
3602 error=_(b'unpacking %s: %s')
3600 error=_(b'unpacking %s: %s')
3603 % (short(node), stringutil.forcebytestr(e)),
3601 % (short(node), stringutil.forcebytestr(e)),
3604 node=node,
3602 node=node,
3605 )
3603 )
3606 state[b'skipread'].add(node)
3604 state[b'skipread'].add(node)
3607
3605
3608 def storageinfo(
3606 def storageinfo(
3609 self,
3607 self,
3610 exclusivefiles=False,
3608 exclusivefiles=False,
3611 sharedfiles=False,
3609 sharedfiles=False,
3612 revisionscount=False,
3610 revisionscount=False,
3613 trackedsize=False,
3611 trackedsize=False,
3614 storedsize=False,
3612 storedsize=False,
3615 ):
3613 ):
3616 d = {}
3614 d = {}
3617
3615
3618 if exclusivefiles:
3616 if exclusivefiles:
3619 d[b'exclusivefiles'] = [(self.opener, self._indexfile)]
3617 d[b'exclusivefiles'] = [(self.opener, self._indexfile)]
3620 if not self._inline:
3618 if not self._inline:
3621 d[b'exclusivefiles'].append((self.opener, self._datafile))
3619 d[b'exclusivefiles'].append((self.opener, self._datafile))
3622
3620
3623 if sharedfiles:
3621 if sharedfiles:
3624 d[b'sharedfiles'] = []
3622 d[b'sharedfiles'] = []
3625
3623
3626 if revisionscount:
3624 if revisionscount:
3627 d[b'revisionscount'] = len(self)
3625 d[b'revisionscount'] = len(self)
3628
3626
3629 if trackedsize:
3627 if trackedsize:
3630 d[b'trackedsize'] = sum(map(self.rawsize, iter(self)))
3628 d[b'trackedsize'] = sum(map(self.rawsize, iter(self)))
3631
3629
3632 if storedsize:
3630 if storedsize:
3633 d[b'storedsize'] = sum(
3631 d[b'storedsize'] = sum(
3634 self.opener.stat(path).st_size for path in self.files()
3632 self.opener.stat(path).st_size for path in self.files()
3635 )
3633 )
3636
3634
3637 return d
3635 return d
3638
3636
3639 def rewrite_sidedata(self, transaction, helpers, startrev, endrev):
3637 def rewrite_sidedata(self, transaction, helpers, startrev, endrev):
3640 if not self.hassidedata:
3638 if not self.hassidedata:
3641 return
3639 return
3642 # revlog formats with sidedata support does not support inline
3640 # revlog formats with sidedata support does not support inline
3643 assert not self._inline
3641 assert not self._inline
3644 if not helpers[1] and not helpers[2]:
3642 if not helpers[1] and not helpers[2]:
3645 # Nothing to generate or remove
3643 # Nothing to generate or remove
3646 return
3644 return
3647
3645
3648 new_entries = []
3646 new_entries = []
3649 # append the new sidedata
3647 # append the new sidedata
3650 with self._writing(transaction):
3648 with self._writing(transaction):
3651 ifh, dfh, sdfh = self._writinghandles
3649 ifh, dfh, sdfh = self._writinghandles
3652 dfh.seek(self._docket.sidedata_end, os.SEEK_SET)
3650 dfh.seek(self._docket.sidedata_end, os.SEEK_SET)
3653
3651
3654 current_offset = sdfh.tell()
3652 current_offset = sdfh.tell()
3655 for rev in range(startrev, endrev + 1):
3653 for rev in range(startrev, endrev + 1):
3656 entry = self.index[rev]
3654 entry = self.index[rev]
3657 new_sidedata, flags = sidedatautil.run_sidedata_helpers(
3655 new_sidedata, flags = sidedatautil.run_sidedata_helpers(
3658 store=self,
3656 store=self,
3659 sidedata_helpers=helpers,
3657 sidedata_helpers=helpers,
3660 sidedata={},
3658 sidedata={},
3661 rev=rev,
3659 rev=rev,
3662 )
3660 )
3663
3661
3664 serialized_sidedata = sidedatautil.serialize_sidedata(
3662 serialized_sidedata = sidedatautil.serialize_sidedata(
3665 new_sidedata
3663 new_sidedata
3666 )
3664 )
3667
3665
3668 sidedata_compression_mode = COMP_MODE_INLINE
3666 sidedata_compression_mode = COMP_MODE_INLINE
3669 if serialized_sidedata and self.hassidedata:
3667 if serialized_sidedata and self.hassidedata:
3670 sidedata_compression_mode = COMP_MODE_PLAIN
3668 sidedata_compression_mode = COMP_MODE_PLAIN
3671 h, comp_sidedata = self.compress(serialized_sidedata)
3669 h, comp_sidedata = self.compress(serialized_sidedata)
3672 if (
3670 if (
3673 h != b'u'
3671 h != b'u'
3674 and comp_sidedata[0] != b'\0'
3672 and comp_sidedata[0] != b'\0'
3675 and len(comp_sidedata) < len(serialized_sidedata)
3673 and len(comp_sidedata) < len(serialized_sidedata)
3676 ):
3674 ):
3677 assert not h
3675 assert not h
3678 if (
3676 if (
3679 comp_sidedata[0]
3677 comp_sidedata[0]
3680 == self._docket.default_compression_header
3678 == self._docket.default_compression_header
3681 ):
3679 ):
3682 sidedata_compression_mode = COMP_MODE_DEFAULT
3680 sidedata_compression_mode = COMP_MODE_DEFAULT
3683 serialized_sidedata = comp_sidedata
3681 serialized_sidedata = comp_sidedata
3684 else:
3682 else:
3685 sidedata_compression_mode = COMP_MODE_INLINE
3683 sidedata_compression_mode = COMP_MODE_INLINE
3686 serialized_sidedata = comp_sidedata
3684 serialized_sidedata = comp_sidedata
3687 if entry[8] != 0 or entry[9] != 0:
3685 if entry[8] != 0 or entry[9] != 0:
3688 # rewriting entries that already have sidedata is not
3686 # rewriting entries that already have sidedata is not
3689 # supported yet, because it introduces garbage data in the
3687 # supported yet, because it introduces garbage data in the
3690 # revlog.
3688 # revlog.
3691 msg = b"rewriting existing sidedata is not supported yet"
3689 msg = b"rewriting existing sidedata is not supported yet"
3692 raise error.Abort(msg)
3690 raise error.Abort(msg)
3693
3691
3694 # Apply (potential) flags to add and to remove after running
3692 # Apply (potential) flags to add and to remove after running
3695 # the sidedata helpers
3693 # the sidedata helpers
3696 new_offset_flags = entry[0] | flags[0] & ~flags[1]
3694 new_offset_flags = entry[0] | flags[0] & ~flags[1]
3697 entry_update = (
3695 entry_update = (
3698 current_offset,
3696 current_offset,
3699 len(serialized_sidedata),
3697 len(serialized_sidedata),
3700 new_offset_flags,
3698 new_offset_flags,
3701 sidedata_compression_mode,
3699 sidedata_compression_mode,
3702 )
3700 )
3703
3701
3704 # the sidedata computation might have move the file cursors around
3702 # the sidedata computation might have move the file cursors around
3705 sdfh.seek(current_offset, os.SEEK_SET)
3703 sdfh.seek(current_offset, os.SEEK_SET)
3706 sdfh.write(serialized_sidedata)
3704 sdfh.write(serialized_sidedata)
3707 new_entries.append(entry_update)
3705 new_entries.append(entry_update)
3708 current_offset += len(serialized_sidedata)
3706 current_offset += len(serialized_sidedata)
3709 self._docket.sidedata_end = sdfh.tell()
3707 self._docket.sidedata_end = sdfh.tell()
3710
3708
3711 # rewrite the new index entries
3709 # rewrite the new index entries
3712 ifh.seek(startrev * self.index.entry_size)
3710 ifh.seek(startrev * self.index.entry_size)
3713 for i, e in enumerate(new_entries):
3711 for i, e in enumerate(new_entries):
3714 rev = startrev + i
3712 rev = startrev + i
3715 self.index.replace_sidedata_info(rev, *e)
3713 self.index.replace_sidedata_info(rev, *e)
3716 packed = self.index.entry_binary(rev)
3714 packed = self.index.entry_binary(rev)
3717 if rev == 0 and self._docket is None:
3715 if rev == 0 and self._docket is None:
3718 header = self._format_flags | self._format_version
3716 header = self._format_flags | self._format_version
3719 header = self.index.pack_header(header)
3717 header = self.index.pack_header(header)
3720 packed = header + packed
3718 packed = header + packed
3721 ifh.write(packed)
3719 ifh.write(packed)
@@ -1,528 +1,528 b''
1 # test revlog interaction about raw data (flagprocessor)
1 # test revlog interaction about raw data (flagprocessor)
2
2
3
3
4 import hashlib
4 import hashlib
5 import sys
5 import sys
6
6
7 from mercurial import (
7 from mercurial import (
8 encoding,
8 encoding,
9 revlog,
9 revlog,
10 transaction,
10 transaction,
11 vfs,
11 vfs,
12 )
12 )
13
13
14 from mercurial.revlogutils import (
14 from mercurial.revlogutils import (
15 constants,
15 constants,
16 deltas,
16 deltas,
17 flagutil,
17 flagutil,
18 )
18 )
19
19
20
20
21 class _NoTransaction:
21 class _NoTransaction:
22 """transaction like object to update the nodemap outside a transaction"""
22 """transaction like object to update the nodemap outside a transaction"""
23
23
24 def __init__(self):
24 def __init__(self):
25 self._postclose = {}
25 self._postclose = {}
26
26
27 def addpostclose(self, callback_id, callback_func):
27 def addpostclose(self, callback_id, callback_func):
28 self._postclose[callback_id] = callback_func
28 self._postclose[callback_id] = callback_func
29
29
30 def registertmp(self, *args, **kwargs):
30 def registertmp(self, *args, **kwargs):
31 pass
31 pass
32
32
33 def addbackup(self, *args, **kwargs):
33 def addbackup(self, *args, **kwargs):
34 pass
34 pass
35
35
36 def add(self, *args, **kwargs):
36 def add(self, *args, **kwargs):
37 pass
37 pass
38
38
39 def addabort(self, *args, **kwargs):
39 def addabort(self, *args, **kwargs):
40 pass
40 pass
41
41
42 def _report(self, *args):
42 def _report(self, *args):
43 pass
43 pass
44
44
45
45
46 # TESTTMP is optional. This makes it convenient to run without run-tests.py
46 # TESTTMP is optional. This makes it convenient to run without run-tests.py
47 tvfs = vfs.vfs(encoding.environ.get(b'TESTTMP', b'/tmp'))
47 tvfs = vfs.vfs(encoding.environ.get(b'TESTTMP', b'/tmp'))
48
48
49 # Enable generaldelta otherwise revlog won't use delta as expected by the test
49 # Enable generaldelta otherwise revlog won't use delta as expected by the test
50 tvfs.options = {
50 tvfs.options = {
51 b'generaldelta': True,
51 b'generaldelta': True,
52 b'revlogv1': True,
52 b'revlogv1': True,
53 b'sparse-revlog': True,
53 b'delta-config': revlog.DeltaConfig(sparse_revlog=True),
54 }
54 }
55
55
56
56
57 def abort(msg):
57 def abort(msg):
58 print('abort: %s' % msg)
58 print('abort: %s' % msg)
59 # Return 0 so run-tests.py could compare the output.
59 # Return 0 so run-tests.py could compare the output.
60 sys.exit()
60 sys.exit()
61
61
62
62
63 # Register a revlog processor for flag EXTSTORED.
63 # Register a revlog processor for flag EXTSTORED.
64 #
64 #
65 # It simply prepends a fixed header, and replaces '1' to 'i'. So it has
65 # It simply prepends a fixed header, and replaces '1' to 'i'. So it has
66 # insertion and replacement, and may be interesting to test revlog's line-based
66 # insertion and replacement, and may be interesting to test revlog's line-based
67 # deltas.
67 # deltas.
68 _extheader = b'E\n'
68 _extheader = b'E\n'
69
69
70
70
71 def readprocessor(self, rawtext):
71 def readprocessor(self, rawtext):
72 # True: the returned text could be used to verify hash
72 # True: the returned text could be used to verify hash
73 text = rawtext[len(_extheader) :].replace(b'i', b'1')
73 text = rawtext[len(_extheader) :].replace(b'i', b'1')
74 return text, True
74 return text, True
75
75
76
76
77 def writeprocessor(self, text):
77 def writeprocessor(self, text):
78 # False: the returned rawtext shouldn't be used to verify hash
78 # False: the returned rawtext shouldn't be used to verify hash
79 rawtext = _extheader + text.replace(b'1', b'i')
79 rawtext = _extheader + text.replace(b'1', b'i')
80 return rawtext, False
80 return rawtext, False
81
81
82
82
83 def rawprocessor(self, rawtext):
83 def rawprocessor(self, rawtext):
84 # False: do not verify hash. Only the content returned by "readprocessor"
84 # False: do not verify hash. Only the content returned by "readprocessor"
85 # can be used to verify hash.
85 # can be used to verify hash.
86 return False
86 return False
87
87
88
88
89 flagutil.addflagprocessor(
89 flagutil.addflagprocessor(
90 revlog.REVIDX_EXTSTORED, (readprocessor, writeprocessor, rawprocessor)
90 revlog.REVIDX_EXTSTORED, (readprocessor, writeprocessor, rawprocessor)
91 )
91 )
92
92
93 # Utilities about reading and appending revlog
93 # Utilities about reading and appending revlog
94
94
95
95
96 def newtransaction():
96 def newtransaction():
97 # A transaction is required to write revlogs
97 # A transaction is required to write revlogs
98 report = lambda msg: None
98 report = lambda msg: None
99 return transaction.transaction(report, tvfs, {'plain': tvfs}, b'journal')
99 return transaction.transaction(report, tvfs, {'plain': tvfs}, b'journal')
100
100
101
101
102 def newrevlog(name=b'_testrevlog', recreate=False):
102 def newrevlog(name=b'_testrevlog', recreate=False):
103 if recreate:
103 if recreate:
104 tvfs.tryunlink(name + b'.i')
104 tvfs.tryunlink(name + b'.i')
105 target = (constants.KIND_OTHER, b'test')
105 target = (constants.KIND_OTHER, b'test')
106 rlog = revlog.revlog(tvfs, target=target, radix=name)
106 rlog = revlog.revlog(tvfs, target=target, radix=name)
107 return rlog
107 return rlog
108
108
109
109
110 def appendrev(rlog, text, tr, isext=False, isdelta=True):
110 def appendrev(rlog, text, tr, isext=False, isdelta=True):
111 """Append a revision. If isext is True, set the EXTSTORED flag so flag
111 """Append a revision. If isext is True, set the EXTSTORED flag so flag
112 processor will be used (and rawtext is different from text). If isdelta is
112 processor will be used (and rawtext is different from text). If isdelta is
113 True, force the revision to be a delta, otherwise it's full text.
113 True, force the revision to be a delta, otherwise it's full text.
114 """
114 """
115 nextrev = len(rlog)
115 nextrev = len(rlog)
116 p1 = rlog.node(nextrev - 1)
116 p1 = rlog.node(nextrev - 1)
117 p2 = rlog.nullid
117 p2 = rlog.nullid
118 if isext:
118 if isext:
119 flags = revlog.REVIDX_EXTSTORED
119 flags = revlog.REVIDX_EXTSTORED
120 else:
120 else:
121 flags = revlog.REVIDX_DEFAULT_FLAGS
121 flags = revlog.REVIDX_DEFAULT_FLAGS
122 # Change storedeltachains temporarily, to override revlog's delta decision
122 # Change storedeltachains temporarily, to override revlog's delta decision
123 rlog._storedeltachains = isdelta
123 rlog._storedeltachains = isdelta
124 try:
124 try:
125 rlog.addrevision(text, tr, nextrev, p1, p2, flags=flags)
125 rlog.addrevision(text, tr, nextrev, p1, p2, flags=flags)
126 return nextrev
126 return nextrev
127 except Exception as ex:
127 except Exception as ex:
128 abort('rev %d: failed to append: %s' % (nextrev, ex))
128 abort('rev %d: failed to append: %s' % (nextrev, ex))
129 finally:
129 finally:
130 # Restore storedeltachains. It is always True, see revlog.__init__
130 # Restore storedeltachains. It is always True, see revlog.__init__
131 rlog._storedeltachains = True
131 rlog._storedeltachains = True
132
132
133
133
134 def addgroupcopy(rlog, tr, destname=b'_destrevlog', optimaldelta=True):
134 def addgroupcopy(rlog, tr, destname=b'_destrevlog', optimaldelta=True):
135 """Copy revlog to destname using revlog.addgroup. Return the copied revlog.
135 """Copy revlog to destname using revlog.addgroup. Return the copied revlog.
136
136
137 This emulates push or pull. They use changegroup. Changegroup requires
137 This emulates push or pull. They use changegroup. Changegroup requires
138 repo to work. We don't have a repo, so a dummy changegroup is used.
138 repo to work. We don't have a repo, so a dummy changegroup is used.
139
139
140 If optimaldelta is True, use optimized delta parent, so the destination
140 If optimaldelta is True, use optimized delta parent, so the destination
141 revlog could probably reuse it. Otherwise it builds sub-optimal delta, and
141 revlog could probably reuse it. Otherwise it builds sub-optimal delta, and
142 the destination revlog needs more work to use it.
142 the destination revlog needs more work to use it.
143
143
144 This exercises some revlog.addgroup (and revlog._addrevision(text=None))
144 This exercises some revlog.addgroup (and revlog._addrevision(text=None))
145 code path, which is not covered by "appendrev" alone.
145 code path, which is not covered by "appendrev" alone.
146 """
146 """
147
147
148 class dummychangegroup:
148 class dummychangegroup:
149 @staticmethod
149 @staticmethod
150 def deltachunk(pnode):
150 def deltachunk(pnode):
151 pnode = pnode or rlog.nullid
151 pnode = pnode or rlog.nullid
152 parentrev = rlog.rev(pnode)
152 parentrev = rlog.rev(pnode)
153 r = parentrev + 1
153 r = parentrev + 1
154 if r >= len(rlog):
154 if r >= len(rlog):
155 return {}
155 return {}
156 if optimaldelta:
156 if optimaldelta:
157 deltaparent = parentrev
157 deltaparent = parentrev
158 else:
158 else:
159 # suboptimal deltaparent
159 # suboptimal deltaparent
160 deltaparent = min(0, parentrev)
160 deltaparent = min(0, parentrev)
161 if not rlog._candelta(deltaparent, r):
161 if not rlog._candelta(deltaparent, r):
162 deltaparent = -1
162 deltaparent = -1
163 return {
163 return {
164 b'node': rlog.node(r),
164 b'node': rlog.node(r),
165 b'p1': pnode,
165 b'p1': pnode,
166 b'p2': rlog.nullid,
166 b'p2': rlog.nullid,
167 b'cs': rlog.node(rlog.linkrev(r)),
167 b'cs': rlog.node(rlog.linkrev(r)),
168 b'flags': rlog.flags(r),
168 b'flags': rlog.flags(r),
169 b'deltabase': rlog.node(deltaparent),
169 b'deltabase': rlog.node(deltaparent),
170 b'delta': rlog.revdiff(deltaparent, r),
170 b'delta': rlog.revdiff(deltaparent, r),
171 b'sidedata': rlog.sidedata(r),
171 b'sidedata': rlog.sidedata(r),
172 }
172 }
173
173
174 def deltaiter(self):
174 def deltaiter(self):
175 chain = None
175 chain = None
176 for chunkdata in iter(lambda: self.deltachunk(chain), {}):
176 for chunkdata in iter(lambda: self.deltachunk(chain), {}):
177 node = chunkdata[b'node']
177 node = chunkdata[b'node']
178 p1 = chunkdata[b'p1']
178 p1 = chunkdata[b'p1']
179 p2 = chunkdata[b'p2']
179 p2 = chunkdata[b'p2']
180 cs = chunkdata[b'cs']
180 cs = chunkdata[b'cs']
181 deltabase = chunkdata[b'deltabase']
181 deltabase = chunkdata[b'deltabase']
182 delta = chunkdata[b'delta']
182 delta = chunkdata[b'delta']
183 flags = chunkdata[b'flags']
183 flags = chunkdata[b'flags']
184 sidedata = chunkdata[b'sidedata']
184 sidedata = chunkdata[b'sidedata']
185
185
186 chain = node
186 chain = node
187
187
188 yield (node, p1, p2, cs, deltabase, delta, flags, sidedata)
188 yield (node, p1, p2, cs, deltabase, delta, flags, sidedata)
189
189
190 def linkmap(lnode):
190 def linkmap(lnode):
191 return rlog.rev(lnode)
191 return rlog.rev(lnode)
192
192
193 dlog = newrevlog(destname, recreate=True)
193 dlog = newrevlog(destname, recreate=True)
194 dummydeltas = dummychangegroup().deltaiter()
194 dummydeltas = dummychangegroup().deltaiter()
195 dlog.addgroup(dummydeltas, linkmap, tr)
195 dlog.addgroup(dummydeltas, linkmap, tr)
196 return dlog
196 return dlog
197
197
198
198
199 def lowlevelcopy(rlog, tr, destname=b'_destrevlog'):
199 def lowlevelcopy(rlog, tr, destname=b'_destrevlog'):
200 """Like addgroupcopy, but use the low level revlog._addrevision directly.
200 """Like addgroupcopy, but use the low level revlog._addrevision directly.
201
201
202 It exercises some code paths that are hard to reach easily otherwise.
202 It exercises some code paths that are hard to reach easily otherwise.
203 """
203 """
204 dlog = newrevlog(destname, recreate=True)
204 dlog = newrevlog(destname, recreate=True)
205 for r in rlog:
205 for r in rlog:
206 p1 = rlog.node(r - 1)
206 p1 = rlog.node(r - 1)
207 p2 = rlog.nullid
207 p2 = rlog.nullid
208 if r == 0 or (rlog.flags(r) & revlog.REVIDX_EXTSTORED):
208 if r == 0 or (rlog.flags(r) & revlog.REVIDX_EXTSTORED):
209 text = rlog.rawdata(r)
209 text = rlog.rawdata(r)
210 cachedelta = None
210 cachedelta = None
211 else:
211 else:
212 # deltaparent cannot have EXTSTORED flag.
212 # deltaparent cannot have EXTSTORED flag.
213 deltaparent = max(
213 deltaparent = max(
214 [-1]
214 [-1]
215 + [
215 + [
216 p
216 p
217 for p in range(r)
217 for p in range(r)
218 if rlog.flags(p) & revlog.REVIDX_EXTSTORED == 0
218 if rlog.flags(p) & revlog.REVIDX_EXTSTORED == 0
219 ]
219 ]
220 )
220 )
221 text = None
221 text = None
222 cachedelta = (deltaparent, rlog.revdiff(deltaparent, r))
222 cachedelta = (deltaparent, rlog.revdiff(deltaparent, r))
223 flags = rlog.flags(r)
223 flags = rlog.flags(r)
224 with dlog._writing(_NoTransaction()):
224 with dlog._writing(_NoTransaction()):
225 dlog._addrevision(
225 dlog._addrevision(
226 rlog.node(r),
226 rlog.node(r),
227 text,
227 text,
228 tr,
228 tr,
229 r,
229 r,
230 p1,
230 p1,
231 p2,
231 p2,
232 flags,
232 flags,
233 cachedelta,
233 cachedelta,
234 )
234 )
235 return dlog
235 return dlog
236
236
237
237
238 # Utilities to generate revisions for testing
238 # Utilities to generate revisions for testing
239
239
240
240
241 def genbits(n):
241 def genbits(n):
242 """Given a number n, generate (2 ** (n * 2) + 1) numbers in range(2 ** n).
242 """Given a number n, generate (2 ** (n * 2) + 1) numbers in range(2 ** n).
243 i.e. the generated numbers have a width of n bits.
243 i.e. the generated numbers have a width of n bits.
244
244
245 The combination of two adjacent numbers will cover all possible cases.
245 The combination of two adjacent numbers will cover all possible cases.
246 That is to say, given any x, y where both x, and y are in range(2 ** n),
246 That is to say, given any x, y where both x, and y are in range(2 ** n),
247 there is an x followed immediately by y in the generated sequence.
247 there is an x followed immediately by y in the generated sequence.
248 """
248 """
249 m = 2 ** n
249 m = 2 ** n
250
250
251 # Gray Code. See https://en.wikipedia.org/wiki/Gray_code
251 # Gray Code. See https://en.wikipedia.org/wiki/Gray_code
252 gray = lambda x: x ^ (x >> 1)
252 gray = lambda x: x ^ (x >> 1)
253 reversegray = {gray(i): i for i in range(m)}
253 reversegray = {gray(i): i for i in range(m)}
254
254
255 # Generate (n * 2) bit gray code, yield lower n bits as X, and look for
255 # Generate (n * 2) bit gray code, yield lower n bits as X, and look for
256 # the next unused gray code where higher n bits equal to X.
256 # the next unused gray code where higher n bits equal to X.
257
257
258 # For gray codes whose higher bits are X, a[X] of them have been used.
258 # For gray codes whose higher bits are X, a[X] of them have been used.
259 a = [0] * m
259 a = [0] * m
260
260
261 # Iterate from 0.
261 # Iterate from 0.
262 x = 0
262 x = 0
263 yield x
263 yield x
264 for i in range(m * m):
264 for i in range(m * m):
265 x = reversegray[x]
265 x = reversegray[x]
266 y = gray(a[x] + x * m) & (m - 1)
266 y = gray(a[x] + x * m) & (m - 1)
267 assert a[x] < m
267 assert a[x] < m
268 a[x] += 1
268 a[x] += 1
269 x = y
269 x = y
270 yield x
270 yield x
271
271
272
272
273 def gentext(rev):
273 def gentext(rev):
274 '''Given a revision number, generate dummy text'''
274 '''Given a revision number, generate dummy text'''
275 return b''.join(b'%d\n' % j for j in range(-1, rev % 5))
275 return b''.join(b'%d\n' % j for j in range(-1, rev % 5))
276
276
277
277
278 def writecases(rlog, tr):
278 def writecases(rlog, tr):
279 """Write some revisions interested to the test.
279 """Write some revisions interested to the test.
280
280
281 The test is interested in 3 properties of a revision:
281 The test is interested in 3 properties of a revision:
282
282
283 - Is it a delta or a full text? (isdelta)
283 - Is it a delta or a full text? (isdelta)
284 This is to catch some delta application issues.
284 This is to catch some delta application issues.
285 - Does it have a flag of EXTSTORED? (isext)
285 - Does it have a flag of EXTSTORED? (isext)
286 This is to catch some flag processor issues. Especially when
286 This is to catch some flag processor issues. Especially when
287 interacted with revlog deltas.
287 interacted with revlog deltas.
288 - Is its text empty? (isempty)
288 - Is its text empty? (isempty)
289 This is less important. It is intended to try to catch some careless
289 This is less important. It is intended to try to catch some careless
290 checks like "if text" instead of "if text is None". Note: if flag
290 checks like "if text" instead of "if text is None". Note: if flag
291 processor is involved, raw text may be not empty.
291 processor is involved, raw text may be not empty.
292
292
293 Write 65 revisions. So that all combinations of the above flags for
293 Write 65 revisions. So that all combinations of the above flags for
294 adjacent revisions are covered. That is to say,
294 adjacent revisions are covered. That is to say,
295
295
296 len(set(
296 len(set(
297 (r.delta, r.ext, r.empty, (r+1).delta, (r+1).ext, (r+1).empty)
297 (r.delta, r.ext, r.empty, (r+1).delta, (r+1).ext, (r+1).empty)
298 for r in range(len(rlog) - 1)
298 for r in range(len(rlog) - 1)
299 )) is 64.
299 )) is 64.
300
300
301 Where "r.delta", "r.ext", and "r.empty" are booleans matching properties
301 Where "r.delta", "r.ext", and "r.empty" are booleans matching properties
302 mentioned above.
302 mentioned above.
303
303
304 Return expected [(text, rawtext)].
304 Return expected [(text, rawtext)].
305 """
305 """
306 result = []
306 result = []
307 for i, x in enumerate(genbits(3)):
307 for i, x in enumerate(genbits(3)):
308 isdelta, isext, isempty = bool(x & 1), bool(x & 2), bool(x & 4)
308 isdelta, isext, isempty = bool(x & 1), bool(x & 2), bool(x & 4)
309 if isempty:
309 if isempty:
310 text = b''
310 text = b''
311 else:
311 else:
312 text = gentext(i)
312 text = gentext(i)
313 rev = appendrev(rlog, text, tr, isext=isext, isdelta=isdelta)
313 rev = appendrev(rlog, text, tr, isext=isext, isdelta=isdelta)
314
314
315 # Verify text, rawtext, and rawsize
315 # Verify text, rawtext, and rawsize
316 if isext:
316 if isext:
317 rawtext = writeprocessor(None, text)[0]
317 rawtext = writeprocessor(None, text)[0]
318 else:
318 else:
319 rawtext = text
319 rawtext = text
320 if rlog.rawsize(rev) != len(rawtext):
320 if rlog.rawsize(rev) != len(rawtext):
321 abort('rev %d: wrong rawsize' % rev)
321 abort('rev %d: wrong rawsize' % rev)
322 if rlog.revision(rev) != text:
322 if rlog.revision(rev) != text:
323 abort('rev %d: wrong text' % rev)
323 abort('rev %d: wrong text' % rev)
324 if rlog.rawdata(rev) != rawtext:
324 if rlog.rawdata(rev) != rawtext:
325 abort('rev %d: wrong rawtext' % rev)
325 abort('rev %d: wrong rawtext' % rev)
326 result.append((text, rawtext))
326 result.append((text, rawtext))
327
327
328 # Verify flags like isdelta, isext work as expected
328 # Verify flags like isdelta, isext work as expected
329 # isdelta can be overridden to False if this or p1 has isext set
329 # isdelta can be overridden to False if this or p1 has isext set
330 if bool(rlog.deltaparent(rev) > -1) and not isdelta:
330 if bool(rlog.deltaparent(rev) > -1) and not isdelta:
331 abort('rev %d: isdelta is unexpected' % rev)
331 abort('rev %d: isdelta is unexpected' % rev)
332 if bool(rlog.flags(rev)) != isext:
332 if bool(rlog.flags(rev)) != isext:
333 abort('rev %d: isext is ineffective' % rev)
333 abort('rev %d: isext is ineffective' % rev)
334 return result
334 return result
335
335
336
336
337 # Main test and checking
337 # Main test and checking
338
338
339
339
340 def checkrevlog(rlog, expected):
340 def checkrevlog(rlog, expected):
341 '''Check if revlog has expected contents. expected is [(text, rawtext)]'''
341 '''Check if revlog has expected contents. expected is [(text, rawtext)]'''
342 # Test using different access orders. This could expose some issues
342 # Test using different access orders. This could expose some issues
343 # depending on revlog caching (see revlog._cache).
343 # depending on revlog caching (see revlog._cache).
344 for r0 in range(len(rlog) - 1):
344 for r0 in range(len(rlog) - 1):
345 r1 = r0 + 1
345 r1 = r0 + 1
346 for revorder in [[r0, r1], [r1, r0]]:
346 for revorder in [[r0, r1], [r1, r0]]:
347 for raworder in [[True], [False], [True, False], [False, True]]:
347 for raworder in [[True], [False], [True, False], [False, True]]:
348 nlog = newrevlog()
348 nlog = newrevlog()
349 for rev in revorder:
349 for rev in revorder:
350 for raw in raworder:
350 for raw in raworder:
351 if raw:
351 if raw:
352 t = nlog.rawdata(rev)
352 t = nlog.rawdata(rev)
353 else:
353 else:
354 t = nlog.revision(rev)
354 t = nlog.revision(rev)
355 if t != expected[rev][int(raw)]:
355 if t != expected[rev][int(raw)]:
356 abort(
356 abort(
357 'rev %d: corrupted %stext'
357 'rev %d: corrupted %stext'
358 % (rev, raw and 'raw' or '')
358 % (rev, raw and 'raw' or '')
359 )
359 )
360
360
361
361
362 slicingdata = [
362 slicingdata = [
363 ([0, 1, 2, 3, 55, 56, 58, 59, 60], [[0, 1], [2], [58], [59, 60]], 10),
363 ([0, 1, 2, 3, 55, 56, 58, 59, 60], [[0, 1], [2], [58], [59, 60]], 10),
364 ([0, 1, 2, 3, 55, 56, 58, 59, 60], [[0, 1], [2], [58], [59, 60]], 10),
364 ([0, 1, 2, 3, 55, 56, 58, 59, 60], [[0, 1], [2], [58], [59, 60]], 10),
365 (
365 (
366 [-1, 0, 1, 2, 3, 55, 56, 58, 59, 60],
366 [-1, 0, 1, 2, 3, 55, 56, 58, 59, 60],
367 [[-1, 0, 1], [2], [58], [59, 60]],
367 [[-1, 0, 1], [2], [58], [59, 60]],
368 10,
368 10,
369 ),
369 ),
370 ]
370 ]
371
371
372
372
373 def slicingtest(rlog):
373 def slicingtest(rlog):
374 old_delta_config = rlog.delta_config
374 old_delta_config = rlog.delta_config
375 old_data_config = rlog.data_config
375 old_data_config = rlog.data_config
376 rlog.delta_config = rlog.delta_config.copy()
376 rlog.delta_config = rlog.delta_config.copy()
377 rlog.data_config = rlog.data_config.copy()
377 rlog.data_config = rlog.data_config.copy()
378 try:
378 try:
379 # the test revlog is small, we remove the floor under which we
379 # the test revlog is small, we remove the floor under which we
380 # slicing is diregarded.
380 # slicing is diregarded.
381 rlog.data_config.sr_min_gap_size = 0
381 rlog.data_config.sr_min_gap_size = 0
382 rlog.delta_config.sr_min_gap_size = 0
382 rlog.delta_config.sr_min_gap_size = 0
383 for item in slicingdata:
383 for item in slicingdata:
384 chain, expected, target = item
384 chain, expected, target = item
385 result = deltas.slicechunk(rlog, chain, targetsize=target)
385 result = deltas.slicechunk(rlog, chain, targetsize=target)
386 result = list(result)
386 result = list(result)
387 if result != expected:
387 if result != expected:
388 print('slicing differ:')
388 print('slicing differ:')
389 print(' chain: %s' % chain)
389 print(' chain: %s' % chain)
390 print(' target: %s' % target)
390 print(' target: %s' % target)
391 print(' expected: %s' % expected)
391 print(' expected: %s' % expected)
392 print(' result: %s' % result)
392 print(' result: %s' % result)
393 finally:
393 finally:
394 rlog.delta_config = old_delta_config
394 rlog.delta_config = old_delta_config
395 rlog.data_config = old_data_config
395 rlog.data_config = old_data_config
396
396
397
397
398 def md5sum(s):
398 def md5sum(s):
399 return hashlib.md5(s).digest()
399 return hashlib.md5(s).digest()
400
400
401
401
402 def _maketext(*coord):
402 def _maketext(*coord):
403 """create piece of text according to range of integers
403 """create piece of text according to range of integers
404
404
405 The test returned use a md5sum of the integer to make it less
405 The test returned use a md5sum of the integer to make it less
406 compressible"""
406 compressible"""
407 pieces = []
407 pieces = []
408 for start, size in coord:
408 for start, size in coord:
409 num = range(start, start + size)
409 num = range(start, start + size)
410 p = [md5sum(b'%d' % r) for r in num]
410 p = [md5sum(b'%d' % r) for r in num]
411 pieces.append(b'\n'.join(p))
411 pieces.append(b'\n'.join(p))
412 return b'\n'.join(pieces) + b'\n'
412 return b'\n'.join(pieces) + b'\n'
413
413
414
414
415 data = [
415 data = [
416 _maketext((0, 120), (456, 60)),
416 _maketext((0, 120), (456, 60)),
417 _maketext((0, 120), (345, 60)),
417 _maketext((0, 120), (345, 60)),
418 _maketext((0, 120), (734, 60)),
418 _maketext((0, 120), (734, 60)),
419 _maketext((0, 120), (734, 60), (923, 45)),
419 _maketext((0, 120), (734, 60), (923, 45)),
420 _maketext((0, 120), (734, 60), (234, 45)),
420 _maketext((0, 120), (734, 60), (234, 45)),
421 _maketext((0, 120), (734, 60), (564, 45)),
421 _maketext((0, 120), (734, 60), (564, 45)),
422 _maketext((0, 120), (734, 60), (361, 45)),
422 _maketext((0, 120), (734, 60), (361, 45)),
423 _maketext((0, 120), (734, 60), (489, 45)),
423 _maketext((0, 120), (734, 60), (489, 45)),
424 _maketext((0, 120), (123, 60)),
424 _maketext((0, 120), (123, 60)),
425 _maketext((0, 120), (145, 60)),
425 _maketext((0, 120), (145, 60)),
426 _maketext((0, 120), (104, 60)),
426 _maketext((0, 120), (104, 60)),
427 _maketext((0, 120), (430, 60)),
427 _maketext((0, 120), (430, 60)),
428 _maketext((0, 120), (430, 60), (923, 45)),
428 _maketext((0, 120), (430, 60), (923, 45)),
429 _maketext((0, 120), (430, 60), (234, 45)),
429 _maketext((0, 120), (430, 60), (234, 45)),
430 _maketext((0, 120), (430, 60), (564, 45)),
430 _maketext((0, 120), (430, 60), (564, 45)),
431 _maketext((0, 120), (430, 60), (361, 45)),
431 _maketext((0, 120), (430, 60), (361, 45)),
432 _maketext((0, 120), (430, 60), (489, 45)),
432 _maketext((0, 120), (430, 60), (489, 45)),
433 _maketext((0, 120), (249, 60)),
433 _maketext((0, 120), (249, 60)),
434 _maketext((0, 120), (832, 60)),
434 _maketext((0, 120), (832, 60)),
435 _maketext((0, 120), (891, 60)),
435 _maketext((0, 120), (891, 60)),
436 _maketext((0, 120), (543, 60)),
436 _maketext((0, 120), (543, 60)),
437 _maketext((0, 120), (120, 60)),
437 _maketext((0, 120), (120, 60)),
438 _maketext((0, 120), (60, 60), (768, 30)),
438 _maketext((0, 120), (60, 60), (768, 30)),
439 _maketext((0, 120), (60, 60), (260, 30)),
439 _maketext((0, 120), (60, 60), (260, 30)),
440 _maketext((0, 120), (60, 60), (450, 30)),
440 _maketext((0, 120), (60, 60), (450, 30)),
441 _maketext((0, 120), (60, 60), (361, 30)),
441 _maketext((0, 120), (60, 60), (361, 30)),
442 _maketext((0, 120), (60, 60), (886, 30)),
442 _maketext((0, 120), (60, 60), (886, 30)),
443 _maketext((0, 120), (60, 60), (116, 30)),
443 _maketext((0, 120), (60, 60), (116, 30)),
444 _maketext((0, 120), (60, 60), (567, 30), (629, 40)),
444 _maketext((0, 120), (60, 60), (567, 30), (629, 40)),
445 _maketext((0, 120), (60, 60), (569, 30), (745, 40)),
445 _maketext((0, 120), (60, 60), (569, 30), (745, 40)),
446 _maketext((0, 120), (60, 60), (777, 30), (700, 40)),
446 _maketext((0, 120), (60, 60), (777, 30), (700, 40)),
447 _maketext((0, 120), (60, 60), (618, 30), (398, 40), (158, 10)),
447 _maketext((0, 120), (60, 60), (618, 30), (398, 40), (158, 10)),
448 ]
448 ]
449
449
450
450
451 def makesnapshot(tr):
451 def makesnapshot(tr):
452 rl = newrevlog(name=b'_snaprevlog3', recreate=True)
452 rl = newrevlog(name=b'_snaprevlog3', recreate=True)
453 for i in data:
453 for i in data:
454 appendrev(rl, i, tr)
454 appendrev(rl, i, tr)
455 return rl
455 return rl
456
456
457
457
458 snapshots = [-1, 0, 6, 8, 11, 17, 19, 21, 25, 30]
458 snapshots = [-1, 0, 6, 8, 11, 17, 19, 21, 25, 30]
459
459
460
460
461 def issnapshottest(rlog):
461 def issnapshottest(rlog):
462 result = []
462 result = []
463 if rlog.issnapshot(-1):
463 if rlog.issnapshot(-1):
464 result.append(-1)
464 result.append(-1)
465 for rev in rlog:
465 for rev in rlog:
466 if rlog.issnapshot(rev):
466 if rlog.issnapshot(rev):
467 result.append(rev)
467 result.append(rev)
468 if snapshots != result:
468 if snapshots != result:
469 print('snapshot differ:')
469 print('snapshot differ:')
470 print(' expected: %s' % snapshots)
470 print(' expected: %s' % snapshots)
471 print(' got: %s' % result)
471 print(' got: %s' % result)
472
472
473
473
474 snapshotmapall = {0: {6, 8, 11, 17, 19, 25}, 8: {21}, -1: {0, 30}}
474 snapshotmapall = {0: {6, 8, 11, 17, 19, 25}, 8: {21}, -1: {0, 30}}
475 snapshotmap15 = {0: {17, 19, 25}, 8: {21}, -1: {30}}
475 snapshotmap15 = {0: {17, 19, 25}, 8: {21}, -1: {30}}
476
476
477
477
478 def findsnapshottest(rlog):
478 def findsnapshottest(rlog):
479 cache = deltas.SnapshotCache()
479 cache = deltas.SnapshotCache()
480 cache.update(rlog)
480 cache.update(rlog)
481 resultall = dict(cache.snapshots)
481 resultall = dict(cache.snapshots)
482 if resultall != snapshotmapall:
482 if resultall != snapshotmapall:
483 print('snapshot map differ:')
483 print('snapshot map differ:')
484 print(' expected: %s' % snapshotmapall)
484 print(' expected: %s' % snapshotmapall)
485 print(' got: %s' % resultall)
485 print(' got: %s' % resultall)
486 cache15 = deltas.SnapshotCache()
486 cache15 = deltas.SnapshotCache()
487 cache15.update(rlog, 15)
487 cache15.update(rlog, 15)
488 result15 = dict(cache15.snapshots)
488 result15 = dict(cache15.snapshots)
489 if result15 != snapshotmap15:
489 if result15 != snapshotmap15:
490 print('snapshot map differ:')
490 print('snapshot map differ:')
491 print(' expected: %s' % snapshotmap15)
491 print(' expected: %s' % snapshotmap15)
492 print(' got: %s' % result15)
492 print(' got: %s' % result15)
493
493
494
494
495 def maintest():
495 def maintest():
496 with newtransaction() as tr:
496 with newtransaction() as tr:
497 rl = newrevlog(recreate=True)
497 rl = newrevlog(recreate=True)
498 expected = writecases(rl, tr)
498 expected = writecases(rl, tr)
499 checkrevlog(rl, expected)
499 checkrevlog(rl, expected)
500 print('local test passed')
500 print('local test passed')
501 # Copy via revlog.addgroup
501 # Copy via revlog.addgroup
502 rl1 = addgroupcopy(rl, tr)
502 rl1 = addgroupcopy(rl, tr)
503 checkrevlog(rl1, expected)
503 checkrevlog(rl1, expected)
504 rl2 = addgroupcopy(rl, tr, optimaldelta=False)
504 rl2 = addgroupcopy(rl, tr, optimaldelta=False)
505 checkrevlog(rl2, expected)
505 checkrevlog(rl2, expected)
506 print('addgroupcopy test passed')
506 print('addgroupcopy test passed')
507 # Copy via revlog.clone
507 # Copy via revlog.clone
508 rl3 = newrevlog(name=b'_destrevlog3', recreate=True)
508 rl3 = newrevlog(name=b'_destrevlog3', recreate=True)
509 rl.clone(tr, rl3)
509 rl.clone(tr, rl3)
510 checkrevlog(rl3, expected)
510 checkrevlog(rl3, expected)
511 print('clone test passed')
511 print('clone test passed')
512 # Copy via low-level revlog._addrevision
512 # Copy via low-level revlog._addrevision
513 rl4 = lowlevelcopy(rl, tr)
513 rl4 = lowlevelcopy(rl, tr)
514 checkrevlog(rl4, expected)
514 checkrevlog(rl4, expected)
515 print('lowlevelcopy test passed')
515 print('lowlevelcopy test passed')
516 slicingtest(rl)
516 slicingtest(rl)
517 print('slicing test passed')
517 print('slicing test passed')
518 rl5 = makesnapshot(tr)
518 rl5 = makesnapshot(tr)
519 issnapshottest(rl5)
519 issnapshottest(rl5)
520 print('issnapshot test passed')
520 print('issnapshot test passed')
521 findsnapshottest(rl5)
521 findsnapshottest(rl5)
522 print('findsnapshot test passed')
522 print('findsnapshot test passed')
523
523
524
524
525 try:
525 try:
526 maintest()
526 maintest()
527 except Exception as ex:
527 except Exception as ex:
528 abort('crashed: %s' % ex)
528 abort('crashed: %s' % ex)
General Comments 0
You need to be logged in to leave comments. Login now