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