Mercurial > hg
annotate mercurial/sparse.py @ 33298:f41a99c45956
sparse: move profile reading into core
One more step towards weaning off methods on repo instances and
moving code to core. While this function is only used once and
is simple, it needs to exist on its own so Facebook can monkeypatch
it to enable simplecache integration.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 06 Jul 2017 12:14:12 -0700 |
parents | ba5d89774db6 |
children | 41448fc51510 |
rev | line source |
---|---|
33297
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 # sparse.py - functionality for sparse checkouts |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 # |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 # Copyright 2014 Facebook, Inc. |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 # |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 from __future__ import absolute_import |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
10 from .i18n import _ |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 from . import ( |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
12 error, |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 ) |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 def parseconfig(ui, raw): |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 """Parse sparse config file content. |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 Returns a tuple of includes, excludes, and profiles. |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 """ |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 includes = set() |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 excludes = set() |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 current = includes |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 profiles = [] |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 for line in raw.split('\n'): |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 line = line.strip() |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 if not line or line.startswith('#'): |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 # empty or comment line, skip |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 continue |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 elif line.startswith('%include '): |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
30 line = line[9:].strip() |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
31 if line: |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 profiles.append(line) |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 elif line == '[include]': |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 if current != includes: |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 # TODO pass filename into this API so we can report it. |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 raise error.Abort(_('sparse config cannot have includes ' + |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 'after excludes')) |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 continue |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
39 elif line == '[exclude]': |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
40 current = excludes |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
41 elif line: |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
42 if line.strip().startswith('/'): |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
43 ui.warn(_('warning: sparse profile cannot use' + |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
44 ' paths starting with /, ignoring %s\n') % line) |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
45 continue |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
46 current.add(line) |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 |
ba5d89774db6
sparse: move config parsing into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 return includes, excludes, profiles |
33298
f41a99c45956
sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33297
diff
changeset
|
49 |
f41a99c45956
sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33297
diff
changeset
|
50 # Exists as separate function to facilitate monkeypatching. |
f41a99c45956
sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33297
diff
changeset
|
51 def readprofile(repo, profile, changeid): |
f41a99c45956
sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33297
diff
changeset
|
52 """Resolve the raw content of a sparse profile file.""" |
f41a99c45956
sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33297
diff
changeset
|
53 # TODO add some kind of cache here because this incurs a manifest |
f41a99c45956
sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33297
diff
changeset
|
54 # resolve and can be slow. |
f41a99c45956
sparse: move profile reading into core
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33297
diff
changeset
|
55 return repo.filectx(profile, changeid=changeid).data() |