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