Debian Codegraph @

hgext/narrow/__init__.py

raw
1 # __init__.py - narrowhg extension
2 #
3 # Copyright 2017 Google, Inc.
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7 """create clones which fetch history data for subsets of files (EXPERIMENTAL)
8
9 Only the server holds the full store: narrow clones will have the full history
10 of fewer files in their store.
11
12 Concepts
13 --------
14
15 The narrow extension introduces a number of concepts that are detailed below.
16
17 Shape (experimental)
18 ....................
19
20 A shape is used to define a set of paths to consider for clients of the narrow
21 server. And it does so through the entire history, regardless the state of these
22 files in the heads of the repository (deleted, for example).
23
24 As of Mercurial 7.2, only a limited scope of all planned shape-related features
25 have been implemented. Shapes are only directly interacted with by server
26 administrators. They can be used to generate narrowspecs (used by clients) and
27 to create narrow streaming clonebundles.
28
29 This means that as of Mercurial 7.2, clients have no knowledge of shapes and
30 must be given the set of narrow patterns by server administrators in order to
31 use a streaming clonebundle for a given shape::
32
33 $ hg clone server --narrow --narrowspec NARROW_SPEC_FILE
34
35 Fingerprint
36 ...........
37
38 Every shape corresponds to a normalized and minimal set of included and excluded
39 paths in a repository's store. This normalization ensures that we can derive a
40 fingerprint, meaning that two shapes with the same fingerprint mean they match
41 the same set of paths.
42
43 Checking the fingerprint of a narrow clone can be done by running::
44
45 $ hg admin::narrow-client --store-fingerprint
46
47 For now, we guarantee no stability of the fingerprints. Before we take shapes
48 out of experimental, we will have a fingerprint versioning scheme, to allow
49 for future changes with no confusion.
50
51 Shards
52 ......
53
54 Shards are used server-side to slice the store. Each shard is defined as a set
55 of paths it contains: files or directories matching each path will be included
56 in that shard. Moreover shards are mutually exclusive, meaning that a given file
57 can only ever be in one shard and one shard only. When a shard's path matches a
58 directory, the shard contains all files in that directory's tree, except for
59 those matched by a more deeply nested shard.
60
61 Example
62 .......
63
64 Given the following store, representing all files ever known to the repository::
65
66 .
67 ├── file1
68 ├── file2
69 └── foo
70 └── bar
71 ├── baz
72 │ ├── file1
73 │ └── file2
74 └── confidential
75 ├── confidential-file1
76 └── confidential-file2
77
78 One could define the following shards::
79
80 * ``foo``, that includes ``foo``
81 * ``foo.confidential``, that includes ``foo/bar/confidential``
82
83 This would mean that shard ``foo`` contains::
84
85 * ``foo/bar/baz/file1``
86 * ``foo/bar/baz/file2``
87
88 And shard ``foo.confidential`` contains::
89
90 * ``foo/bar/confidential/confidential-file1``
91 * ``foo/bar/confidential/confidential-file2``
92
93 In this example, ``file1`` and ``file2`` are not contained in any explicitly
94 defined shard. As a result, they are contained by the implicit ``base`` shard.
95
96 It is important to note that adding a new shard (e.g. that includes `file1` and
97 ``file2``) would remove its paths from any shard that previously contained them
98 (in this case, only ``base``).
99
100 Server-side, shards are used to define shapes, which are sets of shards. Each
101 shape defines the shards it includes, hence excluding the contents of all other
102 shards.
103
104 To simplify the creation of shapes, we can compose shards together by defining
105 explicit dependencies between shards. In our above example, we could define a
106 ``foo.full`` (that does not itself have to define any paths), but requires both
107 ``foo`` and ``foo.confidential``, giving us a shard with the full ``foo`` store
108 subtree.
109
110 Note that the set of paths in the current working copy may only be a subset of
111 the the store. In our example, we can imagine that
112 ``foo/bar/confidential-module`` has been split into the files in
113 ``foo/bar/confidential`` and removed from the working copy.
114
115 In addition to the implicit ``base`` shard, there is also the implicit
116 ``.hg-files`` shard, which contains special paths used by Mercurial::
117
118 * ``.hgignore``
119 * ``.hgtags``
120 * ``.hgsub``
121 * ``.hgsubstate``
122
123 This shard will be automatically included in every ``Shape`` to make sure
124 Mercurial has the data it needs to operate on each revision.
125
126 Narrowspec (legacy)
127 ...................
128
129 Note: the narrowspec in its current form will be phased out in favor of the
130 ``Shape`` system within the next few versions of Mercurial, as of Mercurial 7.2.
131 See the ``Shapes vs narrowspec`` section below.
132
133 The ``narrowspec`` is a client-side configuration that consists of expressions
134 to match remote files and/or directories that should be pulled
135 into a client. The ``narrowspec`` has *include* and *exclude* expressions,
136 with excludes always trumping includes: that is, if a file matches an exclude
137 expression, it will be excluded even if it also matches an include expression.
138 Excluding files that were never included has no effect.
139
140 Each included or excluded entry is in the format described by
141 :hg:`help patterns`, but can only use ``path:`` or ``rootfilesin:``, except
142 when using ``Shard`` and ``Shape`` where you can only use ``path:``.
143
144 Shapes vs narrowspec
145 ....................
146
147 The ``narrowspec`` was introduced alongside the experimental narrow extension
148 back in 2018. It enabled the client to chose exactly what it cares about from
149 the server, with the server merely cooperating with the patterns that the
150 client asks for.
151
152 Unless this flexibility becomes really important for someone in the future,
153 the current direction is to deprecate the narrowspec usage and remove its
154 support entirely in later versions in favor of store shapes.
155
156 Here the advantages of store shapes::
157
158 * We can define clonebundles for usual patterns
159 * We can nest and generally compose includes and excludes
160 * We can generate a fingerprint for equivalent patterns
161 * We can require that the server and the client agree on patterns
162 * Client are made aware of changes on the server, invalidating their patterns
163 * A solid permissions system could be built on top of store shapes
164 * Some legacy problems (e.g. CLI parsing) will be solved
165
166 Some of these points could be (and have been) bolted on top of the original
167 implementation, but we would not go very far.
168
169 Usage
170 -----
171
172 Server configuration
173 ....................
174
175 For Mercurial 7.2, the ``.hg/store/server-shapes`` file is a TOML file, to be
176 created and modified directly by the narrow server's administrators.
177
178 **Warning**: using dedicated ``hg`` command to modify the shapes config will
179 become mandatory in future releases, once such a command exists,
180 as changing shapes can lead to (sometimes surprising) client breakage.
181
182 The current (experimental!) format is the following::
183
184 * ``version`` (required): its value must be ``0``
185 * ``shards`` (required): a list of <shardconfig>
186 * <shardconfig>:
187
188 * ``name`` (required): canonical name for this shard.
189 * ``paths`` (optional): A list of UTF8 paths that this shard concerns,
190 relative to the repo root.
191 * ``requires`` (optional): A list of the names of the shards that this
192 shard depends on.
193 * ``shape`` (optional): If ``true``, this as a user-accessible shape.
194
195 Constraints::
196
197 * Shard names must be unique within the config
198 * Shard names must only contain bytes that are either lowercase alphanumeric
199 ascii, dot or hyphen
200 * ``full``, ``base`` and ``.hg-files`` are reserved shard names
201 * Each <shardconfig> must define one of ``requires`` or ``paths``, or both
202 * Shard requirements cannot form a cycle
203 * A given path cannot be in more than one shard
204 * ``paths`` must be a list of UTF8 paths. Support for non-UTF8 paths will be
205 added in an upcoming version, via an ``encoded_paths`` field
206 * ``paths`` must be a list of syntactically valid paths relative to the
207 repository root (no ``\\n``, ``\\r`` or ``\\x00``, no consecutive slash, no
208 slashes at the start or end, etc.)
209
210 Example::
211
212 version = 0
213
214 [[shards]]
215 name = "foo"
216 paths = ["foo", "bar.txt", "baz/nested"]
217 shape = true
218
219 [[shards]]
220 name = "subproject1"
221 paths = ["subproject1", "utils/only-this-dir"]
222
223 [[shards]]
224 name = "backend"
225 paths = ["subproject2"]
226 requires = ["subproject1"]
227 shape = true
228
229 [[shards]]
230 name = "full-stack"
231 requires = ["backend", "foo"]
232 shape = true
233
234 Bundle generation
235 .................
236
237 Server administrators manually (and likely frequently) create streaming
238 clonebundles for every shape and expose them in the server clonebundle manifest
239 with their fingerprint. In future versions, non-streaming clonebundles will
240 be supported and their generations more automated.
241
242 You may generate the bundles with the following::
243
244 $ hg bundle --all --type"none-v2;stream-v2;shape=my-shape" ../bundle.hg
245 $ bundlespec="$(hg debugbundle --spec ../bundle.hg)"
246 $ # [move bundle to static server or to `.hg/bundle-cache` for inline serving]
247 $ bundleentry="https://static.com/bundle.hg BUNDLESPEC=$bundlespec"
248 $ # [or use inline SSH clonebundles]
249 $ bundleentry="peer-bundle-cache://bundle.hg BUNDLESPEC=$bundlespec"
250 $ echo $bundleentry >> .hg/clonebundles.manifest
251
252 You can export the includes and exclude patterns for every shape and give
253 them manually to clients::
254
255 $ hg admin::narrow-server --shape-narrow-patterns my-shape
256 [exclude]
257 path:foo
258 path:bar
259
260 Clients can then use said patterns when doing a narrow stream clone, which
261 matches the fingerprint from the patterns with that of the server clonebundles::
262
263 $ hg clone --narrow --narrowspec $NARROWSPECFILE
264
265 Client
266 ......
267
268 TODO explain current client-side config and how it might evolve (storing the
269 shape, fingerprint, etc.).
270 """
271
272 from __future__ import annotations
273
274 from mercurial import (
275 localrepo,
276 registrar,
277 requirements,
278 )
279
280
281 from . import (
282 narrowbundle2,
283 narrowcommands,
284 narrowrepo,
285 narrowtemplates,
286 narrowwirepeer,
287 )
288
289 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
290 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
291 # be specifying the version(s) of Mercurial they are tested with, or
292 # leave the attribute unspecified.
293 testedwith = b'ships-with-hg-core'
294
295 configtable = {}
296 configitem = registrar.configitem(configtable)
297 # Narrowhg *has* support for serving ellipsis nodes (which are used at
298 # least by Google's internal server), but that support is pretty
299 # fragile and has a lot of problems on real-world repositories that
300 # have complex graph topologies. This could probably be corrected, but
301 # absent someone needing the full support for ellipsis nodes in
302 # repositories with merges, it's unlikely this work will get done. As
303 # of this writining in late 2017, all repositories large enough for
304 # ellipsis nodes to be a hard requirement also enforce strictly linear
305 # history for other scaling reasons.
306 configitem(
307 b'experimental',
308 b'narrowservebrokenellipses',
309 default=False,
310 alias=[(b'narrow', b'serveellipses')],
311 )
312
313 # Export the commands table for Mercurial to see.
314 cmdtable = narrowcommands.table
315
316
317 def featuresetup(ui, features):
318 features.add(requirements.NARROW_REQUIREMENT)
319
320
321 def uisetup(ui):
322 """Wraps user-facing mercurial commands with narrow-aware versions."""
323 localrepo.featuresetupfuncs.add(featuresetup)
324 narrowbundle2.setup()
325 narrowcommands.setup()
326 narrowwirepeer.uisetup()
327
328
329 def reposetup(ui, repo):
330 """Wraps local repositories with narrow repo support."""
331 if not repo.local():
332 return
333
334 repo.ui.setconfig(b'experimental', b'narrow', True, b'narrow-ext')
335 if repo.is_narrow:
336 narrowrepo.wraprepo(repo)
337 narrowwirepeer.reposetup(repo)
338
339
340 templatekeyword = narrowtemplates.templatekeyword
341 revsetpredicate = narrowtemplates.revsetpredicate