##// END OF EJS Templates
vfsward: register 'write with no lock' warnings as 'check-locks' config...
Boris Feld -
r33532:d645fdfb default
parent child Browse files
Show More
@@ -1,2247 +1,2247 b''
1 # localrepo.py - read/write repository class for mercurial
1 # localrepo.py - read/write repository class for mercurial
2 #
2 #
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 from __future__ import absolute_import
8 from __future__ import absolute_import
9
9
10 import errno
10 import errno
11 import hashlib
11 import hashlib
12 import inspect
12 import inspect
13 import os
13 import os
14 import random
14 import random
15 import time
15 import time
16 import weakref
16 import weakref
17
17
18 from .i18n import _
18 from .i18n import _
19 from .node import (
19 from .node import (
20 hex,
20 hex,
21 nullid,
21 nullid,
22 short,
22 short,
23 )
23 )
24 from . import (
24 from . import (
25 bookmarks,
25 bookmarks,
26 branchmap,
26 branchmap,
27 bundle2,
27 bundle2,
28 changegroup,
28 changegroup,
29 changelog,
29 changelog,
30 color,
30 color,
31 context,
31 context,
32 dirstate,
32 dirstate,
33 dirstateguard,
33 dirstateguard,
34 encoding,
34 encoding,
35 error,
35 error,
36 exchange,
36 exchange,
37 extensions,
37 extensions,
38 filelog,
38 filelog,
39 hook,
39 hook,
40 lock as lockmod,
40 lock as lockmod,
41 manifest,
41 manifest,
42 match as matchmod,
42 match as matchmod,
43 merge as mergemod,
43 merge as mergemod,
44 mergeutil,
44 mergeutil,
45 namespaces,
45 namespaces,
46 obsolete,
46 obsolete,
47 pathutil,
47 pathutil,
48 peer,
48 peer,
49 phases,
49 phases,
50 pushkey,
50 pushkey,
51 pycompat,
51 pycompat,
52 repoview,
52 repoview,
53 revset,
53 revset,
54 revsetlang,
54 revsetlang,
55 scmutil,
55 scmutil,
56 sparse,
56 sparse,
57 store,
57 store,
58 subrepo,
58 subrepo,
59 tags as tagsmod,
59 tags as tagsmod,
60 transaction,
60 transaction,
61 txnutil,
61 txnutil,
62 util,
62 util,
63 vfs as vfsmod,
63 vfs as vfsmod,
64 )
64 )
65
65
66 release = lockmod.release
66 release = lockmod.release
67 urlerr = util.urlerr
67 urlerr = util.urlerr
68 urlreq = util.urlreq
68 urlreq = util.urlreq
69
69
70 # set of (path, vfs-location) tuples. vfs-location is:
70 # set of (path, vfs-location) tuples. vfs-location is:
71 # - 'plain for vfs relative paths
71 # - 'plain for vfs relative paths
72 # - '' for svfs relative paths
72 # - '' for svfs relative paths
73 _cachedfiles = set()
73 _cachedfiles = set()
74
74
75 class _basefilecache(scmutil.filecache):
75 class _basefilecache(scmutil.filecache):
76 """All filecache usage on repo are done for logic that should be unfiltered
76 """All filecache usage on repo are done for logic that should be unfiltered
77 """
77 """
78 def __get__(self, repo, type=None):
78 def __get__(self, repo, type=None):
79 if repo is None:
79 if repo is None:
80 return self
80 return self
81 return super(_basefilecache, self).__get__(repo.unfiltered(), type)
81 return super(_basefilecache, self).__get__(repo.unfiltered(), type)
82 def __set__(self, repo, value):
82 def __set__(self, repo, value):
83 return super(_basefilecache, self).__set__(repo.unfiltered(), value)
83 return super(_basefilecache, self).__set__(repo.unfiltered(), value)
84 def __delete__(self, repo):
84 def __delete__(self, repo):
85 return super(_basefilecache, self).__delete__(repo.unfiltered())
85 return super(_basefilecache, self).__delete__(repo.unfiltered())
86
86
87 class repofilecache(_basefilecache):
87 class repofilecache(_basefilecache):
88 """filecache for files in .hg but outside of .hg/store"""
88 """filecache for files in .hg but outside of .hg/store"""
89 def __init__(self, *paths):
89 def __init__(self, *paths):
90 super(repofilecache, self).__init__(*paths)
90 super(repofilecache, self).__init__(*paths)
91 for path in paths:
91 for path in paths:
92 _cachedfiles.add((path, 'plain'))
92 _cachedfiles.add((path, 'plain'))
93
93
94 def join(self, obj, fname):
94 def join(self, obj, fname):
95 return obj.vfs.join(fname)
95 return obj.vfs.join(fname)
96
96
97 class storecache(_basefilecache):
97 class storecache(_basefilecache):
98 """filecache for files in the store"""
98 """filecache for files in the store"""
99 def __init__(self, *paths):
99 def __init__(self, *paths):
100 super(storecache, self).__init__(*paths)
100 super(storecache, self).__init__(*paths)
101 for path in paths:
101 for path in paths:
102 _cachedfiles.add((path, ''))
102 _cachedfiles.add((path, ''))
103
103
104 def join(self, obj, fname):
104 def join(self, obj, fname):
105 return obj.sjoin(fname)
105 return obj.sjoin(fname)
106
106
107 def isfilecached(repo, name):
107 def isfilecached(repo, name):
108 """check if a repo has already cached "name" filecache-ed property
108 """check if a repo has already cached "name" filecache-ed property
109
109
110 This returns (cachedobj-or-None, iscached) tuple.
110 This returns (cachedobj-or-None, iscached) tuple.
111 """
111 """
112 cacheentry = repo.unfiltered()._filecache.get(name, None)
112 cacheentry = repo.unfiltered()._filecache.get(name, None)
113 if not cacheentry:
113 if not cacheentry:
114 return None, False
114 return None, False
115 return cacheentry.obj, True
115 return cacheentry.obj, True
116
116
117 class unfilteredpropertycache(util.propertycache):
117 class unfilteredpropertycache(util.propertycache):
118 """propertycache that apply to unfiltered repo only"""
118 """propertycache that apply to unfiltered repo only"""
119
119
120 def __get__(self, repo, type=None):
120 def __get__(self, repo, type=None):
121 unfi = repo.unfiltered()
121 unfi = repo.unfiltered()
122 if unfi is repo:
122 if unfi is repo:
123 return super(unfilteredpropertycache, self).__get__(unfi)
123 return super(unfilteredpropertycache, self).__get__(unfi)
124 return getattr(unfi, self.name)
124 return getattr(unfi, self.name)
125
125
126 class filteredpropertycache(util.propertycache):
126 class filteredpropertycache(util.propertycache):
127 """propertycache that must take filtering in account"""
127 """propertycache that must take filtering in account"""
128
128
129 def cachevalue(self, obj, value):
129 def cachevalue(self, obj, value):
130 object.__setattr__(obj, self.name, value)
130 object.__setattr__(obj, self.name, value)
131
131
132
132
133 def hasunfilteredcache(repo, name):
133 def hasunfilteredcache(repo, name):
134 """check if a repo has an unfilteredpropertycache value for <name>"""
134 """check if a repo has an unfilteredpropertycache value for <name>"""
135 return name in vars(repo.unfiltered())
135 return name in vars(repo.unfiltered())
136
136
137 def unfilteredmethod(orig):
137 def unfilteredmethod(orig):
138 """decorate method that always need to be run on unfiltered version"""
138 """decorate method that always need to be run on unfiltered version"""
139 def wrapper(repo, *args, **kwargs):
139 def wrapper(repo, *args, **kwargs):
140 return orig(repo.unfiltered(), *args, **kwargs)
140 return orig(repo.unfiltered(), *args, **kwargs)
141 return wrapper
141 return wrapper
142
142
143 moderncaps = {'lookup', 'branchmap', 'pushkey', 'known', 'getbundle',
143 moderncaps = {'lookup', 'branchmap', 'pushkey', 'known', 'getbundle',
144 'unbundle'}
144 'unbundle'}
145 legacycaps = moderncaps.union({'changegroupsubset'})
145 legacycaps = moderncaps.union({'changegroupsubset'})
146
146
147 class localpeer(peer.peerrepository):
147 class localpeer(peer.peerrepository):
148 '''peer for a local repo; reflects only the most recent API'''
148 '''peer for a local repo; reflects only the most recent API'''
149
149
150 def __init__(self, repo, caps=None):
150 def __init__(self, repo, caps=None):
151 if caps is None:
151 if caps is None:
152 caps = moderncaps.copy()
152 caps = moderncaps.copy()
153 peer.peerrepository.__init__(self)
153 peer.peerrepository.__init__(self)
154 self._repo = repo.filtered('served')
154 self._repo = repo.filtered('served')
155 self.ui = repo.ui
155 self.ui = repo.ui
156 self._caps = repo._restrictcapabilities(caps)
156 self._caps = repo._restrictcapabilities(caps)
157 self.requirements = repo.requirements
157 self.requirements = repo.requirements
158 self.supportedformats = repo.supportedformats
158 self.supportedformats = repo.supportedformats
159
159
160 def close(self):
160 def close(self):
161 self._repo.close()
161 self._repo.close()
162
162
163 def _capabilities(self):
163 def _capabilities(self):
164 return self._caps
164 return self._caps
165
165
166 def local(self):
166 def local(self):
167 return self._repo
167 return self._repo
168
168
169 def canpush(self):
169 def canpush(self):
170 return True
170 return True
171
171
172 def url(self):
172 def url(self):
173 return self._repo.url()
173 return self._repo.url()
174
174
175 def lookup(self, key):
175 def lookup(self, key):
176 return self._repo.lookup(key)
176 return self._repo.lookup(key)
177
177
178 def branchmap(self):
178 def branchmap(self):
179 return self._repo.branchmap()
179 return self._repo.branchmap()
180
180
181 def heads(self):
181 def heads(self):
182 return self._repo.heads()
182 return self._repo.heads()
183
183
184 def known(self, nodes):
184 def known(self, nodes):
185 return self._repo.known(nodes)
185 return self._repo.known(nodes)
186
186
187 def getbundle(self, source, heads=None, common=None, bundlecaps=None,
187 def getbundle(self, source, heads=None, common=None, bundlecaps=None,
188 **kwargs):
188 **kwargs):
189 chunks = exchange.getbundlechunks(self._repo, source, heads=heads,
189 chunks = exchange.getbundlechunks(self._repo, source, heads=heads,
190 common=common, bundlecaps=bundlecaps,
190 common=common, bundlecaps=bundlecaps,
191 **kwargs)
191 **kwargs)
192 cb = util.chunkbuffer(chunks)
192 cb = util.chunkbuffer(chunks)
193
193
194 if exchange.bundle2requested(bundlecaps):
194 if exchange.bundle2requested(bundlecaps):
195 # When requesting a bundle2, getbundle returns a stream to make the
195 # When requesting a bundle2, getbundle returns a stream to make the
196 # wire level function happier. We need to build a proper object
196 # wire level function happier. We need to build a proper object
197 # from it in local peer.
197 # from it in local peer.
198 return bundle2.getunbundler(self.ui, cb)
198 return bundle2.getunbundler(self.ui, cb)
199 else:
199 else:
200 return changegroup.getunbundler('01', cb, None)
200 return changegroup.getunbundler('01', cb, None)
201
201
202 # TODO We might want to move the next two calls into legacypeer and add
202 # TODO We might want to move the next two calls into legacypeer and add
203 # unbundle instead.
203 # unbundle instead.
204
204
205 def unbundle(self, cg, heads, url):
205 def unbundle(self, cg, heads, url):
206 """apply a bundle on a repo
206 """apply a bundle on a repo
207
207
208 This function handles the repo locking itself."""
208 This function handles the repo locking itself."""
209 try:
209 try:
210 try:
210 try:
211 cg = exchange.readbundle(self.ui, cg, None)
211 cg = exchange.readbundle(self.ui, cg, None)
212 ret = exchange.unbundle(self._repo, cg, heads, 'push', url)
212 ret = exchange.unbundle(self._repo, cg, heads, 'push', url)
213 if util.safehasattr(ret, 'getchunks'):
213 if util.safehasattr(ret, 'getchunks'):
214 # This is a bundle20 object, turn it into an unbundler.
214 # This is a bundle20 object, turn it into an unbundler.
215 # This little dance should be dropped eventually when the
215 # This little dance should be dropped eventually when the
216 # API is finally improved.
216 # API is finally improved.
217 stream = util.chunkbuffer(ret.getchunks())
217 stream = util.chunkbuffer(ret.getchunks())
218 ret = bundle2.getunbundler(self.ui, stream)
218 ret = bundle2.getunbundler(self.ui, stream)
219 return ret
219 return ret
220 except Exception as exc:
220 except Exception as exc:
221 # If the exception contains output salvaged from a bundle2
221 # If the exception contains output salvaged from a bundle2
222 # reply, we need to make sure it is printed before continuing
222 # reply, we need to make sure it is printed before continuing
223 # to fail. So we build a bundle2 with such output and consume
223 # to fail. So we build a bundle2 with such output and consume
224 # it directly.
224 # it directly.
225 #
225 #
226 # This is not very elegant but allows a "simple" solution for
226 # This is not very elegant but allows a "simple" solution for
227 # issue4594
227 # issue4594
228 output = getattr(exc, '_bundle2salvagedoutput', ())
228 output = getattr(exc, '_bundle2salvagedoutput', ())
229 if output:
229 if output:
230 bundler = bundle2.bundle20(self._repo.ui)
230 bundler = bundle2.bundle20(self._repo.ui)
231 for out in output:
231 for out in output:
232 bundler.addpart(out)
232 bundler.addpart(out)
233 stream = util.chunkbuffer(bundler.getchunks())
233 stream = util.chunkbuffer(bundler.getchunks())
234 b = bundle2.getunbundler(self.ui, stream)
234 b = bundle2.getunbundler(self.ui, stream)
235 bundle2.processbundle(self._repo, b)
235 bundle2.processbundle(self._repo, b)
236 raise
236 raise
237 except error.PushRaced as exc:
237 except error.PushRaced as exc:
238 raise error.ResponseError(_('push failed:'), str(exc))
238 raise error.ResponseError(_('push failed:'), str(exc))
239
239
240 def lock(self):
240 def lock(self):
241 return self._repo.lock()
241 return self._repo.lock()
242
242
243 def pushkey(self, namespace, key, old, new):
243 def pushkey(self, namespace, key, old, new):
244 return self._repo.pushkey(namespace, key, old, new)
244 return self._repo.pushkey(namespace, key, old, new)
245
245
246 def listkeys(self, namespace):
246 def listkeys(self, namespace):
247 return self._repo.listkeys(namespace)
247 return self._repo.listkeys(namespace)
248
248
249 def debugwireargs(self, one, two, three=None, four=None, five=None):
249 def debugwireargs(self, one, two, three=None, four=None, five=None):
250 '''used to test argument passing over the wire'''
250 '''used to test argument passing over the wire'''
251 return "%s %s %s %s %s" % (one, two, three, four, five)
251 return "%s %s %s %s %s" % (one, two, three, four, five)
252
252
253 class locallegacypeer(localpeer):
253 class locallegacypeer(localpeer):
254 '''peer extension which implements legacy methods too; used for tests with
254 '''peer extension which implements legacy methods too; used for tests with
255 restricted capabilities'''
255 restricted capabilities'''
256
256
257 def __init__(self, repo):
257 def __init__(self, repo):
258 localpeer.__init__(self, repo, caps=legacycaps)
258 localpeer.__init__(self, repo, caps=legacycaps)
259
259
260 def branches(self, nodes):
260 def branches(self, nodes):
261 return self._repo.branches(nodes)
261 return self._repo.branches(nodes)
262
262
263 def between(self, pairs):
263 def between(self, pairs):
264 return self._repo.between(pairs)
264 return self._repo.between(pairs)
265
265
266 def changegroup(self, basenodes, source):
266 def changegroup(self, basenodes, source):
267 return changegroup.changegroup(self._repo, basenodes, source)
267 return changegroup.changegroup(self._repo, basenodes, source)
268
268
269 def changegroupsubset(self, bases, heads, source):
269 def changegroupsubset(self, bases, heads, source):
270 return changegroup.changegroupsubset(self._repo, bases, heads, source)
270 return changegroup.changegroupsubset(self._repo, bases, heads, source)
271
271
272 # Increment the sub-version when the revlog v2 format changes to lock out old
272 # Increment the sub-version when the revlog v2 format changes to lock out old
273 # clients.
273 # clients.
274 REVLOGV2_REQUIREMENT = 'exp-revlogv2.0'
274 REVLOGV2_REQUIREMENT = 'exp-revlogv2.0'
275
275
276 class localrepository(object):
276 class localrepository(object):
277
277
278 supportedformats = {
278 supportedformats = {
279 'revlogv1',
279 'revlogv1',
280 'generaldelta',
280 'generaldelta',
281 'treemanifest',
281 'treemanifest',
282 'manifestv2',
282 'manifestv2',
283 REVLOGV2_REQUIREMENT,
283 REVLOGV2_REQUIREMENT,
284 }
284 }
285 _basesupported = supportedformats | {
285 _basesupported = supportedformats | {
286 'store',
286 'store',
287 'fncache',
287 'fncache',
288 'shared',
288 'shared',
289 'relshared',
289 'relshared',
290 'dotencode',
290 'dotencode',
291 }
291 }
292 openerreqs = {
292 openerreqs = {
293 'revlogv1',
293 'revlogv1',
294 'generaldelta',
294 'generaldelta',
295 'treemanifest',
295 'treemanifest',
296 'manifestv2',
296 'manifestv2',
297 }
297 }
298
298
299 # a list of (ui, featureset) functions.
299 # a list of (ui, featureset) functions.
300 # only functions defined in module of enabled extensions are invoked
300 # only functions defined in module of enabled extensions are invoked
301 featuresetupfuncs = set()
301 featuresetupfuncs = set()
302
302
303 # list of prefix for file which can be written without 'wlock'
303 # list of prefix for file which can be written without 'wlock'
304 # Extensions should extend this list when needed
304 # Extensions should extend this list when needed
305 _wlockfreeprefix = {
305 _wlockfreeprefix = {
306 # We migh consider requiring 'wlock' for the next
306 # We migh consider requiring 'wlock' for the next
307 # two, but pretty much all the existing code assume
307 # two, but pretty much all the existing code assume
308 # wlock is not needed so we keep them excluded for
308 # wlock is not needed so we keep them excluded for
309 # now.
309 # now.
310 'hgrc',
310 'hgrc',
311 'requires',
311 'requires',
312 # XXX cache is a complicatged business someone
312 # XXX cache is a complicatged business someone
313 # should investigate this in depth at some point
313 # should investigate this in depth at some point
314 'cache/',
314 'cache/',
315 # XXX shouldn't be dirstate covered by the wlock?
315 # XXX shouldn't be dirstate covered by the wlock?
316 'dirstate',
316 'dirstate',
317 # XXX bisect was still a bit too messy at the time
317 # XXX bisect was still a bit too messy at the time
318 # this changeset was introduced. Someone should fix
318 # this changeset was introduced. Someone should fix
319 # the remainig bit and drop this line
319 # the remainig bit and drop this line
320 'bisect.state',
320 'bisect.state',
321 }
321 }
322
322
323 def __init__(self, baseui, path, create=False):
323 def __init__(self, baseui, path, create=False):
324 self.requirements = set()
324 self.requirements = set()
325 self.filtername = None
325 self.filtername = None
326 # wvfs: rooted at the repository root, used to access the working copy
326 # wvfs: rooted at the repository root, used to access the working copy
327 self.wvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
327 self.wvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
328 # vfs: rooted at .hg, used to access repo files outside of .hg/store
328 # vfs: rooted at .hg, used to access repo files outside of .hg/store
329 self.vfs = None
329 self.vfs = None
330 # svfs: usually rooted at .hg/store, used to access repository history
330 # svfs: usually rooted at .hg/store, used to access repository history
331 # If this is a shared repository, this vfs may point to another
331 # If this is a shared repository, this vfs may point to another
332 # repository's .hg/store directory.
332 # repository's .hg/store directory.
333 self.svfs = None
333 self.svfs = None
334 self.root = self.wvfs.base
334 self.root = self.wvfs.base
335 self.path = self.wvfs.join(".hg")
335 self.path = self.wvfs.join(".hg")
336 self.origroot = path
336 self.origroot = path
337 # These auditor are not used by the vfs,
337 # These auditor are not used by the vfs,
338 # only used when writing this comment: basectx.match
338 # only used when writing this comment: basectx.match
339 self.auditor = pathutil.pathauditor(self.root, self._checknested)
339 self.auditor = pathutil.pathauditor(self.root, self._checknested)
340 self.nofsauditor = pathutil.pathauditor(self.root, self._checknested,
340 self.nofsauditor = pathutil.pathauditor(self.root, self._checknested,
341 realfs=False)
341 realfs=False)
342 self.baseui = baseui
342 self.baseui = baseui
343 self.ui = baseui.copy()
343 self.ui = baseui.copy()
344 self.ui.copy = baseui.copy # prevent copying repo configuration
344 self.ui.copy = baseui.copy # prevent copying repo configuration
345 self.vfs = vfsmod.vfs(self.path)
345 self.vfs = vfsmod.vfs(self.path)
346 if (self.ui.configbool('devel', 'all-warnings') or
346 if (self.ui.configbool('devel', 'all-warnings') or
347 self.ui.configbool('devel', 'check-locks')):
347 self.ui.configbool('devel', 'check-locks')):
348 self.vfs.audit = self._getvfsward(self.vfs.audit)
348 self.vfs.audit = self._getvfsward(self.vfs.audit)
349 # A list of callback to shape the phase if no data were found.
349 # A list of callback to shape the phase if no data were found.
350 # Callback are in the form: func(repo, roots) --> processed root.
350 # Callback are in the form: func(repo, roots) --> processed root.
351 # This list it to be filled by extension during repo setup
351 # This list it to be filled by extension during repo setup
352 self._phasedefaults = []
352 self._phasedefaults = []
353 try:
353 try:
354 self.ui.readconfig(self.vfs.join("hgrc"), self.root)
354 self.ui.readconfig(self.vfs.join("hgrc"), self.root)
355 self._loadextensions()
355 self._loadextensions()
356 except IOError:
356 except IOError:
357 pass
357 pass
358
358
359 if self.featuresetupfuncs:
359 if self.featuresetupfuncs:
360 self.supported = set(self._basesupported) # use private copy
360 self.supported = set(self._basesupported) # use private copy
361 extmods = set(m.__name__ for n, m
361 extmods = set(m.__name__ for n, m
362 in extensions.extensions(self.ui))
362 in extensions.extensions(self.ui))
363 for setupfunc in self.featuresetupfuncs:
363 for setupfunc in self.featuresetupfuncs:
364 if setupfunc.__module__ in extmods:
364 if setupfunc.__module__ in extmods:
365 setupfunc(self.ui, self.supported)
365 setupfunc(self.ui, self.supported)
366 else:
366 else:
367 self.supported = self._basesupported
367 self.supported = self._basesupported
368 color.setup(self.ui)
368 color.setup(self.ui)
369
369
370 # Add compression engines.
370 # Add compression engines.
371 for name in util.compengines:
371 for name in util.compengines:
372 engine = util.compengines[name]
372 engine = util.compengines[name]
373 if engine.revlogheader():
373 if engine.revlogheader():
374 self.supported.add('exp-compression-%s' % name)
374 self.supported.add('exp-compression-%s' % name)
375
375
376 if not self.vfs.isdir():
376 if not self.vfs.isdir():
377 if create:
377 if create:
378 self.requirements = newreporequirements(self)
378 self.requirements = newreporequirements(self)
379
379
380 if not self.wvfs.exists():
380 if not self.wvfs.exists():
381 self.wvfs.makedirs()
381 self.wvfs.makedirs()
382 self.vfs.makedir(notindexed=True)
382 self.vfs.makedir(notindexed=True)
383
383
384 if 'store' in self.requirements:
384 if 'store' in self.requirements:
385 self.vfs.mkdir("store")
385 self.vfs.mkdir("store")
386
386
387 # create an invalid changelog
387 # create an invalid changelog
388 self.vfs.append(
388 self.vfs.append(
389 "00changelog.i",
389 "00changelog.i",
390 '\0\0\0\2' # represents revlogv2
390 '\0\0\0\2' # represents revlogv2
391 ' dummy changelog to prevent using the old repo layout'
391 ' dummy changelog to prevent using the old repo layout'
392 )
392 )
393 else:
393 else:
394 raise error.RepoError(_("repository %s not found") % path)
394 raise error.RepoError(_("repository %s not found") % path)
395 elif create:
395 elif create:
396 raise error.RepoError(_("repository %s already exists") % path)
396 raise error.RepoError(_("repository %s already exists") % path)
397 else:
397 else:
398 try:
398 try:
399 self.requirements = scmutil.readrequires(
399 self.requirements = scmutil.readrequires(
400 self.vfs, self.supported)
400 self.vfs, self.supported)
401 except IOError as inst:
401 except IOError as inst:
402 if inst.errno != errno.ENOENT:
402 if inst.errno != errno.ENOENT:
403 raise
403 raise
404
404
405 self.sharedpath = self.path
405 self.sharedpath = self.path
406 try:
406 try:
407 sharedpath = self.vfs.read("sharedpath").rstrip('\n')
407 sharedpath = self.vfs.read("sharedpath").rstrip('\n')
408 if 'relshared' in self.requirements:
408 if 'relshared' in self.requirements:
409 sharedpath = self.vfs.join(sharedpath)
409 sharedpath = self.vfs.join(sharedpath)
410 vfs = vfsmod.vfs(sharedpath, realpath=True)
410 vfs = vfsmod.vfs(sharedpath, realpath=True)
411 s = vfs.base
411 s = vfs.base
412 if not vfs.exists():
412 if not vfs.exists():
413 raise error.RepoError(
413 raise error.RepoError(
414 _('.hg/sharedpath points to nonexistent directory %s') % s)
414 _('.hg/sharedpath points to nonexistent directory %s') % s)
415 self.sharedpath = s
415 self.sharedpath = s
416 except IOError as inst:
416 except IOError as inst:
417 if inst.errno != errno.ENOENT:
417 if inst.errno != errno.ENOENT:
418 raise
418 raise
419
419
420 self.store = store.store(
420 self.store = store.store(
421 self.requirements, self.sharedpath, vfsmod.vfs)
421 self.requirements, self.sharedpath, vfsmod.vfs)
422 self.spath = self.store.path
422 self.spath = self.store.path
423 self.svfs = self.store.vfs
423 self.svfs = self.store.vfs
424 self.sjoin = self.store.join
424 self.sjoin = self.store.join
425 self.vfs.createmode = self.store.createmode
425 self.vfs.createmode = self.store.createmode
426 if (self.ui.configbool('devel', 'all-warnings') or
426 if (self.ui.configbool('devel', 'all-warnings') or
427 self.ui.configbool('devel', 'check-locks')):
427 self.ui.configbool('devel', 'check-locks')):
428 if util.safehasattr(self.svfs, 'vfs'): # this is filtervfs
428 if util.safehasattr(self.svfs, 'vfs'): # this is filtervfs
429 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
429 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
430 else: # standard vfs
430 else: # standard vfs
431 self.svfs.audit = self._getsvfsward(self.svfs.audit)
431 self.svfs.audit = self._getsvfsward(self.svfs.audit)
432 self._applyopenerreqs()
432 self._applyopenerreqs()
433 if create:
433 if create:
434 self._writerequirements()
434 self._writerequirements()
435
435
436 self._dirstatevalidatewarned = False
436 self._dirstatevalidatewarned = False
437
437
438 self._branchcaches = {}
438 self._branchcaches = {}
439 self._revbranchcache = None
439 self._revbranchcache = None
440 self.filterpats = {}
440 self.filterpats = {}
441 self._datafilters = {}
441 self._datafilters = {}
442 self._transref = self._lockref = self._wlockref = None
442 self._transref = self._lockref = self._wlockref = None
443
443
444 # A cache for various files under .hg/ that tracks file changes,
444 # A cache for various files under .hg/ that tracks file changes,
445 # (used by the filecache decorator)
445 # (used by the filecache decorator)
446 #
446 #
447 # Maps a property name to its util.filecacheentry
447 # Maps a property name to its util.filecacheentry
448 self._filecache = {}
448 self._filecache = {}
449
449
450 # hold sets of revision to be filtered
450 # hold sets of revision to be filtered
451 # should be cleared when something might have changed the filter value:
451 # should be cleared when something might have changed the filter value:
452 # - new changesets,
452 # - new changesets,
453 # - phase change,
453 # - phase change,
454 # - new obsolescence marker,
454 # - new obsolescence marker,
455 # - working directory parent change,
455 # - working directory parent change,
456 # - bookmark changes
456 # - bookmark changes
457 self.filteredrevcache = {}
457 self.filteredrevcache = {}
458
458
459 # post-dirstate-status hooks
459 # post-dirstate-status hooks
460 self._postdsstatus = []
460 self._postdsstatus = []
461
461
462 # Cache of types representing filtered repos.
462 # Cache of types representing filtered repos.
463 self._filteredrepotypes = weakref.WeakKeyDictionary()
463 self._filteredrepotypes = weakref.WeakKeyDictionary()
464
464
465 # generic mapping between names and nodes
465 # generic mapping between names and nodes
466 self.names = namespaces.namespaces()
466 self.names = namespaces.namespaces()
467
467
468 # Key to signature value.
468 # Key to signature value.
469 self._sparsesignaturecache = {}
469 self._sparsesignaturecache = {}
470 # Signature to cached matcher instance.
470 # Signature to cached matcher instance.
471 self._sparsematchercache = {}
471 self._sparsematchercache = {}
472
472
473 def _getvfsward(self, origfunc):
473 def _getvfsward(self, origfunc):
474 """build a ward for self.vfs"""
474 """build a ward for self.vfs"""
475 rref = weakref.ref(self)
475 rref = weakref.ref(self)
476 def checkvfs(path, mode=None):
476 def checkvfs(path, mode=None):
477 ret = origfunc(path, mode=mode)
477 ret = origfunc(path, mode=mode)
478 repo = rref()
478 repo = rref()
479 if (repo is None
479 if (repo is None
480 or not util.safehasattr(repo, '_wlockref')
480 or not util.safehasattr(repo, '_wlockref')
481 or not util.safehasattr(repo, '_lockref')):
481 or not util.safehasattr(repo, '_lockref')):
482 return
482 return
483 if mode in (None, 'r', 'rb'):
483 if mode in (None, 'r', 'rb'):
484 return
484 return
485 if path.startswith(repo.path):
485 if path.startswith(repo.path):
486 # truncate name relative to the repository (.hg)
486 # truncate name relative to the repository (.hg)
487 path = path[len(repo.path) + 1:]
487 path = path[len(repo.path) + 1:]
488 if path.startswith('journal.'):
488 if path.startswith('journal.'):
489 # journal is covered by 'lock'
489 # journal is covered by 'lock'
490 if repo._currentlock(repo._lockref) is None:
490 if repo._currentlock(repo._lockref) is None:
491 repo.ui.develwarn('write with no lock: "%s"' % path,
491 repo.ui.develwarn('write with no lock: "%s"' % path,
492 stacklevel=2)
492 stacklevel=2, config='check-locks')
493 elif repo._currentlock(repo._wlockref) is None:
493 elif repo._currentlock(repo._wlockref) is None:
494 # rest of vfs files are covered by 'wlock'
494 # rest of vfs files are covered by 'wlock'
495 #
495 #
496 # exclude special files
496 # exclude special files
497 for prefix in self._wlockfreeprefix:
497 for prefix in self._wlockfreeprefix:
498 if path.startswith(prefix):
498 if path.startswith(prefix):
499 return
499 return
500 repo.ui.develwarn('write with no wlock: "%s"' % path,
500 repo.ui.develwarn('write with no wlock: "%s"' % path,
501 stacklevel=2)
501 stacklevel=2, config='check-locks')
502 return ret
502 return ret
503 return checkvfs
503 return checkvfs
504
504
505 def _getsvfsward(self, origfunc):
505 def _getsvfsward(self, origfunc):
506 """build a ward for self.svfs"""
506 """build a ward for self.svfs"""
507 rref = weakref.ref(self)
507 rref = weakref.ref(self)
508 def checksvfs(path, mode=None):
508 def checksvfs(path, mode=None):
509 ret = origfunc(path, mode=mode)
509 ret = origfunc(path, mode=mode)
510 repo = rref()
510 repo = rref()
511 if repo is None or not util.safehasattr(repo, '_lockref'):
511 if repo is None or not util.safehasattr(repo, '_lockref'):
512 return
512 return
513 if mode in (None, 'r', 'rb'):
513 if mode in (None, 'r', 'rb'):
514 return
514 return
515 if path.startswith(repo.sharedpath):
515 if path.startswith(repo.sharedpath):
516 # truncate name relative to the repository (.hg)
516 # truncate name relative to the repository (.hg)
517 path = path[len(repo.sharedpath) + 1:]
517 path = path[len(repo.sharedpath) + 1:]
518 if repo._currentlock(repo._lockref) is None:
518 if repo._currentlock(repo._lockref) is None:
519 repo.ui.develwarn('write with no lock: "%s"' % path,
519 repo.ui.develwarn('write with no lock: "%s"' % path,
520 stacklevel=3)
520 stacklevel=3)
521 return ret
521 return ret
522 return checksvfs
522 return checksvfs
523
523
524 def close(self):
524 def close(self):
525 self._writecaches()
525 self._writecaches()
526
526
527 def _loadextensions(self):
527 def _loadextensions(self):
528 extensions.loadall(self.ui)
528 extensions.loadall(self.ui)
529
529
530 def _writecaches(self):
530 def _writecaches(self):
531 if self._revbranchcache:
531 if self._revbranchcache:
532 self._revbranchcache.write()
532 self._revbranchcache.write()
533
533
534 def _restrictcapabilities(self, caps):
534 def _restrictcapabilities(self, caps):
535 if self.ui.configbool('experimental', 'bundle2-advertise'):
535 if self.ui.configbool('experimental', 'bundle2-advertise'):
536 caps = set(caps)
536 caps = set(caps)
537 capsblob = bundle2.encodecaps(bundle2.getrepocaps(self))
537 capsblob = bundle2.encodecaps(bundle2.getrepocaps(self))
538 caps.add('bundle2=' + urlreq.quote(capsblob))
538 caps.add('bundle2=' + urlreq.quote(capsblob))
539 return caps
539 return caps
540
540
541 def _applyopenerreqs(self):
541 def _applyopenerreqs(self):
542 self.svfs.options = dict((r, 1) for r in self.requirements
542 self.svfs.options = dict((r, 1) for r in self.requirements
543 if r in self.openerreqs)
543 if r in self.openerreqs)
544 # experimental config: format.chunkcachesize
544 # experimental config: format.chunkcachesize
545 chunkcachesize = self.ui.configint('format', 'chunkcachesize')
545 chunkcachesize = self.ui.configint('format', 'chunkcachesize')
546 if chunkcachesize is not None:
546 if chunkcachesize is not None:
547 self.svfs.options['chunkcachesize'] = chunkcachesize
547 self.svfs.options['chunkcachesize'] = chunkcachesize
548 # experimental config: format.maxchainlen
548 # experimental config: format.maxchainlen
549 maxchainlen = self.ui.configint('format', 'maxchainlen')
549 maxchainlen = self.ui.configint('format', 'maxchainlen')
550 if maxchainlen is not None:
550 if maxchainlen is not None:
551 self.svfs.options['maxchainlen'] = maxchainlen
551 self.svfs.options['maxchainlen'] = maxchainlen
552 # experimental config: format.manifestcachesize
552 # experimental config: format.manifestcachesize
553 manifestcachesize = self.ui.configint('format', 'manifestcachesize')
553 manifestcachesize = self.ui.configint('format', 'manifestcachesize')
554 if manifestcachesize is not None:
554 if manifestcachesize is not None:
555 self.svfs.options['manifestcachesize'] = manifestcachesize
555 self.svfs.options['manifestcachesize'] = manifestcachesize
556 # experimental config: format.aggressivemergedeltas
556 # experimental config: format.aggressivemergedeltas
557 aggressivemergedeltas = self.ui.configbool('format',
557 aggressivemergedeltas = self.ui.configbool('format',
558 'aggressivemergedeltas')
558 'aggressivemergedeltas')
559 self.svfs.options['aggressivemergedeltas'] = aggressivemergedeltas
559 self.svfs.options['aggressivemergedeltas'] = aggressivemergedeltas
560 self.svfs.options['lazydeltabase'] = not scmutil.gddeltaconfig(self.ui)
560 self.svfs.options['lazydeltabase'] = not scmutil.gddeltaconfig(self.ui)
561 chainspan = self.ui.configbytes('experimental', 'maxdeltachainspan', -1)
561 chainspan = self.ui.configbytes('experimental', 'maxdeltachainspan', -1)
562 if 0 <= chainspan:
562 if 0 <= chainspan:
563 self.svfs.options['maxdeltachainspan'] = chainspan
563 self.svfs.options['maxdeltachainspan'] = chainspan
564
564
565 for r in self.requirements:
565 for r in self.requirements:
566 if r.startswith('exp-compression-'):
566 if r.startswith('exp-compression-'):
567 self.svfs.options['compengine'] = r[len('exp-compression-'):]
567 self.svfs.options['compengine'] = r[len('exp-compression-'):]
568
568
569 # TODO move "revlogv2" to openerreqs once finalized.
569 # TODO move "revlogv2" to openerreqs once finalized.
570 if REVLOGV2_REQUIREMENT in self.requirements:
570 if REVLOGV2_REQUIREMENT in self.requirements:
571 self.svfs.options['revlogv2'] = True
571 self.svfs.options['revlogv2'] = True
572
572
573 def _writerequirements(self):
573 def _writerequirements(self):
574 scmutil.writerequires(self.vfs, self.requirements)
574 scmutil.writerequires(self.vfs, self.requirements)
575
575
576 def _checknested(self, path):
576 def _checknested(self, path):
577 """Determine if path is a legal nested repository."""
577 """Determine if path is a legal nested repository."""
578 if not path.startswith(self.root):
578 if not path.startswith(self.root):
579 return False
579 return False
580 subpath = path[len(self.root) + 1:]
580 subpath = path[len(self.root) + 1:]
581 normsubpath = util.pconvert(subpath)
581 normsubpath = util.pconvert(subpath)
582
582
583 # XXX: Checking against the current working copy is wrong in
583 # XXX: Checking against the current working copy is wrong in
584 # the sense that it can reject things like
584 # the sense that it can reject things like
585 #
585 #
586 # $ hg cat -r 10 sub/x.txt
586 # $ hg cat -r 10 sub/x.txt
587 #
587 #
588 # if sub/ is no longer a subrepository in the working copy
588 # if sub/ is no longer a subrepository in the working copy
589 # parent revision.
589 # parent revision.
590 #
590 #
591 # However, it can of course also allow things that would have
591 # However, it can of course also allow things that would have
592 # been rejected before, such as the above cat command if sub/
592 # been rejected before, such as the above cat command if sub/
593 # is a subrepository now, but was a normal directory before.
593 # is a subrepository now, but was a normal directory before.
594 # The old path auditor would have rejected by mistake since it
594 # The old path auditor would have rejected by mistake since it
595 # panics when it sees sub/.hg/.
595 # panics when it sees sub/.hg/.
596 #
596 #
597 # All in all, checking against the working copy seems sensible
597 # All in all, checking against the working copy seems sensible
598 # since we want to prevent access to nested repositories on
598 # since we want to prevent access to nested repositories on
599 # the filesystem *now*.
599 # the filesystem *now*.
600 ctx = self[None]
600 ctx = self[None]
601 parts = util.splitpath(subpath)
601 parts = util.splitpath(subpath)
602 while parts:
602 while parts:
603 prefix = '/'.join(parts)
603 prefix = '/'.join(parts)
604 if prefix in ctx.substate:
604 if prefix in ctx.substate:
605 if prefix == normsubpath:
605 if prefix == normsubpath:
606 return True
606 return True
607 else:
607 else:
608 sub = ctx.sub(prefix)
608 sub = ctx.sub(prefix)
609 return sub.checknested(subpath[len(prefix) + 1:])
609 return sub.checknested(subpath[len(prefix) + 1:])
610 else:
610 else:
611 parts.pop()
611 parts.pop()
612 return False
612 return False
613
613
614 def peer(self):
614 def peer(self):
615 return localpeer(self) # not cached to avoid reference cycle
615 return localpeer(self) # not cached to avoid reference cycle
616
616
617 def unfiltered(self):
617 def unfiltered(self):
618 """Return unfiltered version of the repository
618 """Return unfiltered version of the repository
619
619
620 Intended to be overwritten by filtered repo."""
620 Intended to be overwritten by filtered repo."""
621 return self
621 return self
622
622
623 def filtered(self, name):
623 def filtered(self, name):
624 """Return a filtered version of a repository"""
624 """Return a filtered version of a repository"""
625 # Python <3.4 easily leaks types via __mro__. See
625 # Python <3.4 easily leaks types via __mro__. See
626 # https://bugs.python.org/issue17950. We cache dynamically
626 # https://bugs.python.org/issue17950. We cache dynamically
627 # created types so this method doesn't leak on every
627 # created types so this method doesn't leak on every
628 # invocation.
628 # invocation.
629
629
630 key = self.unfiltered().__class__
630 key = self.unfiltered().__class__
631 if key not in self._filteredrepotypes:
631 if key not in self._filteredrepotypes:
632 # Build a new type with the repoview mixin and the base
632 # Build a new type with the repoview mixin and the base
633 # class of this repo. Give it a name containing the
633 # class of this repo. Give it a name containing the
634 # filter name to aid debugging.
634 # filter name to aid debugging.
635 bases = (repoview.repoview, key)
635 bases = (repoview.repoview, key)
636 cls = type(r'%sfilteredrepo' % name, bases, {})
636 cls = type(r'%sfilteredrepo' % name, bases, {})
637 self._filteredrepotypes[key] = cls
637 self._filteredrepotypes[key] = cls
638
638
639 return self._filteredrepotypes[key](self, name)
639 return self._filteredrepotypes[key](self, name)
640
640
641 @repofilecache('bookmarks', 'bookmarks.current')
641 @repofilecache('bookmarks', 'bookmarks.current')
642 def _bookmarks(self):
642 def _bookmarks(self):
643 return bookmarks.bmstore(self)
643 return bookmarks.bmstore(self)
644
644
645 @property
645 @property
646 def _activebookmark(self):
646 def _activebookmark(self):
647 return self._bookmarks.active
647 return self._bookmarks.active
648
648
649 # _phaserevs and _phasesets depend on changelog. what we need is to
649 # _phaserevs and _phasesets depend on changelog. what we need is to
650 # call _phasecache.invalidate() if '00changelog.i' was changed, but it
650 # call _phasecache.invalidate() if '00changelog.i' was changed, but it
651 # can't be easily expressed in filecache mechanism.
651 # can't be easily expressed in filecache mechanism.
652 @storecache('phaseroots', '00changelog.i')
652 @storecache('phaseroots', '00changelog.i')
653 def _phasecache(self):
653 def _phasecache(self):
654 return phases.phasecache(self, self._phasedefaults)
654 return phases.phasecache(self, self._phasedefaults)
655
655
656 @storecache('obsstore')
656 @storecache('obsstore')
657 def obsstore(self):
657 def obsstore(self):
658 return obsolete.makestore(self.ui, self)
658 return obsolete.makestore(self.ui, self)
659
659
660 @storecache('00changelog.i')
660 @storecache('00changelog.i')
661 def changelog(self):
661 def changelog(self):
662 return changelog.changelog(self.svfs,
662 return changelog.changelog(self.svfs,
663 trypending=txnutil.mayhavepending(self.root))
663 trypending=txnutil.mayhavepending(self.root))
664
664
665 def _constructmanifest(self):
665 def _constructmanifest(self):
666 # This is a temporary function while we migrate from manifest to
666 # This is a temporary function while we migrate from manifest to
667 # manifestlog. It allows bundlerepo and unionrepo to intercept the
667 # manifestlog. It allows bundlerepo and unionrepo to intercept the
668 # manifest creation.
668 # manifest creation.
669 return manifest.manifestrevlog(self.svfs)
669 return manifest.manifestrevlog(self.svfs)
670
670
671 @storecache('00manifest.i')
671 @storecache('00manifest.i')
672 def manifestlog(self):
672 def manifestlog(self):
673 return manifest.manifestlog(self.svfs, self)
673 return manifest.manifestlog(self.svfs, self)
674
674
675 @repofilecache('dirstate')
675 @repofilecache('dirstate')
676 def dirstate(self):
676 def dirstate(self):
677 sparsematchfn = lambda: sparse.matcher(self)
677 sparsematchfn = lambda: sparse.matcher(self)
678
678
679 return dirstate.dirstate(self.vfs, self.ui, self.root,
679 return dirstate.dirstate(self.vfs, self.ui, self.root,
680 self._dirstatevalidate, sparsematchfn)
680 self._dirstatevalidate, sparsematchfn)
681
681
682 def _dirstatevalidate(self, node):
682 def _dirstatevalidate(self, node):
683 try:
683 try:
684 self.changelog.rev(node)
684 self.changelog.rev(node)
685 return node
685 return node
686 except error.LookupError:
686 except error.LookupError:
687 if not self._dirstatevalidatewarned:
687 if not self._dirstatevalidatewarned:
688 self._dirstatevalidatewarned = True
688 self._dirstatevalidatewarned = True
689 self.ui.warn(_("warning: ignoring unknown"
689 self.ui.warn(_("warning: ignoring unknown"
690 " working parent %s!\n") % short(node))
690 " working parent %s!\n") % short(node))
691 return nullid
691 return nullid
692
692
693 def __getitem__(self, changeid):
693 def __getitem__(self, changeid):
694 if changeid is None:
694 if changeid is None:
695 return context.workingctx(self)
695 return context.workingctx(self)
696 if isinstance(changeid, slice):
696 if isinstance(changeid, slice):
697 # wdirrev isn't contiguous so the slice shouldn't include it
697 # wdirrev isn't contiguous so the slice shouldn't include it
698 return [context.changectx(self, i)
698 return [context.changectx(self, i)
699 for i in xrange(*changeid.indices(len(self)))
699 for i in xrange(*changeid.indices(len(self)))
700 if i not in self.changelog.filteredrevs]
700 if i not in self.changelog.filteredrevs]
701 try:
701 try:
702 return context.changectx(self, changeid)
702 return context.changectx(self, changeid)
703 except error.WdirUnsupported:
703 except error.WdirUnsupported:
704 return context.workingctx(self)
704 return context.workingctx(self)
705
705
706 def __contains__(self, changeid):
706 def __contains__(self, changeid):
707 """True if the given changeid exists
707 """True if the given changeid exists
708
708
709 error.LookupError is raised if an ambiguous node specified.
709 error.LookupError is raised if an ambiguous node specified.
710 """
710 """
711 try:
711 try:
712 self[changeid]
712 self[changeid]
713 return True
713 return True
714 except error.RepoLookupError:
714 except error.RepoLookupError:
715 return False
715 return False
716
716
717 def __nonzero__(self):
717 def __nonzero__(self):
718 return True
718 return True
719
719
720 __bool__ = __nonzero__
720 __bool__ = __nonzero__
721
721
722 def __len__(self):
722 def __len__(self):
723 return len(self.changelog)
723 return len(self.changelog)
724
724
725 def __iter__(self):
725 def __iter__(self):
726 return iter(self.changelog)
726 return iter(self.changelog)
727
727
728 def revs(self, expr, *args):
728 def revs(self, expr, *args):
729 '''Find revisions matching a revset.
729 '''Find revisions matching a revset.
730
730
731 The revset is specified as a string ``expr`` that may contain
731 The revset is specified as a string ``expr`` that may contain
732 %-formatting to escape certain types. See ``revsetlang.formatspec``.
732 %-formatting to escape certain types. See ``revsetlang.formatspec``.
733
733
734 Revset aliases from the configuration are not expanded. To expand
734 Revset aliases from the configuration are not expanded. To expand
735 user aliases, consider calling ``scmutil.revrange()`` or
735 user aliases, consider calling ``scmutil.revrange()`` or
736 ``repo.anyrevs([expr], user=True)``.
736 ``repo.anyrevs([expr], user=True)``.
737
737
738 Returns a revset.abstractsmartset, which is a list-like interface
738 Returns a revset.abstractsmartset, which is a list-like interface
739 that contains integer revisions.
739 that contains integer revisions.
740 '''
740 '''
741 expr = revsetlang.formatspec(expr, *args)
741 expr = revsetlang.formatspec(expr, *args)
742 m = revset.match(None, expr)
742 m = revset.match(None, expr)
743 return m(self)
743 return m(self)
744
744
745 def set(self, expr, *args):
745 def set(self, expr, *args):
746 '''Find revisions matching a revset and emit changectx instances.
746 '''Find revisions matching a revset and emit changectx instances.
747
747
748 This is a convenience wrapper around ``revs()`` that iterates the
748 This is a convenience wrapper around ``revs()`` that iterates the
749 result and is a generator of changectx instances.
749 result and is a generator of changectx instances.
750
750
751 Revset aliases from the configuration are not expanded. To expand
751 Revset aliases from the configuration are not expanded. To expand
752 user aliases, consider calling ``scmutil.revrange()``.
752 user aliases, consider calling ``scmutil.revrange()``.
753 '''
753 '''
754 for r in self.revs(expr, *args):
754 for r in self.revs(expr, *args):
755 yield self[r]
755 yield self[r]
756
756
757 def anyrevs(self, specs, user=False, localalias=None):
757 def anyrevs(self, specs, user=False, localalias=None):
758 '''Find revisions matching one of the given revsets.
758 '''Find revisions matching one of the given revsets.
759
759
760 Revset aliases from the configuration are not expanded by default. To
760 Revset aliases from the configuration are not expanded by default. To
761 expand user aliases, specify ``user=True``. To provide some local
761 expand user aliases, specify ``user=True``. To provide some local
762 definitions overriding user aliases, set ``localalias`` to
762 definitions overriding user aliases, set ``localalias`` to
763 ``{name: definitionstring}``.
763 ``{name: definitionstring}``.
764 '''
764 '''
765 if user:
765 if user:
766 m = revset.matchany(self.ui, specs, repo=self,
766 m = revset.matchany(self.ui, specs, repo=self,
767 localalias=localalias)
767 localalias=localalias)
768 else:
768 else:
769 m = revset.matchany(None, specs, localalias=localalias)
769 m = revset.matchany(None, specs, localalias=localalias)
770 return m(self)
770 return m(self)
771
771
772 def url(self):
772 def url(self):
773 return 'file:' + self.root
773 return 'file:' + self.root
774
774
775 def hook(self, name, throw=False, **args):
775 def hook(self, name, throw=False, **args):
776 """Call a hook, passing this repo instance.
776 """Call a hook, passing this repo instance.
777
777
778 This a convenience method to aid invoking hooks. Extensions likely
778 This a convenience method to aid invoking hooks. Extensions likely
779 won't call this unless they have registered a custom hook or are
779 won't call this unless they have registered a custom hook or are
780 replacing code that is expected to call a hook.
780 replacing code that is expected to call a hook.
781 """
781 """
782 return hook.hook(self.ui, self, name, throw, **args)
782 return hook.hook(self.ui, self, name, throw, **args)
783
783
784 @filteredpropertycache
784 @filteredpropertycache
785 def _tagscache(self):
785 def _tagscache(self):
786 '''Returns a tagscache object that contains various tags related
786 '''Returns a tagscache object that contains various tags related
787 caches.'''
787 caches.'''
788
788
789 # This simplifies its cache management by having one decorated
789 # This simplifies its cache management by having one decorated
790 # function (this one) and the rest simply fetch things from it.
790 # function (this one) and the rest simply fetch things from it.
791 class tagscache(object):
791 class tagscache(object):
792 def __init__(self):
792 def __init__(self):
793 # These two define the set of tags for this repository. tags
793 # These two define the set of tags for this repository. tags
794 # maps tag name to node; tagtypes maps tag name to 'global' or
794 # maps tag name to node; tagtypes maps tag name to 'global' or
795 # 'local'. (Global tags are defined by .hgtags across all
795 # 'local'. (Global tags are defined by .hgtags across all
796 # heads, and local tags are defined in .hg/localtags.)
796 # heads, and local tags are defined in .hg/localtags.)
797 # They constitute the in-memory cache of tags.
797 # They constitute the in-memory cache of tags.
798 self.tags = self.tagtypes = None
798 self.tags = self.tagtypes = None
799
799
800 self.nodetagscache = self.tagslist = None
800 self.nodetagscache = self.tagslist = None
801
801
802 cache = tagscache()
802 cache = tagscache()
803 cache.tags, cache.tagtypes = self._findtags()
803 cache.tags, cache.tagtypes = self._findtags()
804
804
805 return cache
805 return cache
806
806
807 def tags(self):
807 def tags(self):
808 '''return a mapping of tag to node'''
808 '''return a mapping of tag to node'''
809 t = {}
809 t = {}
810 if self.changelog.filteredrevs:
810 if self.changelog.filteredrevs:
811 tags, tt = self._findtags()
811 tags, tt = self._findtags()
812 else:
812 else:
813 tags = self._tagscache.tags
813 tags = self._tagscache.tags
814 for k, v in tags.iteritems():
814 for k, v in tags.iteritems():
815 try:
815 try:
816 # ignore tags to unknown nodes
816 # ignore tags to unknown nodes
817 self.changelog.rev(v)
817 self.changelog.rev(v)
818 t[k] = v
818 t[k] = v
819 except (error.LookupError, ValueError):
819 except (error.LookupError, ValueError):
820 pass
820 pass
821 return t
821 return t
822
822
823 def _findtags(self):
823 def _findtags(self):
824 '''Do the hard work of finding tags. Return a pair of dicts
824 '''Do the hard work of finding tags. Return a pair of dicts
825 (tags, tagtypes) where tags maps tag name to node, and tagtypes
825 (tags, tagtypes) where tags maps tag name to node, and tagtypes
826 maps tag name to a string like \'global\' or \'local\'.
826 maps tag name to a string like \'global\' or \'local\'.
827 Subclasses or extensions are free to add their own tags, but
827 Subclasses or extensions are free to add their own tags, but
828 should be aware that the returned dicts will be retained for the
828 should be aware that the returned dicts will be retained for the
829 duration of the localrepo object.'''
829 duration of the localrepo object.'''
830
830
831 # XXX what tagtype should subclasses/extensions use? Currently
831 # XXX what tagtype should subclasses/extensions use? Currently
832 # mq and bookmarks add tags, but do not set the tagtype at all.
832 # mq and bookmarks add tags, but do not set the tagtype at all.
833 # Should each extension invent its own tag type? Should there
833 # Should each extension invent its own tag type? Should there
834 # be one tagtype for all such "virtual" tags? Or is the status
834 # be one tagtype for all such "virtual" tags? Or is the status
835 # quo fine?
835 # quo fine?
836
836
837
837
838 # map tag name to (node, hist)
838 # map tag name to (node, hist)
839 alltags = tagsmod.findglobaltags(self.ui, self)
839 alltags = tagsmod.findglobaltags(self.ui, self)
840 # map tag name to tag type
840 # map tag name to tag type
841 tagtypes = dict((tag, 'global') for tag in alltags)
841 tagtypes = dict((tag, 'global') for tag in alltags)
842
842
843 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
843 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
844
844
845 # Build the return dicts. Have to re-encode tag names because
845 # Build the return dicts. Have to re-encode tag names because
846 # the tags module always uses UTF-8 (in order not to lose info
846 # the tags module always uses UTF-8 (in order not to lose info
847 # writing to the cache), but the rest of Mercurial wants them in
847 # writing to the cache), but the rest of Mercurial wants them in
848 # local encoding.
848 # local encoding.
849 tags = {}
849 tags = {}
850 for (name, (node, hist)) in alltags.iteritems():
850 for (name, (node, hist)) in alltags.iteritems():
851 if node != nullid:
851 if node != nullid:
852 tags[encoding.tolocal(name)] = node
852 tags[encoding.tolocal(name)] = node
853 tags['tip'] = self.changelog.tip()
853 tags['tip'] = self.changelog.tip()
854 tagtypes = dict([(encoding.tolocal(name), value)
854 tagtypes = dict([(encoding.tolocal(name), value)
855 for (name, value) in tagtypes.iteritems()])
855 for (name, value) in tagtypes.iteritems()])
856 return (tags, tagtypes)
856 return (tags, tagtypes)
857
857
858 def tagtype(self, tagname):
858 def tagtype(self, tagname):
859 '''
859 '''
860 return the type of the given tag. result can be:
860 return the type of the given tag. result can be:
861
861
862 'local' : a local tag
862 'local' : a local tag
863 'global' : a global tag
863 'global' : a global tag
864 None : tag does not exist
864 None : tag does not exist
865 '''
865 '''
866
866
867 return self._tagscache.tagtypes.get(tagname)
867 return self._tagscache.tagtypes.get(tagname)
868
868
869 def tagslist(self):
869 def tagslist(self):
870 '''return a list of tags ordered by revision'''
870 '''return a list of tags ordered by revision'''
871 if not self._tagscache.tagslist:
871 if not self._tagscache.tagslist:
872 l = []
872 l = []
873 for t, n in self.tags().iteritems():
873 for t, n in self.tags().iteritems():
874 l.append((self.changelog.rev(n), t, n))
874 l.append((self.changelog.rev(n), t, n))
875 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
875 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
876
876
877 return self._tagscache.tagslist
877 return self._tagscache.tagslist
878
878
879 def nodetags(self, node):
879 def nodetags(self, node):
880 '''return the tags associated with a node'''
880 '''return the tags associated with a node'''
881 if not self._tagscache.nodetagscache:
881 if not self._tagscache.nodetagscache:
882 nodetagscache = {}
882 nodetagscache = {}
883 for t, n in self._tagscache.tags.iteritems():
883 for t, n in self._tagscache.tags.iteritems():
884 nodetagscache.setdefault(n, []).append(t)
884 nodetagscache.setdefault(n, []).append(t)
885 for tags in nodetagscache.itervalues():
885 for tags in nodetagscache.itervalues():
886 tags.sort()
886 tags.sort()
887 self._tagscache.nodetagscache = nodetagscache
887 self._tagscache.nodetagscache = nodetagscache
888 return self._tagscache.nodetagscache.get(node, [])
888 return self._tagscache.nodetagscache.get(node, [])
889
889
890 def nodebookmarks(self, node):
890 def nodebookmarks(self, node):
891 """return the list of bookmarks pointing to the specified node"""
891 """return the list of bookmarks pointing to the specified node"""
892 marks = []
892 marks = []
893 for bookmark, n in self._bookmarks.iteritems():
893 for bookmark, n in self._bookmarks.iteritems():
894 if n == node:
894 if n == node:
895 marks.append(bookmark)
895 marks.append(bookmark)
896 return sorted(marks)
896 return sorted(marks)
897
897
898 def branchmap(self):
898 def branchmap(self):
899 '''returns a dictionary {branch: [branchheads]} with branchheads
899 '''returns a dictionary {branch: [branchheads]} with branchheads
900 ordered by increasing revision number'''
900 ordered by increasing revision number'''
901 branchmap.updatecache(self)
901 branchmap.updatecache(self)
902 return self._branchcaches[self.filtername]
902 return self._branchcaches[self.filtername]
903
903
904 @unfilteredmethod
904 @unfilteredmethod
905 def revbranchcache(self):
905 def revbranchcache(self):
906 if not self._revbranchcache:
906 if not self._revbranchcache:
907 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
907 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
908 return self._revbranchcache
908 return self._revbranchcache
909
909
910 def branchtip(self, branch, ignoremissing=False):
910 def branchtip(self, branch, ignoremissing=False):
911 '''return the tip node for a given branch
911 '''return the tip node for a given branch
912
912
913 If ignoremissing is True, then this method will not raise an error.
913 If ignoremissing is True, then this method will not raise an error.
914 This is helpful for callers that only expect None for a missing branch
914 This is helpful for callers that only expect None for a missing branch
915 (e.g. namespace).
915 (e.g. namespace).
916
916
917 '''
917 '''
918 try:
918 try:
919 return self.branchmap().branchtip(branch)
919 return self.branchmap().branchtip(branch)
920 except KeyError:
920 except KeyError:
921 if not ignoremissing:
921 if not ignoremissing:
922 raise error.RepoLookupError(_("unknown branch '%s'") % branch)
922 raise error.RepoLookupError(_("unknown branch '%s'") % branch)
923 else:
923 else:
924 pass
924 pass
925
925
926 def lookup(self, key):
926 def lookup(self, key):
927 return self[key].node()
927 return self[key].node()
928
928
929 def lookupbranch(self, key, remote=None):
929 def lookupbranch(self, key, remote=None):
930 repo = remote or self
930 repo = remote or self
931 if key in repo.branchmap():
931 if key in repo.branchmap():
932 return key
932 return key
933
933
934 repo = (remote and remote.local()) and remote or self
934 repo = (remote and remote.local()) and remote or self
935 return repo[key].branch()
935 return repo[key].branch()
936
936
937 def known(self, nodes):
937 def known(self, nodes):
938 cl = self.changelog
938 cl = self.changelog
939 nm = cl.nodemap
939 nm = cl.nodemap
940 filtered = cl.filteredrevs
940 filtered = cl.filteredrevs
941 result = []
941 result = []
942 for n in nodes:
942 for n in nodes:
943 r = nm.get(n)
943 r = nm.get(n)
944 resp = not (r is None or r in filtered)
944 resp = not (r is None or r in filtered)
945 result.append(resp)
945 result.append(resp)
946 return result
946 return result
947
947
948 def local(self):
948 def local(self):
949 return self
949 return self
950
950
951 def publishing(self):
951 def publishing(self):
952 # it's safe (and desirable) to trust the publish flag unconditionally
952 # it's safe (and desirable) to trust the publish flag unconditionally
953 # so that we don't finalize changes shared between users via ssh or nfs
953 # so that we don't finalize changes shared between users via ssh or nfs
954 return self.ui.configbool('phases', 'publish', untrusted=True)
954 return self.ui.configbool('phases', 'publish', untrusted=True)
955
955
956 def cancopy(self):
956 def cancopy(self):
957 # so statichttprepo's override of local() works
957 # so statichttprepo's override of local() works
958 if not self.local():
958 if not self.local():
959 return False
959 return False
960 if not self.publishing():
960 if not self.publishing():
961 return True
961 return True
962 # if publishing we can't copy if there is filtered content
962 # if publishing we can't copy if there is filtered content
963 return not self.filtered('visible').changelog.filteredrevs
963 return not self.filtered('visible').changelog.filteredrevs
964
964
965 def shared(self):
965 def shared(self):
966 '''the type of shared repository (None if not shared)'''
966 '''the type of shared repository (None if not shared)'''
967 if self.sharedpath != self.path:
967 if self.sharedpath != self.path:
968 return 'store'
968 return 'store'
969 return None
969 return None
970
970
971 def wjoin(self, f, *insidef):
971 def wjoin(self, f, *insidef):
972 return self.vfs.reljoin(self.root, f, *insidef)
972 return self.vfs.reljoin(self.root, f, *insidef)
973
973
974 def file(self, f):
974 def file(self, f):
975 if f[0] == '/':
975 if f[0] == '/':
976 f = f[1:]
976 f = f[1:]
977 return filelog.filelog(self.svfs, f)
977 return filelog.filelog(self.svfs, f)
978
978
979 def changectx(self, changeid):
979 def changectx(self, changeid):
980 return self[changeid]
980 return self[changeid]
981
981
982 def setparents(self, p1, p2=nullid):
982 def setparents(self, p1, p2=nullid):
983 with self.dirstate.parentchange():
983 with self.dirstate.parentchange():
984 copies = self.dirstate.setparents(p1, p2)
984 copies = self.dirstate.setparents(p1, p2)
985 pctx = self[p1]
985 pctx = self[p1]
986 if copies:
986 if copies:
987 # Adjust copy records, the dirstate cannot do it, it
987 # Adjust copy records, the dirstate cannot do it, it
988 # requires access to parents manifests. Preserve them
988 # requires access to parents manifests. Preserve them
989 # only for entries added to first parent.
989 # only for entries added to first parent.
990 for f in copies:
990 for f in copies:
991 if f not in pctx and copies[f] in pctx:
991 if f not in pctx and copies[f] in pctx:
992 self.dirstate.copy(copies[f], f)
992 self.dirstate.copy(copies[f], f)
993 if p2 == nullid:
993 if p2 == nullid:
994 for f, s in sorted(self.dirstate.copies().items()):
994 for f, s in sorted(self.dirstate.copies().items()):
995 if f not in pctx and s not in pctx:
995 if f not in pctx and s not in pctx:
996 self.dirstate.copy(None, f)
996 self.dirstate.copy(None, f)
997
997
998 def filectx(self, path, changeid=None, fileid=None):
998 def filectx(self, path, changeid=None, fileid=None):
999 """changeid can be a changeset revision, node, or tag.
999 """changeid can be a changeset revision, node, or tag.
1000 fileid can be a file revision or node."""
1000 fileid can be a file revision or node."""
1001 return context.filectx(self, path, changeid, fileid)
1001 return context.filectx(self, path, changeid, fileid)
1002
1002
1003 def getcwd(self):
1003 def getcwd(self):
1004 return self.dirstate.getcwd()
1004 return self.dirstate.getcwd()
1005
1005
1006 def pathto(self, f, cwd=None):
1006 def pathto(self, f, cwd=None):
1007 return self.dirstate.pathto(f, cwd)
1007 return self.dirstate.pathto(f, cwd)
1008
1008
1009 def _loadfilter(self, filter):
1009 def _loadfilter(self, filter):
1010 if filter not in self.filterpats:
1010 if filter not in self.filterpats:
1011 l = []
1011 l = []
1012 for pat, cmd in self.ui.configitems(filter):
1012 for pat, cmd in self.ui.configitems(filter):
1013 if cmd == '!':
1013 if cmd == '!':
1014 continue
1014 continue
1015 mf = matchmod.match(self.root, '', [pat])
1015 mf = matchmod.match(self.root, '', [pat])
1016 fn = None
1016 fn = None
1017 params = cmd
1017 params = cmd
1018 for name, filterfn in self._datafilters.iteritems():
1018 for name, filterfn in self._datafilters.iteritems():
1019 if cmd.startswith(name):
1019 if cmd.startswith(name):
1020 fn = filterfn
1020 fn = filterfn
1021 params = cmd[len(name):].lstrip()
1021 params = cmd[len(name):].lstrip()
1022 break
1022 break
1023 if not fn:
1023 if not fn:
1024 fn = lambda s, c, **kwargs: util.filter(s, c)
1024 fn = lambda s, c, **kwargs: util.filter(s, c)
1025 # Wrap old filters not supporting keyword arguments
1025 # Wrap old filters not supporting keyword arguments
1026 if not inspect.getargspec(fn)[2]:
1026 if not inspect.getargspec(fn)[2]:
1027 oldfn = fn
1027 oldfn = fn
1028 fn = lambda s, c, **kwargs: oldfn(s, c)
1028 fn = lambda s, c, **kwargs: oldfn(s, c)
1029 l.append((mf, fn, params))
1029 l.append((mf, fn, params))
1030 self.filterpats[filter] = l
1030 self.filterpats[filter] = l
1031 return self.filterpats[filter]
1031 return self.filterpats[filter]
1032
1032
1033 def _filter(self, filterpats, filename, data):
1033 def _filter(self, filterpats, filename, data):
1034 for mf, fn, cmd in filterpats:
1034 for mf, fn, cmd in filterpats:
1035 if mf(filename):
1035 if mf(filename):
1036 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
1036 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
1037 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
1037 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
1038 break
1038 break
1039
1039
1040 return data
1040 return data
1041
1041
1042 @unfilteredpropertycache
1042 @unfilteredpropertycache
1043 def _encodefilterpats(self):
1043 def _encodefilterpats(self):
1044 return self._loadfilter('encode')
1044 return self._loadfilter('encode')
1045
1045
1046 @unfilteredpropertycache
1046 @unfilteredpropertycache
1047 def _decodefilterpats(self):
1047 def _decodefilterpats(self):
1048 return self._loadfilter('decode')
1048 return self._loadfilter('decode')
1049
1049
1050 def adddatafilter(self, name, filter):
1050 def adddatafilter(self, name, filter):
1051 self._datafilters[name] = filter
1051 self._datafilters[name] = filter
1052
1052
1053 def wread(self, filename):
1053 def wread(self, filename):
1054 if self.wvfs.islink(filename):
1054 if self.wvfs.islink(filename):
1055 data = self.wvfs.readlink(filename)
1055 data = self.wvfs.readlink(filename)
1056 else:
1056 else:
1057 data = self.wvfs.read(filename)
1057 data = self.wvfs.read(filename)
1058 return self._filter(self._encodefilterpats, filename, data)
1058 return self._filter(self._encodefilterpats, filename, data)
1059
1059
1060 def wwrite(self, filename, data, flags, backgroundclose=False):
1060 def wwrite(self, filename, data, flags, backgroundclose=False):
1061 """write ``data`` into ``filename`` in the working directory
1061 """write ``data`` into ``filename`` in the working directory
1062
1062
1063 This returns length of written (maybe decoded) data.
1063 This returns length of written (maybe decoded) data.
1064 """
1064 """
1065 data = self._filter(self._decodefilterpats, filename, data)
1065 data = self._filter(self._decodefilterpats, filename, data)
1066 if 'l' in flags:
1066 if 'l' in flags:
1067 self.wvfs.symlink(data, filename)
1067 self.wvfs.symlink(data, filename)
1068 else:
1068 else:
1069 self.wvfs.write(filename, data, backgroundclose=backgroundclose)
1069 self.wvfs.write(filename, data, backgroundclose=backgroundclose)
1070 if 'x' in flags:
1070 if 'x' in flags:
1071 self.wvfs.setflags(filename, False, True)
1071 self.wvfs.setflags(filename, False, True)
1072 return len(data)
1072 return len(data)
1073
1073
1074 def wwritedata(self, filename, data):
1074 def wwritedata(self, filename, data):
1075 return self._filter(self._decodefilterpats, filename, data)
1075 return self._filter(self._decodefilterpats, filename, data)
1076
1076
1077 def currenttransaction(self):
1077 def currenttransaction(self):
1078 """return the current transaction or None if non exists"""
1078 """return the current transaction or None if non exists"""
1079 if self._transref:
1079 if self._transref:
1080 tr = self._transref()
1080 tr = self._transref()
1081 else:
1081 else:
1082 tr = None
1082 tr = None
1083
1083
1084 if tr and tr.running():
1084 if tr and tr.running():
1085 return tr
1085 return tr
1086 return None
1086 return None
1087
1087
1088 def transaction(self, desc, report=None):
1088 def transaction(self, desc, report=None):
1089 if (self.ui.configbool('devel', 'all-warnings')
1089 if (self.ui.configbool('devel', 'all-warnings')
1090 or self.ui.configbool('devel', 'check-locks')):
1090 or self.ui.configbool('devel', 'check-locks')):
1091 if self._currentlock(self._lockref) is None:
1091 if self._currentlock(self._lockref) is None:
1092 raise error.ProgrammingError('transaction requires locking')
1092 raise error.ProgrammingError('transaction requires locking')
1093 tr = self.currenttransaction()
1093 tr = self.currenttransaction()
1094 if tr is not None:
1094 if tr is not None:
1095 return tr.nest()
1095 return tr.nest()
1096
1096
1097 # abort here if the journal already exists
1097 # abort here if the journal already exists
1098 if self.svfs.exists("journal"):
1098 if self.svfs.exists("journal"):
1099 raise error.RepoError(
1099 raise error.RepoError(
1100 _("abandoned transaction found"),
1100 _("abandoned transaction found"),
1101 hint=_("run 'hg recover' to clean up transaction"))
1101 hint=_("run 'hg recover' to clean up transaction"))
1102
1102
1103 idbase = "%.40f#%f" % (random.random(), time.time())
1103 idbase = "%.40f#%f" % (random.random(), time.time())
1104 ha = hex(hashlib.sha1(idbase).digest())
1104 ha = hex(hashlib.sha1(idbase).digest())
1105 txnid = 'TXN:' + ha
1105 txnid = 'TXN:' + ha
1106 self.hook('pretxnopen', throw=True, txnname=desc, txnid=txnid)
1106 self.hook('pretxnopen', throw=True, txnname=desc, txnid=txnid)
1107
1107
1108 self._writejournal(desc)
1108 self._writejournal(desc)
1109 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
1109 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
1110 if report:
1110 if report:
1111 rp = report
1111 rp = report
1112 else:
1112 else:
1113 rp = self.ui.warn
1113 rp = self.ui.warn
1114 vfsmap = {'plain': self.vfs} # root of .hg/
1114 vfsmap = {'plain': self.vfs} # root of .hg/
1115 # we must avoid cyclic reference between repo and transaction.
1115 # we must avoid cyclic reference between repo and transaction.
1116 reporef = weakref.ref(self)
1116 reporef = weakref.ref(self)
1117 # Code to track tag movement
1117 # Code to track tag movement
1118 #
1118 #
1119 # Since tags are all handled as file content, it is actually quite hard
1119 # Since tags are all handled as file content, it is actually quite hard
1120 # to track these movement from a code perspective. So we fallback to a
1120 # to track these movement from a code perspective. So we fallback to a
1121 # tracking at the repository level. One could envision to track changes
1121 # tracking at the repository level. One could envision to track changes
1122 # to the '.hgtags' file through changegroup apply but that fails to
1122 # to the '.hgtags' file through changegroup apply but that fails to
1123 # cope with case where transaction expose new heads without changegroup
1123 # cope with case where transaction expose new heads without changegroup
1124 # being involved (eg: phase movement).
1124 # being involved (eg: phase movement).
1125 #
1125 #
1126 # For now, We gate the feature behind a flag since this likely comes
1126 # For now, We gate the feature behind a flag since this likely comes
1127 # with performance impacts. The current code run more often than needed
1127 # with performance impacts. The current code run more often than needed
1128 # and do not use caches as much as it could. The current focus is on
1128 # and do not use caches as much as it could. The current focus is on
1129 # the behavior of the feature so we disable it by default. The flag
1129 # the behavior of the feature so we disable it by default. The flag
1130 # will be removed when we are happy with the performance impact.
1130 # will be removed when we are happy with the performance impact.
1131 #
1131 #
1132 # Once this feature is no longer experimental move the following
1132 # Once this feature is no longer experimental move the following
1133 # documentation to the appropriate help section:
1133 # documentation to the appropriate help section:
1134 #
1134 #
1135 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
1135 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
1136 # tags (new or changed or deleted tags). In addition the details of
1136 # tags (new or changed or deleted tags). In addition the details of
1137 # these changes are made available in a file at:
1137 # these changes are made available in a file at:
1138 # ``REPOROOT/.hg/changes/tags.changes``.
1138 # ``REPOROOT/.hg/changes/tags.changes``.
1139 # Make sure you check for HG_TAG_MOVED before reading that file as it
1139 # Make sure you check for HG_TAG_MOVED before reading that file as it
1140 # might exist from a previous transaction even if no tag were touched
1140 # might exist from a previous transaction even if no tag were touched
1141 # in this one. Changes are recorded in a line base format::
1141 # in this one. Changes are recorded in a line base format::
1142 #
1142 #
1143 # <action> <hex-node> <tag-name>\n
1143 # <action> <hex-node> <tag-name>\n
1144 #
1144 #
1145 # Actions are defined as follow:
1145 # Actions are defined as follow:
1146 # "-R": tag is removed,
1146 # "-R": tag is removed,
1147 # "+A": tag is added,
1147 # "+A": tag is added,
1148 # "-M": tag is moved (old value),
1148 # "-M": tag is moved (old value),
1149 # "+M": tag is moved (new value),
1149 # "+M": tag is moved (new value),
1150 tracktags = lambda x: None
1150 tracktags = lambda x: None
1151 # experimental config: experimental.hook-track-tags
1151 # experimental config: experimental.hook-track-tags
1152 shouldtracktags = self.ui.configbool('experimental', 'hook-track-tags')
1152 shouldtracktags = self.ui.configbool('experimental', 'hook-track-tags')
1153 if desc != 'strip' and shouldtracktags:
1153 if desc != 'strip' and shouldtracktags:
1154 oldheads = self.changelog.headrevs()
1154 oldheads = self.changelog.headrevs()
1155 def tracktags(tr2):
1155 def tracktags(tr2):
1156 repo = reporef()
1156 repo = reporef()
1157 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
1157 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
1158 newheads = repo.changelog.headrevs()
1158 newheads = repo.changelog.headrevs()
1159 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
1159 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
1160 # notes: we compare lists here.
1160 # notes: we compare lists here.
1161 # As we do it only once buiding set would not be cheaper
1161 # As we do it only once buiding set would not be cheaper
1162 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
1162 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
1163 if changes:
1163 if changes:
1164 tr2.hookargs['tag_moved'] = '1'
1164 tr2.hookargs['tag_moved'] = '1'
1165 with repo.vfs('changes/tags.changes', 'w',
1165 with repo.vfs('changes/tags.changes', 'w',
1166 atomictemp=True) as changesfile:
1166 atomictemp=True) as changesfile:
1167 # note: we do not register the file to the transaction
1167 # note: we do not register the file to the transaction
1168 # because we needs it to still exist on the transaction
1168 # because we needs it to still exist on the transaction
1169 # is close (for txnclose hooks)
1169 # is close (for txnclose hooks)
1170 tagsmod.writediff(changesfile, changes)
1170 tagsmod.writediff(changesfile, changes)
1171 def validate(tr2):
1171 def validate(tr2):
1172 """will run pre-closing hooks"""
1172 """will run pre-closing hooks"""
1173 # XXX the transaction API is a bit lacking here so we take a hacky
1173 # XXX the transaction API is a bit lacking here so we take a hacky
1174 # path for now
1174 # path for now
1175 #
1175 #
1176 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
1176 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
1177 # dict is copied before these run. In addition we needs the data
1177 # dict is copied before these run. In addition we needs the data
1178 # available to in memory hooks too.
1178 # available to in memory hooks too.
1179 #
1179 #
1180 # Moreover, we also need to make sure this runs before txnclose
1180 # Moreover, we also need to make sure this runs before txnclose
1181 # hooks and there is no "pending" mechanism that would execute
1181 # hooks and there is no "pending" mechanism that would execute
1182 # logic only if hooks are about to run.
1182 # logic only if hooks are about to run.
1183 #
1183 #
1184 # Fixing this limitation of the transaction is also needed to track
1184 # Fixing this limitation of the transaction is also needed to track
1185 # other families of changes (bookmarks, phases, obsolescence).
1185 # other families of changes (bookmarks, phases, obsolescence).
1186 #
1186 #
1187 # This will have to be fixed before we remove the experimental
1187 # This will have to be fixed before we remove the experimental
1188 # gating.
1188 # gating.
1189 tracktags(tr2)
1189 tracktags(tr2)
1190 reporef().hook('pretxnclose', throw=True,
1190 reporef().hook('pretxnclose', throw=True,
1191 txnname=desc, **pycompat.strkwargs(tr.hookargs))
1191 txnname=desc, **pycompat.strkwargs(tr.hookargs))
1192 def releasefn(tr, success):
1192 def releasefn(tr, success):
1193 repo = reporef()
1193 repo = reporef()
1194 if success:
1194 if success:
1195 # this should be explicitly invoked here, because
1195 # this should be explicitly invoked here, because
1196 # in-memory changes aren't written out at closing
1196 # in-memory changes aren't written out at closing
1197 # transaction, if tr.addfilegenerator (via
1197 # transaction, if tr.addfilegenerator (via
1198 # dirstate.write or so) isn't invoked while
1198 # dirstate.write or so) isn't invoked while
1199 # transaction running
1199 # transaction running
1200 repo.dirstate.write(None)
1200 repo.dirstate.write(None)
1201 else:
1201 else:
1202 # discard all changes (including ones already written
1202 # discard all changes (including ones already written
1203 # out) in this transaction
1203 # out) in this transaction
1204 repo.dirstate.restorebackup(None, 'journal.dirstate')
1204 repo.dirstate.restorebackup(None, 'journal.dirstate')
1205
1205
1206 repo.invalidate(clearfilecache=True)
1206 repo.invalidate(clearfilecache=True)
1207
1207
1208 tr = transaction.transaction(rp, self.svfs, vfsmap,
1208 tr = transaction.transaction(rp, self.svfs, vfsmap,
1209 "journal",
1209 "journal",
1210 "undo",
1210 "undo",
1211 aftertrans(renames),
1211 aftertrans(renames),
1212 self.store.createmode,
1212 self.store.createmode,
1213 validator=validate,
1213 validator=validate,
1214 releasefn=releasefn,
1214 releasefn=releasefn,
1215 checkambigfiles=_cachedfiles)
1215 checkambigfiles=_cachedfiles)
1216 tr.changes['revs'] = set()
1216 tr.changes['revs'] = set()
1217 tr.changes['obsmarkers'] = set()
1217 tr.changes['obsmarkers'] = set()
1218 tr.changes['phases'] = {}
1218 tr.changes['phases'] = {}
1219 tr.changes['bookmarks'] = {}
1219 tr.changes['bookmarks'] = {}
1220
1220
1221 tr.hookargs['txnid'] = txnid
1221 tr.hookargs['txnid'] = txnid
1222 # note: writing the fncache only during finalize mean that the file is
1222 # note: writing the fncache only during finalize mean that the file is
1223 # outdated when running hooks. As fncache is used for streaming clone,
1223 # outdated when running hooks. As fncache is used for streaming clone,
1224 # this is not expected to break anything that happen during the hooks.
1224 # this is not expected to break anything that happen during the hooks.
1225 tr.addfinalize('flush-fncache', self.store.write)
1225 tr.addfinalize('flush-fncache', self.store.write)
1226 def txnclosehook(tr2):
1226 def txnclosehook(tr2):
1227 """To be run if transaction is successful, will schedule a hook run
1227 """To be run if transaction is successful, will schedule a hook run
1228 """
1228 """
1229 # Don't reference tr2 in hook() so we don't hold a reference.
1229 # Don't reference tr2 in hook() so we don't hold a reference.
1230 # This reduces memory consumption when there are multiple
1230 # This reduces memory consumption when there are multiple
1231 # transactions per lock. This can likely go away if issue5045
1231 # transactions per lock. This can likely go away if issue5045
1232 # fixes the function accumulation.
1232 # fixes the function accumulation.
1233 hookargs = tr2.hookargs
1233 hookargs = tr2.hookargs
1234
1234
1235 def hook():
1235 def hook():
1236 reporef().hook('txnclose', throw=False, txnname=desc,
1236 reporef().hook('txnclose', throw=False, txnname=desc,
1237 **pycompat.strkwargs(hookargs))
1237 **pycompat.strkwargs(hookargs))
1238 reporef()._afterlock(hook)
1238 reporef()._afterlock(hook)
1239 tr.addfinalize('txnclose-hook', txnclosehook)
1239 tr.addfinalize('txnclose-hook', txnclosehook)
1240 tr.addpostclose('warms-cache', self._buildcacheupdater(tr))
1240 tr.addpostclose('warms-cache', self._buildcacheupdater(tr))
1241 def txnaborthook(tr2):
1241 def txnaborthook(tr2):
1242 """To be run if transaction is aborted
1242 """To be run if transaction is aborted
1243 """
1243 """
1244 reporef().hook('txnabort', throw=False, txnname=desc,
1244 reporef().hook('txnabort', throw=False, txnname=desc,
1245 **tr2.hookargs)
1245 **tr2.hookargs)
1246 tr.addabort('txnabort-hook', txnaborthook)
1246 tr.addabort('txnabort-hook', txnaborthook)
1247 # avoid eager cache invalidation. in-memory data should be identical
1247 # avoid eager cache invalidation. in-memory data should be identical
1248 # to stored data if transaction has no error.
1248 # to stored data if transaction has no error.
1249 tr.addpostclose('refresh-filecachestats', self._refreshfilecachestats)
1249 tr.addpostclose('refresh-filecachestats', self._refreshfilecachestats)
1250 self._transref = weakref.ref(tr)
1250 self._transref = weakref.ref(tr)
1251 return tr
1251 return tr
1252
1252
1253 def _journalfiles(self):
1253 def _journalfiles(self):
1254 return ((self.svfs, 'journal'),
1254 return ((self.svfs, 'journal'),
1255 (self.vfs, 'journal.dirstate'),
1255 (self.vfs, 'journal.dirstate'),
1256 (self.vfs, 'journal.branch'),
1256 (self.vfs, 'journal.branch'),
1257 (self.vfs, 'journal.desc'),
1257 (self.vfs, 'journal.desc'),
1258 (self.vfs, 'journal.bookmarks'),
1258 (self.vfs, 'journal.bookmarks'),
1259 (self.svfs, 'journal.phaseroots'))
1259 (self.svfs, 'journal.phaseroots'))
1260
1260
1261 def undofiles(self):
1261 def undofiles(self):
1262 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
1262 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
1263
1263
1264 @unfilteredmethod
1264 @unfilteredmethod
1265 def _writejournal(self, desc):
1265 def _writejournal(self, desc):
1266 self.dirstate.savebackup(None, 'journal.dirstate')
1266 self.dirstate.savebackup(None, 'journal.dirstate')
1267 self.vfs.write("journal.branch",
1267 self.vfs.write("journal.branch",
1268 encoding.fromlocal(self.dirstate.branch()))
1268 encoding.fromlocal(self.dirstate.branch()))
1269 self.vfs.write("journal.desc",
1269 self.vfs.write("journal.desc",
1270 "%d\n%s\n" % (len(self), desc))
1270 "%d\n%s\n" % (len(self), desc))
1271 self.vfs.write("journal.bookmarks",
1271 self.vfs.write("journal.bookmarks",
1272 self.vfs.tryread("bookmarks"))
1272 self.vfs.tryread("bookmarks"))
1273 self.svfs.write("journal.phaseroots",
1273 self.svfs.write("journal.phaseroots",
1274 self.svfs.tryread("phaseroots"))
1274 self.svfs.tryread("phaseroots"))
1275
1275
1276 def recover(self):
1276 def recover(self):
1277 with self.lock():
1277 with self.lock():
1278 if self.svfs.exists("journal"):
1278 if self.svfs.exists("journal"):
1279 self.ui.status(_("rolling back interrupted transaction\n"))
1279 self.ui.status(_("rolling back interrupted transaction\n"))
1280 vfsmap = {'': self.svfs,
1280 vfsmap = {'': self.svfs,
1281 'plain': self.vfs,}
1281 'plain': self.vfs,}
1282 transaction.rollback(self.svfs, vfsmap, "journal",
1282 transaction.rollback(self.svfs, vfsmap, "journal",
1283 self.ui.warn,
1283 self.ui.warn,
1284 checkambigfiles=_cachedfiles)
1284 checkambigfiles=_cachedfiles)
1285 self.invalidate()
1285 self.invalidate()
1286 return True
1286 return True
1287 else:
1287 else:
1288 self.ui.warn(_("no interrupted transaction available\n"))
1288 self.ui.warn(_("no interrupted transaction available\n"))
1289 return False
1289 return False
1290
1290
1291 def rollback(self, dryrun=False, force=False):
1291 def rollback(self, dryrun=False, force=False):
1292 wlock = lock = dsguard = None
1292 wlock = lock = dsguard = None
1293 try:
1293 try:
1294 wlock = self.wlock()
1294 wlock = self.wlock()
1295 lock = self.lock()
1295 lock = self.lock()
1296 if self.svfs.exists("undo"):
1296 if self.svfs.exists("undo"):
1297 dsguard = dirstateguard.dirstateguard(self, 'rollback')
1297 dsguard = dirstateguard.dirstateguard(self, 'rollback')
1298
1298
1299 return self._rollback(dryrun, force, dsguard)
1299 return self._rollback(dryrun, force, dsguard)
1300 else:
1300 else:
1301 self.ui.warn(_("no rollback information available\n"))
1301 self.ui.warn(_("no rollback information available\n"))
1302 return 1
1302 return 1
1303 finally:
1303 finally:
1304 release(dsguard, lock, wlock)
1304 release(dsguard, lock, wlock)
1305
1305
1306 @unfilteredmethod # Until we get smarter cache management
1306 @unfilteredmethod # Until we get smarter cache management
1307 def _rollback(self, dryrun, force, dsguard):
1307 def _rollback(self, dryrun, force, dsguard):
1308 ui = self.ui
1308 ui = self.ui
1309 try:
1309 try:
1310 args = self.vfs.read('undo.desc').splitlines()
1310 args = self.vfs.read('undo.desc').splitlines()
1311 (oldlen, desc, detail) = (int(args[0]), args[1], None)
1311 (oldlen, desc, detail) = (int(args[0]), args[1], None)
1312 if len(args) >= 3:
1312 if len(args) >= 3:
1313 detail = args[2]
1313 detail = args[2]
1314 oldtip = oldlen - 1
1314 oldtip = oldlen - 1
1315
1315
1316 if detail and ui.verbose:
1316 if detail and ui.verbose:
1317 msg = (_('repository tip rolled back to revision %d'
1317 msg = (_('repository tip rolled back to revision %d'
1318 ' (undo %s: %s)\n')
1318 ' (undo %s: %s)\n')
1319 % (oldtip, desc, detail))
1319 % (oldtip, desc, detail))
1320 else:
1320 else:
1321 msg = (_('repository tip rolled back to revision %d'
1321 msg = (_('repository tip rolled back to revision %d'
1322 ' (undo %s)\n')
1322 ' (undo %s)\n')
1323 % (oldtip, desc))
1323 % (oldtip, desc))
1324 except IOError:
1324 except IOError:
1325 msg = _('rolling back unknown transaction\n')
1325 msg = _('rolling back unknown transaction\n')
1326 desc = None
1326 desc = None
1327
1327
1328 if not force and self['.'] != self['tip'] and desc == 'commit':
1328 if not force and self['.'] != self['tip'] and desc == 'commit':
1329 raise error.Abort(
1329 raise error.Abort(
1330 _('rollback of last commit while not checked out '
1330 _('rollback of last commit while not checked out '
1331 'may lose data'), hint=_('use -f to force'))
1331 'may lose data'), hint=_('use -f to force'))
1332
1332
1333 ui.status(msg)
1333 ui.status(msg)
1334 if dryrun:
1334 if dryrun:
1335 return 0
1335 return 0
1336
1336
1337 parents = self.dirstate.parents()
1337 parents = self.dirstate.parents()
1338 self.destroying()
1338 self.destroying()
1339 vfsmap = {'plain': self.vfs, '': self.svfs}
1339 vfsmap = {'plain': self.vfs, '': self.svfs}
1340 transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn,
1340 transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn,
1341 checkambigfiles=_cachedfiles)
1341 checkambigfiles=_cachedfiles)
1342 if self.vfs.exists('undo.bookmarks'):
1342 if self.vfs.exists('undo.bookmarks'):
1343 self.vfs.rename('undo.bookmarks', 'bookmarks', checkambig=True)
1343 self.vfs.rename('undo.bookmarks', 'bookmarks', checkambig=True)
1344 if self.svfs.exists('undo.phaseroots'):
1344 if self.svfs.exists('undo.phaseroots'):
1345 self.svfs.rename('undo.phaseroots', 'phaseroots', checkambig=True)
1345 self.svfs.rename('undo.phaseroots', 'phaseroots', checkambig=True)
1346 self.invalidate()
1346 self.invalidate()
1347
1347
1348 parentgone = (parents[0] not in self.changelog.nodemap or
1348 parentgone = (parents[0] not in self.changelog.nodemap or
1349 parents[1] not in self.changelog.nodemap)
1349 parents[1] not in self.changelog.nodemap)
1350 if parentgone:
1350 if parentgone:
1351 # prevent dirstateguard from overwriting already restored one
1351 # prevent dirstateguard from overwriting already restored one
1352 dsguard.close()
1352 dsguard.close()
1353
1353
1354 self.dirstate.restorebackup(None, 'undo.dirstate')
1354 self.dirstate.restorebackup(None, 'undo.dirstate')
1355 try:
1355 try:
1356 branch = self.vfs.read('undo.branch')
1356 branch = self.vfs.read('undo.branch')
1357 self.dirstate.setbranch(encoding.tolocal(branch))
1357 self.dirstate.setbranch(encoding.tolocal(branch))
1358 except IOError:
1358 except IOError:
1359 ui.warn(_('named branch could not be reset: '
1359 ui.warn(_('named branch could not be reset: '
1360 'current branch is still \'%s\'\n')
1360 'current branch is still \'%s\'\n')
1361 % self.dirstate.branch())
1361 % self.dirstate.branch())
1362
1362
1363 parents = tuple([p.rev() for p in self[None].parents()])
1363 parents = tuple([p.rev() for p in self[None].parents()])
1364 if len(parents) > 1:
1364 if len(parents) > 1:
1365 ui.status(_('working directory now based on '
1365 ui.status(_('working directory now based on '
1366 'revisions %d and %d\n') % parents)
1366 'revisions %d and %d\n') % parents)
1367 else:
1367 else:
1368 ui.status(_('working directory now based on '
1368 ui.status(_('working directory now based on '
1369 'revision %d\n') % parents)
1369 'revision %d\n') % parents)
1370 mergemod.mergestate.clean(self, self['.'].node())
1370 mergemod.mergestate.clean(self, self['.'].node())
1371
1371
1372 # TODO: if we know which new heads may result from this rollback, pass
1372 # TODO: if we know which new heads may result from this rollback, pass
1373 # them to destroy(), which will prevent the branchhead cache from being
1373 # them to destroy(), which will prevent the branchhead cache from being
1374 # invalidated.
1374 # invalidated.
1375 self.destroyed()
1375 self.destroyed()
1376 return 0
1376 return 0
1377
1377
1378 def _buildcacheupdater(self, newtransaction):
1378 def _buildcacheupdater(self, newtransaction):
1379 """called during transaction to build the callback updating cache
1379 """called during transaction to build the callback updating cache
1380
1380
1381 Lives on the repository to help extension who might want to augment
1381 Lives on the repository to help extension who might want to augment
1382 this logic. For this purpose, the created transaction is passed to the
1382 this logic. For this purpose, the created transaction is passed to the
1383 method.
1383 method.
1384 """
1384 """
1385 # we must avoid cyclic reference between repo and transaction.
1385 # we must avoid cyclic reference between repo and transaction.
1386 reporef = weakref.ref(self)
1386 reporef = weakref.ref(self)
1387 def updater(tr):
1387 def updater(tr):
1388 repo = reporef()
1388 repo = reporef()
1389 repo.updatecaches(tr)
1389 repo.updatecaches(tr)
1390 return updater
1390 return updater
1391
1391
1392 @unfilteredmethod
1392 @unfilteredmethod
1393 def updatecaches(self, tr=None):
1393 def updatecaches(self, tr=None):
1394 """warm appropriate caches
1394 """warm appropriate caches
1395
1395
1396 If this function is called after a transaction closed. The transaction
1396 If this function is called after a transaction closed. The transaction
1397 will be available in the 'tr' argument. This can be used to selectively
1397 will be available in the 'tr' argument. This can be used to selectively
1398 update caches relevant to the changes in that transaction.
1398 update caches relevant to the changes in that transaction.
1399 """
1399 """
1400 if tr is not None and tr.hookargs.get('source') == 'strip':
1400 if tr is not None and tr.hookargs.get('source') == 'strip':
1401 # During strip, many caches are invalid but
1401 # During strip, many caches are invalid but
1402 # later call to `destroyed` will refresh them.
1402 # later call to `destroyed` will refresh them.
1403 return
1403 return
1404
1404
1405 if tr is None or tr.changes['revs']:
1405 if tr is None or tr.changes['revs']:
1406 # updating the unfiltered branchmap should refresh all the others,
1406 # updating the unfiltered branchmap should refresh all the others,
1407 self.ui.debug('updating the branch cache\n')
1407 self.ui.debug('updating the branch cache\n')
1408 branchmap.updatecache(self.filtered('served'))
1408 branchmap.updatecache(self.filtered('served'))
1409
1409
1410 def invalidatecaches(self):
1410 def invalidatecaches(self):
1411
1411
1412 if '_tagscache' in vars(self):
1412 if '_tagscache' in vars(self):
1413 # can't use delattr on proxy
1413 # can't use delattr on proxy
1414 del self.__dict__['_tagscache']
1414 del self.__dict__['_tagscache']
1415
1415
1416 self.unfiltered()._branchcaches.clear()
1416 self.unfiltered()._branchcaches.clear()
1417 self.invalidatevolatilesets()
1417 self.invalidatevolatilesets()
1418 self._sparsesignaturecache.clear()
1418 self._sparsesignaturecache.clear()
1419
1419
1420 def invalidatevolatilesets(self):
1420 def invalidatevolatilesets(self):
1421 self.filteredrevcache.clear()
1421 self.filteredrevcache.clear()
1422 obsolete.clearobscaches(self)
1422 obsolete.clearobscaches(self)
1423
1423
1424 def invalidatedirstate(self):
1424 def invalidatedirstate(self):
1425 '''Invalidates the dirstate, causing the next call to dirstate
1425 '''Invalidates the dirstate, causing the next call to dirstate
1426 to check if it was modified since the last time it was read,
1426 to check if it was modified since the last time it was read,
1427 rereading it if it has.
1427 rereading it if it has.
1428
1428
1429 This is different to dirstate.invalidate() that it doesn't always
1429 This is different to dirstate.invalidate() that it doesn't always
1430 rereads the dirstate. Use dirstate.invalidate() if you want to
1430 rereads the dirstate. Use dirstate.invalidate() if you want to
1431 explicitly read the dirstate again (i.e. restoring it to a previous
1431 explicitly read the dirstate again (i.e. restoring it to a previous
1432 known good state).'''
1432 known good state).'''
1433 if hasunfilteredcache(self, 'dirstate'):
1433 if hasunfilteredcache(self, 'dirstate'):
1434 for k in self.dirstate._filecache:
1434 for k in self.dirstate._filecache:
1435 try:
1435 try:
1436 delattr(self.dirstate, k)
1436 delattr(self.dirstate, k)
1437 except AttributeError:
1437 except AttributeError:
1438 pass
1438 pass
1439 delattr(self.unfiltered(), 'dirstate')
1439 delattr(self.unfiltered(), 'dirstate')
1440
1440
1441 def invalidate(self, clearfilecache=False):
1441 def invalidate(self, clearfilecache=False):
1442 '''Invalidates both store and non-store parts other than dirstate
1442 '''Invalidates both store and non-store parts other than dirstate
1443
1443
1444 If a transaction is running, invalidation of store is omitted,
1444 If a transaction is running, invalidation of store is omitted,
1445 because discarding in-memory changes might cause inconsistency
1445 because discarding in-memory changes might cause inconsistency
1446 (e.g. incomplete fncache causes unintentional failure, but
1446 (e.g. incomplete fncache causes unintentional failure, but
1447 redundant one doesn't).
1447 redundant one doesn't).
1448 '''
1448 '''
1449 unfiltered = self.unfiltered() # all file caches are stored unfiltered
1449 unfiltered = self.unfiltered() # all file caches are stored unfiltered
1450 for k in list(self._filecache.keys()):
1450 for k in list(self._filecache.keys()):
1451 # dirstate is invalidated separately in invalidatedirstate()
1451 # dirstate is invalidated separately in invalidatedirstate()
1452 if k == 'dirstate':
1452 if k == 'dirstate':
1453 continue
1453 continue
1454
1454
1455 if clearfilecache:
1455 if clearfilecache:
1456 del self._filecache[k]
1456 del self._filecache[k]
1457 try:
1457 try:
1458 delattr(unfiltered, k)
1458 delattr(unfiltered, k)
1459 except AttributeError:
1459 except AttributeError:
1460 pass
1460 pass
1461 self.invalidatecaches()
1461 self.invalidatecaches()
1462 if not self.currenttransaction():
1462 if not self.currenttransaction():
1463 # TODO: Changing contents of store outside transaction
1463 # TODO: Changing contents of store outside transaction
1464 # causes inconsistency. We should make in-memory store
1464 # causes inconsistency. We should make in-memory store
1465 # changes detectable, and abort if changed.
1465 # changes detectable, and abort if changed.
1466 self.store.invalidatecaches()
1466 self.store.invalidatecaches()
1467
1467
1468 def invalidateall(self):
1468 def invalidateall(self):
1469 '''Fully invalidates both store and non-store parts, causing the
1469 '''Fully invalidates both store and non-store parts, causing the
1470 subsequent operation to reread any outside changes.'''
1470 subsequent operation to reread any outside changes.'''
1471 # extension should hook this to invalidate its caches
1471 # extension should hook this to invalidate its caches
1472 self.invalidate()
1472 self.invalidate()
1473 self.invalidatedirstate()
1473 self.invalidatedirstate()
1474
1474
1475 @unfilteredmethod
1475 @unfilteredmethod
1476 def _refreshfilecachestats(self, tr):
1476 def _refreshfilecachestats(self, tr):
1477 """Reload stats of cached files so that they are flagged as valid"""
1477 """Reload stats of cached files so that they are flagged as valid"""
1478 for k, ce in self._filecache.items():
1478 for k, ce in self._filecache.items():
1479 if k == 'dirstate' or k not in self.__dict__:
1479 if k == 'dirstate' or k not in self.__dict__:
1480 continue
1480 continue
1481 ce.refresh()
1481 ce.refresh()
1482
1482
1483 def _lock(self, vfs, lockname, wait, releasefn, acquirefn, desc,
1483 def _lock(self, vfs, lockname, wait, releasefn, acquirefn, desc,
1484 inheritchecker=None, parentenvvar=None):
1484 inheritchecker=None, parentenvvar=None):
1485 parentlock = None
1485 parentlock = None
1486 # the contents of parentenvvar are used by the underlying lock to
1486 # the contents of parentenvvar are used by the underlying lock to
1487 # determine whether it can be inherited
1487 # determine whether it can be inherited
1488 if parentenvvar is not None:
1488 if parentenvvar is not None:
1489 parentlock = encoding.environ.get(parentenvvar)
1489 parentlock = encoding.environ.get(parentenvvar)
1490 try:
1490 try:
1491 l = lockmod.lock(vfs, lockname, 0, releasefn=releasefn,
1491 l = lockmod.lock(vfs, lockname, 0, releasefn=releasefn,
1492 acquirefn=acquirefn, desc=desc,
1492 acquirefn=acquirefn, desc=desc,
1493 inheritchecker=inheritchecker,
1493 inheritchecker=inheritchecker,
1494 parentlock=parentlock)
1494 parentlock=parentlock)
1495 except error.LockHeld as inst:
1495 except error.LockHeld as inst:
1496 if not wait:
1496 if not wait:
1497 raise
1497 raise
1498 # show more details for new-style locks
1498 # show more details for new-style locks
1499 if ':' in inst.locker:
1499 if ':' in inst.locker:
1500 host, pid = inst.locker.split(":", 1)
1500 host, pid = inst.locker.split(":", 1)
1501 self.ui.warn(
1501 self.ui.warn(
1502 _("waiting for lock on %s held by process %r "
1502 _("waiting for lock on %s held by process %r "
1503 "on host %r\n") % (desc, pid, host))
1503 "on host %r\n") % (desc, pid, host))
1504 else:
1504 else:
1505 self.ui.warn(_("waiting for lock on %s held by %r\n") %
1505 self.ui.warn(_("waiting for lock on %s held by %r\n") %
1506 (desc, inst.locker))
1506 (desc, inst.locker))
1507 # default to 600 seconds timeout
1507 # default to 600 seconds timeout
1508 l = lockmod.lock(vfs, lockname,
1508 l = lockmod.lock(vfs, lockname,
1509 int(self.ui.config("ui", "timeout")),
1509 int(self.ui.config("ui", "timeout")),
1510 releasefn=releasefn, acquirefn=acquirefn,
1510 releasefn=releasefn, acquirefn=acquirefn,
1511 desc=desc)
1511 desc=desc)
1512 self.ui.warn(_("got lock after %s seconds\n") % l.delay)
1512 self.ui.warn(_("got lock after %s seconds\n") % l.delay)
1513 return l
1513 return l
1514
1514
1515 def _afterlock(self, callback):
1515 def _afterlock(self, callback):
1516 """add a callback to be run when the repository is fully unlocked
1516 """add a callback to be run when the repository is fully unlocked
1517
1517
1518 The callback will be executed when the outermost lock is released
1518 The callback will be executed when the outermost lock is released
1519 (with wlock being higher level than 'lock')."""
1519 (with wlock being higher level than 'lock')."""
1520 for ref in (self._wlockref, self._lockref):
1520 for ref in (self._wlockref, self._lockref):
1521 l = ref and ref()
1521 l = ref and ref()
1522 if l and l.held:
1522 if l and l.held:
1523 l.postrelease.append(callback)
1523 l.postrelease.append(callback)
1524 break
1524 break
1525 else: # no lock have been found.
1525 else: # no lock have been found.
1526 callback()
1526 callback()
1527
1527
1528 def lock(self, wait=True):
1528 def lock(self, wait=True):
1529 '''Lock the repository store (.hg/store) and return a weak reference
1529 '''Lock the repository store (.hg/store) and return a weak reference
1530 to the lock. Use this before modifying the store (e.g. committing or
1530 to the lock. Use this before modifying the store (e.g. committing or
1531 stripping). If you are opening a transaction, get a lock as well.)
1531 stripping). If you are opening a transaction, get a lock as well.)
1532
1532
1533 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1533 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1534 'wlock' first to avoid a dead-lock hazard.'''
1534 'wlock' first to avoid a dead-lock hazard.'''
1535 l = self._currentlock(self._lockref)
1535 l = self._currentlock(self._lockref)
1536 if l is not None:
1536 if l is not None:
1537 l.lock()
1537 l.lock()
1538 return l
1538 return l
1539
1539
1540 l = self._lock(self.svfs, "lock", wait, None,
1540 l = self._lock(self.svfs, "lock", wait, None,
1541 self.invalidate, _('repository %s') % self.origroot)
1541 self.invalidate, _('repository %s') % self.origroot)
1542 self._lockref = weakref.ref(l)
1542 self._lockref = weakref.ref(l)
1543 return l
1543 return l
1544
1544
1545 def _wlockchecktransaction(self):
1545 def _wlockchecktransaction(self):
1546 if self.currenttransaction() is not None:
1546 if self.currenttransaction() is not None:
1547 raise error.LockInheritanceContractViolation(
1547 raise error.LockInheritanceContractViolation(
1548 'wlock cannot be inherited in the middle of a transaction')
1548 'wlock cannot be inherited in the middle of a transaction')
1549
1549
1550 def wlock(self, wait=True):
1550 def wlock(self, wait=True):
1551 '''Lock the non-store parts of the repository (everything under
1551 '''Lock the non-store parts of the repository (everything under
1552 .hg except .hg/store) and return a weak reference to the lock.
1552 .hg except .hg/store) and return a weak reference to the lock.
1553
1553
1554 Use this before modifying files in .hg.
1554 Use this before modifying files in .hg.
1555
1555
1556 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1556 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
1557 'wlock' first to avoid a dead-lock hazard.'''
1557 'wlock' first to avoid a dead-lock hazard.'''
1558 l = self._wlockref and self._wlockref()
1558 l = self._wlockref and self._wlockref()
1559 if l is not None and l.held:
1559 if l is not None and l.held:
1560 l.lock()
1560 l.lock()
1561 return l
1561 return l
1562
1562
1563 # We do not need to check for non-waiting lock acquisition. Such
1563 # We do not need to check for non-waiting lock acquisition. Such
1564 # acquisition would not cause dead-lock as they would just fail.
1564 # acquisition would not cause dead-lock as they would just fail.
1565 if wait and (self.ui.configbool('devel', 'all-warnings')
1565 if wait and (self.ui.configbool('devel', 'all-warnings')
1566 or self.ui.configbool('devel', 'check-locks')):
1566 or self.ui.configbool('devel', 'check-locks')):
1567 if self._currentlock(self._lockref) is not None:
1567 if self._currentlock(self._lockref) is not None:
1568 self.ui.develwarn('"wlock" acquired after "lock"')
1568 self.ui.develwarn('"wlock" acquired after "lock"')
1569
1569
1570 def unlock():
1570 def unlock():
1571 if self.dirstate.pendingparentchange():
1571 if self.dirstate.pendingparentchange():
1572 self.dirstate.invalidate()
1572 self.dirstate.invalidate()
1573 else:
1573 else:
1574 self.dirstate.write(None)
1574 self.dirstate.write(None)
1575
1575
1576 self._filecache['dirstate'].refresh()
1576 self._filecache['dirstate'].refresh()
1577
1577
1578 l = self._lock(self.vfs, "wlock", wait, unlock,
1578 l = self._lock(self.vfs, "wlock", wait, unlock,
1579 self.invalidatedirstate, _('working directory of %s') %
1579 self.invalidatedirstate, _('working directory of %s') %
1580 self.origroot,
1580 self.origroot,
1581 inheritchecker=self._wlockchecktransaction,
1581 inheritchecker=self._wlockchecktransaction,
1582 parentenvvar='HG_WLOCK_LOCKER')
1582 parentenvvar='HG_WLOCK_LOCKER')
1583 self._wlockref = weakref.ref(l)
1583 self._wlockref = weakref.ref(l)
1584 return l
1584 return l
1585
1585
1586 def _currentlock(self, lockref):
1586 def _currentlock(self, lockref):
1587 """Returns the lock if it's held, or None if it's not."""
1587 """Returns the lock if it's held, or None if it's not."""
1588 if lockref is None:
1588 if lockref is None:
1589 return None
1589 return None
1590 l = lockref()
1590 l = lockref()
1591 if l is None or not l.held:
1591 if l is None or not l.held:
1592 return None
1592 return None
1593 return l
1593 return l
1594
1594
1595 def currentwlock(self):
1595 def currentwlock(self):
1596 """Returns the wlock if it's held, or None if it's not."""
1596 """Returns the wlock if it's held, or None if it's not."""
1597 return self._currentlock(self._wlockref)
1597 return self._currentlock(self._wlockref)
1598
1598
1599 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
1599 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
1600 """
1600 """
1601 commit an individual file as part of a larger transaction
1601 commit an individual file as part of a larger transaction
1602 """
1602 """
1603
1603
1604 fname = fctx.path()
1604 fname = fctx.path()
1605 fparent1 = manifest1.get(fname, nullid)
1605 fparent1 = manifest1.get(fname, nullid)
1606 fparent2 = manifest2.get(fname, nullid)
1606 fparent2 = manifest2.get(fname, nullid)
1607 if isinstance(fctx, context.filectx):
1607 if isinstance(fctx, context.filectx):
1608 node = fctx.filenode()
1608 node = fctx.filenode()
1609 if node in [fparent1, fparent2]:
1609 if node in [fparent1, fparent2]:
1610 self.ui.debug('reusing %s filelog entry\n' % fname)
1610 self.ui.debug('reusing %s filelog entry\n' % fname)
1611 if manifest1.flags(fname) != fctx.flags():
1611 if manifest1.flags(fname) != fctx.flags():
1612 changelist.append(fname)
1612 changelist.append(fname)
1613 return node
1613 return node
1614
1614
1615 flog = self.file(fname)
1615 flog = self.file(fname)
1616 meta = {}
1616 meta = {}
1617 copy = fctx.renamed()
1617 copy = fctx.renamed()
1618 if copy and copy[0] != fname:
1618 if copy and copy[0] != fname:
1619 # Mark the new revision of this file as a copy of another
1619 # Mark the new revision of this file as a copy of another
1620 # file. This copy data will effectively act as a parent
1620 # file. This copy data will effectively act as a parent
1621 # of this new revision. If this is a merge, the first
1621 # of this new revision. If this is a merge, the first
1622 # parent will be the nullid (meaning "look up the copy data")
1622 # parent will be the nullid (meaning "look up the copy data")
1623 # and the second one will be the other parent. For example:
1623 # and the second one will be the other parent. For example:
1624 #
1624 #
1625 # 0 --- 1 --- 3 rev1 changes file foo
1625 # 0 --- 1 --- 3 rev1 changes file foo
1626 # \ / rev2 renames foo to bar and changes it
1626 # \ / rev2 renames foo to bar and changes it
1627 # \- 2 -/ rev3 should have bar with all changes and
1627 # \- 2 -/ rev3 should have bar with all changes and
1628 # should record that bar descends from
1628 # should record that bar descends from
1629 # bar in rev2 and foo in rev1
1629 # bar in rev2 and foo in rev1
1630 #
1630 #
1631 # this allows this merge to succeed:
1631 # this allows this merge to succeed:
1632 #
1632 #
1633 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
1633 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
1634 # \ / merging rev3 and rev4 should use bar@rev2
1634 # \ / merging rev3 and rev4 should use bar@rev2
1635 # \- 2 --- 4 as the merge base
1635 # \- 2 --- 4 as the merge base
1636 #
1636 #
1637
1637
1638 cfname = copy[0]
1638 cfname = copy[0]
1639 crev = manifest1.get(cfname)
1639 crev = manifest1.get(cfname)
1640 newfparent = fparent2
1640 newfparent = fparent2
1641
1641
1642 if manifest2: # branch merge
1642 if manifest2: # branch merge
1643 if fparent2 == nullid or crev is None: # copied on remote side
1643 if fparent2 == nullid or crev is None: # copied on remote side
1644 if cfname in manifest2:
1644 if cfname in manifest2:
1645 crev = manifest2[cfname]
1645 crev = manifest2[cfname]
1646 newfparent = fparent1
1646 newfparent = fparent1
1647
1647
1648 # Here, we used to search backwards through history to try to find
1648 # Here, we used to search backwards through history to try to find
1649 # where the file copy came from if the source of a copy was not in
1649 # where the file copy came from if the source of a copy was not in
1650 # the parent directory. However, this doesn't actually make sense to
1650 # the parent directory. However, this doesn't actually make sense to
1651 # do (what does a copy from something not in your working copy even
1651 # do (what does a copy from something not in your working copy even
1652 # mean?) and it causes bugs (eg, issue4476). Instead, we will warn
1652 # mean?) and it causes bugs (eg, issue4476). Instead, we will warn
1653 # the user that copy information was dropped, so if they didn't
1653 # the user that copy information was dropped, so if they didn't
1654 # expect this outcome it can be fixed, but this is the correct
1654 # expect this outcome it can be fixed, but this is the correct
1655 # behavior in this circumstance.
1655 # behavior in this circumstance.
1656
1656
1657 if crev:
1657 if crev:
1658 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1658 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1659 meta["copy"] = cfname
1659 meta["copy"] = cfname
1660 meta["copyrev"] = hex(crev)
1660 meta["copyrev"] = hex(crev)
1661 fparent1, fparent2 = nullid, newfparent
1661 fparent1, fparent2 = nullid, newfparent
1662 else:
1662 else:
1663 self.ui.warn(_("warning: can't find ancestor for '%s' "
1663 self.ui.warn(_("warning: can't find ancestor for '%s' "
1664 "copied from '%s'!\n") % (fname, cfname))
1664 "copied from '%s'!\n") % (fname, cfname))
1665
1665
1666 elif fparent1 == nullid:
1666 elif fparent1 == nullid:
1667 fparent1, fparent2 = fparent2, nullid
1667 fparent1, fparent2 = fparent2, nullid
1668 elif fparent2 != nullid:
1668 elif fparent2 != nullid:
1669 # is one parent an ancestor of the other?
1669 # is one parent an ancestor of the other?
1670 fparentancestors = flog.commonancestorsheads(fparent1, fparent2)
1670 fparentancestors = flog.commonancestorsheads(fparent1, fparent2)
1671 if fparent1 in fparentancestors:
1671 if fparent1 in fparentancestors:
1672 fparent1, fparent2 = fparent2, nullid
1672 fparent1, fparent2 = fparent2, nullid
1673 elif fparent2 in fparentancestors:
1673 elif fparent2 in fparentancestors:
1674 fparent2 = nullid
1674 fparent2 = nullid
1675
1675
1676 # is the file changed?
1676 # is the file changed?
1677 text = fctx.data()
1677 text = fctx.data()
1678 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1678 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1679 changelist.append(fname)
1679 changelist.append(fname)
1680 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1680 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1681 # are just the flags changed during merge?
1681 # are just the flags changed during merge?
1682 elif fname in manifest1 and manifest1.flags(fname) != fctx.flags():
1682 elif fname in manifest1 and manifest1.flags(fname) != fctx.flags():
1683 changelist.append(fname)
1683 changelist.append(fname)
1684
1684
1685 return fparent1
1685 return fparent1
1686
1686
1687 def checkcommitpatterns(self, wctx, vdirs, match, status, fail):
1687 def checkcommitpatterns(self, wctx, vdirs, match, status, fail):
1688 """check for commit arguments that aren't committable"""
1688 """check for commit arguments that aren't committable"""
1689 if match.isexact() or match.prefix():
1689 if match.isexact() or match.prefix():
1690 matched = set(status.modified + status.added + status.removed)
1690 matched = set(status.modified + status.added + status.removed)
1691
1691
1692 for f in match.files():
1692 for f in match.files():
1693 f = self.dirstate.normalize(f)
1693 f = self.dirstate.normalize(f)
1694 if f == '.' or f in matched or f in wctx.substate:
1694 if f == '.' or f in matched or f in wctx.substate:
1695 continue
1695 continue
1696 if f in status.deleted:
1696 if f in status.deleted:
1697 fail(f, _('file not found!'))
1697 fail(f, _('file not found!'))
1698 if f in vdirs: # visited directory
1698 if f in vdirs: # visited directory
1699 d = f + '/'
1699 d = f + '/'
1700 for mf in matched:
1700 for mf in matched:
1701 if mf.startswith(d):
1701 if mf.startswith(d):
1702 break
1702 break
1703 else:
1703 else:
1704 fail(f, _("no match under directory!"))
1704 fail(f, _("no match under directory!"))
1705 elif f not in self.dirstate:
1705 elif f not in self.dirstate:
1706 fail(f, _("file not tracked!"))
1706 fail(f, _("file not tracked!"))
1707
1707
1708 @unfilteredmethod
1708 @unfilteredmethod
1709 def commit(self, text="", user=None, date=None, match=None, force=False,
1709 def commit(self, text="", user=None, date=None, match=None, force=False,
1710 editor=False, extra=None):
1710 editor=False, extra=None):
1711 """Add a new revision to current repository.
1711 """Add a new revision to current repository.
1712
1712
1713 Revision information is gathered from the working directory,
1713 Revision information is gathered from the working directory,
1714 match can be used to filter the committed files. If editor is
1714 match can be used to filter the committed files. If editor is
1715 supplied, it is called to get a commit message.
1715 supplied, it is called to get a commit message.
1716 """
1716 """
1717 if extra is None:
1717 if extra is None:
1718 extra = {}
1718 extra = {}
1719
1719
1720 def fail(f, msg):
1720 def fail(f, msg):
1721 raise error.Abort('%s: %s' % (f, msg))
1721 raise error.Abort('%s: %s' % (f, msg))
1722
1722
1723 if not match:
1723 if not match:
1724 match = matchmod.always(self.root, '')
1724 match = matchmod.always(self.root, '')
1725
1725
1726 if not force:
1726 if not force:
1727 vdirs = []
1727 vdirs = []
1728 match.explicitdir = vdirs.append
1728 match.explicitdir = vdirs.append
1729 match.bad = fail
1729 match.bad = fail
1730
1730
1731 wlock = lock = tr = None
1731 wlock = lock = tr = None
1732 try:
1732 try:
1733 wlock = self.wlock()
1733 wlock = self.wlock()
1734 lock = self.lock() # for recent changelog (see issue4368)
1734 lock = self.lock() # for recent changelog (see issue4368)
1735
1735
1736 wctx = self[None]
1736 wctx = self[None]
1737 merge = len(wctx.parents()) > 1
1737 merge = len(wctx.parents()) > 1
1738
1738
1739 if not force and merge and not match.always():
1739 if not force and merge and not match.always():
1740 raise error.Abort(_('cannot partially commit a merge '
1740 raise error.Abort(_('cannot partially commit a merge '
1741 '(do not specify files or patterns)'))
1741 '(do not specify files or patterns)'))
1742
1742
1743 status = self.status(match=match, clean=force)
1743 status = self.status(match=match, clean=force)
1744 if force:
1744 if force:
1745 status.modified.extend(status.clean) # mq may commit clean files
1745 status.modified.extend(status.clean) # mq may commit clean files
1746
1746
1747 # check subrepos
1747 # check subrepos
1748 subs = []
1748 subs = []
1749 commitsubs = set()
1749 commitsubs = set()
1750 newstate = wctx.substate.copy()
1750 newstate = wctx.substate.copy()
1751 # only manage subrepos and .hgsubstate if .hgsub is present
1751 # only manage subrepos and .hgsubstate if .hgsub is present
1752 if '.hgsub' in wctx:
1752 if '.hgsub' in wctx:
1753 # we'll decide whether to track this ourselves, thanks
1753 # we'll decide whether to track this ourselves, thanks
1754 for c in status.modified, status.added, status.removed:
1754 for c in status.modified, status.added, status.removed:
1755 if '.hgsubstate' in c:
1755 if '.hgsubstate' in c:
1756 c.remove('.hgsubstate')
1756 c.remove('.hgsubstate')
1757
1757
1758 # compare current state to last committed state
1758 # compare current state to last committed state
1759 # build new substate based on last committed state
1759 # build new substate based on last committed state
1760 oldstate = wctx.p1().substate
1760 oldstate = wctx.p1().substate
1761 for s in sorted(newstate.keys()):
1761 for s in sorted(newstate.keys()):
1762 if not match(s):
1762 if not match(s):
1763 # ignore working copy, use old state if present
1763 # ignore working copy, use old state if present
1764 if s in oldstate:
1764 if s in oldstate:
1765 newstate[s] = oldstate[s]
1765 newstate[s] = oldstate[s]
1766 continue
1766 continue
1767 if not force:
1767 if not force:
1768 raise error.Abort(
1768 raise error.Abort(
1769 _("commit with new subrepo %s excluded") % s)
1769 _("commit with new subrepo %s excluded") % s)
1770 dirtyreason = wctx.sub(s).dirtyreason(True)
1770 dirtyreason = wctx.sub(s).dirtyreason(True)
1771 if dirtyreason:
1771 if dirtyreason:
1772 if not self.ui.configbool('ui', 'commitsubrepos'):
1772 if not self.ui.configbool('ui', 'commitsubrepos'):
1773 raise error.Abort(dirtyreason,
1773 raise error.Abort(dirtyreason,
1774 hint=_("use --subrepos for recursive commit"))
1774 hint=_("use --subrepos for recursive commit"))
1775 subs.append(s)
1775 subs.append(s)
1776 commitsubs.add(s)
1776 commitsubs.add(s)
1777 else:
1777 else:
1778 bs = wctx.sub(s).basestate()
1778 bs = wctx.sub(s).basestate()
1779 newstate[s] = (newstate[s][0], bs, newstate[s][2])
1779 newstate[s] = (newstate[s][0], bs, newstate[s][2])
1780 if oldstate.get(s, (None, None, None))[1] != bs:
1780 if oldstate.get(s, (None, None, None))[1] != bs:
1781 subs.append(s)
1781 subs.append(s)
1782
1782
1783 # check for removed subrepos
1783 # check for removed subrepos
1784 for p in wctx.parents():
1784 for p in wctx.parents():
1785 r = [s for s in p.substate if s not in newstate]
1785 r = [s for s in p.substate if s not in newstate]
1786 subs += [s for s in r if match(s)]
1786 subs += [s for s in r if match(s)]
1787 if subs:
1787 if subs:
1788 if (not match('.hgsub') and
1788 if (not match('.hgsub') and
1789 '.hgsub' in (wctx.modified() + wctx.added())):
1789 '.hgsub' in (wctx.modified() + wctx.added())):
1790 raise error.Abort(
1790 raise error.Abort(
1791 _("can't commit subrepos without .hgsub"))
1791 _("can't commit subrepos without .hgsub"))
1792 status.modified.insert(0, '.hgsubstate')
1792 status.modified.insert(0, '.hgsubstate')
1793
1793
1794 elif '.hgsub' in status.removed:
1794 elif '.hgsub' in status.removed:
1795 # clean up .hgsubstate when .hgsub is removed
1795 # clean up .hgsubstate when .hgsub is removed
1796 if ('.hgsubstate' in wctx and
1796 if ('.hgsubstate' in wctx and
1797 '.hgsubstate' not in (status.modified + status.added +
1797 '.hgsubstate' not in (status.modified + status.added +
1798 status.removed)):
1798 status.removed)):
1799 status.removed.insert(0, '.hgsubstate')
1799 status.removed.insert(0, '.hgsubstate')
1800
1800
1801 # make sure all explicit patterns are matched
1801 # make sure all explicit patterns are matched
1802 if not force:
1802 if not force:
1803 self.checkcommitpatterns(wctx, vdirs, match, status, fail)
1803 self.checkcommitpatterns(wctx, vdirs, match, status, fail)
1804
1804
1805 cctx = context.workingcommitctx(self, status,
1805 cctx = context.workingcommitctx(self, status,
1806 text, user, date, extra)
1806 text, user, date, extra)
1807
1807
1808 # internal config: ui.allowemptycommit
1808 # internal config: ui.allowemptycommit
1809 allowemptycommit = (wctx.branch() != wctx.p1().branch()
1809 allowemptycommit = (wctx.branch() != wctx.p1().branch()
1810 or extra.get('close') or merge or cctx.files()
1810 or extra.get('close') or merge or cctx.files()
1811 or self.ui.configbool('ui', 'allowemptycommit'))
1811 or self.ui.configbool('ui', 'allowemptycommit'))
1812 if not allowemptycommit:
1812 if not allowemptycommit:
1813 return None
1813 return None
1814
1814
1815 if merge and cctx.deleted():
1815 if merge and cctx.deleted():
1816 raise error.Abort(_("cannot commit merge with missing files"))
1816 raise error.Abort(_("cannot commit merge with missing files"))
1817
1817
1818 ms = mergemod.mergestate.read(self)
1818 ms = mergemod.mergestate.read(self)
1819 mergeutil.checkunresolved(ms)
1819 mergeutil.checkunresolved(ms)
1820
1820
1821 if editor:
1821 if editor:
1822 cctx._text = editor(self, cctx, subs)
1822 cctx._text = editor(self, cctx, subs)
1823 edited = (text != cctx._text)
1823 edited = (text != cctx._text)
1824
1824
1825 # Save commit message in case this transaction gets rolled back
1825 # Save commit message in case this transaction gets rolled back
1826 # (e.g. by a pretxncommit hook). Leave the content alone on
1826 # (e.g. by a pretxncommit hook). Leave the content alone on
1827 # the assumption that the user will use the same editor again.
1827 # the assumption that the user will use the same editor again.
1828 msgfn = self.savecommitmessage(cctx._text)
1828 msgfn = self.savecommitmessage(cctx._text)
1829
1829
1830 # commit subs and write new state
1830 # commit subs and write new state
1831 if subs:
1831 if subs:
1832 for s in sorted(commitsubs):
1832 for s in sorted(commitsubs):
1833 sub = wctx.sub(s)
1833 sub = wctx.sub(s)
1834 self.ui.status(_('committing subrepository %s\n') %
1834 self.ui.status(_('committing subrepository %s\n') %
1835 subrepo.subrelpath(sub))
1835 subrepo.subrelpath(sub))
1836 sr = sub.commit(cctx._text, user, date)
1836 sr = sub.commit(cctx._text, user, date)
1837 newstate[s] = (newstate[s][0], sr)
1837 newstate[s] = (newstate[s][0], sr)
1838 subrepo.writestate(self, newstate)
1838 subrepo.writestate(self, newstate)
1839
1839
1840 p1, p2 = self.dirstate.parents()
1840 p1, p2 = self.dirstate.parents()
1841 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1841 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1842 try:
1842 try:
1843 self.hook("precommit", throw=True, parent1=hookp1,
1843 self.hook("precommit", throw=True, parent1=hookp1,
1844 parent2=hookp2)
1844 parent2=hookp2)
1845 tr = self.transaction('commit')
1845 tr = self.transaction('commit')
1846 ret = self.commitctx(cctx, True)
1846 ret = self.commitctx(cctx, True)
1847 except: # re-raises
1847 except: # re-raises
1848 if edited:
1848 if edited:
1849 self.ui.write(
1849 self.ui.write(
1850 _('note: commit message saved in %s\n') % msgfn)
1850 _('note: commit message saved in %s\n') % msgfn)
1851 raise
1851 raise
1852 # update bookmarks, dirstate and mergestate
1852 # update bookmarks, dirstate and mergestate
1853 bookmarks.update(self, [p1, p2], ret)
1853 bookmarks.update(self, [p1, p2], ret)
1854 cctx.markcommitted(ret)
1854 cctx.markcommitted(ret)
1855 ms.reset()
1855 ms.reset()
1856 tr.close()
1856 tr.close()
1857
1857
1858 finally:
1858 finally:
1859 lockmod.release(tr, lock, wlock)
1859 lockmod.release(tr, lock, wlock)
1860
1860
1861 def commithook(node=hex(ret), parent1=hookp1, parent2=hookp2):
1861 def commithook(node=hex(ret), parent1=hookp1, parent2=hookp2):
1862 # hack for command that use a temporary commit (eg: histedit)
1862 # hack for command that use a temporary commit (eg: histedit)
1863 # temporary commit got stripped before hook release
1863 # temporary commit got stripped before hook release
1864 if self.changelog.hasnode(ret):
1864 if self.changelog.hasnode(ret):
1865 self.hook("commit", node=node, parent1=parent1,
1865 self.hook("commit", node=node, parent1=parent1,
1866 parent2=parent2)
1866 parent2=parent2)
1867 self._afterlock(commithook)
1867 self._afterlock(commithook)
1868 return ret
1868 return ret
1869
1869
1870 @unfilteredmethod
1870 @unfilteredmethod
1871 def commitctx(self, ctx, error=False):
1871 def commitctx(self, ctx, error=False):
1872 """Add a new revision to current repository.
1872 """Add a new revision to current repository.
1873 Revision information is passed via the context argument.
1873 Revision information is passed via the context argument.
1874 """
1874 """
1875
1875
1876 tr = None
1876 tr = None
1877 p1, p2 = ctx.p1(), ctx.p2()
1877 p1, p2 = ctx.p1(), ctx.p2()
1878 user = ctx.user()
1878 user = ctx.user()
1879
1879
1880 lock = self.lock()
1880 lock = self.lock()
1881 try:
1881 try:
1882 tr = self.transaction("commit")
1882 tr = self.transaction("commit")
1883 trp = weakref.proxy(tr)
1883 trp = weakref.proxy(tr)
1884
1884
1885 if ctx.manifestnode():
1885 if ctx.manifestnode():
1886 # reuse an existing manifest revision
1886 # reuse an existing manifest revision
1887 mn = ctx.manifestnode()
1887 mn = ctx.manifestnode()
1888 files = ctx.files()
1888 files = ctx.files()
1889 elif ctx.files():
1889 elif ctx.files():
1890 m1ctx = p1.manifestctx()
1890 m1ctx = p1.manifestctx()
1891 m2ctx = p2.manifestctx()
1891 m2ctx = p2.manifestctx()
1892 mctx = m1ctx.copy()
1892 mctx = m1ctx.copy()
1893
1893
1894 m = mctx.read()
1894 m = mctx.read()
1895 m1 = m1ctx.read()
1895 m1 = m1ctx.read()
1896 m2 = m2ctx.read()
1896 m2 = m2ctx.read()
1897
1897
1898 # check in files
1898 # check in files
1899 added = []
1899 added = []
1900 changed = []
1900 changed = []
1901 removed = list(ctx.removed())
1901 removed = list(ctx.removed())
1902 linkrev = len(self)
1902 linkrev = len(self)
1903 self.ui.note(_("committing files:\n"))
1903 self.ui.note(_("committing files:\n"))
1904 for f in sorted(ctx.modified() + ctx.added()):
1904 for f in sorted(ctx.modified() + ctx.added()):
1905 self.ui.note(f + "\n")
1905 self.ui.note(f + "\n")
1906 try:
1906 try:
1907 fctx = ctx[f]
1907 fctx = ctx[f]
1908 if fctx is None:
1908 if fctx is None:
1909 removed.append(f)
1909 removed.append(f)
1910 else:
1910 else:
1911 added.append(f)
1911 added.append(f)
1912 m[f] = self._filecommit(fctx, m1, m2, linkrev,
1912 m[f] = self._filecommit(fctx, m1, m2, linkrev,
1913 trp, changed)
1913 trp, changed)
1914 m.setflag(f, fctx.flags())
1914 m.setflag(f, fctx.flags())
1915 except OSError as inst:
1915 except OSError as inst:
1916 self.ui.warn(_("trouble committing %s!\n") % f)
1916 self.ui.warn(_("trouble committing %s!\n") % f)
1917 raise
1917 raise
1918 except IOError as inst:
1918 except IOError as inst:
1919 errcode = getattr(inst, 'errno', errno.ENOENT)
1919 errcode = getattr(inst, 'errno', errno.ENOENT)
1920 if error or errcode and errcode != errno.ENOENT:
1920 if error or errcode and errcode != errno.ENOENT:
1921 self.ui.warn(_("trouble committing %s!\n") % f)
1921 self.ui.warn(_("trouble committing %s!\n") % f)
1922 raise
1922 raise
1923
1923
1924 # update manifest
1924 # update manifest
1925 self.ui.note(_("committing manifest\n"))
1925 self.ui.note(_("committing manifest\n"))
1926 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1926 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1927 drop = [f for f in removed if f in m]
1927 drop = [f for f in removed if f in m]
1928 for f in drop:
1928 for f in drop:
1929 del m[f]
1929 del m[f]
1930 mn = mctx.write(trp, linkrev,
1930 mn = mctx.write(trp, linkrev,
1931 p1.manifestnode(), p2.manifestnode(),
1931 p1.manifestnode(), p2.manifestnode(),
1932 added, drop)
1932 added, drop)
1933 files = changed + removed
1933 files = changed + removed
1934 else:
1934 else:
1935 mn = p1.manifestnode()
1935 mn = p1.manifestnode()
1936 files = []
1936 files = []
1937
1937
1938 # update changelog
1938 # update changelog
1939 self.ui.note(_("committing changelog\n"))
1939 self.ui.note(_("committing changelog\n"))
1940 self.changelog.delayupdate(tr)
1940 self.changelog.delayupdate(tr)
1941 n = self.changelog.add(mn, files, ctx.description(),
1941 n = self.changelog.add(mn, files, ctx.description(),
1942 trp, p1.node(), p2.node(),
1942 trp, p1.node(), p2.node(),
1943 user, ctx.date(), ctx.extra().copy())
1943 user, ctx.date(), ctx.extra().copy())
1944 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1944 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1945 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1945 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1946 parent2=xp2)
1946 parent2=xp2)
1947 # set the new commit is proper phase
1947 # set the new commit is proper phase
1948 targetphase = subrepo.newcommitphase(self.ui, ctx)
1948 targetphase = subrepo.newcommitphase(self.ui, ctx)
1949 if targetphase:
1949 if targetphase:
1950 # retract boundary do not alter parent changeset.
1950 # retract boundary do not alter parent changeset.
1951 # if a parent have higher the resulting phase will
1951 # if a parent have higher the resulting phase will
1952 # be compliant anyway
1952 # be compliant anyway
1953 #
1953 #
1954 # if minimal phase was 0 we don't need to retract anything
1954 # if minimal phase was 0 we don't need to retract anything
1955 phases.registernew(self, tr, targetphase, [n])
1955 phases.registernew(self, tr, targetphase, [n])
1956 tr.close()
1956 tr.close()
1957 return n
1957 return n
1958 finally:
1958 finally:
1959 if tr:
1959 if tr:
1960 tr.release()
1960 tr.release()
1961 lock.release()
1961 lock.release()
1962
1962
1963 @unfilteredmethod
1963 @unfilteredmethod
1964 def destroying(self):
1964 def destroying(self):
1965 '''Inform the repository that nodes are about to be destroyed.
1965 '''Inform the repository that nodes are about to be destroyed.
1966 Intended for use by strip and rollback, so there's a common
1966 Intended for use by strip and rollback, so there's a common
1967 place for anything that has to be done before destroying history.
1967 place for anything that has to be done before destroying history.
1968
1968
1969 This is mostly useful for saving state that is in memory and waiting
1969 This is mostly useful for saving state that is in memory and waiting
1970 to be flushed when the current lock is released. Because a call to
1970 to be flushed when the current lock is released. Because a call to
1971 destroyed is imminent, the repo will be invalidated causing those
1971 destroyed is imminent, the repo will be invalidated causing those
1972 changes to stay in memory (waiting for the next unlock), or vanish
1972 changes to stay in memory (waiting for the next unlock), or vanish
1973 completely.
1973 completely.
1974 '''
1974 '''
1975 # When using the same lock to commit and strip, the phasecache is left
1975 # When using the same lock to commit and strip, the phasecache is left
1976 # dirty after committing. Then when we strip, the repo is invalidated,
1976 # dirty after committing. Then when we strip, the repo is invalidated,
1977 # causing those changes to disappear.
1977 # causing those changes to disappear.
1978 if '_phasecache' in vars(self):
1978 if '_phasecache' in vars(self):
1979 self._phasecache.write()
1979 self._phasecache.write()
1980
1980
1981 @unfilteredmethod
1981 @unfilteredmethod
1982 def destroyed(self):
1982 def destroyed(self):
1983 '''Inform the repository that nodes have been destroyed.
1983 '''Inform the repository that nodes have been destroyed.
1984 Intended for use by strip and rollback, so there's a common
1984 Intended for use by strip and rollback, so there's a common
1985 place for anything that has to be done after destroying history.
1985 place for anything that has to be done after destroying history.
1986 '''
1986 '''
1987 # When one tries to:
1987 # When one tries to:
1988 # 1) destroy nodes thus calling this method (e.g. strip)
1988 # 1) destroy nodes thus calling this method (e.g. strip)
1989 # 2) use phasecache somewhere (e.g. commit)
1989 # 2) use phasecache somewhere (e.g. commit)
1990 #
1990 #
1991 # then 2) will fail because the phasecache contains nodes that were
1991 # then 2) will fail because the phasecache contains nodes that were
1992 # removed. We can either remove phasecache from the filecache,
1992 # removed. We can either remove phasecache from the filecache,
1993 # causing it to reload next time it is accessed, or simply filter
1993 # causing it to reload next time it is accessed, or simply filter
1994 # the removed nodes now and write the updated cache.
1994 # the removed nodes now and write the updated cache.
1995 self._phasecache.filterunknown(self)
1995 self._phasecache.filterunknown(self)
1996 self._phasecache.write()
1996 self._phasecache.write()
1997
1997
1998 # refresh all repository caches
1998 # refresh all repository caches
1999 self.updatecaches()
1999 self.updatecaches()
2000
2000
2001 # Ensure the persistent tag cache is updated. Doing it now
2001 # Ensure the persistent tag cache is updated. Doing it now
2002 # means that the tag cache only has to worry about destroyed
2002 # means that the tag cache only has to worry about destroyed
2003 # heads immediately after a strip/rollback. That in turn
2003 # heads immediately after a strip/rollback. That in turn
2004 # guarantees that "cachetip == currenttip" (comparing both rev
2004 # guarantees that "cachetip == currenttip" (comparing both rev
2005 # and node) always means no nodes have been added or destroyed.
2005 # and node) always means no nodes have been added or destroyed.
2006
2006
2007 # XXX this is suboptimal when qrefresh'ing: we strip the current
2007 # XXX this is suboptimal when qrefresh'ing: we strip the current
2008 # head, refresh the tag cache, then immediately add a new head.
2008 # head, refresh the tag cache, then immediately add a new head.
2009 # But I think doing it this way is necessary for the "instant
2009 # But I think doing it this way is necessary for the "instant
2010 # tag cache retrieval" case to work.
2010 # tag cache retrieval" case to work.
2011 self.invalidate()
2011 self.invalidate()
2012
2012
2013 def walk(self, match, node=None):
2013 def walk(self, match, node=None):
2014 '''
2014 '''
2015 walk recursively through the directory tree or a given
2015 walk recursively through the directory tree or a given
2016 changeset, finding all files matched by the match
2016 changeset, finding all files matched by the match
2017 function
2017 function
2018 '''
2018 '''
2019 self.ui.deprecwarn('use repo[node].walk instead of repo.walk', '4.3')
2019 self.ui.deprecwarn('use repo[node].walk instead of repo.walk', '4.3')
2020 return self[node].walk(match)
2020 return self[node].walk(match)
2021
2021
2022 def status(self, node1='.', node2=None, match=None,
2022 def status(self, node1='.', node2=None, match=None,
2023 ignored=False, clean=False, unknown=False,
2023 ignored=False, clean=False, unknown=False,
2024 listsubrepos=False):
2024 listsubrepos=False):
2025 '''a convenience method that calls node1.status(node2)'''
2025 '''a convenience method that calls node1.status(node2)'''
2026 return self[node1].status(node2, match, ignored, clean, unknown,
2026 return self[node1].status(node2, match, ignored, clean, unknown,
2027 listsubrepos)
2027 listsubrepos)
2028
2028
2029 def addpostdsstatus(self, ps):
2029 def addpostdsstatus(self, ps):
2030 """Add a callback to run within the wlock, at the point at which status
2030 """Add a callback to run within the wlock, at the point at which status
2031 fixups happen.
2031 fixups happen.
2032
2032
2033 On status completion, callback(wctx, status) will be called with the
2033 On status completion, callback(wctx, status) will be called with the
2034 wlock held, unless the dirstate has changed from underneath or the wlock
2034 wlock held, unless the dirstate has changed from underneath or the wlock
2035 couldn't be grabbed.
2035 couldn't be grabbed.
2036
2036
2037 Callbacks should not capture and use a cached copy of the dirstate --
2037 Callbacks should not capture and use a cached copy of the dirstate --
2038 it might change in the meanwhile. Instead, they should access the
2038 it might change in the meanwhile. Instead, they should access the
2039 dirstate via wctx.repo().dirstate.
2039 dirstate via wctx.repo().dirstate.
2040
2040
2041 This list is emptied out after each status run -- extensions should
2041 This list is emptied out after each status run -- extensions should
2042 make sure it adds to this list each time dirstate.status is called.
2042 make sure it adds to this list each time dirstate.status is called.
2043 Extensions should also make sure they don't call this for statuses
2043 Extensions should also make sure they don't call this for statuses
2044 that don't involve the dirstate.
2044 that don't involve the dirstate.
2045 """
2045 """
2046
2046
2047 # The list is located here for uniqueness reasons -- it is actually
2047 # The list is located here for uniqueness reasons -- it is actually
2048 # managed by the workingctx, but that isn't unique per-repo.
2048 # managed by the workingctx, but that isn't unique per-repo.
2049 self._postdsstatus.append(ps)
2049 self._postdsstatus.append(ps)
2050
2050
2051 def postdsstatus(self):
2051 def postdsstatus(self):
2052 """Used by workingctx to get the list of post-dirstate-status hooks."""
2052 """Used by workingctx to get the list of post-dirstate-status hooks."""
2053 return self._postdsstatus
2053 return self._postdsstatus
2054
2054
2055 def clearpostdsstatus(self):
2055 def clearpostdsstatus(self):
2056 """Used by workingctx to clear post-dirstate-status hooks."""
2056 """Used by workingctx to clear post-dirstate-status hooks."""
2057 del self._postdsstatus[:]
2057 del self._postdsstatus[:]
2058
2058
2059 def heads(self, start=None):
2059 def heads(self, start=None):
2060 if start is None:
2060 if start is None:
2061 cl = self.changelog
2061 cl = self.changelog
2062 headrevs = reversed(cl.headrevs())
2062 headrevs = reversed(cl.headrevs())
2063 return [cl.node(rev) for rev in headrevs]
2063 return [cl.node(rev) for rev in headrevs]
2064
2064
2065 heads = self.changelog.heads(start)
2065 heads = self.changelog.heads(start)
2066 # sort the output in rev descending order
2066 # sort the output in rev descending order
2067 return sorted(heads, key=self.changelog.rev, reverse=True)
2067 return sorted(heads, key=self.changelog.rev, reverse=True)
2068
2068
2069 def branchheads(self, branch=None, start=None, closed=False):
2069 def branchheads(self, branch=None, start=None, closed=False):
2070 '''return a (possibly filtered) list of heads for the given branch
2070 '''return a (possibly filtered) list of heads for the given branch
2071
2071
2072 Heads are returned in topological order, from newest to oldest.
2072 Heads are returned in topological order, from newest to oldest.
2073 If branch is None, use the dirstate branch.
2073 If branch is None, use the dirstate branch.
2074 If start is not None, return only heads reachable from start.
2074 If start is not None, return only heads reachable from start.
2075 If closed is True, return heads that are marked as closed as well.
2075 If closed is True, return heads that are marked as closed as well.
2076 '''
2076 '''
2077 if branch is None:
2077 if branch is None:
2078 branch = self[None].branch()
2078 branch = self[None].branch()
2079 branches = self.branchmap()
2079 branches = self.branchmap()
2080 if branch not in branches:
2080 if branch not in branches:
2081 return []
2081 return []
2082 # the cache returns heads ordered lowest to highest
2082 # the cache returns heads ordered lowest to highest
2083 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
2083 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
2084 if start is not None:
2084 if start is not None:
2085 # filter out the heads that cannot be reached from startrev
2085 # filter out the heads that cannot be reached from startrev
2086 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
2086 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
2087 bheads = [h for h in bheads if h in fbheads]
2087 bheads = [h for h in bheads if h in fbheads]
2088 return bheads
2088 return bheads
2089
2089
2090 def branches(self, nodes):
2090 def branches(self, nodes):
2091 if not nodes:
2091 if not nodes:
2092 nodes = [self.changelog.tip()]
2092 nodes = [self.changelog.tip()]
2093 b = []
2093 b = []
2094 for n in nodes:
2094 for n in nodes:
2095 t = n
2095 t = n
2096 while True:
2096 while True:
2097 p = self.changelog.parents(n)
2097 p = self.changelog.parents(n)
2098 if p[1] != nullid or p[0] == nullid:
2098 if p[1] != nullid or p[0] == nullid:
2099 b.append((t, n, p[0], p[1]))
2099 b.append((t, n, p[0], p[1]))
2100 break
2100 break
2101 n = p[0]
2101 n = p[0]
2102 return b
2102 return b
2103
2103
2104 def between(self, pairs):
2104 def between(self, pairs):
2105 r = []
2105 r = []
2106
2106
2107 for top, bottom in pairs:
2107 for top, bottom in pairs:
2108 n, l, i = top, [], 0
2108 n, l, i = top, [], 0
2109 f = 1
2109 f = 1
2110
2110
2111 while n != bottom and n != nullid:
2111 while n != bottom and n != nullid:
2112 p = self.changelog.parents(n)[0]
2112 p = self.changelog.parents(n)[0]
2113 if i == f:
2113 if i == f:
2114 l.append(n)
2114 l.append(n)
2115 f = f * 2
2115 f = f * 2
2116 n = p
2116 n = p
2117 i += 1
2117 i += 1
2118
2118
2119 r.append(l)
2119 r.append(l)
2120
2120
2121 return r
2121 return r
2122
2122
2123 def checkpush(self, pushop):
2123 def checkpush(self, pushop):
2124 """Extensions can override this function if additional checks have
2124 """Extensions can override this function if additional checks have
2125 to be performed before pushing, or call it if they override push
2125 to be performed before pushing, or call it if they override push
2126 command.
2126 command.
2127 """
2127 """
2128 pass
2128 pass
2129
2129
2130 @unfilteredpropertycache
2130 @unfilteredpropertycache
2131 def prepushoutgoinghooks(self):
2131 def prepushoutgoinghooks(self):
2132 """Return util.hooks consists of a pushop with repo, remote, outgoing
2132 """Return util.hooks consists of a pushop with repo, remote, outgoing
2133 methods, which are called before pushing changesets.
2133 methods, which are called before pushing changesets.
2134 """
2134 """
2135 return util.hooks()
2135 return util.hooks()
2136
2136
2137 def pushkey(self, namespace, key, old, new):
2137 def pushkey(self, namespace, key, old, new):
2138 try:
2138 try:
2139 tr = self.currenttransaction()
2139 tr = self.currenttransaction()
2140 hookargs = {}
2140 hookargs = {}
2141 if tr is not None:
2141 if tr is not None:
2142 hookargs.update(tr.hookargs)
2142 hookargs.update(tr.hookargs)
2143 hookargs['namespace'] = namespace
2143 hookargs['namespace'] = namespace
2144 hookargs['key'] = key
2144 hookargs['key'] = key
2145 hookargs['old'] = old
2145 hookargs['old'] = old
2146 hookargs['new'] = new
2146 hookargs['new'] = new
2147 self.hook('prepushkey', throw=True, **hookargs)
2147 self.hook('prepushkey', throw=True, **hookargs)
2148 except error.HookAbort as exc:
2148 except error.HookAbort as exc:
2149 self.ui.write_err(_("pushkey-abort: %s\n") % exc)
2149 self.ui.write_err(_("pushkey-abort: %s\n") % exc)
2150 if exc.hint:
2150 if exc.hint:
2151 self.ui.write_err(_("(%s)\n") % exc.hint)
2151 self.ui.write_err(_("(%s)\n") % exc.hint)
2152 return False
2152 return False
2153 self.ui.debug('pushing key for "%s:%s"\n' % (namespace, key))
2153 self.ui.debug('pushing key for "%s:%s"\n' % (namespace, key))
2154 ret = pushkey.push(self, namespace, key, old, new)
2154 ret = pushkey.push(self, namespace, key, old, new)
2155 def runhook():
2155 def runhook():
2156 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2156 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2157 ret=ret)
2157 ret=ret)
2158 self._afterlock(runhook)
2158 self._afterlock(runhook)
2159 return ret
2159 return ret
2160
2160
2161 def listkeys(self, namespace):
2161 def listkeys(self, namespace):
2162 self.hook('prelistkeys', throw=True, namespace=namespace)
2162 self.hook('prelistkeys', throw=True, namespace=namespace)
2163 self.ui.debug('listing keys for "%s"\n' % namespace)
2163 self.ui.debug('listing keys for "%s"\n' % namespace)
2164 values = pushkey.list(self, namespace)
2164 values = pushkey.list(self, namespace)
2165 self.hook('listkeys', namespace=namespace, values=values)
2165 self.hook('listkeys', namespace=namespace, values=values)
2166 return values
2166 return values
2167
2167
2168 def debugwireargs(self, one, two, three=None, four=None, five=None):
2168 def debugwireargs(self, one, two, three=None, four=None, five=None):
2169 '''used to test argument passing over the wire'''
2169 '''used to test argument passing over the wire'''
2170 return "%s %s %s %s %s" % (one, two, three, four, five)
2170 return "%s %s %s %s %s" % (one, two, three, four, five)
2171
2171
2172 def savecommitmessage(self, text):
2172 def savecommitmessage(self, text):
2173 fp = self.vfs('last-message.txt', 'wb')
2173 fp = self.vfs('last-message.txt', 'wb')
2174 try:
2174 try:
2175 fp.write(text)
2175 fp.write(text)
2176 finally:
2176 finally:
2177 fp.close()
2177 fp.close()
2178 return self.pathto(fp.name[len(self.root) + 1:])
2178 return self.pathto(fp.name[len(self.root) + 1:])
2179
2179
2180 # used to avoid circular references so destructors work
2180 # used to avoid circular references so destructors work
2181 def aftertrans(files):
2181 def aftertrans(files):
2182 renamefiles = [tuple(t) for t in files]
2182 renamefiles = [tuple(t) for t in files]
2183 def a():
2183 def a():
2184 for vfs, src, dest in renamefiles:
2184 for vfs, src, dest in renamefiles:
2185 # if src and dest refer to a same file, vfs.rename is a no-op,
2185 # if src and dest refer to a same file, vfs.rename is a no-op,
2186 # leaving both src and dest on disk. delete dest to make sure
2186 # leaving both src and dest on disk. delete dest to make sure
2187 # the rename couldn't be such a no-op.
2187 # the rename couldn't be such a no-op.
2188 vfs.tryunlink(dest)
2188 vfs.tryunlink(dest)
2189 try:
2189 try:
2190 vfs.rename(src, dest)
2190 vfs.rename(src, dest)
2191 except OSError: # journal file does not yet exist
2191 except OSError: # journal file does not yet exist
2192 pass
2192 pass
2193 return a
2193 return a
2194
2194
2195 def undoname(fn):
2195 def undoname(fn):
2196 base, name = os.path.split(fn)
2196 base, name = os.path.split(fn)
2197 assert name.startswith('journal')
2197 assert name.startswith('journal')
2198 return os.path.join(base, name.replace('journal', 'undo', 1))
2198 return os.path.join(base, name.replace('journal', 'undo', 1))
2199
2199
2200 def instance(ui, path, create):
2200 def instance(ui, path, create):
2201 return localrepository(ui, util.urllocalpath(path), create)
2201 return localrepository(ui, util.urllocalpath(path), create)
2202
2202
2203 def islocal(path):
2203 def islocal(path):
2204 return True
2204 return True
2205
2205
2206 def newreporequirements(repo):
2206 def newreporequirements(repo):
2207 """Determine the set of requirements for a new local repository.
2207 """Determine the set of requirements for a new local repository.
2208
2208
2209 Extensions can wrap this function to specify custom requirements for
2209 Extensions can wrap this function to specify custom requirements for
2210 new repositories.
2210 new repositories.
2211 """
2211 """
2212 ui = repo.ui
2212 ui = repo.ui
2213 requirements = {'revlogv1'}
2213 requirements = {'revlogv1'}
2214 if ui.configbool('format', 'usestore'):
2214 if ui.configbool('format', 'usestore'):
2215 requirements.add('store')
2215 requirements.add('store')
2216 if ui.configbool('format', 'usefncache'):
2216 if ui.configbool('format', 'usefncache'):
2217 requirements.add('fncache')
2217 requirements.add('fncache')
2218 if ui.configbool('format', 'dotencode'):
2218 if ui.configbool('format', 'dotencode'):
2219 requirements.add('dotencode')
2219 requirements.add('dotencode')
2220
2220
2221 compengine = ui.config('experimental', 'format.compression')
2221 compengine = ui.config('experimental', 'format.compression')
2222 if compengine not in util.compengines:
2222 if compengine not in util.compengines:
2223 raise error.Abort(_('compression engine %s defined by '
2223 raise error.Abort(_('compression engine %s defined by '
2224 'experimental.format.compression not available') %
2224 'experimental.format.compression not available') %
2225 compengine,
2225 compengine,
2226 hint=_('run "hg debuginstall" to list available '
2226 hint=_('run "hg debuginstall" to list available '
2227 'compression engines'))
2227 'compression engines'))
2228
2228
2229 # zlib is the historical default and doesn't need an explicit requirement.
2229 # zlib is the historical default and doesn't need an explicit requirement.
2230 if compengine != 'zlib':
2230 if compengine != 'zlib':
2231 requirements.add('exp-compression-%s' % compengine)
2231 requirements.add('exp-compression-%s' % compengine)
2232
2232
2233 if scmutil.gdinitconfig(ui):
2233 if scmutil.gdinitconfig(ui):
2234 requirements.add('generaldelta')
2234 requirements.add('generaldelta')
2235 if ui.configbool('experimental', 'treemanifest'):
2235 if ui.configbool('experimental', 'treemanifest'):
2236 requirements.add('treemanifest')
2236 requirements.add('treemanifest')
2237 if ui.configbool('experimental', 'manifestv2'):
2237 if ui.configbool('experimental', 'manifestv2'):
2238 requirements.add('manifestv2')
2238 requirements.add('manifestv2')
2239
2239
2240 revlogv2 = ui.config('experimental', 'revlogv2')
2240 revlogv2 = ui.config('experimental', 'revlogv2')
2241 if revlogv2 == 'enable-unstable-format-and-corrupt-my-data':
2241 if revlogv2 == 'enable-unstable-format-and-corrupt-my-data':
2242 requirements.remove('revlogv1')
2242 requirements.remove('revlogv1')
2243 # generaldelta is implied by revlogv2.
2243 # generaldelta is implied by revlogv2.
2244 requirements.discard('generaldelta')
2244 requirements.discard('generaldelta')
2245 requirements.add(REVLOGV2_REQUIREMENT)
2245 requirements.add(REVLOGV2_REQUIREMENT)
2246
2246
2247 return requirements
2247 return requirements
General Comments 0
You need to be logged in to leave comments. Login now