Debian Codegraph @

docs/design/sparse-v2.md

raw
1 # Sparse Patterns v2 redesign
2
3 Authors: [Daniel Ploch](mailto:[email protected])
4
5 **Summary:** This Document documents a redesign of the sparse command and
6 it's internal storage format in jj, in order to facilitate several desirable
7 improvements for large repos. It covers both the migration path and the planned
8 end state.
9
10 ## Objective
11
12 Redesign Sparse Patterns to accommodate more advanced features for native
13 and custom implementations. This includes three main goals:
14
15 1. Sparse Patterns should be versioned with the working copy
16 1. Sparse Patterns should support more [flexible matching rules](https://github.com/jj-vcs/jj/issues/1896)
17 1. Sparse Patterns should support [client path remapping](https://github.com/jj-vcs/jj/issues/2288)
18
19 ## Current State (as of jj 0.13.0)
20
21 Sparse patterns are an effectively unordered list of prefix strings:
22
23 ```txt
24 path/one
25 path/to/dir/two
26 ```
27
28 The _set_ of files identified by the Sparse Patterns is all paths which match
29 any provided prefix. This governs what gets materialized in the working copy on
30 checkout, and what is updated on snapshot. The set is stored in working copy
31 state files which are not versioned in the Op Store.
32
33 Because all paths are bare strings with no escaping or higher-level formatting,
34 the current design makes it difficult to add new features like exclusions or
35 path remappings.
36
37 ## Proposed State (Sparse Patterns v2)
38
39 Sparse Patterns v2 will be stored as objects in the Op Store, referenced
40 by a `WorkingCopyPatternsId` from the active `View`. They will have a new,
41 ordered structure which can fully represent previous patterns.
42
43 ```rust
44 /// Analogues of RepoPath, specifically describing paths in the working copy.
45 struct WorkingCopyPathBuf {
46 String
47 }
48 struct WorkingCopyPath {
49 str
50 }
51
52 pub enum SparsePatternsPathType {
53 Dir, // Everything under <path>/...
54 Files, // Files under <path>/*
55 Exact, // <path> exactly
56 }
57
58 pub struct SparsePatternsPath {
59 path_type: SparsePatternsPathType,
60 include: bool, // True if included, false if excluded.
61 path: RepoPathBuf,
62 }
63
64 pub struct WorkingCopyMapping {
65 src_path: RepoPathBuf,
66 dst_path: WorkingCopyPathBuf,
67 recursive: bool, // If false, only immediate children of src_path (files) are renamed.
68 }
69
70 pub struct WorkingCopyPatterns {
71 sparse_paths: Vec<SparsePatternsPath>,
72 mappings: Vec<WorkingCopyMapping>,
73 }
74
75 pub trait OpStore {
76 ...
77 pub fn read_working_copy_patterns(&self, id: &WorkingCopyPatternsId) -> OpStoreResult<WorkingCopyPatterns> { ... }
78 pub fn write_working_copy_patterns(&self, sparse_patterns: &WorkingCopyPatterns) -> OpStoreResult<WorkingCopyPatternsId> { .. }
79 }
80 ```
81
82 To support these more complex behaviors, a new `WorkingCopyPatterns` trait will
83 be introduced, initially only as a thin wrapper around the existing prefix
84 format, but soon to be expanded with richer types and functionality.
85
86 ```rust
87 impl WorkingCopyPatterns {
88 pub fn to_matcher(&self) -> Box<dyn Matcher> {
89 ...
90 }
91
92 ...
93 }
94 ```
95
96 ### Command Syntax
97
98 `SparsePatternsPath` rules can be specified on the CLI and in an editor via a
99 compact syntax:
100
101 ```txt
102 (include|exclude):(dir|files|exact):<path>
103 ```
104
105 If both prefix terms are omitted, then `include:dir:` is assumed. If any prefix
106 is specified, both must be specified. The editor and CLI will both accept path
107 rules in either format going forward.
108
109 - `jj sparse set --add foo/bar` is equal to `jj sparse set --add include:dir:foo/bar`
110 - `jj sparse set --add exclude:dir:foo/bar` adds a new `Dir` type rule with `include = false`
111 - `jj sparse set --exclude foo/bar` as a possible shorthand for the above
112 - `jj sparse list` will print the explicit rules
113
114 Paths will be stored in an ordered, canonical form which unambiguously describes
115 the set of files to be included. Every `--add` command will append to the end of
116 this list before the patterns are canonicalized. Whether a file is included is
117 determined by the first matching rule in reverse order.
118
119 For example:
120
121 ```txt
122 include:dir:foo
123 exclude:dir:foo/bar
124 include:dir:foo/bar/baz
125 exclude:dir:foo/bar/baz/qux
126 ```
127
128 Produces rule set which includes "foo/file.txt", excludes "foo/bar/file.txt",
129 includes "foo/bar/baz/file.txt", and excludes "foo/bar/baz/qux/file.txt".
130
131 If the rules are subtly re-ordered, they become canonicalized to a smaller, but
132 functionally equivalent form:
133
134 ```txt
135 # Before
136 include:dir:foo
137 exclude:dir:foo/bar/baz/qux
138 include:dir:foo/bar/baz
139 exclude:dir:foo/bar
140
141 # Canonicalized
142 include:dir:foo
143 exclude:dir:foo/bar
144 ```
145
146 #### Canonicalization
147
148 There are many ways to represent functionally equivalent `WorkingCopyPatterns`.
149 For instance, the following 4 rule sets are all functionally equivalent:
150
151 ```txt
152 # Set 1
153 include:dir:bar
154 include:dir:foo
155
156 # Set 2
157 include:dir:foo
158 include:dir:bar
159
160 # Set 3
161 include:dir:bar
162 include:dir:bar/baz/qux
163 include:dir:foo
164
165 # Set 4
166 include:dir:foo
167 exclude:dir:foo/baz
168 include:dir:bar
169 include:dir:foo/baz
170 ```
171
172 Because these patterns are stored in the Op Store now, it is useful for all of
173 these representations to be rewritten into a minimal, canonical form before
174 serialization. In this case, `Set 1` will be the canonical set. The canonical
175 form of a `WorkingCopyPatterns` is defined as the form such that:
176
177 - Every rule affects the functionality (there are no redundant rules)
178 - Rules are sorted lexicographically, but with '/' sorted before all else
179 - This special sorting order is useful for constructing path tries
180
181 ### Working Copy Map
182
183 !!! warning
184 This section is intentionally lacking, more research is needed.
185
186 All `WorkingCopyPatterns` will come equipped with a default no-op mapping.
187 These mappings are inspired by and similar to [Perforce client views](https://www.perforce.com/manuals/cmdref/Content/CmdRef/views.html).
188
189 ```rust
190 vec![WorkingCopyMapping {
191 src_path: RepoPathBuf::root(),
192 dst_path: WorkingCopyPathBuf::root(),
193 recursive: true,
194 }]
195 ```
196
197 `WorkingCopyPatterns` will provide an interface to map working copy paths into
198 repo paths and vice versa. The `WorkingCopy` trait will apply this mapping to
199 all snapshot and checkout operations, and jj commands which accept relative
200 paths will need to be updated to perform working copy path -> repo path
201 translations as needed. It's not clear at this time _which_ commands will need
202 changing, as some are more likely to refer to repo paths rather than working
203 copy paths.
204
205 TODO: Expand this section.
206
207 In particular, the path rules for sparse patterns will _always_ be repo paths,
208 not working copy paths. Thus, if the working copy wants to track "foo" and
209 rename it to "subdir/bar", they must `jj sparse set --add foo` and
210 `jj map set --from foo --to bar`. In other words, the mapping operation can
211 be thought of as always _after_ the sparse operation.
212
213 #### Command Syntax
214
215 New commands will enable editing of the `WorkingCopyMapping`s:
216
217 TODO: Maybe this should be `jj workspace map ...`?
218
219 - `jj map list` will print all mapping pairs.
220 - `jj map add --from foo --to bar` will add a new mapping to the end of the list.
221 - `jj map remove --from foo` will remove a specific mapping rule.
222 - `jj map edit` will pull up a text editor for manual editing.
223
224 Like sparse paths, mappings will have a compact text syntax for editing in file
225 form, or for adding a rule textually on the CLI:
226
227 ```txt
228 "<from>" -> "<to>" [nonrecursive]
229 ```
230
231 Like sparse paths, mapping rules are defined to apply in _order_ and on any
232 save operation will be modified to a minimal canonical form. Thus,
233 `jj map set --from "" --to ""` will always completely wipe the map.
234 The first matching rule in reverse list order determines how a particular
235 repo path should be mapped into the working copy, and likewise how a particular
236 working copy path should be mapped into the repo. For simplicity, the
237 'last rule wins' applies both for repo->WC conversions, as well as WC->repo
238 conversions, using the same ordering.
239
240 If a working copy mapping places the same repo file at two distinct working
241 copy paths, snapshotting will fail unless these files are identical. Some
242 specialized filesystems may even treat these as the 'same' file, allowing this
243 to work in some cases.
244
245 If a working copy mapping places two distinct repo files at the same working
246 copy path, checkout will fail with an error regardless of equivalence.
247
248 ### Versioning and Storage
249
250 Updating the active `WorkingCopyPatterns` for a particular working copy will now
251 take place in two separate steps: one transaction which updates the op store,
252 and a separate `LockedWorkingCopy` operation which actually updates the working
253 copy. The working copy proto will no longer store `WorkingCopyPatterns`
254 directly, instead storing only a `WorkingCopyPatternsId`. On mismatch with the
255 current op head, the user will be prompted to run `jj workspace update-stale`.
256
257 This gives the user the ability to update the active `WorkingCopyPatterns`
258 whilst not interacting with the local working copy, which is useful for custom
259 integrations which may not be _able_ to check out particular working copy
260 patterns due to problems with the backend (encoding, permission errors, etc.). A
261 bad `jj sparse set --add oops` command can thus be undone, even via `jj undo` if
262 desired.
263
264 #### View Updates
265
266 The View object will be migrated to store working copy patterns via id. The
267 indirection will save on storage since working copy patterns are not expected to
268 change very frequently.
269
270 ```rust
271 // Before:
272 pub wc_commit_ids: HashMap<WorkspaceNameBuf, CommitId>,
273
274 // After:
275 pub struct WorkingCopyInfo {
276 pub commit_id: CommitId,
277 pub wc_patterns_id: WorkingCopyPatternsId,
278 }
279 ...
280 pub wc_info: HashMap<WorkspaceNameBuf, WorkingCopyInfo>,
281 ```
282
283 A View object with no stored working copy patterns will be modified at read
284 time to include the current working copy patterns, thus all `read_view`
285 operations will need to pass in the current working copy patterns for a
286 migration period of at least 6 months. After that, we may choose to auto-fill
287 missing working copy infos with a default `WorkingCopyPatterns` as needed.
288
289 ### Appendix
290
291 #### Related Work
292
293 [Perforce client maps](https://www.perforce.com/manuals/cmdref/Content/CmdRef/views.html)
294 are very similar in concept to the entirety of `WorkingCopyPatterns`, and this
295 design aims to achieve similar functionality.
296
297 The [Josh Project](https://github.com/josh-project/josh) implements partial git
298 clones in a way similar to how sparse patterns try to work.
299
300 #### Patterns via configuration
301
302 There may be some scenarios where it is valuable to configure working copy
303 patterns via a configuration file, rather than through explicit commands.
304 Generally this only makes sense for automated repos, with the configuration
305 coming from outside the repo - there are too many caveats and edge cases if the
306 configuration comes from inside the repo and/or is fought with by a human.
307
308 No configuration syntax is planned at this time but if we add any, we should
309 probably reuse the compact line syntaxes as much as possible for consistency.