##// END OF EJS Templates
localrepo: delete obsolete comment about `prefix in repo` raising exception...
Martin von Zweigbergk -
r46730:c6ae1982 default
parent child Browse files
Show More
@@ -1,3596 +1,3592
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import errno
10 import errno
11 import functools
11 import functools
12 import os
12 import os
13 import random
13 import random
14 import sys
14 import sys
15 import time
15 import time
16 import weakref
16 import weakref
17
17
18 from .i18n import _
18 from .i18n import _
19 from .node import (
19 from .node import (
20 bin,
20 bin,
21 hex,
21 hex,
22 nullid,
22 nullid,
23 nullrev,
23 nullrev,
24 short,
24 short,
25 )
25 )
26 from .pycompat import (
26 from .pycompat import (
27 delattr,
27 delattr,
28 getattr,
28 getattr,
29 )
29 )
30 from . import (
30 from . import (
31 bookmarks,
31 bookmarks,
32 branchmap,
32 branchmap,
33 bundle2,
33 bundle2,
34 bundlecaches,
34 bundlecaches,
35 changegroup,
35 changegroup,
36 color,
36 color,
37 commit,
37 commit,
38 context,
38 context,
39 dirstate,
39 dirstate,
40 dirstateguard,
40 dirstateguard,
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 pushkey,
57 pushkey,
58 pycompat,
58 pycompat,
59 rcutil,
59 rcutil,
60 repoview,
60 repoview,
61 requirements as requirementsmod,
61 requirements as requirementsmod,
62 revset,
62 revset,
63 revsetlang,
63 revsetlang,
64 scmutil,
64 scmutil,
65 sparse,
65 sparse,
66 store as storemod,
66 store as storemod,
67 subrepoutil,
67 subrepoutil,
68 tags as tagsmod,
68 tags as tagsmod,
69 transaction,
69 transaction,
70 txnutil,
70 txnutil,
71 util,
71 util,
72 vfs as vfsmod,
72 vfs as vfsmod,
73 )
73 )
74
74
75 from .interfaces import (
75 from .interfaces import (
76 repository,
76 repository,
77 util as interfaceutil,
77 util as interfaceutil,
78 )
78 )
79
79
80 from .utils import (
80 from .utils import (
81 hashutil,
81 hashutil,
82 procutil,
82 procutil,
83 stringutil,
83 stringutil,
84 )
84 )
85
85
86 from .revlogutils import constants as revlogconst
86 from .revlogutils import constants as revlogconst
87
87
88 release = lockmod.release
88 release = lockmod.release
89 urlerr = util.urlerr
89 urlerr = util.urlerr
90 urlreq = util.urlreq
90 urlreq = util.urlreq
91
91
92 # set of (path, vfs-location) tuples. vfs-location is:
92 # set of (path, vfs-location) tuples. vfs-location is:
93 # - 'plain for vfs relative paths
93 # - 'plain for vfs relative paths
94 # - '' for svfs relative paths
94 # - '' for svfs relative paths
95 _cachedfiles = set()
95 _cachedfiles = set()
96
96
97
97
98 class _basefilecache(scmutil.filecache):
98 class _basefilecache(scmutil.filecache):
99 """All filecache usage on repo are done for logic that should be unfiltered"""
99 """All filecache usage on repo are done for logic that should be unfiltered"""
100
100
101 def __get__(self, repo, type=None):
101 def __get__(self, repo, type=None):
102 if repo is None:
102 if repo is None:
103 return self
103 return self
104 # proxy to unfiltered __dict__ since filtered repo has no entry
104 # proxy to unfiltered __dict__ since filtered repo has no entry
105 unfi = repo.unfiltered()
105 unfi = repo.unfiltered()
106 try:
106 try:
107 return unfi.__dict__[self.sname]
107 return unfi.__dict__[self.sname]
108 except KeyError:
108 except KeyError:
109 pass
109 pass
110 return super(_basefilecache, self).__get__(unfi, type)
110 return super(_basefilecache, self).__get__(unfi, type)
111
111
112 def set(self, repo, value):
112 def set(self, repo, value):
113 return super(_basefilecache, self).set(repo.unfiltered(), value)
113 return super(_basefilecache, self).set(repo.unfiltered(), value)
114
114
115
115
116 class repofilecache(_basefilecache):
116 class repofilecache(_basefilecache):
117 """filecache for files in .hg but outside of .hg/store"""
117 """filecache for files in .hg but outside of .hg/store"""
118
118
119 def __init__(self, *paths):
119 def __init__(self, *paths):
120 super(repofilecache, self).__init__(*paths)
120 super(repofilecache, self).__init__(*paths)
121 for path in paths:
121 for path in paths:
122 _cachedfiles.add((path, b'plain'))
122 _cachedfiles.add((path, b'plain'))
123
123
124 def join(self, obj, fname):
124 def join(self, obj, fname):
125 return obj.vfs.join(fname)
125 return obj.vfs.join(fname)
126
126
127
127
128 class storecache(_basefilecache):
128 class storecache(_basefilecache):
129 """filecache for files in the store"""
129 """filecache for files in the store"""
130
130
131 def __init__(self, *paths):
131 def __init__(self, *paths):
132 super(storecache, self).__init__(*paths)
132 super(storecache, self).__init__(*paths)
133 for path in paths:
133 for path in paths:
134 _cachedfiles.add((path, b''))
134 _cachedfiles.add((path, b''))
135
135
136 def join(self, obj, fname):
136 def join(self, obj, fname):
137 return obj.sjoin(fname)
137 return obj.sjoin(fname)
138
138
139
139
140 class mixedrepostorecache(_basefilecache):
140 class mixedrepostorecache(_basefilecache):
141 """filecache for a mix files in .hg/store and outside"""
141 """filecache for a mix files in .hg/store and outside"""
142
142
143 def __init__(self, *pathsandlocations):
143 def __init__(self, *pathsandlocations):
144 # scmutil.filecache only uses the path for passing back into our
144 # scmutil.filecache only uses the path for passing back into our
145 # join(), so we can safely pass a list of paths and locations
145 # join(), so we can safely pass a list of paths and locations
146 super(mixedrepostorecache, self).__init__(*pathsandlocations)
146 super(mixedrepostorecache, self).__init__(*pathsandlocations)
147 _cachedfiles.update(pathsandlocations)
147 _cachedfiles.update(pathsandlocations)
148
148
149 def join(self, obj, fnameandlocation):
149 def join(self, obj, fnameandlocation):
150 fname, location = fnameandlocation
150 fname, location = fnameandlocation
151 if location == b'plain':
151 if location == b'plain':
152 return obj.vfs.join(fname)
152 return obj.vfs.join(fname)
153 else:
153 else:
154 if location != b'':
154 if location != b'':
155 raise error.ProgrammingError(
155 raise error.ProgrammingError(
156 b'unexpected location: %s' % location
156 b'unexpected location: %s' % location
157 )
157 )
158 return obj.sjoin(fname)
158 return obj.sjoin(fname)
159
159
160
160
161 def isfilecached(repo, name):
161 def isfilecached(repo, name):
162 """check if a repo has already cached "name" filecache-ed property
162 """check if a repo has already cached "name" filecache-ed property
163
163
164 This returns (cachedobj-or-None, iscached) tuple.
164 This returns (cachedobj-or-None, iscached) tuple.
165 """
165 """
166 cacheentry = repo.unfiltered()._filecache.get(name, None)
166 cacheentry = repo.unfiltered()._filecache.get(name, None)
167 if not cacheentry:
167 if not cacheentry:
168 return None, False
168 return None, False
169 return cacheentry.obj, True
169 return cacheentry.obj, True
170
170
171
171
172 class unfilteredpropertycache(util.propertycache):
172 class unfilteredpropertycache(util.propertycache):
173 """propertycache that apply to unfiltered repo only"""
173 """propertycache that apply to unfiltered repo only"""
174
174
175 def __get__(self, repo, type=None):
175 def __get__(self, repo, type=None):
176 unfi = repo.unfiltered()
176 unfi = repo.unfiltered()
177 if unfi is repo:
177 if unfi is repo:
178 return super(unfilteredpropertycache, self).__get__(unfi)
178 return super(unfilteredpropertycache, self).__get__(unfi)
179 return getattr(unfi, self.name)
179 return getattr(unfi, self.name)
180
180
181
181
182 class filteredpropertycache(util.propertycache):
182 class filteredpropertycache(util.propertycache):
183 """propertycache that must take filtering in account"""
183 """propertycache that must take filtering in account"""
184
184
185 def cachevalue(self, obj, value):
185 def cachevalue(self, obj, value):
186 object.__setattr__(obj, self.name, value)
186 object.__setattr__(obj, self.name, value)
187
187
188
188
189 def hasunfilteredcache(repo, name):
189 def hasunfilteredcache(repo, name):
190 """check if a repo has an unfilteredpropertycache value for <name>"""
190 """check if a repo has an unfilteredpropertycache value for <name>"""
191 return name in vars(repo.unfiltered())
191 return name in vars(repo.unfiltered())
192
192
193
193
194 def unfilteredmethod(orig):
194 def unfilteredmethod(orig):
195 """decorate method that always need to be run on unfiltered version"""
195 """decorate method that always need to be run on unfiltered version"""
196
196
197 @functools.wraps(orig)
197 @functools.wraps(orig)
198 def wrapper(repo, *args, **kwargs):
198 def wrapper(repo, *args, **kwargs):
199 return orig(repo.unfiltered(), *args, **kwargs)
199 return orig(repo.unfiltered(), *args, **kwargs)
200
200
201 return wrapper
201 return wrapper
202
202
203
203
204 moderncaps = {
204 moderncaps = {
205 b'lookup',
205 b'lookup',
206 b'branchmap',
206 b'branchmap',
207 b'pushkey',
207 b'pushkey',
208 b'known',
208 b'known',
209 b'getbundle',
209 b'getbundle',
210 b'unbundle',
210 b'unbundle',
211 }
211 }
212 legacycaps = moderncaps.union({b'changegroupsubset'})
212 legacycaps = moderncaps.union({b'changegroupsubset'})
213
213
214
214
215 @interfaceutil.implementer(repository.ipeercommandexecutor)
215 @interfaceutil.implementer(repository.ipeercommandexecutor)
216 class localcommandexecutor(object):
216 class localcommandexecutor(object):
217 def __init__(self, peer):
217 def __init__(self, peer):
218 self._peer = peer
218 self._peer = peer
219 self._sent = False
219 self._sent = False
220 self._closed = False
220 self._closed = False
221
221
222 def __enter__(self):
222 def __enter__(self):
223 return self
223 return self
224
224
225 def __exit__(self, exctype, excvalue, exctb):
225 def __exit__(self, exctype, excvalue, exctb):
226 self.close()
226 self.close()
227
227
228 def callcommand(self, command, args):
228 def callcommand(self, command, args):
229 if self._sent:
229 if self._sent:
230 raise error.ProgrammingError(
230 raise error.ProgrammingError(
231 b'callcommand() cannot be used after sendcommands()'
231 b'callcommand() cannot be used after sendcommands()'
232 )
232 )
233
233
234 if self._closed:
234 if self._closed:
235 raise error.ProgrammingError(
235 raise error.ProgrammingError(
236 b'callcommand() cannot be used after close()'
236 b'callcommand() cannot be used after close()'
237 )
237 )
238
238
239 # We don't need to support anything fancy. Just call the named
239 # We don't need to support anything fancy. Just call the named
240 # method on the peer and return a resolved future.
240 # method on the peer and return a resolved future.
241 fn = getattr(self._peer, pycompat.sysstr(command))
241 fn = getattr(self._peer, pycompat.sysstr(command))
242
242
243 f = pycompat.futures.Future()
243 f = pycompat.futures.Future()
244
244
245 try:
245 try:
246 result = fn(**pycompat.strkwargs(args))
246 result = fn(**pycompat.strkwargs(args))
247 except Exception:
247 except Exception:
248 pycompat.future_set_exception_info(f, sys.exc_info()[1:])
248 pycompat.future_set_exception_info(f, sys.exc_info()[1:])
249 else:
249 else:
250 f.set_result(result)
250 f.set_result(result)
251
251
252 return f
252 return f
253
253
254 def sendcommands(self):
254 def sendcommands(self):
255 self._sent = True
255 self._sent = True
256
256
257 def close(self):
257 def close(self):
258 self._closed = True
258 self._closed = True
259
259
260
260
261 @interfaceutil.implementer(repository.ipeercommands)
261 @interfaceutil.implementer(repository.ipeercommands)
262 class localpeer(repository.peer):
262 class localpeer(repository.peer):
263 '''peer for a local repo; reflects only the most recent API'''
263 '''peer for a local repo; reflects only the most recent API'''
264
264
265 def __init__(self, repo, caps=None):
265 def __init__(self, repo, caps=None):
266 super(localpeer, self).__init__()
266 super(localpeer, self).__init__()
267
267
268 if caps is None:
268 if caps is None:
269 caps = moderncaps.copy()
269 caps = moderncaps.copy()
270 self._repo = repo.filtered(b'served')
270 self._repo = repo.filtered(b'served')
271 self.ui = repo.ui
271 self.ui = repo.ui
272 self._caps = repo._restrictcapabilities(caps)
272 self._caps = repo._restrictcapabilities(caps)
273
273
274 # Begin of _basepeer interface.
274 # Begin of _basepeer interface.
275
275
276 def url(self):
276 def url(self):
277 return self._repo.url()
277 return self._repo.url()
278
278
279 def local(self):
279 def local(self):
280 return self._repo
280 return self._repo
281
281
282 def peer(self):
282 def peer(self):
283 return self
283 return self
284
284
285 def canpush(self):
285 def canpush(self):
286 return True
286 return True
287
287
288 def close(self):
288 def close(self):
289 self._repo.close()
289 self._repo.close()
290
290
291 # End of _basepeer interface.
291 # End of _basepeer interface.
292
292
293 # Begin of _basewirecommands interface.
293 # Begin of _basewirecommands interface.
294
294
295 def branchmap(self):
295 def branchmap(self):
296 return self._repo.branchmap()
296 return self._repo.branchmap()
297
297
298 def capabilities(self):
298 def capabilities(self):
299 return self._caps
299 return self._caps
300
300
301 def clonebundles(self):
301 def clonebundles(self):
302 return self._repo.tryread(bundlecaches.CB_MANIFEST_FILE)
302 return self._repo.tryread(bundlecaches.CB_MANIFEST_FILE)
303
303
304 def debugwireargs(self, one, two, three=None, four=None, five=None):
304 def debugwireargs(self, one, two, three=None, four=None, five=None):
305 """Used to test argument passing over the wire"""
305 """Used to test argument passing over the wire"""
306 return b"%s %s %s %s %s" % (
306 return b"%s %s %s %s %s" % (
307 one,
307 one,
308 two,
308 two,
309 pycompat.bytestr(three),
309 pycompat.bytestr(three),
310 pycompat.bytestr(four),
310 pycompat.bytestr(four),
311 pycompat.bytestr(five),
311 pycompat.bytestr(five),
312 )
312 )
313
313
314 def getbundle(
314 def getbundle(
315 self, source, heads=None, common=None, bundlecaps=None, **kwargs
315 self, source, heads=None, common=None, bundlecaps=None, **kwargs
316 ):
316 ):
317 chunks = exchange.getbundlechunks(
317 chunks = exchange.getbundlechunks(
318 self._repo,
318 self._repo,
319 source,
319 source,
320 heads=heads,
320 heads=heads,
321 common=common,
321 common=common,
322 bundlecaps=bundlecaps,
322 bundlecaps=bundlecaps,
323 **kwargs
323 **kwargs
324 )[1]
324 )[1]
325 cb = util.chunkbuffer(chunks)
325 cb = util.chunkbuffer(chunks)
326
326
327 if exchange.bundle2requested(bundlecaps):
327 if exchange.bundle2requested(bundlecaps):
328 # When requesting a bundle2, getbundle returns a stream to make the
328 # When requesting a bundle2, getbundle returns a stream to make the
329 # wire level function happier. We need to build a proper object
329 # wire level function happier. We need to build a proper object
330 # from it in local peer.
330 # from it in local peer.
331 return bundle2.getunbundler(self.ui, cb)
331 return bundle2.getunbundler(self.ui, cb)
332 else:
332 else:
333 return changegroup.getunbundler(b'01', cb, None)
333 return changegroup.getunbundler(b'01', cb, None)
334
334
335 def heads(self):
335 def heads(self):
336 return self._repo.heads()
336 return self._repo.heads()
337
337
338 def known(self, nodes):
338 def known(self, nodes):
339 return self._repo.known(nodes)
339 return self._repo.known(nodes)
340
340
341 def listkeys(self, namespace):
341 def listkeys(self, namespace):
342 return self._repo.listkeys(namespace)
342 return self._repo.listkeys(namespace)
343
343
344 def lookup(self, key):
344 def lookup(self, key):
345 return self._repo.lookup(key)
345 return self._repo.lookup(key)
346
346
347 def pushkey(self, namespace, key, old, new):
347 def pushkey(self, namespace, key, old, new):
348 return self._repo.pushkey(namespace, key, old, new)
348 return self._repo.pushkey(namespace, key, old, new)
349
349
350 def stream_out(self):
350 def stream_out(self):
351 raise error.Abort(_(b'cannot perform stream clone against local peer'))
351 raise error.Abort(_(b'cannot perform stream clone against local peer'))
352
352
353 def unbundle(self, bundle, heads, url):
353 def unbundle(self, bundle, heads, url):
354 """apply a bundle on a repo
354 """apply a bundle on a repo
355
355
356 This function handles the repo locking itself."""
356 This function handles the repo locking itself."""
357 try:
357 try:
358 try:
358 try:
359 bundle = exchange.readbundle(self.ui, bundle, None)
359 bundle = exchange.readbundle(self.ui, bundle, None)
360 ret = exchange.unbundle(self._repo, bundle, heads, b'push', url)
360 ret = exchange.unbundle(self._repo, bundle, heads, b'push', url)
361 if util.safehasattr(ret, b'getchunks'):
361 if util.safehasattr(ret, b'getchunks'):
362 # This is a bundle20 object, turn it into an unbundler.
362 # This is a bundle20 object, turn it into an unbundler.
363 # This little dance should be dropped eventually when the
363 # This little dance should be dropped eventually when the
364 # API is finally improved.
364 # API is finally improved.
365 stream = util.chunkbuffer(ret.getchunks())
365 stream = util.chunkbuffer(ret.getchunks())
366 ret = bundle2.getunbundler(self.ui, stream)
366 ret = bundle2.getunbundler(self.ui, stream)
367 return ret
367 return ret
368 except Exception as exc:
368 except Exception as exc:
369 # If the exception contains output salvaged from a bundle2
369 # If the exception contains output salvaged from a bundle2
370 # reply, we need to make sure it is printed before continuing
370 # reply, we need to make sure it is printed before continuing
371 # to fail. So we build a bundle2 with such output and consume
371 # to fail. So we build a bundle2 with such output and consume
372 # it directly.
372 # it directly.
373 #
373 #
374 # This is not very elegant but allows a "simple" solution for
374 # This is not very elegant but allows a "simple" solution for
375 # issue4594
375 # issue4594
376 output = getattr(exc, '_bundle2salvagedoutput', ())
376 output = getattr(exc, '_bundle2salvagedoutput', ())
377 if output:
377 if output:
378 bundler = bundle2.bundle20(self._repo.ui)
378 bundler = bundle2.bundle20(self._repo.ui)
379 for out in output:
379 for out in output:
380 bundler.addpart(out)
380 bundler.addpart(out)
381 stream = util.chunkbuffer(bundler.getchunks())
381 stream = util.chunkbuffer(bundler.getchunks())
382 b = bundle2.getunbundler(self.ui, stream)
382 b = bundle2.getunbundler(self.ui, stream)
383 bundle2.processbundle(self._repo, b)
383 bundle2.processbundle(self._repo, b)
384 raise
384 raise
385 except error.PushRaced as exc:
385 except error.PushRaced as exc:
386 raise error.ResponseError(
386 raise error.ResponseError(
387 _(b'push failed:'), stringutil.forcebytestr(exc)
387 _(b'push failed:'), stringutil.forcebytestr(exc)
388 )
388 )
389
389
390 # End of _basewirecommands interface.
390 # End of _basewirecommands interface.
391
391
392 # Begin of peer interface.
392 # Begin of peer interface.
393
393
394 def commandexecutor(self):
394 def commandexecutor(self):
395 return localcommandexecutor(self)
395 return localcommandexecutor(self)
396
396
397 # End of peer interface.
397 # End of peer interface.
398
398
399
399
400 @interfaceutil.implementer(repository.ipeerlegacycommands)
400 @interfaceutil.implementer(repository.ipeerlegacycommands)
401 class locallegacypeer(localpeer):
401 class locallegacypeer(localpeer):
402 """peer extension which implements legacy methods too; used for tests with
402 """peer extension which implements legacy methods too; used for tests with
403 restricted capabilities"""
403 restricted capabilities"""
404
404
405 def __init__(self, repo):
405 def __init__(self, repo):
406 super(locallegacypeer, self).__init__(repo, caps=legacycaps)
406 super(locallegacypeer, self).__init__(repo, caps=legacycaps)
407
407
408 # Begin of baselegacywirecommands interface.
408 # Begin of baselegacywirecommands interface.
409
409
410 def between(self, pairs):
410 def between(self, pairs):
411 return self._repo.between(pairs)
411 return self._repo.between(pairs)
412
412
413 def branches(self, nodes):
413 def branches(self, nodes):
414 return self._repo.branches(nodes)
414 return self._repo.branches(nodes)
415
415
416 def changegroup(self, nodes, source):
416 def changegroup(self, nodes, source):
417 outgoing = discovery.outgoing(
417 outgoing = discovery.outgoing(
418 self._repo, missingroots=nodes, ancestorsof=self._repo.heads()
418 self._repo, missingroots=nodes, ancestorsof=self._repo.heads()
419 )
419 )
420 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
420 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
421
421
422 def changegroupsubset(self, bases, heads, source):
422 def changegroupsubset(self, bases, heads, source):
423 outgoing = discovery.outgoing(
423 outgoing = discovery.outgoing(
424 self._repo, missingroots=bases, ancestorsof=heads
424 self._repo, missingroots=bases, ancestorsof=heads
425 )
425 )
426 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
426 return changegroup.makechangegroup(self._repo, outgoing, b'01', source)
427
427
428 # End of baselegacywirecommands interface.
428 # End of baselegacywirecommands interface.
429
429
430
430
431 # Functions receiving (ui, features) that extensions can register to impact
431 # Functions receiving (ui, features) that extensions can register to impact
432 # the ability to load repositories with custom requirements. Only
432 # the ability to load repositories with custom requirements. Only
433 # functions defined in loaded extensions are called.
433 # functions defined in loaded extensions are called.
434 #
434 #
435 # The function receives a set of requirement strings that the repository
435 # The function receives a set of requirement strings that the repository
436 # is capable of opening. Functions will typically add elements to the
436 # is capable of opening. Functions will typically add elements to the
437 # set to reflect that the extension knows how to handle that requirements.
437 # set to reflect that the extension knows how to handle that requirements.
438 featuresetupfuncs = set()
438 featuresetupfuncs = set()
439
439
440
440
441 def _getsharedvfs(hgvfs, requirements):
441 def _getsharedvfs(hgvfs, requirements):
442 """returns the vfs object pointing to root of shared source
442 """returns the vfs object pointing to root of shared source
443 repo for a shared repository
443 repo for a shared repository
444
444
445 hgvfs is vfs pointing at .hg/ of current repo (shared one)
445 hgvfs is vfs pointing at .hg/ of current repo (shared one)
446 requirements is a set of requirements of current repo (shared one)
446 requirements is a set of requirements of current repo (shared one)
447 """
447 """
448 # The ``shared`` or ``relshared`` requirements indicate the
448 # The ``shared`` or ``relshared`` requirements indicate the
449 # store lives in the path contained in the ``.hg/sharedpath`` file.
449 # store lives in the path contained in the ``.hg/sharedpath`` file.
450 # This is an absolute path for ``shared`` and relative to
450 # This is an absolute path for ``shared`` and relative to
451 # ``.hg/`` for ``relshared``.
451 # ``.hg/`` for ``relshared``.
452 sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
452 sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
453 if requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements:
453 if requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements:
454 sharedpath = hgvfs.join(sharedpath)
454 sharedpath = hgvfs.join(sharedpath)
455
455
456 sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
456 sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
457
457
458 if not sharedvfs.exists():
458 if not sharedvfs.exists():
459 raise error.RepoError(
459 raise error.RepoError(
460 _(b'.hg/sharedpath points to nonexistent directory %s')
460 _(b'.hg/sharedpath points to nonexistent directory %s')
461 % sharedvfs.base
461 % sharedvfs.base
462 )
462 )
463 return sharedvfs
463 return sharedvfs
464
464
465
465
466 def _readrequires(vfs, allowmissing):
466 def _readrequires(vfs, allowmissing):
467 """reads the require file present at root of this vfs
467 """reads the require file present at root of this vfs
468 and return a set of requirements
468 and return a set of requirements
469
469
470 If allowmissing is True, we suppress ENOENT if raised"""
470 If allowmissing is True, we suppress ENOENT if raised"""
471 # requires file contains a newline-delimited list of
471 # requires file contains a newline-delimited list of
472 # features/capabilities the opener (us) must have in order to use
472 # features/capabilities the opener (us) must have in order to use
473 # the repository. This file was introduced in Mercurial 0.9.2,
473 # the repository. This file was introduced in Mercurial 0.9.2,
474 # which means very old repositories may not have one. We assume
474 # which means very old repositories may not have one. We assume
475 # a missing file translates to no requirements.
475 # a missing file translates to no requirements.
476 try:
476 try:
477 requirements = set(vfs.read(b'requires').splitlines())
477 requirements = set(vfs.read(b'requires').splitlines())
478 except IOError as e:
478 except IOError as e:
479 if not (allowmissing and e.errno == errno.ENOENT):
479 if not (allowmissing and e.errno == errno.ENOENT):
480 raise
480 raise
481 requirements = set()
481 requirements = set()
482 return requirements
482 return requirements
483
483
484
484
485 def makelocalrepository(baseui, path, intents=None):
485 def makelocalrepository(baseui, path, intents=None):
486 """Create a local repository object.
486 """Create a local repository object.
487
487
488 Given arguments needed to construct a local repository, this function
488 Given arguments needed to construct a local repository, this function
489 performs various early repository loading functionality (such as
489 performs various early repository loading functionality (such as
490 reading the ``.hg/requires`` and ``.hg/hgrc`` files), validates that
490 reading the ``.hg/requires`` and ``.hg/hgrc`` files), validates that
491 the repository can be opened, derives a type suitable for representing
491 the repository can be opened, derives a type suitable for representing
492 that repository, and returns an instance of it.
492 that repository, and returns an instance of it.
493
493
494 The returned object conforms to the ``repository.completelocalrepository``
494 The returned object conforms to the ``repository.completelocalrepository``
495 interface.
495 interface.
496
496
497 The repository type is derived by calling a series of factory functions
497 The repository type is derived by calling a series of factory functions
498 for each aspect/interface of the final repository. These are defined by
498 for each aspect/interface of the final repository. These are defined by
499 ``REPO_INTERFACES``.
499 ``REPO_INTERFACES``.
500
500
501 Each factory function is called to produce a type implementing a specific
501 Each factory function is called to produce a type implementing a specific
502 interface. The cumulative list of returned types will be combined into a
502 interface. The cumulative list of returned types will be combined into a
503 new type and that type will be instantiated to represent the local
503 new type and that type will be instantiated to represent the local
504 repository.
504 repository.
505
505
506 The factory functions each receive various state that may be consulted
506 The factory functions each receive various state that may be consulted
507 as part of deriving a type.
507 as part of deriving a type.
508
508
509 Extensions should wrap these factory functions to customize repository type
509 Extensions should wrap these factory functions to customize repository type
510 creation. Note that an extension's wrapped function may be called even if
510 creation. Note that an extension's wrapped function may be called even if
511 that extension is not loaded for the repo being constructed. Extensions
511 that extension is not loaded for the repo being constructed. Extensions
512 should check if their ``__name__`` appears in the
512 should check if their ``__name__`` appears in the
513 ``extensionmodulenames`` set passed to the factory function and no-op if
513 ``extensionmodulenames`` set passed to the factory function and no-op if
514 not.
514 not.
515 """
515 """
516 ui = baseui.copy()
516 ui = baseui.copy()
517 # Prevent copying repo configuration.
517 # Prevent copying repo configuration.
518 ui.copy = baseui.copy
518 ui.copy = baseui.copy
519
519
520 # Working directory VFS rooted at repository root.
520 # Working directory VFS rooted at repository root.
521 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
521 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
522
522
523 # Main VFS for .hg/ directory.
523 # Main VFS for .hg/ directory.
524 hgpath = wdirvfs.join(b'.hg')
524 hgpath = wdirvfs.join(b'.hg')
525 hgvfs = vfsmod.vfs(hgpath, cacheaudited=True)
525 hgvfs = vfsmod.vfs(hgpath, cacheaudited=True)
526 # Whether this repository is shared one or not
526 # Whether this repository is shared one or not
527 shared = False
527 shared = False
528 # If this repository is shared, vfs pointing to shared repo
528 # If this repository is shared, vfs pointing to shared repo
529 sharedvfs = None
529 sharedvfs = None
530
530
531 # The .hg/ path should exist and should be a directory. All other
531 # The .hg/ path should exist and should be a directory. All other
532 # cases are errors.
532 # cases are errors.
533 if not hgvfs.isdir():
533 if not hgvfs.isdir():
534 try:
534 try:
535 hgvfs.stat()
535 hgvfs.stat()
536 except OSError as e:
536 except OSError as e:
537 if e.errno != errno.ENOENT:
537 if e.errno != errno.ENOENT:
538 raise
538 raise
539 except ValueError as e:
539 except ValueError as e:
540 # Can be raised on Python 3.8 when path is invalid.
540 # Can be raised on Python 3.8 when path is invalid.
541 raise error.Abort(
541 raise error.Abort(
542 _(b'invalid path %s: %s') % (path, pycompat.bytestr(e))
542 _(b'invalid path %s: %s') % (path, pycompat.bytestr(e))
543 )
543 )
544
544
545 raise error.RepoError(_(b'repository %s not found') % path)
545 raise error.RepoError(_(b'repository %s not found') % path)
546
546
547 requirements = _readrequires(hgvfs, True)
547 requirements = _readrequires(hgvfs, True)
548 shared = (
548 shared = (
549 requirementsmod.SHARED_REQUIREMENT in requirements
549 requirementsmod.SHARED_REQUIREMENT in requirements
550 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
550 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
551 )
551 )
552 if shared:
552 if shared:
553 sharedvfs = _getsharedvfs(hgvfs, requirements)
553 sharedvfs = _getsharedvfs(hgvfs, requirements)
554
554
555 # if .hg/requires contains the sharesafe requirement, it means
555 # if .hg/requires contains the sharesafe requirement, it means
556 # there exists a `.hg/store/requires` too and we should read it
556 # there exists a `.hg/store/requires` too and we should read it
557 # NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement
557 # NOTE: presence of SHARESAFE_REQUIREMENT imply that store requirement
558 # is present. We never write SHARESAFE_REQUIREMENT for a repo if store
558 # is present. We never write SHARESAFE_REQUIREMENT for a repo if store
559 # is not present, refer checkrequirementscompat() for that
559 # is not present, refer checkrequirementscompat() for that
560 #
560 #
561 # However, if SHARESAFE_REQUIREMENT is not present, it means that the
561 # However, if SHARESAFE_REQUIREMENT is not present, it means that the
562 # repository was shared the old way. We check the share source .hg/requires
562 # repository was shared the old way. We check the share source .hg/requires
563 # for SHARESAFE_REQUIREMENT to detect whether the current repository needs
563 # for SHARESAFE_REQUIREMENT to detect whether the current repository needs
564 # to be reshared
564 # to be reshared
565 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
565 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
566
566
567 if (
567 if (
568 shared
568 shared
569 and requirementsmod.SHARESAFE_REQUIREMENT
569 and requirementsmod.SHARESAFE_REQUIREMENT
570 not in _readrequires(sharedvfs, True)
570 not in _readrequires(sharedvfs, True)
571 ):
571 ):
572 raise error.Abort(
572 raise error.Abort(
573 _(b"share source does not support exp-sharesafe requirement")
573 _(b"share source does not support exp-sharesafe requirement")
574 )
574 )
575
575
576 if shared:
576 if shared:
577 # This is a shared repo
577 # This is a shared repo
578 storevfs = vfsmod.vfs(sharedvfs.join(b'store'))
578 storevfs = vfsmod.vfs(sharedvfs.join(b'store'))
579 else:
579 else:
580 storevfs = vfsmod.vfs(hgvfs.join(b'store'))
580 storevfs = vfsmod.vfs(hgvfs.join(b'store'))
581
581
582 requirements |= _readrequires(storevfs, False)
582 requirements |= _readrequires(storevfs, False)
583 elif shared:
583 elif shared:
584 sourcerequires = _readrequires(sharedvfs, False)
584 sourcerequires = _readrequires(sharedvfs, False)
585 if requirementsmod.SHARESAFE_REQUIREMENT in sourcerequires:
585 if requirementsmod.SHARESAFE_REQUIREMENT in sourcerequires:
586 ui.warn(
586 ui.warn(
587 _(
587 _(
588 b'warning: source repository supports share-safe functionality.'
588 b'warning: source repository supports share-safe functionality.'
589 b' Reshare to upgrade.\n'
589 b' Reshare to upgrade.\n'
590 )
590 )
591 )
591 )
592
592
593 # The .hg/hgrc file may load extensions or contain config options
593 # The .hg/hgrc file may load extensions or contain config options
594 # that influence repository construction. Attempt to load it and
594 # that influence repository construction. Attempt to load it and
595 # process any new extensions that it may have pulled in.
595 # process any new extensions that it may have pulled in.
596 if loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs):
596 if loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs):
597 afterhgrcload(ui, wdirvfs, hgvfs, requirements)
597 afterhgrcload(ui, wdirvfs, hgvfs, requirements)
598 extensions.loadall(ui)
598 extensions.loadall(ui)
599 extensions.populateui(ui)
599 extensions.populateui(ui)
600
600
601 # Set of module names of extensions loaded for this repository.
601 # Set of module names of extensions loaded for this repository.
602 extensionmodulenames = {m.__name__ for n, m in extensions.extensions(ui)}
602 extensionmodulenames = {m.__name__ for n, m in extensions.extensions(ui)}
603
603
604 supportedrequirements = gathersupportedrequirements(ui)
604 supportedrequirements = gathersupportedrequirements(ui)
605
605
606 # We first validate the requirements are known.
606 # We first validate the requirements are known.
607 ensurerequirementsrecognized(requirements, supportedrequirements)
607 ensurerequirementsrecognized(requirements, supportedrequirements)
608
608
609 # Then we validate that the known set is reasonable to use together.
609 # Then we validate that the known set is reasonable to use together.
610 ensurerequirementscompatible(ui, requirements)
610 ensurerequirementscompatible(ui, requirements)
611
611
612 # TODO there are unhandled edge cases related to opening repositories with
612 # TODO there are unhandled edge cases related to opening repositories with
613 # shared storage. If storage is shared, we should also test for requirements
613 # shared storage. If storage is shared, we should also test for requirements
614 # compatibility in the pointed-to repo. This entails loading the .hg/hgrc in
614 # compatibility in the pointed-to repo. This entails loading the .hg/hgrc in
615 # that repo, as that repo may load extensions needed to open it. This is a
615 # that repo, as that repo may load extensions needed to open it. This is a
616 # bit complicated because we don't want the other hgrc to overwrite settings
616 # bit complicated because we don't want the other hgrc to overwrite settings
617 # in this hgrc.
617 # in this hgrc.
618 #
618 #
619 # This bug is somewhat mitigated by the fact that we copy the .hg/requires
619 # This bug is somewhat mitigated by the fact that we copy the .hg/requires
620 # file when sharing repos. But if a requirement is added after the share is
620 # file when sharing repos. But if a requirement is added after the share is
621 # performed, thereby introducing a new requirement for the opener, we may
621 # performed, thereby introducing a new requirement for the opener, we may
622 # will not see that and could encounter a run-time error interacting with
622 # will not see that and could encounter a run-time error interacting with
623 # that shared store since it has an unknown-to-us requirement.
623 # that shared store since it has an unknown-to-us requirement.
624
624
625 # At this point, we know we should be capable of opening the repository.
625 # At this point, we know we should be capable of opening the repository.
626 # Now get on with doing that.
626 # Now get on with doing that.
627
627
628 features = set()
628 features = set()
629
629
630 # The "store" part of the repository holds versioned data. How it is
630 # The "store" part of the repository holds versioned data. How it is
631 # accessed is determined by various requirements. If `shared` or
631 # accessed is determined by various requirements. If `shared` or
632 # `relshared` requirements are present, this indicates current repository
632 # `relshared` requirements are present, this indicates current repository
633 # is a share and store exists in path mentioned in `.hg/sharedpath`
633 # is a share and store exists in path mentioned in `.hg/sharedpath`
634 if shared:
634 if shared:
635 storebasepath = sharedvfs.base
635 storebasepath = sharedvfs.base
636 cachepath = sharedvfs.join(b'cache')
636 cachepath = sharedvfs.join(b'cache')
637 features.add(repository.REPO_FEATURE_SHARED_STORAGE)
637 features.add(repository.REPO_FEATURE_SHARED_STORAGE)
638 else:
638 else:
639 storebasepath = hgvfs.base
639 storebasepath = hgvfs.base
640 cachepath = hgvfs.join(b'cache')
640 cachepath = hgvfs.join(b'cache')
641 wcachepath = hgvfs.join(b'wcache')
641 wcachepath = hgvfs.join(b'wcache')
642
642
643 # The store has changed over time and the exact layout is dictated by
643 # The store has changed over time and the exact layout is dictated by
644 # requirements. The store interface abstracts differences across all
644 # requirements. The store interface abstracts differences across all
645 # of them.
645 # of them.
646 store = makestore(
646 store = makestore(
647 requirements,
647 requirements,
648 storebasepath,
648 storebasepath,
649 lambda base: vfsmod.vfs(base, cacheaudited=True),
649 lambda base: vfsmod.vfs(base, cacheaudited=True),
650 )
650 )
651 hgvfs.createmode = store.createmode
651 hgvfs.createmode = store.createmode
652
652
653 storevfs = store.vfs
653 storevfs = store.vfs
654 storevfs.options = resolvestorevfsoptions(ui, requirements, features)
654 storevfs.options = resolvestorevfsoptions(ui, requirements, features)
655
655
656 # The cache vfs is used to manage cache files.
656 # The cache vfs is used to manage cache files.
657 cachevfs = vfsmod.vfs(cachepath, cacheaudited=True)
657 cachevfs = vfsmod.vfs(cachepath, cacheaudited=True)
658 cachevfs.createmode = store.createmode
658 cachevfs.createmode = store.createmode
659 # The cache vfs is used to manage cache files related to the working copy
659 # The cache vfs is used to manage cache files related to the working copy
660 wcachevfs = vfsmod.vfs(wcachepath, cacheaudited=True)
660 wcachevfs = vfsmod.vfs(wcachepath, cacheaudited=True)
661 wcachevfs.createmode = store.createmode
661 wcachevfs.createmode = store.createmode
662
662
663 # Now resolve the type for the repository object. We do this by repeatedly
663 # Now resolve the type for the repository object. We do this by repeatedly
664 # calling a factory function to produces types for specific aspects of the
664 # calling a factory function to produces types for specific aspects of the
665 # repo's operation. The aggregate returned types are used as base classes
665 # repo's operation. The aggregate returned types are used as base classes
666 # for a dynamically-derived type, which will represent our new repository.
666 # for a dynamically-derived type, which will represent our new repository.
667
667
668 bases = []
668 bases = []
669 extrastate = {}
669 extrastate = {}
670
670
671 for iface, fn in REPO_INTERFACES:
671 for iface, fn in REPO_INTERFACES:
672 # We pass all potentially useful state to give extensions tons of
672 # We pass all potentially useful state to give extensions tons of
673 # flexibility.
673 # flexibility.
674 typ = fn()(
674 typ = fn()(
675 ui=ui,
675 ui=ui,
676 intents=intents,
676 intents=intents,
677 requirements=requirements,
677 requirements=requirements,
678 features=features,
678 features=features,
679 wdirvfs=wdirvfs,
679 wdirvfs=wdirvfs,
680 hgvfs=hgvfs,
680 hgvfs=hgvfs,
681 store=store,
681 store=store,
682 storevfs=storevfs,
682 storevfs=storevfs,
683 storeoptions=storevfs.options,
683 storeoptions=storevfs.options,
684 cachevfs=cachevfs,
684 cachevfs=cachevfs,
685 wcachevfs=wcachevfs,
685 wcachevfs=wcachevfs,
686 extensionmodulenames=extensionmodulenames,
686 extensionmodulenames=extensionmodulenames,
687 extrastate=extrastate,
687 extrastate=extrastate,
688 baseclasses=bases,
688 baseclasses=bases,
689 )
689 )
690
690
691 if not isinstance(typ, type):
691 if not isinstance(typ, type):
692 raise error.ProgrammingError(
692 raise error.ProgrammingError(
693 b'unable to construct type for %s' % iface
693 b'unable to construct type for %s' % iface
694 )
694 )
695
695
696 bases.append(typ)
696 bases.append(typ)
697
697
698 # type() allows you to use characters in type names that wouldn't be
698 # type() allows you to use characters in type names that wouldn't be
699 # recognized as Python symbols in source code. We abuse that to add
699 # recognized as Python symbols in source code. We abuse that to add
700 # rich information about our constructed repo.
700 # rich information about our constructed repo.
701 name = pycompat.sysstr(
701 name = pycompat.sysstr(
702 b'derivedrepo:%s<%s>' % (wdirvfs.base, b','.join(sorted(requirements)))
702 b'derivedrepo:%s<%s>' % (wdirvfs.base, b','.join(sorted(requirements)))
703 )
703 )
704
704
705 cls = type(name, tuple(bases), {})
705 cls = type(name, tuple(bases), {})
706
706
707 return cls(
707 return cls(
708 baseui=baseui,
708 baseui=baseui,
709 ui=ui,
709 ui=ui,
710 origroot=path,
710 origroot=path,
711 wdirvfs=wdirvfs,
711 wdirvfs=wdirvfs,
712 hgvfs=hgvfs,
712 hgvfs=hgvfs,
713 requirements=requirements,
713 requirements=requirements,
714 supportedrequirements=supportedrequirements,
714 supportedrequirements=supportedrequirements,
715 sharedpath=storebasepath,
715 sharedpath=storebasepath,
716 store=store,
716 store=store,
717 cachevfs=cachevfs,
717 cachevfs=cachevfs,
718 wcachevfs=wcachevfs,
718 wcachevfs=wcachevfs,
719 features=features,
719 features=features,
720 intents=intents,
720 intents=intents,
721 )
721 )
722
722
723
723
724 def loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs=None):
724 def loadhgrc(ui, wdirvfs, hgvfs, requirements, sharedvfs=None):
725 """Load hgrc files/content into a ui instance.
725 """Load hgrc files/content into a ui instance.
726
726
727 This is called during repository opening to load any additional
727 This is called during repository opening to load any additional
728 config files or settings relevant to the current repository.
728 config files or settings relevant to the current repository.
729
729
730 Returns a bool indicating whether any additional configs were loaded.
730 Returns a bool indicating whether any additional configs were loaded.
731
731
732 Extensions should monkeypatch this function to modify how per-repo
732 Extensions should monkeypatch this function to modify how per-repo
733 configs are loaded. For example, an extension may wish to pull in
733 configs are loaded. For example, an extension may wish to pull in
734 configs from alternate files or sources.
734 configs from alternate files or sources.
735
735
736 sharedvfs is vfs object pointing to source repo if the current one is a
736 sharedvfs is vfs object pointing to source repo if the current one is a
737 shared one
737 shared one
738 """
738 """
739 if not rcutil.use_repo_hgrc():
739 if not rcutil.use_repo_hgrc():
740 return False
740 return False
741
741
742 ret = False
742 ret = False
743 # first load config from shared source if we has to
743 # first load config from shared source if we has to
744 if requirementsmod.SHARESAFE_REQUIREMENT in requirements and sharedvfs:
744 if requirementsmod.SHARESAFE_REQUIREMENT in requirements and sharedvfs:
745 try:
745 try:
746 ui.readconfig(sharedvfs.join(b'hgrc'), root=sharedvfs.base)
746 ui.readconfig(sharedvfs.join(b'hgrc'), root=sharedvfs.base)
747 ret = True
747 ret = True
748 except IOError:
748 except IOError:
749 pass
749 pass
750
750
751 try:
751 try:
752 ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
752 ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
753 ret = True
753 ret = True
754 except IOError:
754 except IOError:
755 pass
755 pass
756
756
757 try:
757 try:
758 ui.readconfig(hgvfs.join(b'hgrc-not-shared'), root=wdirvfs.base)
758 ui.readconfig(hgvfs.join(b'hgrc-not-shared'), root=wdirvfs.base)
759 ret = True
759 ret = True
760 except IOError:
760 except IOError:
761 pass
761 pass
762
762
763 return ret
763 return ret
764
764
765
765
766 def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
766 def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
767 """Perform additional actions after .hg/hgrc is loaded.
767 """Perform additional actions after .hg/hgrc is loaded.
768
768
769 This function is called during repository loading immediately after
769 This function is called during repository loading immediately after
770 the .hg/hgrc file is loaded and before per-repo extensions are loaded.
770 the .hg/hgrc file is loaded and before per-repo extensions are loaded.
771
771
772 The function can be used to validate configs, automatically add
772 The function can be used to validate configs, automatically add
773 options (including extensions) based on requirements, etc.
773 options (including extensions) based on requirements, etc.
774 """
774 """
775
775
776 # Map of requirements to list of extensions to load automatically when
776 # Map of requirements to list of extensions to load automatically when
777 # requirement is present.
777 # requirement is present.
778 autoextensions = {
778 autoextensions = {
779 b'git': [b'git'],
779 b'git': [b'git'],
780 b'largefiles': [b'largefiles'],
780 b'largefiles': [b'largefiles'],
781 b'lfs': [b'lfs'],
781 b'lfs': [b'lfs'],
782 }
782 }
783
783
784 for requirement, names in sorted(autoextensions.items()):
784 for requirement, names in sorted(autoextensions.items()):
785 if requirement not in requirements:
785 if requirement not in requirements:
786 continue
786 continue
787
787
788 for name in names:
788 for name in names:
789 if not ui.hasconfig(b'extensions', name):
789 if not ui.hasconfig(b'extensions', name):
790 ui.setconfig(b'extensions', name, b'', source=b'autoload')
790 ui.setconfig(b'extensions', name, b'', source=b'autoload')
791
791
792
792
793 def gathersupportedrequirements(ui):
793 def gathersupportedrequirements(ui):
794 """Determine the complete set of recognized requirements."""
794 """Determine the complete set of recognized requirements."""
795 # Start with all requirements supported by this file.
795 # Start with all requirements supported by this file.
796 supported = set(localrepository._basesupported)
796 supported = set(localrepository._basesupported)
797
797
798 # Execute ``featuresetupfuncs`` entries if they belong to an extension
798 # Execute ``featuresetupfuncs`` entries if they belong to an extension
799 # relevant to this ui instance.
799 # relevant to this ui instance.
800 modules = {m.__name__ for n, m in extensions.extensions(ui)}
800 modules = {m.__name__ for n, m in extensions.extensions(ui)}
801
801
802 for fn in featuresetupfuncs:
802 for fn in featuresetupfuncs:
803 if fn.__module__ in modules:
803 if fn.__module__ in modules:
804 fn(ui, supported)
804 fn(ui, supported)
805
805
806 # Add derived requirements from registered compression engines.
806 # Add derived requirements from registered compression engines.
807 for name in util.compengines:
807 for name in util.compengines:
808 engine = util.compengines[name]
808 engine = util.compengines[name]
809 if engine.available() and engine.revlogheader():
809 if engine.available() and engine.revlogheader():
810 supported.add(b'exp-compression-%s' % name)
810 supported.add(b'exp-compression-%s' % name)
811 if engine.name() == b'zstd':
811 if engine.name() == b'zstd':
812 supported.add(b'revlog-compression-zstd')
812 supported.add(b'revlog-compression-zstd')
813
813
814 return supported
814 return supported
815
815
816
816
817 def ensurerequirementsrecognized(requirements, supported):
817 def ensurerequirementsrecognized(requirements, supported):
818 """Validate that a set of local requirements is recognized.
818 """Validate that a set of local requirements is recognized.
819
819
820 Receives a set of requirements. Raises an ``error.RepoError`` if there
820 Receives a set of requirements. Raises an ``error.RepoError`` if there
821 exists any requirement in that set that currently loaded code doesn't
821 exists any requirement in that set that currently loaded code doesn't
822 recognize.
822 recognize.
823
823
824 Returns a set of supported requirements.
824 Returns a set of supported requirements.
825 """
825 """
826 missing = set()
826 missing = set()
827
827
828 for requirement in requirements:
828 for requirement in requirements:
829 if requirement in supported:
829 if requirement in supported:
830 continue
830 continue
831
831
832 if not requirement or not requirement[0:1].isalnum():
832 if not requirement or not requirement[0:1].isalnum():
833 raise error.RequirementError(_(b'.hg/requires file is corrupt'))
833 raise error.RequirementError(_(b'.hg/requires file is corrupt'))
834
834
835 missing.add(requirement)
835 missing.add(requirement)
836
836
837 if missing:
837 if missing:
838 raise error.RequirementError(
838 raise error.RequirementError(
839 _(b'repository requires features unknown to this Mercurial: %s')
839 _(b'repository requires features unknown to this Mercurial: %s')
840 % b' '.join(sorted(missing)),
840 % b' '.join(sorted(missing)),
841 hint=_(
841 hint=_(
842 b'see https://mercurial-scm.org/wiki/MissingRequirement '
842 b'see https://mercurial-scm.org/wiki/MissingRequirement '
843 b'for more information'
843 b'for more information'
844 ),
844 ),
845 )
845 )
846
846
847
847
848 def ensurerequirementscompatible(ui, requirements):
848 def ensurerequirementscompatible(ui, requirements):
849 """Validates that a set of recognized requirements is mutually compatible.
849 """Validates that a set of recognized requirements is mutually compatible.
850
850
851 Some requirements may not be compatible with others or require
851 Some requirements may not be compatible with others or require
852 config options that aren't enabled. This function is called during
852 config options that aren't enabled. This function is called during
853 repository opening to ensure that the set of requirements needed
853 repository opening to ensure that the set of requirements needed
854 to open a repository is sane and compatible with config options.
854 to open a repository is sane and compatible with config options.
855
855
856 Extensions can monkeypatch this function to perform additional
856 Extensions can monkeypatch this function to perform additional
857 checking.
857 checking.
858
858
859 ``error.RepoError`` should be raised on failure.
859 ``error.RepoError`` should be raised on failure.
860 """
860 """
861 if (
861 if (
862 requirementsmod.SPARSE_REQUIREMENT in requirements
862 requirementsmod.SPARSE_REQUIREMENT in requirements
863 and not sparse.enabled
863 and not sparse.enabled
864 ):
864 ):
865 raise error.RepoError(
865 raise error.RepoError(
866 _(
866 _(
867 b'repository is using sparse feature but '
867 b'repository is using sparse feature but '
868 b'sparse is not enabled; enable the '
868 b'sparse is not enabled; enable the '
869 b'"sparse" extensions to access'
869 b'"sparse" extensions to access'
870 )
870 )
871 )
871 )
872
872
873
873
874 def makestore(requirements, path, vfstype):
874 def makestore(requirements, path, vfstype):
875 """Construct a storage object for a repository."""
875 """Construct a storage object for a repository."""
876 if b'store' in requirements:
876 if b'store' in requirements:
877 if b'fncache' in requirements:
877 if b'fncache' in requirements:
878 return storemod.fncachestore(
878 return storemod.fncachestore(
879 path, vfstype, b'dotencode' in requirements
879 path, vfstype, b'dotencode' in requirements
880 )
880 )
881
881
882 return storemod.encodedstore(path, vfstype)
882 return storemod.encodedstore(path, vfstype)
883
883
884 return storemod.basicstore(path, vfstype)
884 return storemod.basicstore(path, vfstype)
885
885
886
886
887 def resolvestorevfsoptions(ui, requirements, features):
887 def resolvestorevfsoptions(ui, requirements, features):
888 """Resolve the options to pass to the store vfs opener.
888 """Resolve the options to pass to the store vfs opener.
889
889
890 The returned dict is used to influence behavior of the storage layer.
890 The returned dict is used to influence behavior of the storage layer.
891 """
891 """
892 options = {}
892 options = {}
893
893
894 if requirementsmod.TREEMANIFEST_REQUIREMENT in requirements:
894 if requirementsmod.TREEMANIFEST_REQUIREMENT in requirements:
895 options[b'treemanifest'] = True
895 options[b'treemanifest'] = True
896
896
897 # experimental config: format.manifestcachesize
897 # experimental config: format.manifestcachesize
898 manifestcachesize = ui.configint(b'format', b'manifestcachesize')
898 manifestcachesize = ui.configint(b'format', b'manifestcachesize')
899 if manifestcachesize is not None:
899 if manifestcachesize is not None:
900 options[b'manifestcachesize'] = manifestcachesize
900 options[b'manifestcachesize'] = manifestcachesize
901
901
902 # In the absence of another requirement superseding a revlog-related
902 # In the absence of another requirement superseding a revlog-related
903 # requirement, we have to assume the repo is using revlog version 0.
903 # requirement, we have to assume the repo is using revlog version 0.
904 # This revlog format is super old and we don't bother trying to parse
904 # This revlog format is super old and we don't bother trying to parse
905 # opener options for it because those options wouldn't do anything
905 # opener options for it because those options wouldn't do anything
906 # meaningful on such old repos.
906 # meaningful on such old repos.
907 if (
907 if (
908 b'revlogv1' in requirements
908 b'revlogv1' in requirements
909 or requirementsmod.REVLOGV2_REQUIREMENT in requirements
909 or requirementsmod.REVLOGV2_REQUIREMENT in requirements
910 ):
910 ):
911 options.update(resolverevlogstorevfsoptions(ui, requirements, features))
911 options.update(resolverevlogstorevfsoptions(ui, requirements, features))
912 else: # explicitly mark repo as using revlogv0
912 else: # explicitly mark repo as using revlogv0
913 options[b'revlogv0'] = True
913 options[b'revlogv0'] = True
914
914
915 if requirementsmod.COPIESSDC_REQUIREMENT in requirements:
915 if requirementsmod.COPIESSDC_REQUIREMENT in requirements:
916 options[b'copies-storage'] = b'changeset-sidedata'
916 options[b'copies-storage'] = b'changeset-sidedata'
917 else:
917 else:
918 writecopiesto = ui.config(b'experimental', b'copies.write-to')
918 writecopiesto = ui.config(b'experimental', b'copies.write-to')
919 copiesextramode = (b'changeset-only', b'compatibility')
919 copiesextramode = (b'changeset-only', b'compatibility')
920 if writecopiesto in copiesextramode:
920 if writecopiesto in copiesextramode:
921 options[b'copies-storage'] = b'extra'
921 options[b'copies-storage'] = b'extra'
922
922
923 return options
923 return options
924
924
925
925
926 def resolverevlogstorevfsoptions(ui, requirements, features):
926 def resolverevlogstorevfsoptions(ui, requirements, features):
927 """Resolve opener options specific to revlogs."""
927 """Resolve opener options specific to revlogs."""
928
928
929 options = {}
929 options = {}
930 options[b'flagprocessors'] = {}
930 options[b'flagprocessors'] = {}
931
931
932 if b'revlogv1' in requirements:
932 if b'revlogv1' in requirements:
933 options[b'revlogv1'] = True
933 options[b'revlogv1'] = True
934 if requirementsmod.REVLOGV2_REQUIREMENT in requirements:
934 if requirementsmod.REVLOGV2_REQUIREMENT in requirements:
935 options[b'revlogv2'] = True
935 options[b'revlogv2'] = True
936
936
937 if b'generaldelta' in requirements:
937 if b'generaldelta' in requirements:
938 options[b'generaldelta'] = True
938 options[b'generaldelta'] = True
939
939
940 # experimental config: format.chunkcachesize
940 # experimental config: format.chunkcachesize
941 chunkcachesize = ui.configint(b'format', b'chunkcachesize')
941 chunkcachesize = ui.configint(b'format', b'chunkcachesize')
942 if chunkcachesize is not None:
942 if chunkcachesize is not None:
943 options[b'chunkcachesize'] = chunkcachesize
943 options[b'chunkcachesize'] = chunkcachesize
944
944
945 deltabothparents = ui.configbool(
945 deltabothparents = ui.configbool(
946 b'storage', b'revlog.optimize-delta-parent-choice'
946 b'storage', b'revlog.optimize-delta-parent-choice'
947 )
947 )
948 options[b'deltabothparents'] = deltabothparents
948 options[b'deltabothparents'] = deltabothparents
949
949
950 lazydelta = ui.configbool(b'storage', b'revlog.reuse-external-delta')
950 lazydelta = ui.configbool(b'storage', b'revlog.reuse-external-delta')
951 lazydeltabase = False
951 lazydeltabase = False
952 if lazydelta:
952 if lazydelta:
953 lazydeltabase = ui.configbool(
953 lazydeltabase = ui.configbool(
954 b'storage', b'revlog.reuse-external-delta-parent'
954 b'storage', b'revlog.reuse-external-delta-parent'
955 )
955 )
956 if lazydeltabase is None:
956 if lazydeltabase is None:
957 lazydeltabase = not scmutil.gddeltaconfig(ui)
957 lazydeltabase = not scmutil.gddeltaconfig(ui)
958 options[b'lazydelta'] = lazydelta
958 options[b'lazydelta'] = lazydelta
959 options[b'lazydeltabase'] = lazydeltabase
959 options[b'lazydeltabase'] = lazydeltabase
960
960
961 chainspan = ui.configbytes(b'experimental', b'maxdeltachainspan')
961 chainspan = ui.configbytes(b'experimental', b'maxdeltachainspan')
962 if 0 <= chainspan:
962 if 0 <= chainspan:
963 options[b'maxdeltachainspan'] = chainspan
963 options[b'maxdeltachainspan'] = chainspan
964
964
965 mmapindexthreshold = ui.configbytes(b'experimental', b'mmapindexthreshold')
965 mmapindexthreshold = ui.configbytes(b'experimental', b'mmapindexthreshold')
966 if mmapindexthreshold is not None:
966 if mmapindexthreshold is not None:
967 options[b'mmapindexthreshold'] = mmapindexthreshold
967 options[b'mmapindexthreshold'] = mmapindexthreshold
968
968
969 withsparseread = ui.configbool(b'experimental', b'sparse-read')
969 withsparseread = ui.configbool(b'experimental', b'sparse-read')
970 srdensitythres = float(
970 srdensitythres = float(
971 ui.config(b'experimental', b'sparse-read.density-threshold')
971 ui.config(b'experimental', b'sparse-read.density-threshold')
972 )
972 )
973 srmingapsize = ui.configbytes(b'experimental', b'sparse-read.min-gap-size')
973 srmingapsize = ui.configbytes(b'experimental', b'sparse-read.min-gap-size')
974 options[b'with-sparse-read'] = withsparseread
974 options[b'with-sparse-read'] = withsparseread
975 options[b'sparse-read-density-threshold'] = srdensitythres
975 options[b'sparse-read-density-threshold'] = srdensitythres
976 options[b'sparse-read-min-gap-size'] = srmingapsize
976 options[b'sparse-read-min-gap-size'] = srmingapsize
977
977
978 sparserevlog = requirementsmod.SPARSEREVLOG_REQUIREMENT in requirements
978 sparserevlog = requirementsmod.SPARSEREVLOG_REQUIREMENT in requirements
979 options[b'sparse-revlog'] = sparserevlog
979 options[b'sparse-revlog'] = sparserevlog
980 if sparserevlog:
980 if sparserevlog:
981 options[b'generaldelta'] = True
981 options[b'generaldelta'] = True
982
982
983 sidedata = requirementsmod.SIDEDATA_REQUIREMENT in requirements
983 sidedata = requirementsmod.SIDEDATA_REQUIREMENT in requirements
984 options[b'side-data'] = sidedata
984 options[b'side-data'] = sidedata
985
985
986 maxchainlen = None
986 maxchainlen = None
987 if sparserevlog:
987 if sparserevlog:
988 maxchainlen = revlogconst.SPARSE_REVLOG_MAX_CHAIN_LENGTH
988 maxchainlen = revlogconst.SPARSE_REVLOG_MAX_CHAIN_LENGTH
989 # experimental config: format.maxchainlen
989 # experimental config: format.maxchainlen
990 maxchainlen = ui.configint(b'format', b'maxchainlen', maxchainlen)
990 maxchainlen = ui.configint(b'format', b'maxchainlen', maxchainlen)
991 if maxchainlen is not None:
991 if maxchainlen is not None:
992 options[b'maxchainlen'] = maxchainlen
992 options[b'maxchainlen'] = maxchainlen
993
993
994 for r in requirements:
994 for r in requirements:
995 # we allow multiple compression engine requirement to co-exist because
995 # we allow multiple compression engine requirement to co-exist because
996 # strickly speaking, revlog seems to support mixed compression style.
996 # strickly speaking, revlog seems to support mixed compression style.
997 #
997 #
998 # The compression used for new entries will be "the last one"
998 # The compression used for new entries will be "the last one"
999 prefix = r.startswith
999 prefix = r.startswith
1000 if prefix(b'revlog-compression-') or prefix(b'exp-compression-'):
1000 if prefix(b'revlog-compression-') or prefix(b'exp-compression-'):
1001 options[b'compengine'] = r.split(b'-', 2)[2]
1001 options[b'compengine'] = r.split(b'-', 2)[2]
1002
1002
1003 options[b'zlib.level'] = ui.configint(b'storage', b'revlog.zlib.level')
1003 options[b'zlib.level'] = ui.configint(b'storage', b'revlog.zlib.level')
1004 if options[b'zlib.level'] is not None:
1004 if options[b'zlib.level'] is not None:
1005 if not (0 <= options[b'zlib.level'] <= 9):
1005 if not (0 <= options[b'zlib.level'] <= 9):
1006 msg = _(b'invalid value for `storage.revlog.zlib.level` config: %d')
1006 msg = _(b'invalid value for `storage.revlog.zlib.level` config: %d')
1007 raise error.Abort(msg % options[b'zlib.level'])
1007 raise error.Abort(msg % options[b'zlib.level'])
1008 options[b'zstd.level'] = ui.configint(b'storage', b'revlog.zstd.level')
1008 options[b'zstd.level'] = ui.configint(b'storage', b'revlog.zstd.level')
1009 if options[b'zstd.level'] is not None:
1009 if options[b'zstd.level'] is not None:
1010 if not (0 <= options[b'zstd.level'] <= 22):
1010 if not (0 <= options[b'zstd.level'] <= 22):
1011 msg = _(b'invalid value for `storage.revlog.zstd.level` config: %d')
1011 msg = _(b'invalid value for `storage.revlog.zstd.level` config: %d')
1012 raise error.Abort(msg % options[b'zstd.level'])
1012 raise error.Abort(msg % options[b'zstd.level'])
1013
1013
1014 if requirementsmod.NARROW_REQUIREMENT in requirements:
1014 if requirementsmod.NARROW_REQUIREMENT in requirements:
1015 options[b'enableellipsis'] = True
1015 options[b'enableellipsis'] = True
1016
1016
1017 if ui.configbool(b'experimental', b'rust.index'):
1017 if ui.configbool(b'experimental', b'rust.index'):
1018 options[b'rust.index'] = True
1018 options[b'rust.index'] = True
1019 if requirementsmod.NODEMAP_REQUIREMENT in requirements:
1019 if requirementsmod.NODEMAP_REQUIREMENT in requirements:
1020 options[b'persistent-nodemap'] = True
1020 options[b'persistent-nodemap'] = True
1021 if ui.configbool(b'storage', b'revlog.nodemap.mmap'):
1021 if ui.configbool(b'storage', b'revlog.nodemap.mmap'):
1022 options[b'persistent-nodemap.mmap'] = True
1022 options[b'persistent-nodemap.mmap'] = True
1023 epnm = ui.config(b'storage', b'revlog.nodemap.mode')
1023 epnm = ui.config(b'storage', b'revlog.nodemap.mode')
1024 options[b'persistent-nodemap.mode'] = epnm
1024 options[b'persistent-nodemap.mode'] = epnm
1025 if ui.configbool(b'devel', b'persistent-nodemap'):
1025 if ui.configbool(b'devel', b'persistent-nodemap'):
1026 options[b'devel-force-nodemap'] = True
1026 options[b'devel-force-nodemap'] = True
1027
1027
1028 return options
1028 return options
1029
1029
1030
1030
1031 def makemain(**kwargs):
1031 def makemain(**kwargs):
1032 """Produce a type conforming to ``ilocalrepositorymain``."""
1032 """Produce a type conforming to ``ilocalrepositorymain``."""
1033 return localrepository
1033 return localrepository
1034
1034
1035
1035
1036 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1036 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1037 class revlogfilestorage(object):
1037 class revlogfilestorage(object):
1038 """File storage when using revlogs."""
1038 """File storage when using revlogs."""
1039
1039
1040 def file(self, path):
1040 def file(self, path):
1041 if path[0] == b'/':
1041 if path[0] == b'/':
1042 path = path[1:]
1042 path = path[1:]
1043
1043
1044 return filelog.filelog(self.svfs, path)
1044 return filelog.filelog(self.svfs, path)
1045
1045
1046
1046
1047 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1047 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
1048 class revlognarrowfilestorage(object):
1048 class revlognarrowfilestorage(object):
1049 """File storage when using revlogs and narrow files."""
1049 """File storage when using revlogs and narrow files."""
1050
1050
1051 def file(self, path):
1051 def file(self, path):
1052 if path[0] == b'/':
1052 if path[0] == b'/':
1053 path = path[1:]
1053 path = path[1:]
1054
1054
1055 return filelog.narrowfilelog(self.svfs, path, self._storenarrowmatch)
1055 return filelog.narrowfilelog(self.svfs, path, self._storenarrowmatch)
1056
1056
1057
1057
1058 def makefilestorage(requirements, features, **kwargs):
1058 def makefilestorage(requirements, features, **kwargs):
1059 """Produce a type conforming to ``ilocalrepositoryfilestorage``."""
1059 """Produce a type conforming to ``ilocalrepositoryfilestorage``."""
1060 features.add(repository.REPO_FEATURE_REVLOG_FILE_STORAGE)
1060 features.add(repository.REPO_FEATURE_REVLOG_FILE_STORAGE)
1061 features.add(repository.REPO_FEATURE_STREAM_CLONE)
1061 features.add(repository.REPO_FEATURE_STREAM_CLONE)
1062
1062
1063 if requirementsmod.NARROW_REQUIREMENT in requirements:
1063 if requirementsmod.NARROW_REQUIREMENT in requirements:
1064 return revlognarrowfilestorage
1064 return revlognarrowfilestorage
1065 else:
1065 else:
1066 return revlogfilestorage
1066 return revlogfilestorage
1067
1067
1068
1068
1069 # List of repository interfaces and factory functions for them. Each
1069 # List of repository interfaces and factory functions for them. Each
1070 # will be called in order during ``makelocalrepository()`` to iteratively
1070 # will be called in order during ``makelocalrepository()`` to iteratively
1071 # derive the final type for a local repository instance. We capture the
1071 # derive the final type for a local repository instance. We capture the
1072 # function as a lambda so we don't hold a reference and the module-level
1072 # function as a lambda so we don't hold a reference and the module-level
1073 # functions can be wrapped.
1073 # functions can be wrapped.
1074 REPO_INTERFACES = [
1074 REPO_INTERFACES = [
1075 (repository.ilocalrepositorymain, lambda: makemain),
1075 (repository.ilocalrepositorymain, lambda: makemain),
1076 (repository.ilocalrepositoryfilestorage, lambda: makefilestorage),
1076 (repository.ilocalrepositoryfilestorage, lambda: makefilestorage),
1077 ]
1077 ]
1078
1078
1079
1079
1080 @interfaceutil.implementer(repository.ilocalrepositorymain)
1080 @interfaceutil.implementer(repository.ilocalrepositorymain)
1081 class localrepository(object):
1081 class localrepository(object):
1082 """Main class for representing local repositories.
1082 """Main class for representing local repositories.
1083
1083
1084 All local repositories are instances of this class.
1084 All local repositories are instances of this class.
1085
1085
1086 Constructed on its own, instances of this class are not usable as
1086 Constructed on its own, instances of this class are not usable as
1087 repository objects. To obtain a usable repository object, call
1087 repository objects. To obtain a usable repository object, call
1088 ``hg.repository()``, ``localrepo.instance()``, or
1088 ``hg.repository()``, ``localrepo.instance()``, or
1089 ``localrepo.makelocalrepository()``. The latter is the lowest-level.
1089 ``localrepo.makelocalrepository()``. The latter is the lowest-level.
1090 ``instance()`` adds support for creating new repositories.
1090 ``instance()`` adds support for creating new repositories.
1091 ``hg.repository()`` adds more extension integration, including calling
1091 ``hg.repository()`` adds more extension integration, including calling
1092 ``reposetup()``. Generally speaking, ``hg.repository()`` should be
1092 ``reposetup()``. Generally speaking, ``hg.repository()`` should be
1093 used.
1093 used.
1094 """
1094 """
1095
1095
1096 # obsolete experimental requirements:
1096 # obsolete experimental requirements:
1097 # - manifestv2: An experimental new manifest format that allowed
1097 # - manifestv2: An experimental new manifest format that allowed
1098 # for stem compression of long paths. Experiment ended up not
1098 # for stem compression of long paths. Experiment ended up not
1099 # being successful (repository sizes went up due to worse delta
1099 # being successful (repository sizes went up due to worse delta
1100 # chains), and the code was deleted in 4.6.
1100 # chains), and the code was deleted in 4.6.
1101 supportedformats = {
1101 supportedformats = {
1102 b'revlogv1',
1102 b'revlogv1',
1103 b'generaldelta',
1103 b'generaldelta',
1104 requirementsmod.TREEMANIFEST_REQUIREMENT,
1104 requirementsmod.TREEMANIFEST_REQUIREMENT,
1105 requirementsmod.COPIESSDC_REQUIREMENT,
1105 requirementsmod.COPIESSDC_REQUIREMENT,
1106 requirementsmod.REVLOGV2_REQUIREMENT,
1106 requirementsmod.REVLOGV2_REQUIREMENT,
1107 requirementsmod.SIDEDATA_REQUIREMENT,
1107 requirementsmod.SIDEDATA_REQUIREMENT,
1108 requirementsmod.SPARSEREVLOG_REQUIREMENT,
1108 requirementsmod.SPARSEREVLOG_REQUIREMENT,
1109 requirementsmod.NODEMAP_REQUIREMENT,
1109 requirementsmod.NODEMAP_REQUIREMENT,
1110 bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT,
1110 bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT,
1111 requirementsmod.SHARESAFE_REQUIREMENT,
1111 requirementsmod.SHARESAFE_REQUIREMENT,
1112 }
1112 }
1113 _basesupported = supportedformats | {
1113 _basesupported = supportedformats | {
1114 b'store',
1114 b'store',
1115 b'fncache',
1115 b'fncache',
1116 requirementsmod.SHARED_REQUIREMENT,
1116 requirementsmod.SHARED_REQUIREMENT,
1117 requirementsmod.RELATIVE_SHARED_REQUIREMENT,
1117 requirementsmod.RELATIVE_SHARED_REQUIREMENT,
1118 b'dotencode',
1118 b'dotencode',
1119 requirementsmod.SPARSE_REQUIREMENT,
1119 requirementsmod.SPARSE_REQUIREMENT,
1120 requirementsmod.INTERNAL_PHASE_REQUIREMENT,
1120 requirementsmod.INTERNAL_PHASE_REQUIREMENT,
1121 }
1121 }
1122
1122
1123 # list of prefix for file which can be written without 'wlock'
1123 # list of prefix for file which can be written without 'wlock'
1124 # Extensions should extend this list when needed
1124 # Extensions should extend this list when needed
1125 _wlockfreeprefix = {
1125 _wlockfreeprefix = {
1126 # We migh consider requiring 'wlock' for the next
1126 # We migh consider requiring 'wlock' for the next
1127 # two, but pretty much all the existing code assume
1127 # two, but pretty much all the existing code assume
1128 # wlock is not needed so we keep them excluded for
1128 # wlock is not needed so we keep them excluded for
1129 # now.
1129 # now.
1130 b'hgrc',
1130 b'hgrc',
1131 b'requires',
1131 b'requires',
1132 # XXX cache is a complicatged business someone
1132 # XXX cache is a complicatged business someone
1133 # should investigate this in depth at some point
1133 # should investigate this in depth at some point
1134 b'cache/',
1134 b'cache/',
1135 # XXX shouldn't be dirstate covered by the wlock?
1135 # XXX shouldn't be dirstate covered by the wlock?
1136 b'dirstate',
1136 b'dirstate',
1137 # XXX bisect was still a bit too messy at the time
1137 # XXX bisect was still a bit too messy at the time
1138 # this changeset was introduced. Someone should fix
1138 # this changeset was introduced. Someone should fix
1139 # the remainig bit and drop this line
1139 # the remainig bit and drop this line
1140 b'bisect.state',
1140 b'bisect.state',
1141 }
1141 }
1142
1142
1143 def __init__(
1143 def __init__(
1144 self,
1144 self,
1145 baseui,
1145 baseui,
1146 ui,
1146 ui,
1147 origroot,
1147 origroot,
1148 wdirvfs,
1148 wdirvfs,
1149 hgvfs,
1149 hgvfs,
1150 requirements,
1150 requirements,
1151 supportedrequirements,
1151 supportedrequirements,
1152 sharedpath,
1152 sharedpath,
1153 store,
1153 store,
1154 cachevfs,
1154 cachevfs,
1155 wcachevfs,
1155 wcachevfs,
1156 features,
1156 features,
1157 intents=None,
1157 intents=None,
1158 ):
1158 ):
1159 """Create a new local repository instance.
1159 """Create a new local repository instance.
1160
1160
1161 Most callers should use ``hg.repository()``, ``localrepo.instance()``,
1161 Most callers should use ``hg.repository()``, ``localrepo.instance()``,
1162 or ``localrepo.makelocalrepository()`` for obtaining a new repository
1162 or ``localrepo.makelocalrepository()`` for obtaining a new repository
1163 object.
1163 object.
1164
1164
1165 Arguments:
1165 Arguments:
1166
1166
1167 baseui
1167 baseui
1168 ``ui.ui`` instance that ``ui`` argument was based off of.
1168 ``ui.ui`` instance that ``ui`` argument was based off of.
1169
1169
1170 ui
1170 ui
1171 ``ui.ui`` instance for use by the repository.
1171 ``ui.ui`` instance for use by the repository.
1172
1172
1173 origroot
1173 origroot
1174 ``bytes`` path to working directory root of this repository.
1174 ``bytes`` path to working directory root of this repository.
1175
1175
1176 wdirvfs
1176 wdirvfs
1177 ``vfs.vfs`` rooted at the working directory.
1177 ``vfs.vfs`` rooted at the working directory.
1178
1178
1179 hgvfs
1179 hgvfs
1180 ``vfs.vfs`` rooted at .hg/
1180 ``vfs.vfs`` rooted at .hg/
1181
1181
1182 requirements
1182 requirements
1183 ``set`` of bytestrings representing repository opening requirements.
1183 ``set`` of bytestrings representing repository opening requirements.
1184
1184
1185 supportedrequirements
1185 supportedrequirements
1186 ``set`` of bytestrings representing repository requirements that we
1186 ``set`` of bytestrings representing repository requirements that we
1187 know how to open. May be a supetset of ``requirements``.
1187 know how to open. May be a supetset of ``requirements``.
1188
1188
1189 sharedpath
1189 sharedpath
1190 ``bytes`` Defining path to storage base directory. Points to a
1190 ``bytes`` Defining path to storage base directory. Points to a
1191 ``.hg/`` directory somewhere.
1191 ``.hg/`` directory somewhere.
1192
1192
1193 store
1193 store
1194 ``store.basicstore`` (or derived) instance providing access to
1194 ``store.basicstore`` (or derived) instance providing access to
1195 versioned storage.
1195 versioned storage.
1196
1196
1197 cachevfs
1197 cachevfs
1198 ``vfs.vfs`` used for cache files.
1198 ``vfs.vfs`` used for cache files.
1199
1199
1200 wcachevfs
1200 wcachevfs
1201 ``vfs.vfs`` used for cache files related to the working copy.
1201 ``vfs.vfs`` used for cache files related to the working copy.
1202
1202
1203 features
1203 features
1204 ``set`` of bytestrings defining features/capabilities of this
1204 ``set`` of bytestrings defining features/capabilities of this
1205 instance.
1205 instance.
1206
1206
1207 intents
1207 intents
1208 ``set`` of system strings indicating what this repo will be used
1208 ``set`` of system strings indicating what this repo will be used
1209 for.
1209 for.
1210 """
1210 """
1211 self.baseui = baseui
1211 self.baseui = baseui
1212 self.ui = ui
1212 self.ui = ui
1213 self.origroot = origroot
1213 self.origroot = origroot
1214 # vfs rooted at working directory.
1214 # vfs rooted at working directory.
1215 self.wvfs = wdirvfs
1215 self.wvfs = wdirvfs
1216 self.root = wdirvfs.base
1216 self.root = wdirvfs.base
1217 # vfs rooted at .hg/. Used to access most non-store paths.
1217 # vfs rooted at .hg/. Used to access most non-store paths.
1218 self.vfs = hgvfs
1218 self.vfs = hgvfs
1219 self.path = hgvfs.base
1219 self.path = hgvfs.base
1220 self.requirements = requirements
1220 self.requirements = requirements
1221 self.supported = supportedrequirements
1221 self.supported = supportedrequirements
1222 self.sharedpath = sharedpath
1222 self.sharedpath = sharedpath
1223 self.store = store
1223 self.store = store
1224 self.cachevfs = cachevfs
1224 self.cachevfs = cachevfs
1225 self.wcachevfs = wcachevfs
1225 self.wcachevfs = wcachevfs
1226 self.features = features
1226 self.features = features
1227
1227
1228 self.filtername = None
1228 self.filtername = None
1229
1229
1230 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1230 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1231 b'devel', b'check-locks'
1231 b'devel', b'check-locks'
1232 ):
1232 ):
1233 self.vfs.audit = self._getvfsward(self.vfs.audit)
1233 self.vfs.audit = self._getvfsward(self.vfs.audit)
1234 # A list of callback to shape the phase if no data were found.
1234 # A list of callback to shape the phase if no data were found.
1235 # Callback are in the form: func(repo, roots) --> processed root.
1235 # Callback are in the form: func(repo, roots) --> processed root.
1236 # This list it to be filled by extension during repo setup
1236 # This list it to be filled by extension during repo setup
1237 self._phasedefaults = []
1237 self._phasedefaults = []
1238
1238
1239 color.setup(self.ui)
1239 color.setup(self.ui)
1240
1240
1241 self.spath = self.store.path
1241 self.spath = self.store.path
1242 self.svfs = self.store.vfs
1242 self.svfs = self.store.vfs
1243 self.sjoin = self.store.join
1243 self.sjoin = self.store.join
1244 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1244 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
1245 b'devel', b'check-locks'
1245 b'devel', b'check-locks'
1246 ):
1246 ):
1247 if util.safehasattr(self.svfs, b'vfs'): # this is filtervfs
1247 if util.safehasattr(self.svfs, b'vfs'): # this is filtervfs
1248 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
1248 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
1249 else: # standard vfs
1249 else: # standard vfs
1250 self.svfs.audit = self._getsvfsward(self.svfs.audit)
1250 self.svfs.audit = self._getsvfsward(self.svfs.audit)
1251
1251
1252 self._dirstatevalidatewarned = False
1252 self._dirstatevalidatewarned = False
1253
1253
1254 self._branchcaches = branchmap.BranchMapCache()
1254 self._branchcaches = branchmap.BranchMapCache()
1255 self._revbranchcache = None
1255 self._revbranchcache = None
1256 self._filterpats = {}
1256 self._filterpats = {}
1257 self._datafilters = {}
1257 self._datafilters = {}
1258 self._transref = self._lockref = self._wlockref = None
1258 self._transref = self._lockref = self._wlockref = None
1259
1259
1260 # A cache for various files under .hg/ that tracks file changes,
1260 # A cache for various files under .hg/ that tracks file changes,
1261 # (used by the filecache decorator)
1261 # (used by the filecache decorator)
1262 #
1262 #
1263 # Maps a property name to its util.filecacheentry
1263 # Maps a property name to its util.filecacheentry
1264 self._filecache = {}
1264 self._filecache = {}
1265
1265
1266 # hold sets of revision to be filtered
1266 # hold sets of revision to be filtered
1267 # should be cleared when something might have changed the filter value:
1267 # should be cleared when something might have changed the filter value:
1268 # - new changesets,
1268 # - new changesets,
1269 # - phase change,
1269 # - phase change,
1270 # - new obsolescence marker,
1270 # - new obsolescence marker,
1271 # - working directory parent change,
1271 # - working directory parent change,
1272 # - bookmark changes
1272 # - bookmark changes
1273 self.filteredrevcache = {}
1273 self.filteredrevcache = {}
1274
1274
1275 # post-dirstate-status hooks
1275 # post-dirstate-status hooks
1276 self._postdsstatus = []
1276 self._postdsstatus = []
1277
1277
1278 # generic mapping between names and nodes
1278 # generic mapping between names and nodes
1279 self.names = namespaces.namespaces()
1279 self.names = namespaces.namespaces()
1280
1280
1281 # Key to signature value.
1281 # Key to signature value.
1282 self._sparsesignaturecache = {}
1282 self._sparsesignaturecache = {}
1283 # Signature to cached matcher instance.
1283 # Signature to cached matcher instance.
1284 self._sparsematchercache = {}
1284 self._sparsematchercache = {}
1285
1285
1286 self._extrafilterid = repoview.extrafilter(ui)
1286 self._extrafilterid = repoview.extrafilter(ui)
1287
1287
1288 self.filecopiesmode = None
1288 self.filecopiesmode = None
1289 if requirementsmod.COPIESSDC_REQUIREMENT in self.requirements:
1289 if requirementsmod.COPIESSDC_REQUIREMENT in self.requirements:
1290 self.filecopiesmode = b'changeset-sidedata'
1290 self.filecopiesmode = b'changeset-sidedata'
1291
1291
1292 def _getvfsward(self, origfunc):
1292 def _getvfsward(self, origfunc):
1293 """build a ward for self.vfs"""
1293 """build a ward for self.vfs"""
1294 rref = weakref.ref(self)
1294 rref = weakref.ref(self)
1295
1295
1296 def checkvfs(path, mode=None):
1296 def checkvfs(path, mode=None):
1297 ret = origfunc(path, mode=mode)
1297 ret = origfunc(path, mode=mode)
1298 repo = rref()
1298 repo = rref()
1299 if (
1299 if (
1300 repo is None
1300 repo is None
1301 or not util.safehasattr(repo, b'_wlockref')
1301 or not util.safehasattr(repo, b'_wlockref')
1302 or not util.safehasattr(repo, b'_lockref')
1302 or not util.safehasattr(repo, b'_lockref')
1303 ):
1303 ):
1304 return
1304 return
1305 if mode in (None, b'r', b'rb'):
1305 if mode in (None, b'r', b'rb'):
1306 return
1306 return
1307 if path.startswith(repo.path):
1307 if path.startswith(repo.path):
1308 # truncate name relative to the repository (.hg)
1308 # truncate name relative to the repository (.hg)
1309 path = path[len(repo.path) + 1 :]
1309 path = path[len(repo.path) + 1 :]
1310 if path.startswith(b'cache/'):
1310 if path.startswith(b'cache/'):
1311 msg = b'accessing cache with vfs instead of cachevfs: "%s"'
1311 msg = b'accessing cache with vfs instead of cachevfs: "%s"'
1312 repo.ui.develwarn(msg % path, stacklevel=3, config=b"cache-vfs")
1312 repo.ui.develwarn(msg % path, stacklevel=3, config=b"cache-vfs")
1313 # path prefixes covered by 'lock'
1313 # path prefixes covered by 'lock'
1314 vfs_path_prefixes = (
1314 vfs_path_prefixes = (
1315 b'journal.',
1315 b'journal.',
1316 b'undo.',
1316 b'undo.',
1317 b'strip-backup/',
1317 b'strip-backup/',
1318 b'cache/',
1318 b'cache/',
1319 )
1319 )
1320 if any(path.startswith(prefix) for prefix in vfs_path_prefixes):
1320 if any(path.startswith(prefix) for prefix in vfs_path_prefixes):
1321 if repo._currentlock(repo._lockref) is None:
1321 if repo._currentlock(repo._lockref) is None:
1322 repo.ui.develwarn(
1322 repo.ui.develwarn(
1323 b'write with no lock: "%s"' % path,
1323 b'write with no lock: "%s"' % path,
1324 stacklevel=3,
1324 stacklevel=3,
1325 config=b'check-locks',
1325 config=b'check-locks',
1326 )
1326 )
1327 elif repo._currentlock(repo._wlockref) is None:
1327 elif repo._currentlock(repo._wlockref) is None:
1328 # rest of vfs files are covered by 'wlock'
1328 # rest of vfs files are covered by 'wlock'
1329 #
1329 #
1330 # exclude special files
1330 # exclude special files
1331 for prefix in self._wlockfreeprefix:
1331 for prefix in self._wlockfreeprefix:
1332 if path.startswith(prefix):
1332 if path.startswith(prefix):
1333 return
1333 return
1334 repo.ui.develwarn(
1334 repo.ui.develwarn(
1335 b'write with no wlock: "%s"' % path,
1335 b'write with no wlock: "%s"' % path,
1336 stacklevel=3,
1336 stacklevel=3,
1337 config=b'check-locks',
1337 config=b'check-locks',
1338 )
1338 )
1339 return ret
1339 return ret
1340
1340
1341 return checkvfs
1341 return checkvfs
1342
1342
1343 def _getsvfsward(self, origfunc):
1343 def _getsvfsward(self, origfunc):
1344 """build a ward for self.svfs"""
1344 """build a ward for self.svfs"""
1345 rref = weakref.ref(self)
1345 rref = weakref.ref(self)
1346
1346
1347 def checksvfs(path, mode=None):
1347 def checksvfs(path, mode=None):
1348 ret = origfunc(path, mode=mode)
1348 ret = origfunc(path, mode=mode)
1349 repo = rref()
1349 repo = rref()
1350 if repo is None or not util.safehasattr(repo, b'_lockref'):
1350 if repo is None or not util.safehasattr(repo, b'_lockref'):
1351 return
1351 return
1352 if mode in (None, b'r', b'rb'):
1352 if mode in (None, b'r', b'rb'):
1353 return
1353 return
1354 if path.startswith(repo.sharedpath):
1354 if path.startswith(repo.sharedpath):
1355 # truncate name relative to the repository (.hg)
1355 # truncate name relative to the repository (.hg)
1356 path = path[len(repo.sharedpath) + 1 :]
1356 path = path[len(repo.sharedpath) + 1 :]
1357 if repo._currentlock(repo._lockref) is None:
1357 if repo._currentlock(repo._lockref) is None:
1358 repo.ui.develwarn(
1358 repo.ui.develwarn(
1359 b'write with no lock: "%s"' % path, stacklevel=4
1359 b'write with no lock: "%s"' % path, stacklevel=4
1360 )
1360 )
1361 return ret
1361 return ret
1362
1362
1363 return checksvfs
1363 return checksvfs
1364
1364
1365 def close(self):
1365 def close(self):
1366 self._writecaches()
1366 self._writecaches()
1367
1367
1368 def _writecaches(self):
1368 def _writecaches(self):
1369 if self._revbranchcache:
1369 if self._revbranchcache:
1370 self._revbranchcache.write()
1370 self._revbranchcache.write()
1371
1371
1372 def _restrictcapabilities(self, caps):
1372 def _restrictcapabilities(self, caps):
1373 if self.ui.configbool(b'experimental', b'bundle2-advertise'):
1373 if self.ui.configbool(b'experimental', b'bundle2-advertise'):
1374 caps = set(caps)
1374 caps = set(caps)
1375 capsblob = bundle2.encodecaps(
1375 capsblob = bundle2.encodecaps(
1376 bundle2.getrepocaps(self, role=b'client')
1376 bundle2.getrepocaps(self, role=b'client')
1377 )
1377 )
1378 caps.add(b'bundle2=' + urlreq.quote(capsblob))
1378 caps.add(b'bundle2=' + urlreq.quote(capsblob))
1379 return caps
1379 return caps
1380
1380
1381 # Don't cache auditor/nofsauditor, or you'll end up with reference cycle:
1381 # Don't cache auditor/nofsauditor, or you'll end up with reference cycle:
1382 # self -> auditor -> self._checknested -> self
1382 # self -> auditor -> self._checknested -> self
1383
1383
1384 @property
1384 @property
1385 def auditor(self):
1385 def auditor(self):
1386 # This is only used by context.workingctx.match in order to
1386 # This is only used by context.workingctx.match in order to
1387 # detect files in subrepos.
1387 # detect files in subrepos.
1388 return pathutil.pathauditor(self.root, callback=self._checknested)
1388 return pathutil.pathauditor(self.root, callback=self._checknested)
1389
1389
1390 @property
1390 @property
1391 def nofsauditor(self):
1391 def nofsauditor(self):
1392 # This is only used by context.basectx.match in order to detect
1392 # This is only used by context.basectx.match in order to detect
1393 # files in subrepos.
1393 # files in subrepos.
1394 return pathutil.pathauditor(
1394 return pathutil.pathauditor(
1395 self.root, callback=self._checknested, realfs=False, cached=True
1395 self.root, callback=self._checknested, realfs=False, cached=True
1396 )
1396 )
1397
1397
1398 def _checknested(self, path):
1398 def _checknested(self, path):
1399 """Determine if path is a legal nested repository."""
1399 """Determine if path is a legal nested repository."""
1400 if not path.startswith(self.root):
1400 if not path.startswith(self.root):
1401 return False
1401 return False
1402 subpath = path[len(self.root) + 1 :]
1402 subpath = path[len(self.root) + 1 :]
1403 normsubpath = util.pconvert(subpath)
1403 normsubpath = util.pconvert(subpath)
1404
1404
1405 # XXX: Checking against the current working copy is wrong in
1405 # XXX: Checking against the current working copy is wrong in
1406 # the sense that it can reject things like
1406 # the sense that it can reject things like
1407 #
1407 #
1408 # $ hg cat -r 10 sub/x.txt
1408 # $ hg cat -r 10 sub/x.txt
1409 #
1409 #
1410 # if sub/ is no longer a subrepository in the working copy
1410 # if sub/ is no longer a subrepository in the working copy
1411 # parent revision.
1411 # parent revision.
1412 #
1412 #
1413 # However, it can of course also allow things that would have
1413 # However, it can of course also allow things that would have
1414 # been rejected before, such as the above cat command if sub/
1414 # been rejected before, such as the above cat command if sub/
1415 # is a subrepository now, but was a normal directory before.
1415 # is a subrepository now, but was a normal directory before.
1416 # The old path auditor would have rejected by mistake since it
1416 # The old path auditor would have rejected by mistake since it
1417 # panics when it sees sub/.hg/.
1417 # panics when it sees sub/.hg/.
1418 #
1418 #
1419 # All in all, checking against the working copy seems sensible
1419 # All in all, checking against the working copy seems sensible
1420 # since we want to prevent access to nested repositories on
1420 # since we want to prevent access to nested repositories on
1421 # the filesystem *now*.
1421 # the filesystem *now*.
1422 ctx = self[None]
1422 ctx = self[None]
1423 parts = util.splitpath(subpath)
1423 parts = util.splitpath(subpath)
1424 while parts:
1424 while parts:
1425 prefix = b'/'.join(parts)
1425 prefix = b'/'.join(parts)
1426 if prefix in ctx.substate:
1426 if prefix in ctx.substate:
1427 if prefix == normsubpath:
1427 if prefix == normsubpath:
1428 return True
1428 return True
1429 else:
1429 else:
1430 sub = ctx.sub(prefix)
1430 sub = ctx.sub(prefix)
1431 return sub.checknested(subpath[len(prefix) + 1 :])
1431 return sub.checknested(subpath[len(prefix) + 1 :])
1432 else:
1432 else:
1433 parts.pop()
1433 parts.pop()
1434 return False
1434 return False
1435
1435
1436 def peer(self):
1436 def peer(self):
1437 return localpeer(self) # not cached to avoid reference cycle
1437 return localpeer(self) # not cached to avoid reference cycle
1438
1438
1439 def unfiltered(self):
1439 def unfiltered(self):
1440 """Return unfiltered version of the repository
1440 """Return unfiltered version of the repository
1441
1441
1442 Intended to be overwritten by filtered repo."""
1442 Intended to be overwritten by filtered repo."""
1443 return self
1443 return self
1444
1444
1445 def filtered(self, name, visibilityexceptions=None):
1445 def filtered(self, name, visibilityexceptions=None):
1446 """Return a filtered version of a repository
1446 """Return a filtered version of a repository
1447
1447
1448 The `name` parameter is the identifier of the requested view. This
1448 The `name` parameter is the identifier of the requested view. This
1449 will return a repoview object set "exactly" to the specified view.
1449 will return a repoview object set "exactly" to the specified view.
1450
1450
1451 This function does not apply recursive filtering to a repository. For
1451 This function does not apply recursive filtering to a repository. For
1452 example calling `repo.filtered("served")` will return a repoview using
1452 example calling `repo.filtered("served")` will return a repoview using
1453 the "served" view, regardless of the initial view used by `repo`.
1453 the "served" view, regardless of the initial view used by `repo`.
1454
1454
1455 In other word, there is always only one level of `repoview` "filtering".
1455 In other word, there is always only one level of `repoview` "filtering".
1456 """
1456 """
1457 if self._extrafilterid is not None and b'%' not in name:
1457 if self._extrafilterid is not None and b'%' not in name:
1458 name = name + b'%' + self._extrafilterid
1458 name = name + b'%' + self._extrafilterid
1459
1459
1460 cls = repoview.newtype(self.unfiltered().__class__)
1460 cls = repoview.newtype(self.unfiltered().__class__)
1461 return cls(self, name, visibilityexceptions)
1461 return cls(self, name, visibilityexceptions)
1462
1462
1463 @mixedrepostorecache(
1463 @mixedrepostorecache(
1464 (b'bookmarks', b'plain'),
1464 (b'bookmarks', b'plain'),
1465 (b'bookmarks.current', b'plain'),
1465 (b'bookmarks.current', b'plain'),
1466 (b'bookmarks', b''),
1466 (b'bookmarks', b''),
1467 (b'00changelog.i', b''),
1467 (b'00changelog.i', b''),
1468 )
1468 )
1469 def _bookmarks(self):
1469 def _bookmarks(self):
1470 # Since the multiple files involved in the transaction cannot be
1470 # Since the multiple files involved in the transaction cannot be
1471 # written atomically (with current repository format), there is a race
1471 # written atomically (with current repository format), there is a race
1472 # condition here.
1472 # condition here.
1473 #
1473 #
1474 # 1) changelog content A is read
1474 # 1) changelog content A is read
1475 # 2) outside transaction update changelog to content B
1475 # 2) outside transaction update changelog to content B
1476 # 3) outside transaction update bookmark file referring to content B
1476 # 3) outside transaction update bookmark file referring to content B
1477 # 4) bookmarks file content is read and filtered against changelog-A
1477 # 4) bookmarks file content is read and filtered against changelog-A
1478 #
1478 #
1479 # When this happens, bookmarks against nodes missing from A are dropped.
1479 # When this happens, bookmarks against nodes missing from A are dropped.
1480 #
1480 #
1481 # Having this happening during read is not great, but it become worse
1481 # Having this happening during read is not great, but it become worse
1482 # when this happen during write because the bookmarks to the "unknown"
1482 # when this happen during write because the bookmarks to the "unknown"
1483 # nodes will be dropped for good. However, writes happen within locks.
1483 # nodes will be dropped for good. However, writes happen within locks.
1484 # This locking makes it possible to have a race free consistent read.
1484 # This locking makes it possible to have a race free consistent read.
1485 # For this purpose data read from disc before locking are
1485 # For this purpose data read from disc before locking are
1486 # "invalidated" right after the locks are taken. This invalidations are
1486 # "invalidated" right after the locks are taken. This invalidations are
1487 # "light", the `filecache` mechanism keep the data in memory and will
1487 # "light", the `filecache` mechanism keep the data in memory and will
1488 # reuse them if the underlying files did not changed. Not parsing the
1488 # reuse them if the underlying files did not changed. Not parsing the
1489 # same data multiple times helps performances.
1489 # same data multiple times helps performances.
1490 #
1490 #
1491 # Unfortunately in the case describe above, the files tracked by the
1491 # Unfortunately in the case describe above, the files tracked by the
1492 # bookmarks file cache might not have changed, but the in-memory
1492 # bookmarks file cache might not have changed, but the in-memory
1493 # content is still "wrong" because we used an older changelog content
1493 # content is still "wrong" because we used an older changelog content
1494 # to process the on-disk data. So after locking, the changelog would be
1494 # to process the on-disk data. So after locking, the changelog would be
1495 # refreshed but `_bookmarks` would be preserved.
1495 # refreshed but `_bookmarks` would be preserved.
1496 # Adding `00changelog.i` to the list of tracked file is not
1496 # Adding `00changelog.i` to the list of tracked file is not
1497 # enough, because at the time we build the content for `_bookmarks` in
1497 # enough, because at the time we build the content for `_bookmarks` in
1498 # (4), the changelog file has already diverged from the content used
1498 # (4), the changelog file has already diverged from the content used
1499 # for loading `changelog` in (1)
1499 # for loading `changelog` in (1)
1500 #
1500 #
1501 # To prevent the issue, we force the changelog to be explicitly
1501 # To prevent the issue, we force the changelog to be explicitly
1502 # reloaded while computing `_bookmarks`. The data race can still happen
1502 # reloaded while computing `_bookmarks`. The data race can still happen
1503 # without the lock (with a narrower window), but it would no longer go
1503 # without the lock (with a narrower window), but it would no longer go
1504 # undetected during the lock time refresh.
1504 # undetected during the lock time refresh.
1505 #
1505 #
1506 # The new schedule is as follow
1506 # The new schedule is as follow
1507 #
1507 #
1508 # 1) filecache logic detect that `_bookmarks` needs to be computed
1508 # 1) filecache logic detect that `_bookmarks` needs to be computed
1509 # 2) cachestat for `bookmarks` and `changelog` are captured (for book)
1509 # 2) cachestat for `bookmarks` and `changelog` are captured (for book)
1510 # 3) We force `changelog` filecache to be tested
1510 # 3) We force `changelog` filecache to be tested
1511 # 4) cachestat for `changelog` are captured (for changelog)
1511 # 4) cachestat for `changelog` are captured (for changelog)
1512 # 5) `_bookmarks` is computed and cached
1512 # 5) `_bookmarks` is computed and cached
1513 #
1513 #
1514 # The step in (3) ensure we have a changelog at least as recent as the
1514 # The step in (3) ensure we have a changelog at least as recent as the
1515 # cache stat computed in (1). As a result at locking time:
1515 # cache stat computed in (1). As a result at locking time:
1516 # * if the changelog did not changed since (1) -> we can reuse the data
1516 # * if the changelog did not changed since (1) -> we can reuse the data
1517 # * otherwise -> the bookmarks get refreshed.
1517 # * otherwise -> the bookmarks get refreshed.
1518 self._refreshchangelog()
1518 self._refreshchangelog()
1519 return bookmarks.bmstore(self)
1519 return bookmarks.bmstore(self)
1520
1520
1521 def _refreshchangelog(self):
1521 def _refreshchangelog(self):
1522 """make sure the in memory changelog match the on-disk one"""
1522 """make sure the in memory changelog match the on-disk one"""
1523 if 'changelog' in vars(self) and self.currenttransaction() is None:
1523 if 'changelog' in vars(self) and self.currenttransaction() is None:
1524 del self.changelog
1524 del self.changelog
1525
1525
1526 @property
1526 @property
1527 def _activebookmark(self):
1527 def _activebookmark(self):
1528 return self._bookmarks.active
1528 return self._bookmarks.active
1529
1529
1530 # _phasesets depend on changelog. what we need is to call
1530 # _phasesets depend on changelog. what we need is to call
1531 # _phasecache.invalidate() if '00changelog.i' was changed, but it
1531 # _phasecache.invalidate() if '00changelog.i' was changed, but it
1532 # can't be easily expressed in filecache mechanism.
1532 # can't be easily expressed in filecache mechanism.
1533 @storecache(b'phaseroots', b'00changelog.i')
1533 @storecache(b'phaseroots', b'00changelog.i')
1534 def _phasecache(self):
1534 def _phasecache(self):
1535 return phases.phasecache(self, self._phasedefaults)
1535 return phases.phasecache(self, self._phasedefaults)
1536
1536
1537 @storecache(b'obsstore')
1537 @storecache(b'obsstore')
1538 def obsstore(self):
1538 def obsstore(self):
1539 return obsolete.makestore(self.ui, self)
1539 return obsolete.makestore(self.ui, self)
1540
1540
1541 @storecache(b'00changelog.i')
1541 @storecache(b'00changelog.i')
1542 def changelog(self):
1542 def changelog(self):
1543 # load dirstate before changelog to avoid race see issue6303
1543 # load dirstate before changelog to avoid race see issue6303
1544 self.dirstate.prefetch_parents()
1544 self.dirstate.prefetch_parents()
1545 return self.store.changelog(txnutil.mayhavepending(self.root))
1545 return self.store.changelog(txnutil.mayhavepending(self.root))
1546
1546
1547 @storecache(b'00manifest.i')
1547 @storecache(b'00manifest.i')
1548 def manifestlog(self):
1548 def manifestlog(self):
1549 return self.store.manifestlog(self, self._storenarrowmatch)
1549 return self.store.manifestlog(self, self._storenarrowmatch)
1550
1550
1551 @repofilecache(b'dirstate')
1551 @repofilecache(b'dirstate')
1552 def dirstate(self):
1552 def dirstate(self):
1553 return self._makedirstate()
1553 return self._makedirstate()
1554
1554
1555 def _makedirstate(self):
1555 def _makedirstate(self):
1556 """Extension point for wrapping the dirstate per-repo."""
1556 """Extension point for wrapping the dirstate per-repo."""
1557 sparsematchfn = lambda: sparse.matcher(self)
1557 sparsematchfn = lambda: sparse.matcher(self)
1558
1558
1559 return dirstate.dirstate(
1559 return dirstate.dirstate(
1560 self.vfs, self.ui, self.root, self._dirstatevalidate, sparsematchfn
1560 self.vfs, self.ui, self.root, self._dirstatevalidate, sparsematchfn
1561 )
1561 )
1562
1562
1563 def _dirstatevalidate(self, node):
1563 def _dirstatevalidate(self, node):
1564 try:
1564 try:
1565 self.changelog.rev(node)
1565 self.changelog.rev(node)
1566 return node
1566 return node
1567 except error.LookupError:
1567 except error.LookupError:
1568 if not self._dirstatevalidatewarned:
1568 if not self._dirstatevalidatewarned:
1569 self._dirstatevalidatewarned = True
1569 self._dirstatevalidatewarned = True
1570 self.ui.warn(
1570 self.ui.warn(
1571 _(b"warning: ignoring unknown working parent %s!\n")
1571 _(b"warning: ignoring unknown working parent %s!\n")
1572 % short(node)
1572 % short(node)
1573 )
1573 )
1574 return nullid
1574 return nullid
1575
1575
1576 @storecache(narrowspec.FILENAME)
1576 @storecache(narrowspec.FILENAME)
1577 def narrowpats(self):
1577 def narrowpats(self):
1578 """matcher patterns for this repository's narrowspec
1578 """matcher patterns for this repository's narrowspec
1579
1579
1580 A tuple of (includes, excludes).
1580 A tuple of (includes, excludes).
1581 """
1581 """
1582 return narrowspec.load(self)
1582 return narrowspec.load(self)
1583
1583
1584 @storecache(narrowspec.FILENAME)
1584 @storecache(narrowspec.FILENAME)
1585 def _storenarrowmatch(self):
1585 def _storenarrowmatch(self):
1586 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1586 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1587 return matchmod.always()
1587 return matchmod.always()
1588 include, exclude = self.narrowpats
1588 include, exclude = self.narrowpats
1589 return narrowspec.match(self.root, include=include, exclude=exclude)
1589 return narrowspec.match(self.root, include=include, exclude=exclude)
1590
1590
1591 @storecache(narrowspec.FILENAME)
1591 @storecache(narrowspec.FILENAME)
1592 def _narrowmatch(self):
1592 def _narrowmatch(self):
1593 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1593 if requirementsmod.NARROW_REQUIREMENT not in self.requirements:
1594 return matchmod.always()
1594 return matchmod.always()
1595 narrowspec.checkworkingcopynarrowspec(self)
1595 narrowspec.checkworkingcopynarrowspec(self)
1596 include, exclude = self.narrowpats
1596 include, exclude = self.narrowpats
1597 return narrowspec.match(self.root, include=include, exclude=exclude)
1597 return narrowspec.match(self.root, include=include, exclude=exclude)
1598
1598
1599 def narrowmatch(self, match=None, includeexact=False):
1599 def narrowmatch(self, match=None, includeexact=False):
1600 """matcher corresponding the the repo's narrowspec
1600 """matcher corresponding the the repo's narrowspec
1601
1601
1602 If `match` is given, then that will be intersected with the narrow
1602 If `match` is given, then that will be intersected with the narrow
1603 matcher.
1603 matcher.
1604
1604
1605 If `includeexact` is True, then any exact matches from `match` will
1605 If `includeexact` is True, then any exact matches from `match` will
1606 be included even if they're outside the narrowspec.
1606 be included even if they're outside the narrowspec.
1607 """
1607 """
1608 if match:
1608 if match:
1609 if includeexact and not self._narrowmatch.always():
1609 if includeexact and not self._narrowmatch.always():
1610 # do not exclude explicitly-specified paths so that they can
1610 # do not exclude explicitly-specified paths so that they can
1611 # be warned later on
1611 # be warned later on
1612 em = matchmod.exact(match.files())
1612 em = matchmod.exact(match.files())
1613 nm = matchmod.unionmatcher([self._narrowmatch, em])
1613 nm = matchmod.unionmatcher([self._narrowmatch, em])
1614 return matchmod.intersectmatchers(match, nm)
1614 return matchmod.intersectmatchers(match, nm)
1615 return matchmod.intersectmatchers(match, self._narrowmatch)
1615 return matchmod.intersectmatchers(match, self._narrowmatch)
1616 return self._narrowmatch
1616 return self._narrowmatch
1617
1617
1618 def setnarrowpats(self, newincludes, newexcludes):
1618 def setnarrowpats(self, newincludes, newexcludes):
1619 narrowspec.save(self, newincludes, newexcludes)
1619 narrowspec.save(self, newincludes, newexcludes)
1620 self.invalidate(clearfilecache=True)
1620 self.invalidate(clearfilecache=True)
1621
1621
1622 @unfilteredpropertycache
1622 @unfilteredpropertycache
1623 def _quick_access_changeid_null(self):
1623 def _quick_access_changeid_null(self):
1624 return {
1624 return {
1625 b'null': (nullrev, nullid),
1625 b'null': (nullrev, nullid),
1626 nullrev: (nullrev, nullid),
1626 nullrev: (nullrev, nullid),
1627 nullid: (nullrev, nullid),
1627 nullid: (nullrev, nullid),
1628 }
1628 }
1629
1629
1630 @unfilteredpropertycache
1630 @unfilteredpropertycache
1631 def _quick_access_changeid_wc(self):
1631 def _quick_access_changeid_wc(self):
1632 # also fast path access to the working copy parents
1632 # also fast path access to the working copy parents
1633 # however, only do it for filter that ensure wc is visible.
1633 # however, only do it for filter that ensure wc is visible.
1634 quick = self._quick_access_changeid_null.copy()
1634 quick = self._quick_access_changeid_null.copy()
1635 cl = self.unfiltered().changelog
1635 cl = self.unfiltered().changelog
1636 for node in self.dirstate.parents():
1636 for node in self.dirstate.parents():
1637 if node == nullid:
1637 if node == nullid:
1638 continue
1638 continue
1639 rev = cl.index.get_rev(node)
1639 rev = cl.index.get_rev(node)
1640 if rev is None:
1640 if rev is None:
1641 # unknown working copy parent case:
1641 # unknown working copy parent case:
1642 #
1642 #
1643 # skip the fast path and let higher code deal with it
1643 # skip the fast path and let higher code deal with it
1644 continue
1644 continue
1645 pair = (rev, node)
1645 pair = (rev, node)
1646 quick[rev] = pair
1646 quick[rev] = pair
1647 quick[node] = pair
1647 quick[node] = pair
1648 # also add the parents of the parents
1648 # also add the parents of the parents
1649 for r in cl.parentrevs(rev):
1649 for r in cl.parentrevs(rev):
1650 if r == nullrev:
1650 if r == nullrev:
1651 continue
1651 continue
1652 n = cl.node(r)
1652 n = cl.node(r)
1653 pair = (r, n)
1653 pair = (r, n)
1654 quick[r] = pair
1654 quick[r] = pair
1655 quick[n] = pair
1655 quick[n] = pair
1656 p1node = self.dirstate.p1()
1656 p1node = self.dirstate.p1()
1657 if p1node != nullid:
1657 if p1node != nullid:
1658 quick[b'.'] = quick[p1node]
1658 quick[b'.'] = quick[p1node]
1659 return quick
1659 return quick
1660
1660
1661 @unfilteredmethod
1661 @unfilteredmethod
1662 def _quick_access_changeid_invalidate(self):
1662 def _quick_access_changeid_invalidate(self):
1663 if '_quick_access_changeid_wc' in vars(self):
1663 if '_quick_access_changeid_wc' in vars(self):
1664 del self.__dict__['_quick_access_changeid_wc']
1664 del self.__dict__['_quick_access_changeid_wc']
1665
1665
1666 @property
1666 @property
1667 def _quick_access_changeid(self):
1667 def _quick_access_changeid(self):
1668 """an helper dictionnary for __getitem__ calls
1668 """an helper dictionnary for __getitem__ calls
1669
1669
1670 This contains a list of symbol we can recognise right away without
1670 This contains a list of symbol we can recognise right away without
1671 further processing.
1671 further processing.
1672 """
1672 """
1673 if self.filtername in repoview.filter_has_wc:
1673 if self.filtername in repoview.filter_has_wc:
1674 return self._quick_access_changeid_wc
1674 return self._quick_access_changeid_wc
1675 return self._quick_access_changeid_null
1675 return self._quick_access_changeid_null
1676
1676
1677 def __getitem__(self, changeid):
1677 def __getitem__(self, changeid):
1678 # dealing with special cases
1678 # dealing with special cases
1679 if changeid is None:
1679 if changeid is None:
1680 return context.workingctx(self)
1680 return context.workingctx(self)
1681 if isinstance(changeid, context.basectx):
1681 if isinstance(changeid, context.basectx):
1682 return changeid
1682 return changeid
1683
1683
1684 # dealing with multiple revisions
1684 # dealing with multiple revisions
1685 if isinstance(changeid, slice):
1685 if isinstance(changeid, slice):
1686 # wdirrev isn't contiguous so the slice shouldn't include it
1686 # wdirrev isn't contiguous so the slice shouldn't include it
1687 return [
1687 return [
1688 self[i]
1688 self[i]
1689 for i in pycompat.xrange(*changeid.indices(len(self)))
1689 for i in pycompat.xrange(*changeid.indices(len(self)))
1690 if i not in self.changelog.filteredrevs
1690 if i not in self.changelog.filteredrevs
1691 ]
1691 ]
1692
1692
1693 # dealing with some special values
1693 # dealing with some special values
1694 quick_access = self._quick_access_changeid.get(changeid)
1694 quick_access = self._quick_access_changeid.get(changeid)
1695 if quick_access is not None:
1695 if quick_access is not None:
1696 rev, node = quick_access
1696 rev, node = quick_access
1697 return context.changectx(self, rev, node, maybe_filtered=False)
1697 return context.changectx(self, rev, node, maybe_filtered=False)
1698 if changeid == b'tip':
1698 if changeid == b'tip':
1699 node = self.changelog.tip()
1699 node = self.changelog.tip()
1700 rev = self.changelog.rev(node)
1700 rev = self.changelog.rev(node)
1701 return context.changectx(self, rev, node)
1701 return context.changectx(self, rev, node)
1702
1702
1703 # dealing with arbitrary values
1703 # dealing with arbitrary values
1704 try:
1704 try:
1705 if isinstance(changeid, int):
1705 if isinstance(changeid, int):
1706 node = self.changelog.node(changeid)
1706 node = self.changelog.node(changeid)
1707 rev = changeid
1707 rev = changeid
1708 elif changeid == b'.':
1708 elif changeid == b'.':
1709 # this is a hack to delay/avoid loading obsmarkers
1709 # this is a hack to delay/avoid loading obsmarkers
1710 # when we know that '.' won't be hidden
1710 # when we know that '.' won't be hidden
1711 node = self.dirstate.p1()
1711 node = self.dirstate.p1()
1712 rev = self.unfiltered().changelog.rev(node)
1712 rev = self.unfiltered().changelog.rev(node)
1713 elif len(changeid) == 20:
1713 elif len(changeid) == 20:
1714 try:
1714 try:
1715 node = changeid
1715 node = changeid
1716 rev = self.changelog.rev(changeid)
1716 rev = self.changelog.rev(changeid)
1717 except error.FilteredLookupError:
1717 except error.FilteredLookupError:
1718 changeid = hex(changeid) # for the error message
1718 changeid = hex(changeid) # for the error message
1719 raise
1719 raise
1720 except LookupError:
1720 except LookupError:
1721 # check if it might have come from damaged dirstate
1721 # check if it might have come from damaged dirstate
1722 #
1722 #
1723 # XXX we could avoid the unfiltered if we had a recognizable
1723 # XXX we could avoid the unfiltered if we had a recognizable
1724 # exception for filtered changeset access
1724 # exception for filtered changeset access
1725 if (
1725 if (
1726 self.local()
1726 self.local()
1727 and changeid in self.unfiltered().dirstate.parents()
1727 and changeid in self.unfiltered().dirstate.parents()
1728 ):
1728 ):
1729 msg = _(b"working directory has unknown parent '%s'!")
1729 msg = _(b"working directory has unknown parent '%s'!")
1730 raise error.Abort(msg % short(changeid))
1730 raise error.Abort(msg % short(changeid))
1731 changeid = hex(changeid) # for the error message
1731 changeid = hex(changeid) # for the error message
1732 raise
1732 raise
1733
1733
1734 elif len(changeid) == 40:
1734 elif len(changeid) == 40:
1735 node = bin(changeid)
1735 node = bin(changeid)
1736 rev = self.changelog.rev(node)
1736 rev = self.changelog.rev(node)
1737 else:
1737 else:
1738 raise error.ProgrammingError(
1738 raise error.ProgrammingError(
1739 b"unsupported changeid '%s' of type %s"
1739 b"unsupported changeid '%s' of type %s"
1740 % (changeid, pycompat.bytestr(type(changeid)))
1740 % (changeid, pycompat.bytestr(type(changeid)))
1741 )
1741 )
1742
1742
1743 return context.changectx(self, rev, node)
1743 return context.changectx(self, rev, node)
1744
1744
1745 except (error.FilteredIndexError, error.FilteredLookupError):
1745 except (error.FilteredIndexError, error.FilteredLookupError):
1746 raise error.FilteredRepoLookupError(
1746 raise error.FilteredRepoLookupError(
1747 _(b"filtered revision '%s'") % pycompat.bytestr(changeid)
1747 _(b"filtered revision '%s'") % pycompat.bytestr(changeid)
1748 )
1748 )
1749 except (IndexError, LookupError):
1749 except (IndexError, LookupError):
1750 raise error.RepoLookupError(
1750 raise error.RepoLookupError(
1751 _(b"unknown revision '%s'") % pycompat.bytestr(changeid)
1751 _(b"unknown revision '%s'") % pycompat.bytestr(changeid)
1752 )
1752 )
1753 except error.WdirUnsupported:
1753 except error.WdirUnsupported:
1754 return context.workingctx(self)
1754 return context.workingctx(self)
1755
1755
1756 def __contains__(self, changeid):
1756 def __contains__(self, changeid):
1757 """True if the given changeid exists
1757 """True if the given changeid exists"""
1758
1759 error.AmbiguousPrefixLookupError is raised if an ambiguous node
1760 specified.
1761 """
1762 try:
1758 try:
1763 self[changeid]
1759 self[changeid]
1764 return True
1760 return True
1765 except error.RepoLookupError:
1761 except error.RepoLookupError:
1766 return False
1762 return False
1767
1763
1768 def __nonzero__(self):
1764 def __nonzero__(self):
1769 return True
1765 return True
1770
1766
1771 __bool__ = __nonzero__
1767 __bool__ = __nonzero__
1772
1768
1773 def __len__(self):
1769 def __len__(self):
1774 # no need to pay the cost of repoview.changelog
1770 # no need to pay the cost of repoview.changelog
1775 unfi = self.unfiltered()
1771 unfi = self.unfiltered()
1776 return len(unfi.changelog)
1772 return len(unfi.changelog)
1777
1773
1778 def __iter__(self):
1774 def __iter__(self):
1779 return iter(self.changelog)
1775 return iter(self.changelog)
1780
1776
1781 def revs(self, expr, *args):
1777 def revs(self, expr, *args):
1782 """Find revisions matching a revset.
1778 """Find revisions matching a revset.
1783
1779
1784 The revset is specified as a string ``expr`` that may contain
1780 The revset is specified as a string ``expr`` that may contain
1785 %-formatting to escape certain types. See ``revsetlang.formatspec``.
1781 %-formatting to escape certain types. See ``revsetlang.formatspec``.
1786
1782
1787 Revset aliases from the configuration are not expanded. To expand
1783 Revset aliases from the configuration are not expanded. To expand
1788 user aliases, consider calling ``scmutil.revrange()`` or
1784 user aliases, consider calling ``scmutil.revrange()`` or
1789 ``repo.anyrevs([expr], user=True)``.
1785 ``repo.anyrevs([expr], user=True)``.
1790
1786
1791 Returns a smartset.abstractsmartset, which is a list-like interface
1787 Returns a smartset.abstractsmartset, which is a list-like interface
1792 that contains integer revisions.
1788 that contains integer revisions.
1793 """
1789 """
1794 tree = revsetlang.spectree(expr, *args)
1790 tree = revsetlang.spectree(expr, *args)
1795 return revset.makematcher(tree)(self)
1791 return revset.makematcher(tree)(self)
1796
1792
1797 def set(self, expr, *args):
1793 def set(self, expr, *args):
1798 """Find revisions matching a revset and emit changectx instances.
1794 """Find revisions matching a revset and emit changectx instances.
1799
1795
1800 This is a convenience wrapper around ``revs()`` that iterates the
1796 This is a convenience wrapper around ``revs()`` that iterates the
1801 result and is a generator of changectx instances.
1797 result and is a generator of changectx instances.
1802
1798
1803 Revset aliases from the configuration are not expanded. To expand
1799 Revset aliases from the configuration are not expanded. To expand
1804 user aliases, consider calling ``scmutil.revrange()``.
1800 user aliases, consider calling ``scmutil.revrange()``.
1805 """
1801 """
1806 for r in self.revs(expr, *args):
1802 for r in self.revs(expr, *args):
1807 yield self[r]
1803 yield self[r]
1808
1804
1809 def anyrevs(self, specs, user=False, localalias=None):
1805 def anyrevs(self, specs, user=False, localalias=None):
1810 """Find revisions matching one of the given revsets.
1806 """Find revisions matching one of the given revsets.
1811
1807
1812 Revset aliases from the configuration are not expanded by default. To
1808 Revset aliases from the configuration are not expanded by default. To
1813 expand user aliases, specify ``user=True``. To provide some local
1809 expand user aliases, specify ``user=True``. To provide some local
1814 definitions overriding user aliases, set ``localalias`` to
1810 definitions overriding user aliases, set ``localalias`` to
1815 ``{name: definitionstring}``.
1811 ``{name: definitionstring}``.
1816 """
1812 """
1817 if specs == [b'null']:
1813 if specs == [b'null']:
1818 return revset.baseset([nullrev])
1814 return revset.baseset([nullrev])
1819 if specs == [b'.']:
1815 if specs == [b'.']:
1820 quick_data = self._quick_access_changeid.get(b'.')
1816 quick_data = self._quick_access_changeid.get(b'.')
1821 if quick_data is not None:
1817 if quick_data is not None:
1822 return revset.baseset([quick_data[0]])
1818 return revset.baseset([quick_data[0]])
1823 if user:
1819 if user:
1824 m = revset.matchany(
1820 m = revset.matchany(
1825 self.ui,
1821 self.ui,
1826 specs,
1822 specs,
1827 lookup=revset.lookupfn(self),
1823 lookup=revset.lookupfn(self),
1828 localalias=localalias,
1824 localalias=localalias,
1829 )
1825 )
1830 else:
1826 else:
1831 m = revset.matchany(None, specs, localalias=localalias)
1827 m = revset.matchany(None, specs, localalias=localalias)
1832 return m(self)
1828 return m(self)
1833
1829
1834 def url(self):
1830 def url(self):
1835 return b'file:' + self.root
1831 return b'file:' + self.root
1836
1832
1837 def hook(self, name, throw=False, **args):
1833 def hook(self, name, throw=False, **args):
1838 """Call a hook, passing this repo instance.
1834 """Call a hook, passing this repo instance.
1839
1835
1840 This a convenience method to aid invoking hooks. Extensions likely
1836 This a convenience method to aid invoking hooks. Extensions likely
1841 won't call this unless they have registered a custom hook or are
1837 won't call this unless they have registered a custom hook or are
1842 replacing code that is expected to call a hook.
1838 replacing code that is expected to call a hook.
1843 """
1839 """
1844 return hook.hook(self.ui, self, name, throw, **args)
1840 return hook.hook(self.ui, self, name, throw, **args)
1845
1841
1846 @filteredpropertycache
1842 @filteredpropertycache
1847 def _tagscache(self):
1843 def _tagscache(self):
1848 """Returns a tagscache object that contains various tags related
1844 """Returns a tagscache object that contains various tags related
1849 caches."""
1845 caches."""
1850
1846
1851 # This simplifies its cache management by having one decorated
1847 # This simplifies its cache management by having one decorated
1852 # function (this one) and the rest simply fetch things from it.
1848 # function (this one) and the rest simply fetch things from it.
1853 class tagscache(object):
1849 class tagscache(object):
1854 def __init__(self):
1850 def __init__(self):
1855 # These two define the set of tags for this repository. tags
1851 # These two define the set of tags for this repository. tags
1856 # maps tag name to node; tagtypes maps tag name to 'global' or
1852 # maps tag name to node; tagtypes maps tag name to 'global' or
1857 # 'local'. (Global tags are defined by .hgtags across all
1853 # 'local'. (Global tags are defined by .hgtags across all
1858 # heads, and local tags are defined in .hg/localtags.)
1854 # heads, and local tags are defined in .hg/localtags.)
1859 # They constitute the in-memory cache of tags.
1855 # They constitute the in-memory cache of tags.
1860 self.tags = self.tagtypes = None
1856 self.tags = self.tagtypes = None
1861
1857
1862 self.nodetagscache = self.tagslist = None
1858 self.nodetagscache = self.tagslist = None
1863
1859
1864 cache = tagscache()
1860 cache = tagscache()
1865 cache.tags, cache.tagtypes = self._findtags()
1861 cache.tags, cache.tagtypes = self._findtags()
1866
1862
1867 return cache
1863 return cache
1868
1864
1869 def tags(self):
1865 def tags(self):
1870 '''return a mapping of tag to node'''
1866 '''return a mapping of tag to node'''
1871 t = {}
1867 t = {}
1872 if self.changelog.filteredrevs:
1868 if self.changelog.filteredrevs:
1873 tags, tt = self._findtags()
1869 tags, tt = self._findtags()
1874 else:
1870 else:
1875 tags = self._tagscache.tags
1871 tags = self._tagscache.tags
1876 rev = self.changelog.rev
1872 rev = self.changelog.rev
1877 for k, v in pycompat.iteritems(tags):
1873 for k, v in pycompat.iteritems(tags):
1878 try:
1874 try:
1879 # ignore tags to unknown nodes
1875 # ignore tags to unknown nodes
1880 rev(v)
1876 rev(v)
1881 t[k] = v
1877 t[k] = v
1882 except (error.LookupError, ValueError):
1878 except (error.LookupError, ValueError):
1883 pass
1879 pass
1884 return t
1880 return t
1885
1881
1886 def _findtags(self):
1882 def _findtags(self):
1887 """Do the hard work of finding tags. Return a pair of dicts
1883 """Do the hard work of finding tags. Return a pair of dicts
1888 (tags, tagtypes) where tags maps tag name to node, and tagtypes
1884 (tags, tagtypes) where tags maps tag name to node, and tagtypes
1889 maps tag name to a string like \'global\' or \'local\'.
1885 maps tag name to a string like \'global\' or \'local\'.
1890 Subclasses or extensions are free to add their own tags, but
1886 Subclasses or extensions are free to add their own tags, but
1891 should be aware that the returned dicts will be retained for the
1887 should be aware that the returned dicts will be retained for the
1892 duration of the localrepo object."""
1888 duration of the localrepo object."""
1893
1889
1894 # XXX what tagtype should subclasses/extensions use? Currently
1890 # XXX what tagtype should subclasses/extensions use? Currently
1895 # mq and bookmarks add tags, but do not set the tagtype at all.
1891 # mq and bookmarks add tags, but do not set the tagtype at all.
1896 # Should each extension invent its own tag type? Should there
1892 # Should each extension invent its own tag type? Should there
1897 # be one tagtype for all such "virtual" tags? Or is the status
1893 # be one tagtype for all such "virtual" tags? Or is the status
1898 # quo fine?
1894 # quo fine?
1899
1895
1900 # map tag name to (node, hist)
1896 # map tag name to (node, hist)
1901 alltags = tagsmod.findglobaltags(self.ui, self)
1897 alltags = tagsmod.findglobaltags(self.ui, self)
1902 # map tag name to tag type
1898 # map tag name to tag type
1903 tagtypes = {tag: b'global' for tag in alltags}
1899 tagtypes = {tag: b'global' for tag in alltags}
1904
1900
1905 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
1901 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
1906
1902
1907 # Build the return dicts. Have to re-encode tag names because
1903 # Build the return dicts. Have to re-encode tag names because
1908 # the tags module always uses UTF-8 (in order not to lose info
1904 # the tags module always uses UTF-8 (in order not to lose info
1909 # writing to the cache), but the rest of Mercurial wants them in
1905 # writing to the cache), but the rest of Mercurial wants them in
1910 # local encoding.
1906 # local encoding.
1911 tags = {}
1907 tags = {}
1912 for (name, (node, hist)) in pycompat.iteritems(alltags):
1908 for (name, (node, hist)) in pycompat.iteritems(alltags):
1913 if node != nullid:
1909 if node != nullid:
1914 tags[encoding.tolocal(name)] = node
1910 tags[encoding.tolocal(name)] = node
1915 tags[b'tip'] = self.changelog.tip()
1911 tags[b'tip'] = self.changelog.tip()
1916 tagtypes = {
1912 tagtypes = {
1917 encoding.tolocal(name): value
1913 encoding.tolocal(name): value
1918 for (name, value) in pycompat.iteritems(tagtypes)
1914 for (name, value) in pycompat.iteritems(tagtypes)
1919 }
1915 }
1920 return (tags, tagtypes)
1916 return (tags, tagtypes)
1921
1917
1922 def tagtype(self, tagname):
1918 def tagtype(self, tagname):
1923 """
1919 """
1924 return the type of the given tag. result can be:
1920 return the type of the given tag. result can be:
1925
1921
1926 'local' : a local tag
1922 'local' : a local tag
1927 'global' : a global tag
1923 'global' : a global tag
1928 None : tag does not exist
1924 None : tag does not exist
1929 """
1925 """
1930
1926
1931 return self._tagscache.tagtypes.get(tagname)
1927 return self._tagscache.tagtypes.get(tagname)
1932
1928
1933 def tagslist(self):
1929 def tagslist(self):
1934 '''return a list of tags ordered by revision'''
1930 '''return a list of tags ordered by revision'''
1935 if not self._tagscache.tagslist:
1931 if not self._tagscache.tagslist:
1936 l = []
1932 l = []
1937 for t, n in pycompat.iteritems(self.tags()):
1933 for t, n in pycompat.iteritems(self.tags()):
1938 l.append((self.changelog.rev(n), t, n))
1934 l.append((self.changelog.rev(n), t, n))
1939 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
1935 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
1940
1936
1941 return self._tagscache.tagslist
1937 return self._tagscache.tagslist
1942
1938
1943 def nodetags(self, node):
1939 def nodetags(self, node):
1944 '''return the tags associated with a node'''
1940 '''return the tags associated with a node'''
1945 if not self._tagscache.nodetagscache:
1941 if not self._tagscache.nodetagscache:
1946 nodetagscache = {}
1942 nodetagscache = {}
1947 for t, n in pycompat.iteritems(self._tagscache.tags):
1943 for t, n in pycompat.iteritems(self._tagscache.tags):
1948 nodetagscache.setdefault(n, []).append(t)
1944 nodetagscache.setdefault(n, []).append(t)
1949 for tags in pycompat.itervalues(nodetagscache):
1945 for tags in pycompat.itervalues(nodetagscache):
1950 tags.sort()
1946 tags.sort()
1951 self._tagscache.nodetagscache = nodetagscache
1947 self._tagscache.nodetagscache = nodetagscache
1952 return self._tagscache.nodetagscache.get(node, [])
1948 return self._tagscache.nodetagscache.get(node, [])
1953
1949
1954 def nodebookmarks(self, node):
1950 def nodebookmarks(self, node):
1955 """return the list of bookmarks pointing to the specified node"""
1951 """return the list of bookmarks pointing to the specified node"""
1956 return self._bookmarks.names(node)
1952 return self._bookmarks.names(node)
1957
1953
1958 def branchmap(self):
1954 def branchmap(self):
1959 """returns a dictionary {branch: [branchheads]} with branchheads
1955 """returns a dictionary {branch: [branchheads]} with branchheads
1960 ordered by increasing revision number"""
1956 ordered by increasing revision number"""
1961 return self._branchcaches[self]
1957 return self._branchcaches[self]
1962
1958
1963 @unfilteredmethod
1959 @unfilteredmethod
1964 def revbranchcache(self):
1960 def revbranchcache(self):
1965 if not self._revbranchcache:
1961 if not self._revbranchcache:
1966 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
1962 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
1967 return self._revbranchcache
1963 return self._revbranchcache
1968
1964
1969 def branchtip(self, branch, ignoremissing=False):
1965 def branchtip(self, branch, ignoremissing=False):
1970 """return the tip node for a given branch
1966 """return the tip node for a given branch
1971
1967
1972 If ignoremissing is True, then this method will not raise an error.
1968 If ignoremissing is True, then this method will not raise an error.
1973 This is helpful for callers that only expect None for a missing branch
1969 This is helpful for callers that only expect None for a missing branch
1974 (e.g. namespace).
1970 (e.g. namespace).
1975
1971
1976 """
1972 """
1977 try:
1973 try:
1978 return self.branchmap().branchtip(branch)
1974 return self.branchmap().branchtip(branch)
1979 except KeyError:
1975 except KeyError:
1980 if not ignoremissing:
1976 if not ignoremissing:
1981 raise error.RepoLookupError(_(b"unknown branch '%s'") % branch)
1977 raise error.RepoLookupError(_(b"unknown branch '%s'") % branch)
1982 else:
1978 else:
1983 pass
1979 pass
1984
1980
1985 def lookup(self, key):
1981 def lookup(self, key):
1986 node = scmutil.revsymbol(self, key).node()
1982 node = scmutil.revsymbol(self, key).node()
1987 if node is None:
1983 if node is None:
1988 raise error.RepoLookupError(_(b"unknown revision '%s'") % key)
1984 raise error.RepoLookupError(_(b"unknown revision '%s'") % key)
1989 return node
1985 return node
1990
1986
1991 def lookupbranch(self, key):
1987 def lookupbranch(self, key):
1992 if self.branchmap().hasbranch(key):
1988 if self.branchmap().hasbranch(key):
1993 return key
1989 return key
1994
1990
1995 return scmutil.revsymbol(self, key).branch()
1991 return scmutil.revsymbol(self, key).branch()
1996
1992
1997 def known(self, nodes):
1993 def known(self, nodes):
1998 cl = self.changelog
1994 cl = self.changelog
1999 get_rev = cl.index.get_rev
1995 get_rev = cl.index.get_rev
2000 filtered = cl.filteredrevs
1996 filtered = cl.filteredrevs
2001 result = []
1997 result = []
2002 for n in nodes:
1998 for n in nodes:
2003 r = get_rev(n)
1999 r = get_rev(n)
2004 resp = not (r is None or r in filtered)
2000 resp = not (r is None or r in filtered)
2005 result.append(resp)
2001 result.append(resp)
2006 return result
2002 return result
2007
2003
2008 def local(self):
2004 def local(self):
2009 return self
2005 return self
2010
2006
2011 def publishing(self):
2007 def publishing(self):
2012 # it's safe (and desirable) to trust the publish flag unconditionally
2008 # it's safe (and desirable) to trust the publish flag unconditionally
2013 # so that we don't finalize changes shared between users via ssh or nfs
2009 # so that we don't finalize changes shared between users via ssh or nfs
2014 return self.ui.configbool(b'phases', b'publish', untrusted=True)
2010 return self.ui.configbool(b'phases', b'publish', untrusted=True)
2015
2011
2016 def cancopy(self):
2012 def cancopy(self):
2017 # so statichttprepo's override of local() works
2013 # so statichttprepo's override of local() works
2018 if not self.local():
2014 if not self.local():
2019 return False
2015 return False
2020 if not self.publishing():
2016 if not self.publishing():
2021 return True
2017 return True
2022 # if publishing we can't copy if there is filtered content
2018 # if publishing we can't copy if there is filtered content
2023 return not self.filtered(b'visible').changelog.filteredrevs
2019 return not self.filtered(b'visible').changelog.filteredrevs
2024
2020
2025 def shared(self):
2021 def shared(self):
2026 '''the type of shared repository (None if not shared)'''
2022 '''the type of shared repository (None if not shared)'''
2027 if self.sharedpath != self.path:
2023 if self.sharedpath != self.path:
2028 return b'store'
2024 return b'store'
2029 return None
2025 return None
2030
2026
2031 def wjoin(self, f, *insidef):
2027 def wjoin(self, f, *insidef):
2032 return self.vfs.reljoin(self.root, f, *insidef)
2028 return self.vfs.reljoin(self.root, f, *insidef)
2033
2029
2034 def setparents(self, p1, p2=nullid):
2030 def setparents(self, p1, p2=nullid):
2035 self[None].setparents(p1, p2)
2031 self[None].setparents(p1, p2)
2036 self._quick_access_changeid_invalidate()
2032 self._quick_access_changeid_invalidate()
2037
2033
2038 def filectx(self, path, changeid=None, fileid=None, changectx=None):
2034 def filectx(self, path, changeid=None, fileid=None, changectx=None):
2039 """changeid must be a changeset revision, if specified.
2035 """changeid must be a changeset revision, if specified.
2040 fileid can be a file revision or node."""
2036 fileid can be a file revision or node."""
2041 return context.filectx(
2037 return context.filectx(
2042 self, path, changeid, fileid, changectx=changectx
2038 self, path, changeid, fileid, changectx=changectx
2043 )
2039 )
2044
2040
2045 def getcwd(self):
2041 def getcwd(self):
2046 return self.dirstate.getcwd()
2042 return self.dirstate.getcwd()
2047
2043
2048 def pathto(self, f, cwd=None):
2044 def pathto(self, f, cwd=None):
2049 return self.dirstate.pathto(f, cwd)
2045 return self.dirstate.pathto(f, cwd)
2050
2046
2051 def _loadfilter(self, filter):
2047 def _loadfilter(self, filter):
2052 if filter not in self._filterpats:
2048 if filter not in self._filterpats:
2053 l = []
2049 l = []
2054 for pat, cmd in self.ui.configitems(filter):
2050 for pat, cmd in self.ui.configitems(filter):
2055 if cmd == b'!':
2051 if cmd == b'!':
2056 continue
2052 continue
2057 mf = matchmod.match(self.root, b'', [pat])
2053 mf = matchmod.match(self.root, b'', [pat])
2058 fn = None
2054 fn = None
2059 params = cmd
2055 params = cmd
2060 for name, filterfn in pycompat.iteritems(self._datafilters):
2056 for name, filterfn in pycompat.iteritems(self._datafilters):
2061 if cmd.startswith(name):
2057 if cmd.startswith(name):
2062 fn = filterfn
2058 fn = filterfn
2063 params = cmd[len(name) :].lstrip()
2059 params = cmd[len(name) :].lstrip()
2064 break
2060 break
2065 if not fn:
2061 if not fn:
2066 fn = lambda s, c, **kwargs: procutil.filter(s, c)
2062 fn = lambda s, c, **kwargs: procutil.filter(s, c)
2067 fn.__name__ = 'commandfilter'
2063 fn.__name__ = 'commandfilter'
2068 # Wrap old filters not supporting keyword arguments
2064 # Wrap old filters not supporting keyword arguments
2069 if not pycompat.getargspec(fn)[2]:
2065 if not pycompat.getargspec(fn)[2]:
2070 oldfn = fn
2066 oldfn = fn
2071 fn = lambda s, c, oldfn=oldfn, **kwargs: oldfn(s, c)
2067 fn = lambda s, c, oldfn=oldfn, **kwargs: oldfn(s, c)
2072 fn.__name__ = 'compat-' + oldfn.__name__
2068 fn.__name__ = 'compat-' + oldfn.__name__
2073 l.append((mf, fn, params))
2069 l.append((mf, fn, params))
2074 self._filterpats[filter] = l
2070 self._filterpats[filter] = l
2075 return self._filterpats[filter]
2071 return self._filterpats[filter]
2076
2072
2077 def _filter(self, filterpats, filename, data):
2073 def _filter(self, filterpats, filename, data):
2078 for mf, fn, cmd in filterpats:
2074 for mf, fn, cmd in filterpats:
2079 if mf(filename):
2075 if mf(filename):
2080 self.ui.debug(
2076 self.ui.debug(
2081 b"filtering %s through %s\n"
2077 b"filtering %s through %s\n"
2082 % (filename, cmd or pycompat.sysbytes(fn.__name__))
2078 % (filename, cmd or pycompat.sysbytes(fn.__name__))
2083 )
2079 )
2084 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
2080 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
2085 break
2081 break
2086
2082
2087 return data
2083 return data
2088
2084
2089 @unfilteredpropertycache
2085 @unfilteredpropertycache
2090 def _encodefilterpats(self):
2086 def _encodefilterpats(self):
2091 return self._loadfilter(b'encode')
2087 return self._loadfilter(b'encode')
2092
2088
2093 @unfilteredpropertycache
2089 @unfilteredpropertycache
2094 def _decodefilterpats(self):
2090 def _decodefilterpats(self):
2095 return self._loadfilter(b'decode')
2091 return self._loadfilter(b'decode')
2096
2092
2097 def adddatafilter(self, name, filter):
2093 def adddatafilter(self, name, filter):
2098 self._datafilters[name] = filter
2094 self._datafilters[name] = filter
2099
2095
2100 def wread(self, filename):
2096 def wread(self, filename):
2101 if self.wvfs.islink(filename):
2097 if self.wvfs.islink(filename):
2102 data = self.wvfs.readlink(filename)
2098 data = self.wvfs.readlink(filename)
2103 else:
2099 else:
2104 data = self.wvfs.read(filename)
2100 data = self.wvfs.read(filename)
2105 return self._filter(self._encodefilterpats, filename, data)
2101 return self._filter(self._encodefilterpats, filename, data)
2106
2102
2107 def wwrite(self, filename, data, flags, backgroundclose=False, **kwargs):
2103 def wwrite(self, filename, data, flags, backgroundclose=False, **kwargs):
2108 """write ``data`` into ``filename`` in the working directory
2104 """write ``data`` into ``filename`` in the working directory
2109
2105
2110 This returns length of written (maybe decoded) data.
2106 This returns length of written (maybe decoded) data.
2111 """
2107 """
2112 data = self._filter(self._decodefilterpats, filename, data)
2108 data = self._filter(self._decodefilterpats, filename, data)
2113 if b'l' in flags:
2109 if b'l' in flags:
2114 self.wvfs.symlink(data, filename)
2110 self.wvfs.symlink(data, filename)
2115 else:
2111 else:
2116 self.wvfs.write(
2112 self.wvfs.write(
2117 filename, data, backgroundclose=backgroundclose, **kwargs
2113 filename, data, backgroundclose=backgroundclose, **kwargs
2118 )
2114 )
2119 if b'x' in flags:
2115 if b'x' in flags:
2120 self.wvfs.setflags(filename, False, True)
2116 self.wvfs.setflags(filename, False, True)
2121 else:
2117 else:
2122 self.wvfs.setflags(filename, False, False)
2118 self.wvfs.setflags(filename, False, False)
2123 return len(data)
2119 return len(data)
2124
2120
2125 def wwritedata(self, filename, data):
2121 def wwritedata(self, filename, data):
2126 return self._filter(self._decodefilterpats, filename, data)
2122 return self._filter(self._decodefilterpats, filename, data)
2127
2123
2128 def currenttransaction(self):
2124 def currenttransaction(self):
2129 """return the current transaction or None if non exists"""
2125 """return the current transaction or None if non exists"""
2130 if self._transref:
2126 if self._transref:
2131 tr = self._transref()
2127 tr = self._transref()
2132 else:
2128 else:
2133 tr = None
2129 tr = None
2134
2130
2135 if tr and tr.running():
2131 if tr and tr.running():
2136 return tr
2132 return tr
2137 return None
2133 return None
2138
2134
2139 def transaction(self, desc, report=None):
2135 def transaction(self, desc, report=None):
2140 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
2136 if self.ui.configbool(b'devel', b'all-warnings') or self.ui.configbool(
2141 b'devel', b'check-locks'
2137 b'devel', b'check-locks'
2142 ):
2138 ):
2143 if self._currentlock(self._lockref) is None:
2139 if self._currentlock(self._lockref) is None:
2144 raise error.ProgrammingError(b'transaction requires locking')
2140 raise error.ProgrammingError(b'transaction requires locking')
2145 tr = self.currenttransaction()
2141 tr = self.currenttransaction()
2146 if tr is not None:
2142 if tr is not None:
2147 return tr.nest(name=desc)
2143 return tr.nest(name=desc)
2148
2144
2149 # abort here if the journal already exists
2145 # abort here if the journal already exists
2150 if self.svfs.exists(b"journal"):
2146 if self.svfs.exists(b"journal"):
2151 raise error.RepoError(
2147 raise error.RepoError(
2152 _(b"abandoned transaction found"),
2148 _(b"abandoned transaction found"),
2153 hint=_(b"run 'hg recover' to clean up transaction"),
2149 hint=_(b"run 'hg recover' to clean up transaction"),
2154 )
2150 )
2155
2151
2156 idbase = b"%.40f#%f" % (random.random(), time.time())
2152 idbase = b"%.40f#%f" % (random.random(), time.time())
2157 ha = hex(hashutil.sha1(idbase).digest())
2153 ha = hex(hashutil.sha1(idbase).digest())
2158 txnid = b'TXN:' + ha
2154 txnid = b'TXN:' + ha
2159 self.hook(b'pretxnopen', throw=True, txnname=desc, txnid=txnid)
2155 self.hook(b'pretxnopen', throw=True, txnname=desc, txnid=txnid)
2160
2156
2161 self._writejournal(desc)
2157 self._writejournal(desc)
2162 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
2158 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
2163 if report:
2159 if report:
2164 rp = report
2160 rp = report
2165 else:
2161 else:
2166 rp = self.ui.warn
2162 rp = self.ui.warn
2167 vfsmap = {b'plain': self.vfs, b'store': self.svfs} # root of .hg/
2163 vfsmap = {b'plain': self.vfs, b'store': self.svfs} # root of .hg/
2168 # we must avoid cyclic reference between repo and transaction.
2164 # we must avoid cyclic reference between repo and transaction.
2169 reporef = weakref.ref(self)
2165 reporef = weakref.ref(self)
2170 # Code to track tag movement
2166 # Code to track tag movement
2171 #
2167 #
2172 # Since tags are all handled as file content, it is actually quite hard
2168 # Since tags are all handled as file content, it is actually quite hard
2173 # to track these movement from a code perspective. So we fallback to a
2169 # to track these movement from a code perspective. So we fallback to a
2174 # tracking at the repository level. One could envision to track changes
2170 # tracking at the repository level. One could envision to track changes
2175 # to the '.hgtags' file through changegroup apply but that fails to
2171 # to the '.hgtags' file through changegroup apply but that fails to
2176 # cope with case where transaction expose new heads without changegroup
2172 # cope with case where transaction expose new heads without changegroup
2177 # being involved (eg: phase movement).
2173 # being involved (eg: phase movement).
2178 #
2174 #
2179 # For now, We gate the feature behind a flag since this likely comes
2175 # For now, We gate the feature behind a flag since this likely comes
2180 # with performance impacts. The current code run more often than needed
2176 # with performance impacts. The current code run more often than needed
2181 # and do not use caches as much as it could. The current focus is on
2177 # and do not use caches as much as it could. The current focus is on
2182 # the behavior of the feature so we disable it by default. The flag
2178 # the behavior of the feature so we disable it by default. The flag
2183 # will be removed when we are happy with the performance impact.
2179 # will be removed when we are happy with the performance impact.
2184 #
2180 #
2185 # Once this feature is no longer experimental move the following
2181 # Once this feature is no longer experimental move the following
2186 # documentation to the appropriate help section:
2182 # documentation to the appropriate help section:
2187 #
2183 #
2188 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
2184 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
2189 # tags (new or changed or deleted tags). In addition the details of
2185 # tags (new or changed or deleted tags). In addition the details of
2190 # these changes are made available in a file at:
2186 # these changes are made available in a file at:
2191 # ``REPOROOT/.hg/changes/tags.changes``.
2187 # ``REPOROOT/.hg/changes/tags.changes``.
2192 # Make sure you check for HG_TAG_MOVED before reading that file as it
2188 # Make sure you check for HG_TAG_MOVED before reading that file as it
2193 # might exist from a previous transaction even if no tag were touched
2189 # might exist from a previous transaction even if no tag were touched
2194 # in this one. Changes are recorded in a line base format::
2190 # in this one. Changes are recorded in a line base format::
2195 #
2191 #
2196 # <action> <hex-node> <tag-name>\n
2192 # <action> <hex-node> <tag-name>\n
2197 #
2193 #
2198 # Actions are defined as follow:
2194 # Actions are defined as follow:
2199 # "-R": tag is removed,
2195 # "-R": tag is removed,
2200 # "+A": tag is added,
2196 # "+A": tag is added,
2201 # "-M": tag is moved (old value),
2197 # "-M": tag is moved (old value),
2202 # "+M": tag is moved (new value),
2198 # "+M": tag is moved (new value),
2203 tracktags = lambda x: None
2199 tracktags = lambda x: None
2204 # experimental config: experimental.hook-track-tags
2200 # experimental config: experimental.hook-track-tags
2205 shouldtracktags = self.ui.configbool(
2201 shouldtracktags = self.ui.configbool(
2206 b'experimental', b'hook-track-tags'
2202 b'experimental', b'hook-track-tags'
2207 )
2203 )
2208 if desc != b'strip' and shouldtracktags:
2204 if desc != b'strip' and shouldtracktags:
2209 oldheads = self.changelog.headrevs()
2205 oldheads = self.changelog.headrevs()
2210
2206
2211 def tracktags(tr2):
2207 def tracktags(tr2):
2212 repo = reporef()
2208 repo = reporef()
2213 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
2209 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
2214 newheads = repo.changelog.headrevs()
2210 newheads = repo.changelog.headrevs()
2215 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
2211 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
2216 # notes: we compare lists here.
2212 # notes: we compare lists here.
2217 # As we do it only once buiding set would not be cheaper
2213 # As we do it only once buiding set would not be cheaper
2218 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
2214 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
2219 if changes:
2215 if changes:
2220 tr2.hookargs[b'tag_moved'] = b'1'
2216 tr2.hookargs[b'tag_moved'] = b'1'
2221 with repo.vfs(
2217 with repo.vfs(
2222 b'changes/tags.changes', b'w', atomictemp=True
2218 b'changes/tags.changes', b'w', atomictemp=True
2223 ) as changesfile:
2219 ) as changesfile:
2224 # note: we do not register the file to the transaction
2220 # note: we do not register the file to the transaction
2225 # because we needs it to still exist on the transaction
2221 # because we needs it to still exist on the transaction
2226 # is close (for txnclose hooks)
2222 # is close (for txnclose hooks)
2227 tagsmod.writediff(changesfile, changes)
2223 tagsmod.writediff(changesfile, changes)
2228
2224
2229 def validate(tr2):
2225 def validate(tr2):
2230 """will run pre-closing hooks"""
2226 """will run pre-closing hooks"""
2231 # XXX the transaction API is a bit lacking here so we take a hacky
2227 # XXX the transaction API is a bit lacking here so we take a hacky
2232 # path for now
2228 # path for now
2233 #
2229 #
2234 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
2230 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
2235 # dict is copied before these run. In addition we needs the data
2231 # dict is copied before these run. In addition we needs the data
2236 # available to in memory hooks too.
2232 # available to in memory hooks too.
2237 #
2233 #
2238 # Moreover, we also need to make sure this runs before txnclose
2234 # Moreover, we also need to make sure this runs before txnclose
2239 # hooks and there is no "pending" mechanism that would execute
2235 # hooks and there is no "pending" mechanism that would execute
2240 # logic only if hooks are about to run.
2236 # logic only if hooks are about to run.
2241 #
2237 #
2242 # Fixing this limitation of the transaction is also needed to track
2238 # Fixing this limitation of the transaction is also needed to track
2243 # other families of changes (bookmarks, phases, obsolescence).
2239 # other families of changes (bookmarks, phases, obsolescence).
2244 #
2240 #
2245 # This will have to be fixed before we remove the experimental
2241 # This will have to be fixed before we remove the experimental
2246 # gating.
2242 # gating.
2247 tracktags(tr2)
2243 tracktags(tr2)
2248 repo = reporef()
2244 repo = reporef()
2249
2245
2250 singleheadopt = (b'experimental', b'single-head-per-branch')
2246 singleheadopt = (b'experimental', b'single-head-per-branch')
2251 singlehead = repo.ui.configbool(*singleheadopt)
2247 singlehead = repo.ui.configbool(*singleheadopt)
2252 if singlehead:
2248 if singlehead:
2253 singleheadsub = repo.ui.configsuboptions(*singleheadopt)[1]
2249 singleheadsub = repo.ui.configsuboptions(*singleheadopt)[1]
2254 accountclosed = singleheadsub.get(
2250 accountclosed = singleheadsub.get(
2255 b"account-closed-heads", False
2251 b"account-closed-heads", False
2256 )
2252 )
2257 if singleheadsub.get(b"public-changes-only", False):
2253 if singleheadsub.get(b"public-changes-only", False):
2258 filtername = b"immutable"
2254 filtername = b"immutable"
2259 else:
2255 else:
2260 filtername = b"visible"
2256 filtername = b"visible"
2261 scmutil.enforcesinglehead(
2257 scmutil.enforcesinglehead(
2262 repo, tr2, desc, accountclosed, filtername
2258 repo, tr2, desc, accountclosed, filtername
2263 )
2259 )
2264 if hook.hashook(repo.ui, b'pretxnclose-bookmark'):
2260 if hook.hashook(repo.ui, b'pretxnclose-bookmark'):
2265 for name, (old, new) in sorted(
2261 for name, (old, new) in sorted(
2266 tr.changes[b'bookmarks'].items()
2262 tr.changes[b'bookmarks'].items()
2267 ):
2263 ):
2268 args = tr.hookargs.copy()
2264 args = tr.hookargs.copy()
2269 args.update(bookmarks.preparehookargs(name, old, new))
2265 args.update(bookmarks.preparehookargs(name, old, new))
2270 repo.hook(
2266 repo.hook(
2271 b'pretxnclose-bookmark',
2267 b'pretxnclose-bookmark',
2272 throw=True,
2268 throw=True,
2273 **pycompat.strkwargs(args)
2269 **pycompat.strkwargs(args)
2274 )
2270 )
2275 if hook.hashook(repo.ui, b'pretxnclose-phase'):
2271 if hook.hashook(repo.ui, b'pretxnclose-phase'):
2276 cl = repo.unfiltered().changelog
2272 cl = repo.unfiltered().changelog
2277 for revs, (old, new) in tr.changes[b'phases']:
2273 for revs, (old, new) in tr.changes[b'phases']:
2278 for rev in revs:
2274 for rev in revs:
2279 args = tr.hookargs.copy()
2275 args = tr.hookargs.copy()
2280 node = hex(cl.node(rev))
2276 node = hex(cl.node(rev))
2281 args.update(phases.preparehookargs(node, old, new))
2277 args.update(phases.preparehookargs(node, old, new))
2282 repo.hook(
2278 repo.hook(
2283 b'pretxnclose-phase',
2279 b'pretxnclose-phase',
2284 throw=True,
2280 throw=True,
2285 **pycompat.strkwargs(args)
2281 **pycompat.strkwargs(args)
2286 )
2282 )
2287
2283
2288 repo.hook(
2284 repo.hook(
2289 b'pretxnclose', throw=True, **pycompat.strkwargs(tr.hookargs)
2285 b'pretxnclose', throw=True, **pycompat.strkwargs(tr.hookargs)
2290 )
2286 )
2291
2287
2292 def releasefn(tr, success):
2288 def releasefn(tr, success):
2293 repo = reporef()
2289 repo = reporef()
2294 if repo is None:
2290 if repo is None:
2295 # If the repo has been GC'd (and this release function is being
2291 # If the repo has been GC'd (and this release function is being
2296 # called from transaction.__del__), there's not much we can do,
2292 # called from transaction.__del__), there's not much we can do,
2297 # so just leave the unfinished transaction there and let the
2293 # so just leave the unfinished transaction there and let the
2298 # user run `hg recover`.
2294 # user run `hg recover`.
2299 return
2295 return
2300 if success:
2296 if success:
2301 # this should be explicitly invoked here, because
2297 # this should be explicitly invoked here, because
2302 # in-memory changes aren't written out at closing
2298 # in-memory changes aren't written out at closing
2303 # transaction, if tr.addfilegenerator (via
2299 # transaction, if tr.addfilegenerator (via
2304 # dirstate.write or so) isn't invoked while
2300 # dirstate.write or so) isn't invoked while
2305 # transaction running
2301 # transaction running
2306 repo.dirstate.write(None)
2302 repo.dirstate.write(None)
2307 else:
2303 else:
2308 # discard all changes (including ones already written
2304 # discard all changes (including ones already written
2309 # out) in this transaction
2305 # out) in this transaction
2310 narrowspec.restorebackup(self, b'journal.narrowspec')
2306 narrowspec.restorebackup(self, b'journal.narrowspec')
2311 narrowspec.restorewcbackup(self, b'journal.narrowspec.dirstate')
2307 narrowspec.restorewcbackup(self, b'journal.narrowspec.dirstate')
2312 repo.dirstate.restorebackup(None, b'journal.dirstate')
2308 repo.dirstate.restorebackup(None, b'journal.dirstate')
2313
2309
2314 repo.invalidate(clearfilecache=True)
2310 repo.invalidate(clearfilecache=True)
2315
2311
2316 tr = transaction.transaction(
2312 tr = transaction.transaction(
2317 rp,
2313 rp,
2318 self.svfs,
2314 self.svfs,
2319 vfsmap,
2315 vfsmap,
2320 b"journal",
2316 b"journal",
2321 b"undo",
2317 b"undo",
2322 aftertrans(renames),
2318 aftertrans(renames),
2323 self.store.createmode,
2319 self.store.createmode,
2324 validator=validate,
2320 validator=validate,
2325 releasefn=releasefn,
2321 releasefn=releasefn,
2326 checkambigfiles=_cachedfiles,
2322 checkambigfiles=_cachedfiles,
2327 name=desc,
2323 name=desc,
2328 )
2324 )
2329 tr.changes[b'origrepolen'] = len(self)
2325 tr.changes[b'origrepolen'] = len(self)
2330 tr.changes[b'obsmarkers'] = set()
2326 tr.changes[b'obsmarkers'] = set()
2331 tr.changes[b'phases'] = []
2327 tr.changes[b'phases'] = []
2332 tr.changes[b'bookmarks'] = {}
2328 tr.changes[b'bookmarks'] = {}
2333
2329
2334 tr.hookargs[b'txnid'] = txnid
2330 tr.hookargs[b'txnid'] = txnid
2335 tr.hookargs[b'txnname'] = desc
2331 tr.hookargs[b'txnname'] = desc
2336 tr.hookargs[b'changes'] = tr.changes
2332 tr.hookargs[b'changes'] = tr.changes
2337 # note: writing the fncache only during finalize mean that the file is
2333 # note: writing the fncache only during finalize mean that the file is
2338 # outdated when running hooks. As fncache is used for streaming clone,
2334 # outdated when running hooks. As fncache is used for streaming clone,
2339 # this is not expected to break anything that happen during the hooks.
2335 # this is not expected to break anything that happen during the hooks.
2340 tr.addfinalize(b'flush-fncache', self.store.write)
2336 tr.addfinalize(b'flush-fncache', self.store.write)
2341
2337
2342 def txnclosehook(tr2):
2338 def txnclosehook(tr2):
2343 """To be run if transaction is successful, will schedule a hook run"""
2339 """To be run if transaction is successful, will schedule a hook run"""
2344 # Don't reference tr2 in hook() so we don't hold a reference.
2340 # Don't reference tr2 in hook() so we don't hold a reference.
2345 # This reduces memory consumption when there are multiple
2341 # This reduces memory consumption when there are multiple
2346 # transactions per lock. This can likely go away if issue5045
2342 # transactions per lock. This can likely go away if issue5045
2347 # fixes the function accumulation.
2343 # fixes the function accumulation.
2348 hookargs = tr2.hookargs
2344 hookargs = tr2.hookargs
2349
2345
2350 def hookfunc(unused_success):
2346 def hookfunc(unused_success):
2351 repo = reporef()
2347 repo = reporef()
2352 if hook.hashook(repo.ui, b'txnclose-bookmark'):
2348 if hook.hashook(repo.ui, b'txnclose-bookmark'):
2353 bmchanges = sorted(tr.changes[b'bookmarks'].items())
2349 bmchanges = sorted(tr.changes[b'bookmarks'].items())
2354 for name, (old, new) in bmchanges:
2350 for name, (old, new) in bmchanges:
2355 args = tr.hookargs.copy()
2351 args = tr.hookargs.copy()
2356 args.update(bookmarks.preparehookargs(name, old, new))
2352 args.update(bookmarks.preparehookargs(name, old, new))
2357 repo.hook(
2353 repo.hook(
2358 b'txnclose-bookmark',
2354 b'txnclose-bookmark',
2359 throw=False,
2355 throw=False,
2360 **pycompat.strkwargs(args)
2356 **pycompat.strkwargs(args)
2361 )
2357 )
2362
2358
2363 if hook.hashook(repo.ui, b'txnclose-phase'):
2359 if hook.hashook(repo.ui, b'txnclose-phase'):
2364 cl = repo.unfiltered().changelog
2360 cl = repo.unfiltered().changelog
2365 phasemv = sorted(
2361 phasemv = sorted(
2366 tr.changes[b'phases'], key=lambda r: r[0][0]
2362 tr.changes[b'phases'], key=lambda r: r[0][0]
2367 )
2363 )
2368 for revs, (old, new) in phasemv:
2364 for revs, (old, new) in phasemv:
2369 for rev in revs:
2365 for rev in revs:
2370 args = tr.hookargs.copy()
2366 args = tr.hookargs.copy()
2371 node = hex(cl.node(rev))
2367 node = hex(cl.node(rev))
2372 args.update(phases.preparehookargs(node, old, new))
2368 args.update(phases.preparehookargs(node, old, new))
2373 repo.hook(
2369 repo.hook(
2374 b'txnclose-phase',
2370 b'txnclose-phase',
2375 throw=False,
2371 throw=False,
2376 **pycompat.strkwargs(args)
2372 **pycompat.strkwargs(args)
2377 )
2373 )
2378
2374
2379 repo.hook(
2375 repo.hook(
2380 b'txnclose', throw=False, **pycompat.strkwargs(hookargs)
2376 b'txnclose', throw=False, **pycompat.strkwargs(hookargs)
2381 )
2377 )
2382
2378
2383 reporef()._afterlock(hookfunc)
2379 reporef()._afterlock(hookfunc)
2384
2380
2385 tr.addfinalize(b'txnclose-hook', txnclosehook)
2381 tr.addfinalize(b'txnclose-hook', txnclosehook)
2386 # Include a leading "-" to make it happen before the transaction summary
2382 # Include a leading "-" to make it happen before the transaction summary
2387 # reports registered via scmutil.registersummarycallback() whose names
2383 # reports registered via scmutil.registersummarycallback() whose names
2388 # are 00-txnreport etc. That way, the caches will be warm when the
2384 # are 00-txnreport etc. That way, the caches will be warm when the
2389 # callbacks run.
2385 # callbacks run.
2390 tr.addpostclose(b'-warm-cache', self._buildcacheupdater(tr))
2386 tr.addpostclose(b'-warm-cache', self._buildcacheupdater(tr))
2391
2387
2392 def txnaborthook(tr2):
2388 def txnaborthook(tr2):
2393 """To be run if transaction is aborted"""
2389 """To be run if transaction is aborted"""
2394 reporef().hook(
2390 reporef().hook(
2395 b'txnabort', throw=False, **pycompat.strkwargs(tr2.hookargs)
2391 b'txnabort', throw=False, **pycompat.strkwargs(tr2.hookargs)
2396 )
2392 )
2397
2393
2398 tr.addabort(b'txnabort-hook', txnaborthook)
2394 tr.addabort(b'txnabort-hook', txnaborthook)
2399 # avoid eager cache invalidation. in-memory data should be identical
2395 # avoid eager cache invalidation. in-memory data should be identical
2400 # to stored data if transaction has no error.
2396 # to stored data if transaction has no error.
2401 tr.addpostclose(b'refresh-filecachestats', self._refreshfilecachestats)
2397 tr.addpostclose(b'refresh-filecachestats', self._refreshfilecachestats)
2402 self._transref = weakref.ref(tr)
2398 self._transref = weakref.ref(tr)
2403 scmutil.registersummarycallback(self, tr, desc)
2399 scmutil.registersummarycallback(self, tr, desc)
2404 return tr
2400 return tr
2405
2401
2406 def _journalfiles(self):
2402 def _journalfiles(self):
2407 return (
2403 return (
2408 (self.svfs, b'journal'),
2404 (self.svfs, b'journal'),
2409 (self.svfs, b'journal.narrowspec'),
2405 (self.svfs, b'journal.narrowspec'),
2410 (self.vfs, b'journal.narrowspec.dirstate'),
2406 (self.vfs, b'journal.narrowspec.dirstate'),
2411 (self.vfs, b'journal.dirstate'),
2407 (self.vfs, b'journal.dirstate'),
2412 (self.vfs, b'journal.branch'),
2408 (self.vfs, b'journal.branch'),
2413 (self.vfs, b'journal.desc'),
2409 (self.vfs, b'journal.desc'),
2414 (bookmarks.bookmarksvfs(self), b'journal.bookmarks'),
2410 (bookmarks.bookmarksvfs(self), b'journal.bookmarks'),
2415 (self.svfs, b'journal.phaseroots'),
2411 (self.svfs, b'journal.phaseroots'),
2416 )
2412 )
2417
2413
2418 def undofiles(self):
2414 def undofiles(self):
2419 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
2415 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
2420
2416
2421 @unfilteredmethod
2417 @unfilteredmethod
2422 def _writejournal(self, desc):
2418 def _writejournal(self, desc):
2423 self.dirstate.savebackup(None, b'journal.dirstate')
2419 self.dirstate.savebackup(None, b'journal.dirstate')
2424 narrowspec.savewcbackup(self, b'journal.narrowspec.dirstate')
2420 narrowspec.savewcbackup(self, b'journal.narrowspec.dirstate')
2425 narrowspec.savebackup(self, b'journal.narrowspec')
2421 narrowspec.savebackup(self, b'journal.narrowspec')
2426 self.vfs.write(
2422 self.vfs.write(
2427 b"journal.branch", encoding.fromlocal(self.dirstate.branch())
2423 b"journal.branch", encoding.fromlocal(self.dirstate.branch())
2428 )
2424 )
2429 self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc))
2425 self.vfs.write(b"journal.desc", b"%d\n%s\n" % (len(self), desc))
2430 bookmarksvfs = bookmarks.bookmarksvfs(self)
2426 bookmarksvfs = bookmarks.bookmarksvfs(self)
2431 bookmarksvfs.write(
2427 bookmarksvfs.write(
2432 b"journal.bookmarks", bookmarksvfs.tryread(b"bookmarks")
2428 b"journal.bookmarks", bookmarksvfs.tryread(b"bookmarks")
2433 )
2429 )
2434 self.svfs.write(b"journal.phaseroots", self.svfs.tryread(b"phaseroots"))
2430 self.svfs.write(b"journal.phaseroots", self.svfs.tryread(b"phaseroots"))
2435
2431
2436 def recover(self):
2432 def recover(self):
2437 with self.lock():
2433 with self.lock():
2438 if self.svfs.exists(b"journal"):
2434 if self.svfs.exists(b"journal"):
2439 self.ui.status(_(b"rolling back interrupted transaction\n"))
2435 self.ui.status(_(b"rolling back interrupted transaction\n"))
2440 vfsmap = {
2436 vfsmap = {
2441 b'': self.svfs,
2437 b'': self.svfs,
2442 b'plain': self.vfs,
2438 b'plain': self.vfs,
2443 }
2439 }
2444 transaction.rollback(
2440 transaction.rollback(
2445 self.svfs,
2441 self.svfs,
2446 vfsmap,
2442 vfsmap,
2447 b"journal",
2443 b"journal",
2448 self.ui.warn,
2444 self.ui.warn,
2449 checkambigfiles=_cachedfiles,
2445 checkambigfiles=_cachedfiles,
2450 )
2446 )
2451 self.invalidate()
2447 self.invalidate()
2452 return True
2448 return True
2453 else:
2449 else:
2454 self.ui.warn(_(b"no interrupted transaction available\n"))
2450 self.ui.warn(_(b"no interrupted transaction available\n"))
2455 return False
2451 return False
2456
2452
2457 def rollback(self, dryrun=False, force=False):
2453 def rollback(self, dryrun=False, force=False):
2458 wlock = lock = dsguard = None
2454 wlock = lock = dsguard = None
2459 try:
2455 try:
2460 wlock = self.wlock()
2456 wlock = self.wlock()
2461 lock = self.lock()
2457 lock = self.lock()
2462 if self.svfs.exists(b"undo"):
2458 if self.svfs.exists(b"undo"):
2463 dsguard = dirstateguard.dirstateguard(self, b'rollback')
2459 dsguard = dirstateguard.dirstateguard(self, b'rollback')
2464
2460
2465 return self._rollback(dryrun, force, dsguard)
2461 return self._rollback(dryrun, force, dsguard)
2466 else:
2462 else:
2467 self.ui.warn(_(b"no rollback information available\n"))
2463 self.ui.warn(_(b"no rollback information available\n"))
2468 return 1
2464 return 1
2469 finally:
2465 finally:
2470 release(dsguard, lock, wlock)
2466 release(dsguard, lock, wlock)
2471
2467
2472 @unfilteredmethod # Until we get smarter cache management
2468 @unfilteredmethod # Until we get smarter cache management
2473 def _rollback(self, dryrun, force, dsguard):
2469 def _rollback(self, dryrun, force, dsguard):
2474 ui = self.ui
2470 ui = self.ui
2475 try:
2471 try:
2476 args = self.vfs.read(b'undo.desc').splitlines()
2472 args = self.vfs.read(b'undo.desc').splitlines()
2477 (oldlen, desc, detail) = (int(args[0]), args[1], None)
2473 (oldlen, desc, detail) = (int(args[0]), args[1], None)
2478 if len(args) >= 3:
2474 if len(args) >= 3:
2479 detail = args[2]
2475 detail = args[2]
2480 oldtip = oldlen - 1
2476 oldtip = oldlen - 1
2481
2477
2482 if detail and ui.verbose:
2478 if detail and ui.verbose:
2483 msg = _(
2479 msg = _(
2484 b'repository tip rolled back to revision %d'
2480 b'repository tip rolled back to revision %d'
2485 b' (undo %s: %s)\n'
2481 b' (undo %s: %s)\n'
2486 ) % (oldtip, desc, detail)
2482 ) % (oldtip, desc, detail)
2487 else:
2483 else:
2488 msg = _(
2484 msg = _(
2489 b'repository tip rolled back to revision %d (undo %s)\n'
2485 b'repository tip rolled back to revision %d (undo %s)\n'
2490 ) % (oldtip, desc)
2486 ) % (oldtip, desc)
2491 except IOError:
2487 except IOError:
2492 msg = _(b'rolling back unknown transaction\n')
2488 msg = _(b'rolling back unknown transaction\n')
2493 desc = None
2489 desc = None
2494
2490
2495 if not force and self[b'.'] != self[b'tip'] and desc == b'commit':
2491 if not force and self[b'.'] != self[b'tip'] and desc == b'commit':
2496 raise error.Abort(
2492 raise error.Abort(
2497 _(
2493 _(
2498 b'rollback of last commit while not checked out '
2494 b'rollback of last commit while not checked out '
2499 b'may lose data'
2495 b'may lose data'
2500 ),
2496 ),
2501 hint=_(b'use -f to force'),
2497 hint=_(b'use -f to force'),
2502 )
2498 )
2503
2499
2504 ui.status(msg)
2500 ui.status(msg)
2505 if dryrun:
2501 if dryrun:
2506 return 0
2502 return 0
2507
2503
2508 parents = self.dirstate.parents()
2504 parents = self.dirstate.parents()
2509 self.destroying()
2505 self.destroying()
2510 vfsmap = {b'plain': self.vfs, b'': self.svfs}
2506 vfsmap = {b'plain': self.vfs, b'': self.svfs}
2511 transaction.rollback(
2507 transaction.rollback(
2512 self.svfs, vfsmap, b'undo', ui.warn, checkambigfiles=_cachedfiles
2508 self.svfs, vfsmap, b'undo', ui.warn, checkambigfiles=_cachedfiles
2513 )
2509 )
2514 bookmarksvfs = bookmarks.bookmarksvfs(self)
2510 bookmarksvfs = bookmarks.bookmarksvfs(self)
2515 if bookmarksvfs.exists(b'undo.bookmarks'):
2511 if bookmarksvfs.exists(b'undo.bookmarks'):
2516 bookmarksvfs.rename(
2512 bookmarksvfs.rename(
2517 b'undo.bookmarks', b'bookmarks', checkambig=True
2513 b'undo.bookmarks', b'bookmarks', checkambig=True
2518 )
2514 )
2519 if self.svfs.exists(b'undo.phaseroots'):
2515 if self.svfs.exists(b'undo.phaseroots'):
2520 self.svfs.rename(b'undo.phaseroots', b'phaseroots', checkambig=True)
2516 self.svfs.rename(b'undo.phaseroots', b'phaseroots', checkambig=True)
2521 self.invalidate()
2517 self.invalidate()
2522
2518
2523 has_node = self.changelog.index.has_node
2519 has_node = self.changelog.index.has_node
2524 parentgone = any(not has_node(p) for p in parents)
2520 parentgone = any(not has_node(p) for p in parents)
2525 if parentgone:
2521 if parentgone:
2526 # prevent dirstateguard from overwriting already restored one
2522 # prevent dirstateguard from overwriting already restored one
2527 dsguard.close()
2523 dsguard.close()
2528
2524
2529 narrowspec.restorebackup(self, b'undo.narrowspec')
2525 narrowspec.restorebackup(self, b'undo.narrowspec')
2530 narrowspec.restorewcbackup(self, b'undo.narrowspec.dirstate')
2526 narrowspec.restorewcbackup(self, b'undo.narrowspec.dirstate')
2531 self.dirstate.restorebackup(None, b'undo.dirstate')
2527 self.dirstate.restorebackup(None, b'undo.dirstate')
2532 try:
2528 try:
2533 branch = self.vfs.read(b'undo.branch')
2529 branch = self.vfs.read(b'undo.branch')
2534 self.dirstate.setbranch(encoding.tolocal(branch))
2530 self.dirstate.setbranch(encoding.tolocal(branch))
2535 except IOError:
2531 except IOError:
2536 ui.warn(
2532 ui.warn(
2537 _(
2533 _(
2538 b'named branch could not be reset: '
2534 b'named branch could not be reset: '
2539 b'current branch is still \'%s\'\n'
2535 b'current branch is still \'%s\'\n'
2540 )
2536 )
2541 % self.dirstate.branch()
2537 % self.dirstate.branch()
2542 )
2538 )
2543
2539
2544 parents = tuple([p.rev() for p in self[None].parents()])
2540 parents = tuple([p.rev() for p in self[None].parents()])
2545 if len(parents) > 1:
2541 if len(parents) > 1:
2546 ui.status(
2542 ui.status(
2547 _(
2543 _(
2548 b'working directory now based on '
2544 b'working directory now based on '
2549 b'revisions %d and %d\n'
2545 b'revisions %d and %d\n'
2550 )
2546 )
2551 % parents
2547 % parents
2552 )
2548 )
2553 else:
2549 else:
2554 ui.status(
2550 ui.status(
2555 _(b'working directory now based on revision %d\n') % parents
2551 _(b'working directory now based on revision %d\n') % parents
2556 )
2552 )
2557 mergestatemod.mergestate.clean(self)
2553 mergestatemod.mergestate.clean(self)
2558
2554
2559 # TODO: if we know which new heads may result from this rollback, pass
2555 # TODO: if we know which new heads may result from this rollback, pass
2560 # them to destroy(), which will prevent the branchhead cache from being
2556 # them to destroy(), which will prevent the branchhead cache from being
2561 # invalidated.
2557 # invalidated.
2562 self.destroyed()
2558 self.destroyed()
2563 return 0
2559 return 0
2564
2560
2565 def _buildcacheupdater(self, newtransaction):
2561 def _buildcacheupdater(self, newtransaction):
2566 """called during transaction to build the callback updating cache
2562 """called during transaction to build the callback updating cache
2567
2563
2568 Lives on the repository to help extension who might want to augment
2564 Lives on the repository to help extension who might want to augment
2569 this logic. For this purpose, the created transaction is passed to the
2565 this logic. For this purpose, the created transaction is passed to the
2570 method.
2566 method.
2571 """
2567 """
2572 # we must avoid cyclic reference between repo and transaction.
2568 # we must avoid cyclic reference between repo and transaction.
2573 reporef = weakref.ref(self)
2569 reporef = weakref.ref(self)
2574
2570
2575 def updater(tr):
2571 def updater(tr):
2576 repo = reporef()
2572 repo = reporef()
2577 repo.updatecaches(tr)
2573 repo.updatecaches(tr)
2578
2574
2579 return updater
2575 return updater
2580
2576
2581 @unfilteredmethod
2577 @unfilteredmethod
2582 def updatecaches(self, tr=None, full=False):
2578 def updatecaches(self, tr=None, full=False):
2583 """warm appropriate caches
2579 """warm appropriate caches
2584
2580
2585 If this function is called after a transaction closed. The transaction
2581 If this function is called after a transaction closed. The transaction
2586 will be available in the 'tr' argument. This can be used to selectively
2582 will be available in the 'tr' argument. This can be used to selectively
2587 update caches relevant to the changes in that transaction.
2583 update caches relevant to the changes in that transaction.
2588
2584
2589 If 'full' is set, make sure all caches the function knows about have
2585 If 'full' is set, make sure all caches the function knows about have
2590 up-to-date data. Even the ones usually loaded more lazily.
2586 up-to-date data. Even the ones usually loaded more lazily.
2591 """
2587 """
2592 if tr is not None and tr.hookargs.get(b'source') == b'strip':
2588 if tr is not None and tr.hookargs.get(b'source') == b'strip':
2593 # During strip, many caches are invalid but
2589 # During strip, many caches are invalid but
2594 # later call to `destroyed` will refresh them.
2590 # later call to `destroyed` will refresh them.
2595 return
2591 return
2596
2592
2597 if tr is None or tr.changes[b'origrepolen'] < len(self):
2593 if tr is None or tr.changes[b'origrepolen'] < len(self):
2598 # accessing the 'ser ved' branchmap should refresh all the others,
2594 # accessing the 'ser ved' branchmap should refresh all the others,
2599 self.ui.debug(b'updating the branch cache\n')
2595 self.ui.debug(b'updating the branch cache\n')
2600 self.filtered(b'served').branchmap()
2596 self.filtered(b'served').branchmap()
2601 self.filtered(b'served.hidden').branchmap()
2597 self.filtered(b'served.hidden').branchmap()
2602
2598
2603 if full:
2599 if full:
2604 unfi = self.unfiltered()
2600 unfi = self.unfiltered()
2605
2601
2606 self.changelog.update_caches(transaction=tr)
2602 self.changelog.update_caches(transaction=tr)
2607 self.manifestlog.update_caches(transaction=tr)
2603 self.manifestlog.update_caches(transaction=tr)
2608
2604
2609 rbc = unfi.revbranchcache()
2605 rbc = unfi.revbranchcache()
2610 for r in unfi.changelog:
2606 for r in unfi.changelog:
2611 rbc.branchinfo(r)
2607 rbc.branchinfo(r)
2612 rbc.write()
2608 rbc.write()
2613
2609
2614 # ensure the working copy parents are in the manifestfulltextcache
2610 # ensure the working copy parents are in the manifestfulltextcache
2615 for ctx in self[b'.'].parents():
2611 for ctx in self[b'.'].parents():
2616 ctx.manifest() # accessing the manifest is enough
2612 ctx.manifest() # accessing the manifest is enough
2617
2613
2618 # accessing fnode cache warms the cache
2614 # accessing fnode cache warms the cache
2619 tagsmod.fnoderevs(self.ui, unfi, unfi.changelog.revs())
2615 tagsmod.fnoderevs(self.ui, unfi, unfi.changelog.revs())
2620 # accessing tags warm the cache
2616 # accessing tags warm the cache
2621 self.tags()
2617 self.tags()
2622 self.filtered(b'served').tags()
2618 self.filtered(b'served').tags()
2623
2619
2624 # The `full` arg is documented as updating even the lazily-loaded
2620 # The `full` arg is documented as updating even the lazily-loaded
2625 # caches immediately, so we're forcing a write to cause these caches
2621 # caches immediately, so we're forcing a write to cause these caches
2626 # to be warmed up even if they haven't explicitly been requested
2622 # to be warmed up even if they haven't explicitly been requested
2627 # yet (if they've never been used by hg, they won't ever have been
2623 # yet (if they've never been used by hg, they won't ever have been
2628 # written, even if they're a subset of another kind of cache that
2624 # written, even if they're a subset of another kind of cache that
2629 # *has* been used).
2625 # *has* been used).
2630 for filt in repoview.filtertable.keys():
2626 for filt in repoview.filtertable.keys():
2631 filtered = self.filtered(filt)
2627 filtered = self.filtered(filt)
2632 filtered.branchmap().write(filtered)
2628 filtered.branchmap().write(filtered)
2633
2629
2634 def invalidatecaches(self):
2630 def invalidatecaches(self):
2635
2631
2636 if '_tagscache' in vars(self):
2632 if '_tagscache' in vars(self):
2637 # can't use delattr on proxy
2633 # can't use delattr on proxy
2638 del self.__dict__['_tagscache']
2634 del self.__dict__['_tagscache']
2639
2635
2640 self._branchcaches.clear()
2636 self._branchcaches.clear()
2641 self.invalidatevolatilesets()
2637 self.invalidatevolatilesets()
2642 self._sparsesignaturecache.clear()
2638 self._sparsesignaturecache.clear()
2643
2639
2644 def invalidatevolatilesets(self):
2640 def invalidatevolatilesets(self):
2645 self.filteredrevcache.clear()
2641 self.filteredrevcache.clear()
2646 obsolete.clearobscaches(self)
2642 obsolete.clearobscaches(self)
2647 self._quick_access_changeid_invalidate()
2643 self._quick_access_changeid_invalidate()
2648
2644
2649 def invalidatedirstate(self):
2645 def invalidatedirstate(self):
2650 """Invalidates the dirstate, causing the next call to dirstate
2646 """Invalidates the dirstate, causing the next call to dirstate
2651 to check if it was modified since the last time it was read,
2647 to check if it was modified since the last time it was read,
2652 rereading it if it has.
2648 rereading it if it has.
2653
2649
2654 This is different to dirstate.invalidate() that it doesn't always
2650 This is different to dirstate.invalidate() that it doesn't always
2655 rereads the dirstate. Use dirstate.invalidate() if you want to
2651 rereads the dirstate. Use dirstate.invalidate() if you want to
2656 explicitly read the dirstate again (i.e. restoring it to a previous
2652 explicitly read the dirstate again (i.e. restoring it to a previous
2657 known good state)."""
2653 known good state)."""
2658 if hasunfilteredcache(self, 'dirstate'):
2654 if hasunfilteredcache(self, 'dirstate'):
2659 for k in self.dirstate._filecache:
2655 for k in self.dirstate._filecache:
2660 try:
2656 try:
2661 delattr(self.dirstate, k)
2657 delattr(self.dirstate, k)
2662 except AttributeError:
2658 except AttributeError:
2663 pass
2659 pass
2664 delattr(self.unfiltered(), 'dirstate')
2660 delattr(self.unfiltered(), 'dirstate')
2665
2661
2666 def invalidate(self, clearfilecache=False):
2662 def invalidate(self, clearfilecache=False):
2667 """Invalidates both store and non-store parts other than dirstate
2663 """Invalidates both store and non-store parts other than dirstate
2668
2664
2669 If a transaction is running, invalidation of store is omitted,
2665 If a transaction is running, invalidation of store is omitted,
2670 because discarding in-memory changes might cause inconsistency
2666 because discarding in-memory changes might cause inconsistency
2671 (e.g. incomplete fncache causes unintentional failure, but
2667 (e.g. incomplete fncache causes unintentional failure, but
2672 redundant one doesn't).
2668 redundant one doesn't).
2673 """
2669 """
2674 unfiltered = self.unfiltered() # all file caches are stored unfiltered
2670 unfiltered = self.unfiltered() # all file caches are stored unfiltered
2675 for k in list(self._filecache.keys()):
2671 for k in list(self._filecache.keys()):
2676 # dirstate is invalidated separately in invalidatedirstate()
2672 # dirstate is invalidated separately in invalidatedirstate()
2677 if k == b'dirstate':
2673 if k == b'dirstate':
2678 continue
2674 continue
2679 if (
2675 if (
2680 k == b'changelog'
2676 k == b'changelog'
2681 and self.currenttransaction()
2677 and self.currenttransaction()
2682 and self.changelog._delayed
2678 and self.changelog._delayed
2683 ):
2679 ):
2684 # The changelog object may store unwritten revisions. We don't
2680 # The changelog object may store unwritten revisions. We don't
2685 # want to lose them.
2681 # want to lose them.
2686 # TODO: Solve the problem instead of working around it.
2682 # TODO: Solve the problem instead of working around it.
2687 continue
2683 continue
2688
2684
2689 if clearfilecache:
2685 if clearfilecache:
2690 del self._filecache[k]
2686 del self._filecache[k]
2691 try:
2687 try:
2692 delattr(unfiltered, k)
2688 delattr(unfiltered, k)
2693 except AttributeError:
2689 except AttributeError:
2694 pass
2690 pass
2695 self.invalidatecaches()
2691 self.invalidatecaches()
2696 if not self.currenttransaction():
2692 if not self.currenttransaction():
2697 # TODO: Changing contents of store outside transaction
2693 # TODO: Changing contents of store outside transaction
2698 # causes inconsistency. We should make in-memory store
2694 # causes inconsistency. We should make in-memory store
2699 # changes detectable, and abort if changed.
2695 # changes detectable, and abort if changed.
2700 self.store.invalidatecaches()
2696 self.store.invalidatecaches()
2701
2697
2702 def invalidateall(self):
2698 def invalidateall(self):
2703 """Fully invalidates both store and non-store parts, causing the
2699 """Fully invalidates both store and non-store parts, causing the
2704 subsequent operation to reread any outside changes."""
2700 subsequent operation to reread any outside changes."""
2705 # extension should hook this to invalidate its caches
2701 # extension should hook this to invalidate its caches
2706 self.invalidate()
2702 self.invalidate()
2707 self.invalidatedirstate()
2703 self.invalidatedirstate()
2708
2704
2709 @unfilteredmethod
2705 @unfilteredmethod
2710 def _refreshfilecachestats(self, tr):
2706 def _refreshfilecachestats(self, tr):
2711 """Reload stats of cached files so that they are flagged as valid"""
2707 """Reload stats of cached files so that they are flagged as valid"""
2712 for k, ce in self._filecache.items():
2708 for k, ce in self._filecache.items():
2713 k = pycompat.sysstr(k)
2709 k = pycompat.sysstr(k)
2714 if k == 'dirstate' or k not in self.__dict__:
2710 if k == 'dirstate' or k not in self.__dict__:
2715 continue
2711 continue
2716 ce.refresh()
2712 ce.refresh()
2717
2713
2718 def _lock(
2714 def _lock(
2719 self,
2715 self,
2720 vfs,
2716 vfs,
2721 lockname,
2717 lockname,
2722 wait,
2718 wait,
2723 releasefn,
2719 releasefn,
2724 acquirefn,
2720 acquirefn,
2725 desc,
2721 desc,
2726 ):
2722 ):
2727 timeout = 0
2723 timeout = 0
2728 warntimeout = 0
2724 warntimeout = 0
2729 if wait:
2725 if wait:
2730 timeout = self.ui.configint(b"ui", b"timeout")
2726 timeout = self.ui.configint(b"ui", b"timeout")
2731 warntimeout = self.ui.configint(b"ui", b"timeout.warn")
2727 warntimeout = self.ui.configint(b"ui", b"timeout.warn")
2732 # internal config: ui.signal-safe-lock
2728 # internal config: ui.signal-safe-lock
2733 signalsafe = self.ui.configbool(b'ui', b'signal-safe-lock')
2729 signalsafe = self.ui.configbool(b'ui', b'signal-safe-lock')
2734
2730
2735 l = lockmod.trylock(
2731 l = lockmod.trylock(
2736 self.ui,
2732 self.ui,
2737 vfs,
2733 vfs,
2738 lockname,
2734 lockname,
2739 timeout,
2735 timeout,
2740 warntimeout,
2736 warntimeout,
2741 releasefn=releasefn,
2737 releasefn=releasefn,
2742 acquirefn=acquirefn,
2738 acquirefn=acquirefn,
2743 desc=desc,
2739 desc=desc,
2744 signalsafe=signalsafe,
2740 signalsafe=signalsafe,
2745 )
2741 )
2746 return l
2742 return l
2747
2743
2748 def _afterlock(self, callback):
2744 def _afterlock(self, callback):
2749 """add a callback to be run when the repository is fully unlocked
2745 """add a callback to be run when the repository is fully unlocked
2750
2746
2751 The callback will be executed when the outermost lock is released
2747 The callback will be executed when the outermost lock is released
2752 (with wlock being higher level than 'lock')."""
2748 (with wlock being higher level than 'lock')."""
2753 for ref in (self._wlockref, self._lockref):
2749 for ref in (self._wlockref, self._lockref):
2754 l = ref and ref()
2750 l = ref and ref()
2755 if l and l.held:
2751 if l and l.held:
2756 l.postrelease.append(callback)
2752 l.postrelease.append(callback)
2757 break
2753 break
2758 else: # no lock have been found.
2754 else: # no lock have been found.
2759 callback(True)
2755 callback(True)
2760
2756
2761 def lock(self, wait=True):
2757 def lock(self, wait=True):
2762 """Lock the repository store (.hg/store) and return a weak reference
2758 """Lock the repository store (.hg/store) and return a weak reference
2763 to the lock. Use this before modifying the store (e.g. committing or
2759 to the lock. Use this before modifying the store (e.g. committing or
2764 stripping). If you are opening a transaction, get a lock as well.)
2760 stripping). If you are opening a transaction, get a lock as well.)
2765
2761
2766 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2762 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2767 'wlock' first to avoid a dead-lock hazard."""
2763 'wlock' first to avoid a dead-lock hazard."""
2768 l = self._currentlock(self._lockref)
2764 l = self._currentlock(self._lockref)
2769 if l is not None:
2765 if l is not None:
2770 l.lock()
2766 l.lock()
2771 return l
2767 return l
2772
2768
2773 l = self._lock(
2769 l = self._lock(
2774 vfs=self.svfs,
2770 vfs=self.svfs,
2775 lockname=b"lock",
2771 lockname=b"lock",
2776 wait=wait,
2772 wait=wait,
2777 releasefn=None,
2773 releasefn=None,
2778 acquirefn=self.invalidate,
2774 acquirefn=self.invalidate,
2779 desc=_(b'repository %s') % self.origroot,
2775 desc=_(b'repository %s') % self.origroot,
2780 )
2776 )
2781 self._lockref = weakref.ref(l)
2777 self._lockref = weakref.ref(l)
2782 return l
2778 return l
2783
2779
2784 def wlock(self, wait=True):
2780 def wlock(self, wait=True):
2785 """Lock the non-store parts of the repository (everything under
2781 """Lock the non-store parts of the repository (everything under
2786 .hg except .hg/store) and return a weak reference to the lock.
2782 .hg except .hg/store) and return a weak reference to the lock.
2787
2783
2788 Use this before modifying files in .hg.
2784 Use this before modifying files in .hg.
2789
2785
2790 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2786 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2791 'wlock' first to avoid a dead-lock hazard."""
2787 'wlock' first to avoid a dead-lock hazard."""
2792 l = self._wlockref and self._wlockref()
2788 l = self._wlockref and self._wlockref()
2793 if l is not None and l.held:
2789 if l is not None and l.held:
2794 l.lock()
2790 l.lock()
2795 return l
2791 return l
2796
2792
2797 # We do not need to check for non-waiting lock acquisition. Such
2793 # We do not need to check for non-waiting lock acquisition. Such
2798 # acquisition would not cause dead-lock as they would just fail.
2794 # acquisition would not cause dead-lock as they would just fail.
2799 if wait and (
2795 if wait and (
2800 self.ui.configbool(b'devel', b'all-warnings')
2796 self.ui.configbool(b'devel', b'all-warnings')
2801 or self.ui.configbool(b'devel', b'check-locks')
2797 or self.ui.configbool(b'devel', b'check-locks')
2802 ):
2798 ):
2803 if self._currentlock(self._lockref) is not None:
2799 if self._currentlock(self._lockref) is not None:
2804 self.ui.develwarn(b'"wlock" acquired after "lock"')
2800 self.ui.develwarn(b'"wlock" acquired after "lock"')
2805
2801
2806 def unlock():
2802 def unlock():
2807 if self.dirstate.pendingparentchange():
2803 if self.dirstate.pendingparentchange():
2808 self.dirstate.invalidate()
2804 self.dirstate.invalidate()
2809 else:
2805 else:
2810 self.dirstate.write(None)
2806 self.dirstate.write(None)
2811
2807
2812 self._filecache[b'dirstate'].refresh()
2808 self._filecache[b'dirstate'].refresh()
2813
2809
2814 l = self._lock(
2810 l = self._lock(
2815 self.vfs,
2811 self.vfs,
2816 b"wlock",
2812 b"wlock",
2817 wait,
2813 wait,
2818 unlock,
2814 unlock,
2819 self.invalidatedirstate,
2815 self.invalidatedirstate,
2820 _(b'working directory of %s') % self.origroot,
2816 _(b'working directory of %s') % self.origroot,
2821 )
2817 )
2822 self._wlockref = weakref.ref(l)
2818 self._wlockref = weakref.ref(l)
2823 return l
2819 return l
2824
2820
2825 def _currentlock(self, lockref):
2821 def _currentlock(self, lockref):
2826 """Returns the lock if it's held, or None if it's not."""
2822 """Returns the lock if it's held, or None if it's not."""
2827 if lockref is None:
2823 if lockref is None:
2828 return None
2824 return None
2829 l = lockref()
2825 l = lockref()
2830 if l is None or not l.held:
2826 if l is None or not l.held:
2831 return None
2827 return None
2832 return l
2828 return l
2833
2829
2834 def currentwlock(self):
2830 def currentwlock(self):
2835 """Returns the wlock if it's held, or None if it's not."""
2831 """Returns the wlock if it's held, or None if it's not."""
2836 return self._currentlock(self._wlockref)
2832 return self._currentlock(self._wlockref)
2837
2833
2838 def checkcommitpatterns(self, wctx, match, status, fail):
2834 def checkcommitpatterns(self, wctx, match, status, fail):
2839 """check for commit arguments that aren't committable"""
2835 """check for commit arguments that aren't committable"""
2840 if match.isexact() or match.prefix():
2836 if match.isexact() or match.prefix():
2841 matched = set(status.modified + status.added + status.removed)
2837 matched = set(status.modified + status.added + status.removed)
2842
2838
2843 for f in match.files():
2839 for f in match.files():
2844 f = self.dirstate.normalize(f)
2840 f = self.dirstate.normalize(f)
2845 if f == b'.' or f in matched or f in wctx.substate:
2841 if f == b'.' or f in matched or f in wctx.substate:
2846 continue
2842 continue
2847 if f in status.deleted:
2843 if f in status.deleted:
2848 fail(f, _(b'file not found!'))
2844 fail(f, _(b'file not found!'))
2849 # Is it a directory that exists or used to exist?
2845 # Is it a directory that exists or used to exist?
2850 if self.wvfs.isdir(f) or wctx.p1().hasdir(f):
2846 if self.wvfs.isdir(f) or wctx.p1().hasdir(f):
2851 d = f + b'/'
2847 d = f + b'/'
2852 for mf in matched:
2848 for mf in matched:
2853 if mf.startswith(d):
2849 if mf.startswith(d):
2854 break
2850 break
2855 else:
2851 else:
2856 fail(f, _(b"no match under directory!"))
2852 fail(f, _(b"no match under directory!"))
2857 elif f not in self.dirstate:
2853 elif f not in self.dirstate:
2858 fail(f, _(b"file not tracked!"))
2854 fail(f, _(b"file not tracked!"))
2859
2855
2860 @unfilteredmethod
2856 @unfilteredmethod
2861 def commit(
2857 def commit(
2862 self,
2858 self,
2863 text=b"",
2859 text=b"",
2864 user=None,
2860 user=None,
2865 date=None,
2861 date=None,
2866 match=None,
2862 match=None,
2867 force=False,
2863 force=False,
2868 editor=None,
2864 editor=None,
2869 extra=None,
2865 extra=None,
2870 ):
2866 ):
2871 """Add a new revision to current repository.
2867 """Add a new revision to current repository.
2872
2868
2873 Revision information is gathered from the working directory,
2869 Revision information is gathered from the working directory,
2874 match can be used to filter the committed files. If editor is
2870 match can be used to filter the committed files. If editor is
2875 supplied, it is called to get a commit message.
2871 supplied, it is called to get a commit message.
2876 """
2872 """
2877 if extra is None:
2873 if extra is None:
2878 extra = {}
2874 extra = {}
2879
2875
2880 def fail(f, msg):
2876 def fail(f, msg):
2881 raise error.InputError(b'%s: %s' % (f, msg))
2877 raise error.InputError(b'%s: %s' % (f, msg))
2882
2878
2883 if not match:
2879 if not match:
2884 match = matchmod.always()
2880 match = matchmod.always()
2885
2881
2886 if not force:
2882 if not force:
2887 match.bad = fail
2883 match.bad = fail
2888
2884
2889 # lock() for recent changelog (see issue4368)
2885 # lock() for recent changelog (see issue4368)
2890 with self.wlock(), self.lock():
2886 with self.wlock(), self.lock():
2891 wctx = self[None]
2887 wctx = self[None]
2892 merge = len(wctx.parents()) > 1
2888 merge = len(wctx.parents()) > 1
2893
2889
2894 if not force and merge and not match.always():
2890 if not force and merge and not match.always():
2895 raise error.Abort(
2891 raise error.Abort(
2896 _(
2892 _(
2897 b'cannot partially commit a merge '
2893 b'cannot partially commit a merge '
2898 b'(do not specify files or patterns)'
2894 b'(do not specify files or patterns)'
2899 )
2895 )
2900 )
2896 )
2901
2897
2902 status = self.status(match=match, clean=force)
2898 status = self.status(match=match, clean=force)
2903 if force:
2899 if force:
2904 status.modified.extend(
2900 status.modified.extend(
2905 status.clean
2901 status.clean
2906 ) # mq may commit clean files
2902 ) # mq may commit clean files
2907
2903
2908 # check subrepos
2904 # check subrepos
2909 subs, commitsubs, newstate = subrepoutil.precommit(
2905 subs, commitsubs, newstate = subrepoutil.precommit(
2910 self.ui, wctx, status, match, force=force
2906 self.ui, wctx, status, match, force=force
2911 )
2907 )
2912
2908
2913 # make sure all explicit patterns are matched
2909 # make sure all explicit patterns are matched
2914 if not force:
2910 if not force:
2915 self.checkcommitpatterns(wctx, match, status, fail)
2911 self.checkcommitpatterns(wctx, match, status, fail)
2916
2912
2917 cctx = context.workingcommitctx(
2913 cctx = context.workingcommitctx(
2918 self, status, text, user, date, extra
2914 self, status, text, user, date, extra
2919 )
2915 )
2920
2916
2921 ms = mergestatemod.mergestate.read(self)
2917 ms = mergestatemod.mergestate.read(self)
2922 mergeutil.checkunresolved(ms)
2918 mergeutil.checkunresolved(ms)
2923
2919
2924 # internal config: ui.allowemptycommit
2920 # internal config: ui.allowemptycommit
2925 if cctx.isempty() and not self.ui.configbool(
2921 if cctx.isempty() and not self.ui.configbool(
2926 b'ui', b'allowemptycommit'
2922 b'ui', b'allowemptycommit'
2927 ):
2923 ):
2928 self.ui.debug(b'nothing to commit, clearing merge state\n')
2924 self.ui.debug(b'nothing to commit, clearing merge state\n')
2929 ms.reset()
2925 ms.reset()
2930 return None
2926 return None
2931
2927
2932 if merge and cctx.deleted():
2928 if merge and cctx.deleted():
2933 raise error.Abort(_(b"cannot commit merge with missing files"))
2929 raise error.Abort(_(b"cannot commit merge with missing files"))
2934
2930
2935 if editor:
2931 if editor:
2936 cctx._text = editor(self, cctx, subs)
2932 cctx._text = editor(self, cctx, subs)
2937 edited = text != cctx._text
2933 edited = text != cctx._text
2938
2934
2939 # Save commit message in case this transaction gets rolled back
2935 # Save commit message in case this transaction gets rolled back
2940 # (e.g. by a pretxncommit hook). Leave the content alone on
2936 # (e.g. by a pretxncommit hook). Leave the content alone on
2941 # the assumption that the user will use the same editor again.
2937 # the assumption that the user will use the same editor again.
2942 msgfn = self.savecommitmessage(cctx._text)
2938 msgfn = self.savecommitmessage(cctx._text)
2943
2939
2944 # commit subs and write new state
2940 # commit subs and write new state
2945 if subs:
2941 if subs:
2946 uipathfn = scmutil.getuipathfn(self)
2942 uipathfn = scmutil.getuipathfn(self)
2947 for s in sorted(commitsubs):
2943 for s in sorted(commitsubs):
2948 sub = wctx.sub(s)
2944 sub = wctx.sub(s)
2949 self.ui.status(
2945 self.ui.status(
2950 _(b'committing subrepository %s\n')
2946 _(b'committing subrepository %s\n')
2951 % uipathfn(subrepoutil.subrelpath(sub))
2947 % uipathfn(subrepoutil.subrelpath(sub))
2952 )
2948 )
2953 sr = sub.commit(cctx._text, user, date)
2949 sr = sub.commit(cctx._text, user, date)
2954 newstate[s] = (newstate[s][0], sr)
2950 newstate[s] = (newstate[s][0], sr)
2955 subrepoutil.writestate(self, newstate)
2951 subrepoutil.writestate(self, newstate)
2956
2952
2957 p1, p2 = self.dirstate.parents()
2953 p1, p2 = self.dirstate.parents()
2958 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or b'')
2954 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or b'')
2959 try:
2955 try:
2960 self.hook(
2956 self.hook(
2961 b"precommit", throw=True, parent1=hookp1, parent2=hookp2
2957 b"precommit", throw=True, parent1=hookp1, parent2=hookp2
2962 )
2958 )
2963 with self.transaction(b'commit'):
2959 with self.transaction(b'commit'):
2964 ret = self.commitctx(cctx, True)
2960 ret = self.commitctx(cctx, True)
2965 # update bookmarks, dirstate and mergestate
2961 # update bookmarks, dirstate and mergestate
2966 bookmarks.update(self, [p1, p2], ret)
2962 bookmarks.update(self, [p1, p2], ret)
2967 cctx.markcommitted(ret)
2963 cctx.markcommitted(ret)
2968 ms.reset()
2964 ms.reset()
2969 except: # re-raises
2965 except: # re-raises
2970 if edited:
2966 if edited:
2971 self.ui.write(
2967 self.ui.write(
2972 _(b'note: commit message saved in %s\n') % msgfn
2968 _(b'note: commit message saved in %s\n') % msgfn
2973 )
2969 )
2974 self.ui.write(
2970 self.ui.write(
2975 _(
2971 _(
2976 b"note: use 'hg commit --logfile "
2972 b"note: use 'hg commit --logfile "
2977 b".hg/last-message.txt --edit' to reuse it\n"
2973 b".hg/last-message.txt --edit' to reuse it\n"
2978 )
2974 )
2979 )
2975 )
2980 raise
2976 raise
2981
2977
2982 def commithook(unused_success):
2978 def commithook(unused_success):
2983 # hack for command that use a temporary commit (eg: histedit)
2979 # hack for command that use a temporary commit (eg: histedit)
2984 # temporary commit got stripped before hook release
2980 # temporary commit got stripped before hook release
2985 if self.changelog.hasnode(ret):
2981 if self.changelog.hasnode(ret):
2986 self.hook(
2982 self.hook(
2987 b"commit", node=hex(ret), parent1=hookp1, parent2=hookp2
2983 b"commit", node=hex(ret), parent1=hookp1, parent2=hookp2
2988 )
2984 )
2989
2985
2990 self._afterlock(commithook)
2986 self._afterlock(commithook)
2991 return ret
2987 return ret
2992
2988
2993 @unfilteredmethod
2989 @unfilteredmethod
2994 def commitctx(self, ctx, error=False, origctx=None):
2990 def commitctx(self, ctx, error=False, origctx=None):
2995 return commit.commitctx(self, ctx, error=error, origctx=origctx)
2991 return commit.commitctx(self, ctx, error=error, origctx=origctx)
2996
2992
2997 @unfilteredmethod
2993 @unfilteredmethod
2998 def destroying(self):
2994 def destroying(self):
2999 """Inform the repository that nodes are about to be destroyed.
2995 """Inform the repository that nodes are about to be destroyed.
3000 Intended for use by strip and rollback, so there's a common
2996 Intended for use by strip and rollback, so there's a common
3001 place for anything that has to be done before destroying history.
2997 place for anything that has to be done before destroying history.
3002
2998
3003 This is mostly useful for saving state that is in memory and waiting
2999 This is mostly useful for saving state that is in memory and waiting
3004 to be flushed when the current lock is released. Because a call to
3000 to be flushed when the current lock is released. Because a call to
3005 destroyed is imminent, the repo will be invalidated causing those
3001 destroyed is imminent, the repo will be invalidated causing those
3006 changes to stay in memory (waiting for the next unlock), or vanish
3002 changes to stay in memory (waiting for the next unlock), or vanish
3007 completely.
3003 completely.
3008 """
3004 """
3009 # When using the same lock to commit and strip, the phasecache is left
3005 # When using the same lock to commit and strip, the phasecache is left
3010 # dirty after committing. Then when we strip, the repo is invalidated,
3006 # dirty after committing. Then when we strip, the repo is invalidated,
3011 # causing those changes to disappear.
3007 # causing those changes to disappear.
3012 if '_phasecache' in vars(self):
3008 if '_phasecache' in vars(self):
3013 self._phasecache.write()
3009 self._phasecache.write()
3014
3010
3015 @unfilteredmethod
3011 @unfilteredmethod
3016 def destroyed(self):
3012 def destroyed(self):
3017 """Inform the repository that nodes have been destroyed.
3013 """Inform the repository that nodes have been destroyed.
3018 Intended for use by strip and rollback, so there's a common
3014 Intended for use by strip and rollback, so there's a common
3019 place for anything that has to be done after destroying history.
3015 place for anything that has to be done after destroying history.
3020 """
3016 """
3021 # When one tries to:
3017 # When one tries to:
3022 # 1) destroy nodes thus calling this method (e.g. strip)
3018 # 1) destroy nodes thus calling this method (e.g. strip)
3023 # 2) use phasecache somewhere (e.g. commit)
3019 # 2) use phasecache somewhere (e.g. commit)
3024 #
3020 #
3025 # then 2) will fail because the phasecache contains nodes that were
3021 # then 2) will fail because the phasecache contains nodes that were
3026 # removed. We can either remove phasecache from the filecache,
3022 # removed. We can either remove phasecache from the filecache,
3027 # causing it to reload next time it is accessed, or simply filter
3023 # causing it to reload next time it is accessed, or simply filter
3028 # the removed nodes now and write the updated cache.
3024 # the removed nodes now and write the updated cache.
3029 self._phasecache.filterunknown(self)
3025 self._phasecache.filterunknown(self)
3030 self._phasecache.write()
3026 self._phasecache.write()
3031
3027
3032 # refresh all repository caches
3028 # refresh all repository caches
3033 self.updatecaches()
3029 self.updatecaches()
3034
3030
3035 # Ensure the persistent tag cache is updated. Doing it now
3031 # Ensure the persistent tag cache is updated. Doing it now
3036 # means that the tag cache only has to worry about destroyed
3032 # means that the tag cache only has to worry about destroyed
3037 # heads immediately after a strip/rollback. That in turn
3033 # heads immediately after a strip/rollback. That in turn
3038 # guarantees that "cachetip == currenttip" (comparing both rev
3034 # guarantees that "cachetip == currenttip" (comparing both rev
3039 # and node) always means no nodes have been added or destroyed.
3035 # and node) always means no nodes have been added or destroyed.
3040
3036
3041 # XXX this is suboptimal when qrefresh'ing: we strip the current
3037 # XXX this is suboptimal when qrefresh'ing: we strip the current
3042 # head, refresh the tag cache, then immediately add a new head.
3038 # head, refresh the tag cache, then immediately add a new head.
3043 # But I think doing it this way is necessary for the "instant
3039 # But I think doing it this way is necessary for the "instant
3044 # tag cache retrieval" case to work.
3040 # tag cache retrieval" case to work.
3045 self.invalidate()
3041 self.invalidate()
3046
3042
3047 def status(
3043 def status(
3048 self,
3044 self,
3049 node1=b'.',
3045 node1=b'.',
3050 node2=None,
3046 node2=None,
3051 match=None,
3047 match=None,
3052 ignored=False,
3048 ignored=False,
3053 clean=False,
3049 clean=False,
3054 unknown=False,
3050 unknown=False,
3055 listsubrepos=False,
3051 listsubrepos=False,
3056 ):
3052 ):
3057 '''a convenience method that calls node1.status(node2)'''
3053 '''a convenience method that calls node1.status(node2)'''
3058 return self[node1].status(
3054 return self[node1].status(
3059 node2, match, ignored, clean, unknown, listsubrepos
3055 node2, match, ignored, clean, unknown, listsubrepos
3060 )
3056 )
3061
3057
3062 def addpostdsstatus(self, ps):
3058 def addpostdsstatus(self, ps):
3063 """Add a callback to run within the wlock, at the point at which status
3059 """Add a callback to run within the wlock, at the point at which status
3064 fixups happen.
3060 fixups happen.
3065
3061
3066 On status completion, callback(wctx, status) will be called with the
3062 On status completion, callback(wctx, status) will be called with the
3067 wlock held, unless the dirstate has changed from underneath or the wlock
3063 wlock held, unless the dirstate has changed from underneath or the wlock
3068 couldn't be grabbed.
3064 couldn't be grabbed.
3069
3065
3070 Callbacks should not capture and use a cached copy of the dirstate --
3066 Callbacks should not capture and use a cached copy of the dirstate --
3071 it might change in the meanwhile. Instead, they should access the
3067 it might change in the meanwhile. Instead, they should access the
3072 dirstate via wctx.repo().dirstate.
3068 dirstate via wctx.repo().dirstate.
3073
3069
3074 This list is emptied out after each status run -- extensions should
3070 This list is emptied out after each status run -- extensions should
3075 make sure it adds to this list each time dirstate.status is called.
3071 make sure it adds to this list each time dirstate.status is called.
3076 Extensions should also make sure they don't call this for statuses
3072 Extensions should also make sure they don't call this for statuses
3077 that don't involve the dirstate.
3073 that don't involve the dirstate.
3078 """
3074 """
3079
3075
3080 # The list is located here for uniqueness reasons -- it is actually
3076 # The list is located here for uniqueness reasons -- it is actually
3081 # managed by the workingctx, but that isn't unique per-repo.
3077 # managed by the workingctx, but that isn't unique per-repo.
3082 self._postdsstatus.append(ps)
3078 self._postdsstatus.append(ps)
3083
3079
3084 def postdsstatus(self):
3080 def postdsstatus(self):
3085 """Used by workingctx to get the list of post-dirstate-status hooks."""
3081 """Used by workingctx to get the list of post-dirstate-status hooks."""
3086 return self._postdsstatus
3082 return self._postdsstatus
3087
3083
3088 def clearpostdsstatus(self):
3084 def clearpostdsstatus(self):
3089 """Used by workingctx to clear post-dirstate-status hooks."""
3085 """Used by workingctx to clear post-dirstate-status hooks."""
3090 del self._postdsstatus[:]
3086 del self._postdsstatus[:]
3091
3087
3092 def heads(self, start=None):
3088 def heads(self, start=None):
3093 if start is None:
3089 if start is None:
3094 cl = self.changelog
3090 cl = self.changelog
3095 headrevs = reversed(cl.headrevs())
3091 headrevs = reversed(cl.headrevs())
3096 return [cl.node(rev) for rev in headrevs]
3092 return [cl.node(rev) for rev in headrevs]
3097
3093
3098 heads = self.changelog.heads(start)
3094 heads = self.changelog.heads(start)
3099 # sort the output in rev descending order
3095 # sort the output in rev descending order
3100 return sorted(heads, key=self.changelog.rev, reverse=True)
3096 return sorted(heads, key=self.changelog.rev, reverse=True)
3101
3097
3102 def branchheads(self, branch=None, start=None, closed=False):
3098 def branchheads(self, branch=None, start=None, closed=False):
3103 """return a (possibly filtered) list of heads for the given branch
3099 """return a (possibly filtered) list of heads for the given branch
3104
3100
3105 Heads are returned in topological order, from newest to oldest.
3101 Heads are returned in topological order, from newest to oldest.
3106 If branch is None, use the dirstate branch.
3102 If branch is None, use the dirstate branch.
3107 If start is not None, return only heads reachable from start.
3103 If start is not None, return only heads reachable from start.
3108 If closed is True, return heads that are marked as closed as well.
3104 If closed is True, return heads that are marked as closed as well.
3109 """
3105 """
3110 if branch is None:
3106 if branch is None:
3111 branch = self[None].branch()
3107 branch = self[None].branch()
3112 branches = self.branchmap()
3108 branches = self.branchmap()
3113 if not branches.hasbranch(branch):
3109 if not branches.hasbranch(branch):
3114 return []
3110 return []
3115 # the cache returns heads ordered lowest to highest
3111 # the cache returns heads ordered lowest to highest
3116 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
3112 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
3117 if start is not None:
3113 if start is not None:
3118 # filter out the heads that cannot be reached from startrev
3114 # filter out the heads that cannot be reached from startrev
3119 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
3115 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
3120 bheads = [h for h in bheads if h in fbheads]
3116 bheads = [h for h in bheads if h in fbheads]
3121 return bheads
3117 return bheads
3122
3118
3123 def branches(self, nodes):
3119 def branches(self, nodes):
3124 if not nodes:
3120 if not nodes:
3125 nodes = [self.changelog.tip()]
3121 nodes = [self.changelog.tip()]
3126 b = []
3122 b = []
3127 for n in nodes:
3123 for n in nodes:
3128 t = n
3124 t = n
3129 while True:
3125 while True:
3130 p = self.changelog.parents(n)
3126 p = self.changelog.parents(n)
3131 if p[1] != nullid or p[0] == nullid:
3127 if p[1] != nullid or p[0] == nullid:
3132 b.append((t, n, p[0], p[1]))
3128 b.append((t, n, p[0], p[1]))
3133 break
3129 break
3134 n = p[0]
3130 n = p[0]
3135 return b
3131 return b
3136
3132
3137 def between(self, pairs):
3133 def between(self, pairs):
3138 r = []
3134 r = []
3139
3135
3140 for top, bottom in pairs:
3136 for top, bottom in pairs:
3141 n, l, i = top, [], 0
3137 n, l, i = top, [], 0
3142 f = 1
3138 f = 1
3143
3139
3144 while n != bottom and n != nullid:
3140 while n != bottom and n != nullid:
3145 p = self.changelog.parents(n)[0]
3141 p = self.changelog.parents(n)[0]
3146 if i == f:
3142 if i == f:
3147 l.append(n)
3143 l.append(n)
3148 f = f * 2
3144 f = f * 2
3149 n = p
3145 n = p
3150 i += 1
3146 i += 1
3151
3147
3152 r.append(l)
3148 r.append(l)
3153
3149
3154 return r
3150 return r
3155
3151
3156 def checkpush(self, pushop):
3152 def checkpush(self, pushop):
3157 """Extensions can override this function if additional checks have
3153 """Extensions can override this function if additional checks have
3158 to be performed before pushing, or call it if they override push
3154 to be performed before pushing, or call it if they override push
3159 command.
3155 command.
3160 """
3156 """
3161
3157
3162 @unfilteredpropertycache
3158 @unfilteredpropertycache
3163 def prepushoutgoinghooks(self):
3159 def prepushoutgoinghooks(self):
3164 """Return util.hooks consists of a pushop with repo, remote, outgoing
3160 """Return util.hooks consists of a pushop with repo, remote, outgoing
3165 methods, which are called before pushing changesets.
3161 methods, which are called before pushing changesets.
3166 """
3162 """
3167 return util.hooks()
3163 return util.hooks()
3168
3164
3169 def pushkey(self, namespace, key, old, new):
3165 def pushkey(self, namespace, key, old, new):
3170 try:
3166 try:
3171 tr = self.currenttransaction()
3167 tr = self.currenttransaction()
3172 hookargs = {}
3168 hookargs = {}
3173 if tr is not None:
3169 if tr is not None:
3174 hookargs.update(tr.hookargs)
3170 hookargs.update(tr.hookargs)
3175 hookargs = pycompat.strkwargs(hookargs)
3171 hookargs = pycompat.strkwargs(hookargs)
3176 hookargs['namespace'] = namespace
3172 hookargs['namespace'] = namespace
3177 hookargs['key'] = key
3173 hookargs['key'] = key
3178 hookargs['old'] = old
3174 hookargs['old'] = old
3179 hookargs['new'] = new
3175 hookargs['new'] = new
3180 self.hook(b'prepushkey', throw=True, **hookargs)
3176 self.hook(b'prepushkey', throw=True, **hookargs)
3181 except error.HookAbort as exc:
3177 except error.HookAbort as exc:
3182 self.ui.write_err(_(b"pushkey-abort: %s\n") % exc)
3178 self.ui.write_err(_(b"pushkey-abort: %s\n") % exc)
3183 if exc.hint:
3179 if exc.hint:
3184 self.ui.write_err(_(b"(%s)\n") % exc.hint)
3180 self.ui.write_err(_(b"(%s)\n") % exc.hint)
3185 return False
3181 return False
3186 self.ui.debug(b'pushing key for "%s:%s"\n' % (namespace, key))
3182 self.ui.debug(b'pushing key for "%s:%s"\n' % (namespace, key))
3187 ret = pushkey.push(self, namespace, key, old, new)
3183 ret = pushkey.push(self, namespace, key, old, new)
3188
3184
3189 def runhook(unused_success):
3185 def runhook(unused_success):
3190 self.hook(
3186 self.hook(
3191 b'pushkey',
3187 b'pushkey',
3192 namespace=namespace,
3188 namespace=namespace,
3193 key=key,
3189 key=key,
3194 old=old,
3190 old=old,
3195 new=new,
3191 new=new,
3196 ret=ret,
3192 ret=ret,
3197 )
3193 )
3198
3194
3199 self._afterlock(runhook)
3195 self._afterlock(runhook)
3200 return ret
3196 return ret
3201
3197
3202 def listkeys(self, namespace):
3198 def listkeys(self, namespace):
3203 self.hook(b'prelistkeys', throw=True, namespace=namespace)
3199 self.hook(b'prelistkeys', throw=True, namespace=namespace)
3204 self.ui.debug(b'listing keys for "%s"\n' % namespace)
3200 self.ui.debug(b'listing keys for "%s"\n' % namespace)
3205 values = pushkey.list(self, namespace)
3201 values = pushkey.list(self, namespace)
3206 self.hook(b'listkeys', namespace=namespace, values=values)
3202 self.hook(b'listkeys', namespace=namespace, values=values)
3207 return values
3203 return values
3208
3204
3209 def debugwireargs(self, one, two, three=None, four=None, five=None):
3205 def debugwireargs(self, one, two, three=None, four=None, five=None):
3210 '''used to test argument passing over the wire'''
3206 '''used to test argument passing over the wire'''
3211 return b"%s %s %s %s %s" % (
3207 return b"%s %s %s %s %s" % (
3212 one,
3208 one,
3213 two,
3209 two,
3214 pycompat.bytestr(three),
3210 pycompat.bytestr(three),
3215 pycompat.bytestr(four),
3211 pycompat.bytestr(four),
3216 pycompat.bytestr(five),
3212 pycompat.bytestr(five),
3217 )
3213 )
3218
3214
3219 def savecommitmessage(self, text):
3215 def savecommitmessage(self, text):
3220 fp = self.vfs(b'last-message.txt', b'wb')
3216 fp = self.vfs(b'last-message.txt', b'wb')
3221 try:
3217 try:
3222 fp.write(text)
3218 fp.write(text)
3223 finally:
3219 finally:
3224 fp.close()
3220 fp.close()
3225 return self.pathto(fp.name[len(self.root) + 1 :])
3221 return self.pathto(fp.name[len(self.root) + 1 :])
3226
3222
3227
3223
3228 # used to avoid circular references so destructors work
3224 # used to avoid circular references so destructors work
3229 def aftertrans(files):
3225 def aftertrans(files):
3230 renamefiles = [tuple(t) for t in files]
3226 renamefiles = [tuple(t) for t in files]
3231
3227
3232 def a():
3228 def a():
3233 for vfs, src, dest in renamefiles:
3229 for vfs, src, dest in renamefiles:
3234 # if src and dest refer to a same file, vfs.rename is a no-op,
3230 # if src and dest refer to a same file, vfs.rename is a no-op,
3235 # leaving both src and dest on disk. delete dest to make sure
3231 # leaving both src and dest on disk. delete dest to make sure
3236 # the rename couldn't be such a no-op.
3232 # the rename couldn't be such a no-op.
3237 vfs.tryunlink(dest)
3233 vfs.tryunlink(dest)
3238 try:
3234 try:
3239 vfs.rename(src, dest)
3235 vfs.rename(src, dest)
3240 except OSError: # journal file does not yet exist
3236 except OSError: # journal file does not yet exist
3241 pass
3237 pass
3242
3238
3243 return a
3239 return a
3244
3240
3245
3241
3246 def undoname(fn):
3242 def undoname(fn):
3247 base, name = os.path.split(fn)
3243 base, name = os.path.split(fn)
3248 assert name.startswith(b'journal')
3244 assert name.startswith(b'journal')
3249 return os.path.join(base, name.replace(b'journal', b'undo', 1))
3245 return os.path.join(base, name.replace(b'journal', b'undo', 1))
3250
3246
3251
3247
3252 def instance(ui, path, create, intents=None, createopts=None):
3248 def instance(ui, path, create, intents=None, createopts=None):
3253 localpath = util.urllocalpath(path)
3249 localpath = util.urllocalpath(path)
3254 if create:
3250 if create:
3255 createrepository(ui, localpath, createopts=createopts)
3251 createrepository(ui, localpath, createopts=createopts)
3256
3252
3257 return makelocalrepository(ui, localpath, intents=intents)
3253 return makelocalrepository(ui, localpath, intents=intents)
3258
3254
3259
3255
3260 def islocal(path):
3256 def islocal(path):
3261 return True
3257 return True
3262
3258
3263
3259
3264 def defaultcreateopts(ui, createopts=None):
3260 def defaultcreateopts(ui, createopts=None):
3265 """Populate the default creation options for a repository.
3261 """Populate the default creation options for a repository.
3266
3262
3267 A dictionary of explicitly requested creation options can be passed
3263 A dictionary of explicitly requested creation options can be passed
3268 in. Missing keys will be populated.
3264 in. Missing keys will be populated.
3269 """
3265 """
3270 createopts = dict(createopts or {})
3266 createopts = dict(createopts or {})
3271
3267
3272 if b'backend' not in createopts:
3268 if b'backend' not in createopts:
3273 # experimental config: storage.new-repo-backend
3269 # experimental config: storage.new-repo-backend
3274 createopts[b'backend'] = ui.config(b'storage', b'new-repo-backend')
3270 createopts[b'backend'] = ui.config(b'storage', b'new-repo-backend')
3275
3271
3276 return createopts
3272 return createopts
3277
3273
3278
3274
3279 def newreporequirements(ui, createopts):
3275 def newreporequirements(ui, createopts):
3280 """Determine the set of requirements for a new local repository.
3276 """Determine the set of requirements for a new local repository.
3281
3277
3282 Extensions can wrap this function to specify custom requirements for
3278 Extensions can wrap this function to specify custom requirements for
3283 new repositories.
3279 new repositories.
3284 """
3280 """
3285 # If the repo is being created from a shared repository, we copy
3281 # If the repo is being created from a shared repository, we copy
3286 # its requirements.
3282 # its requirements.
3287 if b'sharedrepo' in createopts:
3283 if b'sharedrepo' in createopts:
3288 requirements = set(createopts[b'sharedrepo'].requirements)
3284 requirements = set(createopts[b'sharedrepo'].requirements)
3289 if createopts.get(b'sharedrelative'):
3285 if createopts.get(b'sharedrelative'):
3290 requirements.add(requirementsmod.RELATIVE_SHARED_REQUIREMENT)
3286 requirements.add(requirementsmod.RELATIVE_SHARED_REQUIREMENT)
3291 else:
3287 else:
3292 requirements.add(requirementsmod.SHARED_REQUIREMENT)
3288 requirements.add(requirementsmod.SHARED_REQUIREMENT)
3293
3289
3294 return requirements
3290 return requirements
3295
3291
3296 if b'backend' not in createopts:
3292 if b'backend' not in createopts:
3297 raise error.ProgrammingError(
3293 raise error.ProgrammingError(
3298 b'backend key not present in createopts; '
3294 b'backend key not present in createopts; '
3299 b'was defaultcreateopts() called?'
3295 b'was defaultcreateopts() called?'
3300 )
3296 )
3301
3297
3302 if createopts[b'backend'] != b'revlogv1':
3298 if createopts[b'backend'] != b'revlogv1':
3303 raise error.Abort(
3299 raise error.Abort(
3304 _(
3300 _(
3305 b'unable to determine repository requirements for '
3301 b'unable to determine repository requirements for '
3306 b'storage backend: %s'
3302 b'storage backend: %s'
3307 )
3303 )
3308 % createopts[b'backend']
3304 % createopts[b'backend']
3309 )
3305 )
3310
3306
3311 requirements = {b'revlogv1'}
3307 requirements = {b'revlogv1'}
3312 if ui.configbool(b'format', b'usestore'):
3308 if ui.configbool(b'format', b'usestore'):
3313 requirements.add(b'store')
3309 requirements.add(b'store')
3314 if ui.configbool(b'format', b'usefncache'):
3310 if ui.configbool(b'format', b'usefncache'):
3315 requirements.add(b'fncache')
3311 requirements.add(b'fncache')
3316 if ui.configbool(b'format', b'dotencode'):
3312 if ui.configbool(b'format', b'dotencode'):
3317 requirements.add(b'dotencode')
3313 requirements.add(b'dotencode')
3318
3314
3319 compengines = ui.configlist(b'format', b'revlog-compression')
3315 compengines = ui.configlist(b'format', b'revlog-compression')
3320 for compengine in compengines:
3316 for compengine in compengines:
3321 if compengine in util.compengines:
3317 if compengine in util.compengines:
3322 break
3318 break
3323 else:
3319 else:
3324 raise error.Abort(
3320 raise error.Abort(
3325 _(
3321 _(
3326 b'compression engines %s defined by '
3322 b'compression engines %s defined by '
3327 b'format.revlog-compression not available'
3323 b'format.revlog-compression not available'
3328 )
3324 )
3329 % b', '.join(b'"%s"' % e for e in compengines),
3325 % b', '.join(b'"%s"' % e for e in compengines),
3330 hint=_(
3326 hint=_(
3331 b'run "hg debuginstall" to list available '
3327 b'run "hg debuginstall" to list available '
3332 b'compression engines'
3328 b'compression engines'
3333 ),
3329 ),
3334 )
3330 )
3335
3331
3336 # zlib is the historical default and doesn't need an explicit requirement.
3332 # zlib is the historical default and doesn't need an explicit requirement.
3337 if compengine == b'zstd':
3333 if compengine == b'zstd':
3338 requirements.add(b'revlog-compression-zstd')
3334 requirements.add(b'revlog-compression-zstd')
3339 elif compengine != b'zlib':
3335 elif compengine != b'zlib':
3340 requirements.add(b'exp-compression-%s' % compengine)
3336 requirements.add(b'exp-compression-%s' % compengine)
3341
3337
3342 if scmutil.gdinitconfig(ui):
3338 if scmutil.gdinitconfig(ui):
3343 requirements.add(b'generaldelta')
3339 requirements.add(b'generaldelta')
3344 if ui.configbool(b'format', b'sparse-revlog'):
3340 if ui.configbool(b'format', b'sparse-revlog'):
3345 requirements.add(requirementsmod.SPARSEREVLOG_REQUIREMENT)
3341 requirements.add(requirementsmod.SPARSEREVLOG_REQUIREMENT)
3346
3342
3347 # experimental config: format.exp-use-side-data
3343 # experimental config: format.exp-use-side-data
3348 if ui.configbool(b'format', b'exp-use-side-data'):
3344 if ui.configbool(b'format', b'exp-use-side-data'):
3349 requirements.add(requirementsmod.SIDEDATA_REQUIREMENT)
3345 requirements.add(requirementsmod.SIDEDATA_REQUIREMENT)
3350 # experimental config: format.exp-use-copies-side-data-changeset
3346 # experimental config: format.exp-use-copies-side-data-changeset
3351 if ui.configbool(b'format', b'exp-use-copies-side-data-changeset'):
3347 if ui.configbool(b'format', b'exp-use-copies-side-data-changeset'):
3352 requirements.add(requirementsmod.SIDEDATA_REQUIREMENT)
3348 requirements.add(requirementsmod.SIDEDATA_REQUIREMENT)
3353 requirements.add(requirementsmod.COPIESSDC_REQUIREMENT)
3349 requirements.add(requirementsmod.COPIESSDC_REQUIREMENT)
3354 if ui.configbool(b'experimental', b'treemanifest'):
3350 if ui.configbool(b'experimental', b'treemanifest'):
3355 requirements.add(requirementsmod.TREEMANIFEST_REQUIREMENT)
3351 requirements.add(requirementsmod.TREEMANIFEST_REQUIREMENT)
3356
3352
3357 revlogv2 = ui.config(b'experimental', b'revlogv2')
3353 revlogv2 = ui.config(b'experimental', b'revlogv2')
3358 if revlogv2 == b'enable-unstable-format-and-corrupt-my-data':
3354 if revlogv2 == b'enable-unstable-format-and-corrupt-my-data':
3359 requirements.remove(b'revlogv1')
3355 requirements.remove(b'revlogv1')
3360 # generaldelta is implied by revlogv2.
3356 # generaldelta is implied by revlogv2.
3361 requirements.discard(b'generaldelta')
3357 requirements.discard(b'generaldelta')
3362 requirements.add(requirementsmod.REVLOGV2_REQUIREMENT)
3358 requirements.add(requirementsmod.REVLOGV2_REQUIREMENT)
3363 # experimental config: format.internal-phase
3359 # experimental config: format.internal-phase
3364 if ui.configbool(b'format', b'internal-phase'):
3360 if ui.configbool(b'format', b'internal-phase'):
3365 requirements.add(requirementsmod.INTERNAL_PHASE_REQUIREMENT)
3361 requirements.add(requirementsmod.INTERNAL_PHASE_REQUIREMENT)
3366
3362
3367 if createopts.get(b'narrowfiles'):
3363 if createopts.get(b'narrowfiles'):
3368 requirements.add(requirementsmod.NARROW_REQUIREMENT)
3364 requirements.add(requirementsmod.NARROW_REQUIREMENT)
3369
3365
3370 if createopts.get(b'lfs'):
3366 if createopts.get(b'lfs'):
3371 requirements.add(b'lfs')
3367 requirements.add(b'lfs')
3372
3368
3373 if ui.configbool(b'format', b'bookmarks-in-store'):
3369 if ui.configbool(b'format', b'bookmarks-in-store'):
3374 requirements.add(bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT)
3370 requirements.add(bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT)
3375
3371
3376 if ui.configbool(b'format', b'use-persistent-nodemap'):
3372 if ui.configbool(b'format', b'use-persistent-nodemap'):
3377 requirements.add(requirementsmod.NODEMAP_REQUIREMENT)
3373 requirements.add(requirementsmod.NODEMAP_REQUIREMENT)
3378
3374
3379 # if share-safe is enabled, let's create the new repository with the new
3375 # if share-safe is enabled, let's create the new repository with the new
3380 # requirement
3376 # requirement
3381 if ui.configbool(b'format', b'exp-share-safe'):
3377 if ui.configbool(b'format', b'exp-share-safe'):
3382 requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
3378 requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
3383
3379
3384 return requirements
3380 return requirements
3385
3381
3386
3382
3387 def checkrequirementscompat(ui, requirements):
3383 def checkrequirementscompat(ui, requirements):
3388 """Checks compatibility of repository requirements enabled and disabled.
3384 """Checks compatibility of repository requirements enabled and disabled.
3389
3385
3390 Returns a set of requirements which needs to be dropped because dependend
3386 Returns a set of requirements which needs to be dropped because dependend
3391 requirements are not enabled. Also warns users about it"""
3387 requirements are not enabled. Also warns users about it"""
3392
3388
3393 dropped = set()
3389 dropped = set()
3394
3390
3395 if b'store' not in requirements:
3391 if b'store' not in requirements:
3396 if bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT in requirements:
3392 if bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT in requirements:
3397 ui.warn(
3393 ui.warn(
3398 _(
3394 _(
3399 b'ignoring enabled \'format.bookmarks-in-store\' config '
3395 b'ignoring enabled \'format.bookmarks-in-store\' config '
3400 b'beacuse it is incompatible with disabled '
3396 b'beacuse it is incompatible with disabled '
3401 b'\'format.usestore\' config\n'
3397 b'\'format.usestore\' config\n'
3402 )
3398 )
3403 )
3399 )
3404 dropped.add(bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT)
3400 dropped.add(bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT)
3405
3401
3406 if (
3402 if (
3407 requirementsmod.SHARED_REQUIREMENT in requirements
3403 requirementsmod.SHARED_REQUIREMENT in requirements
3408 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
3404 or requirementsmod.RELATIVE_SHARED_REQUIREMENT in requirements
3409 ):
3405 ):
3410 raise error.Abort(
3406 raise error.Abort(
3411 _(
3407 _(
3412 b"cannot create shared repository as source was created"
3408 b"cannot create shared repository as source was created"
3413 b" with 'format.usestore' config disabled"
3409 b" with 'format.usestore' config disabled"
3414 )
3410 )
3415 )
3411 )
3416
3412
3417 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
3413 if requirementsmod.SHARESAFE_REQUIREMENT in requirements:
3418 ui.warn(
3414 ui.warn(
3419 _(
3415 _(
3420 b"ignoring enabled 'format.exp-share-safe' config because "
3416 b"ignoring enabled 'format.exp-share-safe' config because "
3421 b"it is incompatible with disabled 'format.usestore'"
3417 b"it is incompatible with disabled 'format.usestore'"
3422 b" config\n"
3418 b" config\n"
3423 )
3419 )
3424 )
3420 )
3425 dropped.add(requirementsmod.SHARESAFE_REQUIREMENT)
3421 dropped.add(requirementsmod.SHARESAFE_REQUIREMENT)
3426
3422
3427 return dropped
3423 return dropped
3428
3424
3429
3425
3430 def filterknowncreateopts(ui, createopts):
3426 def filterknowncreateopts(ui, createopts):
3431 """Filters a dict of repo creation options against options that are known.
3427 """Filters a dict of repo creation options against options that are known.
3432
3428
3433 Receives a dict of repo creation options and returns a dict of those
3429 Receives a dict of repo creation options and returns a dict of those
3434 options that we don't know how to handle.
3430 options that we don't know how to handle.
3435
3431
3436 This function is called as part of repository creation. If the
3432 This function is called as part of repository creation. If the
3437 returned dict contains any items, repository creation will not
3433 returned dict contains any items, repository creation will not
3438 be allowed, as it means there was a request to create a repository
3434 be allowed, as it means there was a request to create a repository
3439 with options not recognized by loaded code.
3435 with options not recognized by loaded code.
3440
3436
3441 Extensions can wrap this function to filter out creation options
3437 Extensions can wrap this function to filter out creation options
3442 they know how to handle.
3438 they know how to handle.
3443 """
3439 """
3444 known = {
3440 known = {
3445 b'backend',
3441 b'backend',
3446 b'lfs',
3442 b'lfs',
3447 b'narrowfiles',
3443 b'narrowfiles',
3448 b'sharedrepo',
3444 b'sharedrepo',
3449 b'sharedrelative',
3445 b'sharedrelative',
3450 b'shareditems',
3446 b'shareditems',
3451 b'shallowfilestore',
3447 b'shallowfilestore',
3452 }
3448 }
3453
3449
3454 return {k: v for k, v in createopts.items() if k not in known}
3450 return {k: v for k, v in createopts.items() if k not in known}
3455
3451
3456
3452
3457 def createrepository(ui, path, createopts=None):
3453 def createrepository(ui, path, createopts=None):
3458 """Create a new repository in a vfs.
3454 """Create a new repository in a vfs.
3459
3455
3460 ``path`` path to the new repo's working directory.
3456 ``path`` path to the new repo's working directory.
3461 ``createopts`` options for the new repository.
3457 ``createopts`` options for the new repository.
3462
3458
3463 The following keys for ``createopts`` are recognized:
3459 The following keys for ``createopts`` are recognized:
3464
3460
3465 backend
3461 backend
3466 The storage backend to use.
3462 The storage backend to use.
3467 lfs
3463 lfs
3468 Repository will be created with ``lfs`` requirement. The lfs extension
3464 Repository will be created with ``lfs`` requirement. The lfs extension
3469 will automatically be loaded when the repository is accessed.
3465 will automatically be loaded when the repository is accessed.
3470 narrowfiles
3466 narrowfiles
3471 Set up repository to support narrow file storage.
3467 Set up repository to support narrow file storage.
3472 sharedrepo
3468 sharedrepo
3473 Repository object from which storage should be shared.
3469 Repository object from which storage should be shared.
3474 sharedrelative
3470 sharedrelative
3475 Boolean indicating if the path to the shared repo should be
3471 Boolean indicating if the path to the shared repo should be
3476 stored as relative. By default, the pointer to the "parent" repo
3472 stored as relative. By default, the pointer to the "parent" repo
3477 is stored as an absolute path.
3473 is stored as an absolute path.
3478 shareditems
3474 shareditems
3479 Set of items to share to the new repository (in addition to storage).
3475 Set of items to share to the new repository (in addition to storage).
3480 shallowfilestore
3476 shallowfilestore
3481 Indicates that storage for files should be shallow (not all ancestor
3477 Indicates that storage for files should be shallow (not all ancestor
3482 revisions are known).
3478 revisions are known).
3483 """
3479 """
3484 createopts = defaultcreateopts(ui, createopts=createopts)
3480 createopts = defaultcreateopts(ui, createopts=createopts)
3485
3481
3486 unknownopts = filterknowncreateopts(ui, createopts)
3482 unknownopts = filterknowncreateopts(ui, createopts)
3487
3483
3488 if not isinstance(unknownopts, dict):
3484 if not isinstance(unknownopts, dict):
3489 raise error.ProgrammingError(
3485 raise error.ProgrammingError(
3490 b'filterknowncreateopts() did not return a dict'
3486 b'filterknowncreateopts() did not return a dict'
3491 )
3487 )
3492
3488
3493 if unknownopts:
3489 if unknownopts:
3494 raise error.Abort(
3490 raise error.Abort(
3495 _(
3491 _(
3496 b'unable to create repository because of unknown '
3492 b'unable to create repository because of unknown '
3497 b'creation option: %s'
3493 b'creation option: %s'
3498 )
3494 )
3499 % b', '.join(sorted(unknownopts)),
3495 % b', '.join(sorted(unknownopts)),
3500 hint=_(b'is a required extension not loaded?'),
3496 hint=_(b'is a required extension not loaded?'),
3501 )
3497 )
3502
3498
3503 requirements = newreporequirements(ui, createopts=createopts)
3499 requirements = newreporequirements(ui, createopts=createopts)
3504 requirements -= checkrequirementscompat(ui, requirements)
3500 requirements -= checkrequirementscompat(ui, requirements)
3505
3501
3506 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
3502 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
3507
3503
3508 hgvfs = vfsmod.vfs(wdirvfs.join(b'.hg'))
3504 hgvfs = vfsmod.vfs(wdirvfs.join(b'.hg'))
3509 if hgvfs.exists():
3505 if hgvfs.exists():
3510 raise error.RepoError(_(b'repository %s already exists') % path)
3506 raise error.RepoError(_(b'repository %s already exists') % path)
3511
3507
3512 if b'sharedrepo' in createopts:
3508 if b'sharedrepo' in createopts:
3513 sharedpath = createopts[b'sharedrepo'].sharedpath
3509 sharedpath = createopts[b'sharedrepo'].sharedpath
3514
3510
3515 if createopts.get(b'sharedrelative'):
3511 if createopts.get(b'sharedrelative'):
3516 try:
3512 try:
3517 sharedpath = os.path.relpath(sharedpath, hgvfs.base)
3513 sharedpath = os.path.relpath(sharedpath, hgvfs.base)
3518 except (IOError, ValueError) as e:
3514 except (IOError, ValueError) as e:
3519 # ValueError is raised on Windows if the drive letters differ
3515 # ValueError is raised on Windows if the drive letters differ
3520 # on each path.
3516 # on each path.
3521 raise error.Abort(
3517 raise error.Abort(
3522 _(b'cannot calculate relative path'),
3518 _(b'cannot calculate relative path'),
3523 hint=stringutil.forcebytestr(e),
3519 hint=stringutil.forcebytestr(e),
3524 )
3520 )
3525
3521
3526 if not wdirvfs.exists():
3522 if not wdirvfs.exists():
3527 wdirvfs.makedirs()
3523 wdirvfs.makedirs()
3528
3524
3529 hgvfs.makedir(notindexed=True)
3525 hgvfs.makedir(notindexed=True)
3530 if b'sharedrepo' not in createopts:
3526 if b'sharedrepo' not in createopts:
3531 hgvfs.mkdir(b'cache')
3527 hgvfs.mkdir(b'cache')
3532 hgvfs.mkdir(b'wcache')
3528 hgvfs.mkdir(b'wcache')
3533
3529
3534 if b'store' in requirements and b'sharedrepo' not in createopts:
3530 if b'store' in requirements and b'sharedrepo' not in createopts:
3535 hgvfs.mkdir(b'store')
3531 hgvfs.mkdir(b'store')
3536
3532
3537 # We create an invalid changelog outside the store so very old
3533 # We create an invalid changelog outside the store so very old
3538 # Mercurial versions (which didn't know about the requirements
3534 # Mercurial versions (which didn't know about the requirements
3539 # file) encounter an error on reading the changelog. This
3535 # file) encounter an error on reading the changelog. This
3540 # effectively locks out old clients and prevents them from
3536 # effectively locks out old clients and prevents them from
3541 # mucking with a repo in an unknown format.
3537 # mucking with a repo in an unknown format.
3542 #
3538 #
3543 # The revlog header has version 2, which won't be recognized by
3539 # The revlog header has version 2, which won't be recognized by
3544 # such old clients.
3540 # such old clients.
3545 hgvfs.append(
3541 hgvfs.append(
3546 b'00changelog.i',
3542 b'00changelog.i',
3547 b'\0\0\0\2 dummy changelog to prevent using the old repo '
3543 b'\0\0\0\2 dummy changelog to prevent using the old repo '
3548 b'layout',
3544 b'layout',
3549 )
3545 )
3550
3546
3551 # Filter the requirements into working copy and store ones
3547 # Filter the requirements into working copy and store ones
3552 wcreq, storereq = scmutil.filterrequirements(requirements)
3548 wcreq, storereq = scmutil.filterrequirements(requirements)
3553 # write working copy ones
3549 # write working copy ones
3554 scmutil.writerequires(hgvfs, wcreq)
3550 scmutil.writerequires(hgvfs, wcreq)
3555 # If there are store requirements and the current repository
3551 # If there are store requirements and the current repository
3556 # is not a shared one, write stored requirements
3552 # is not a shared one, write stored requirements
3557 # For new shared repository, we don't need to write the store
3553 # For new shared repository, we don't need to write the store
3558 # requirements as they are already present in store requires
3554 # requirements as they are already present in store requires
3559 if storereq and b'sharedrepo' not in createopts:
3555 if storereq and b'sharedrepo' not in createopts:
3560 storevfs = vfsmod.vfs(hgvfs.join(b'store'), cacheaudited=True)
3556 storevfs = vfsmod.vfs(hgvfs.join(b'store'), cacheaudited=True)
3561 scmutil.writerequires(storevfs, storereq)
3557 scmutil.writerequires(storevfs, storereq)
3562
3558
3563 # Write out file telling readers where to find the shared store.
3559 # Write out file telling readers where to find the shared store.
3564 if b'sharedrepo' in createopts:
3560 if b'sharedrepo' in createopts:
3565 hgvfs.write(b'sharedpath', sharedpath)
3561 hgvfs.write(b'sharedpath', sharedpath)
3566
3562
3567 if createopts.get(b'shareditems'):
3563 if createopts.get(b'shareditems'):
3568 shared = b'\n'.join(sorted(createopts[b'shareditems'])) + b'\n'
3564 shared = b'\n'.join(sorted(createopts[b'shareditems'])) + b'\n'
3569 hgvfs.write(b'shared', shared)
3565 hgvfs.write(b'shared', shared)
3570
3566
3571
3567
3572 def poisonrepository(repo):
3568 def poisonrepository(repo):
3573 """Poison a repository instance so it can no longer be used."""
3569 """Poison a repository instance so it can no longer be used."""
3574 # Perform any cleanup on the instance.
3570 # Perform any cleanup on the instance.
3575 repo.close()
3571 repo.close()
3576
3572
3577 # Our strategy is to replace the type of the object with one that
3573 # Our strategy is to replace the type of the object with one that
3578 # has all attribute lookups result in error.
3574 # has all attribute lookups result in error.
3579 #
3575 #
3580 # But we have to allow the close() method because some constructors
3576 # But we have to allow the close() method because some constructors
3581 # of repos call close() on repo references.
3577 # of repos call close() on repo references.
3582 class poisonedrepository(object):
3578 class poisonedrepository(object):
3583 def __getattribute__(self, item):
3579 def __getattribute__(self, item):
3584 if item == 'close':
3580 if item == 'close':
3585 return object.__getattribute__(self, item)
3581 return object.__getattribute__(self, item)
3586
3582
3587 raise error.ProgrammingError(
3583 raise error.ProgrammingError(
3588 b'repo instances should not be used after unshare'
3584 b'repo instances should not be used after unshare'
3589 )
3585 )
3590
3586
3591 def close(self):
3587 def close(self):
3592 pass
3588 pass
3593
3589
3594 # We may have a repoview, which intercepts __setattr__. So be sure
3590 # We may have a repoview, which intercepts __setattr__. So be sure
3595 # we operate at the lowest level possible.
3591 # we operate at the lowest level possible.
3596 object.__setattr__(repo, '__class__', poisonedrepository)
3592 object.__setattr__(repo, '__class__', poisonedrepository)
General Comments 0
You need to be logged in to leave comments. Login now