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