Show More
@@ -1,111 +1,111 b'' | |||
|
1 | 1 | use std::path::Path; |
|
2 | 2 | |
|
3 | 3 | use crate::{ |
|
4 | 4 | errors::HgError, |
|
5 | 5 | exit_codes, |
|
6 | 6 | filepatterns::parse_pattern_file_contents, |
|
7 | 7 | matchers::{ |
|
8 | 8 | AlwaysMatcher, DifferenceMatcher, IncludeMatcher, Matcher, |
|
9 | 9 | NeverMatcher, |
|
10 | 10 | }, |
|
11 | 11 | repo::Repo, |
|
12 | 12 | requirements::NARROW_REQUIREMENT, |
|
13 | 13 | sparse::{self, SparseConfigError, SparseWarning}, |
|
14 | 14 | }; |
|
15 | 15 | |
|
16 | 16 | /// The file in .hg/store/ that indicates which paths exit in the store |
|
17 | 17 | const FILENAME: &str = "narrowspec"; |
|
18 | 18 | /// The file in .hg/ that indicates which paths exit in the dirstate |
|
19 | 19 | const DIRSTATE_FILENAME: &str = "narrowspec.dirstate"; |
|
20 | 20 | |
|
21 | 21 | /// Pattern prefixes that are allowed in narrow patterns. This list MUST |
|
22 | 22 | /// only contain patterns that are fast and safe to evaluate. Keep in mind |
|
23 | 23 | /// that patterns are supplied by clients and executed on remote servers |
|
24 | 24 | /// as part of wire protocol commands. That means that changes to this |
|
25 | 25 | /// data structure influence the wire protocol and should not be taken |
|
26 | 26 | /// lightly - especially removals. |
|
27 | 27 | const VALID_PREFIXES: [&str; 2] = ["path:", "rootfilesin:"]; |
|
28 | 28 | |
|
29 | 29 | /// Return the matcher for the current narrow spec, and all configuration |
|
30 | 30 | /// warnings to display. |
|
31 | 31 | pub fn matcher( |
|
32 | 32 | repo: &Repo, |
|
33 | 33 | ) -> Result<(Box<dyn Matcher + Sync>, Vec<SparseWarning>), SparseConfigError> { |
|
34 | 34 | let mut warnings = vec![]; |
|
35 | 35 | if !repo.requirements().contains(NARROW_REQUIREMENT) { |
|
36 | 36 | return Ok((Box::new(AlwaysMatcher), warnings)); |
|
37 | 37 | } |
|
38 | 38 | // Treat "narrowspec does not exist" the same as "narrowspec file exists |
|
39 | 39 | // and is empty". |
|
40 | 40 | let store_spec = repo.store_vfs().try_read(FILENAME)?.unwrap_or(vec![]); |
|
41 | 41 | let working_copy_spec = |
|
42 | 42 | repo.hg_vfs().try_read(DIRSTATE_FILENAME)?.unwrap_or(vec![]); |
|
43 | 43 | if store_spec != working_copy_spec { |
|
44 | 44 | return Err(HgError::abort( |
|
45 | 45 | "working copy's narrowspec is stale", |
|
46 | 46 | exit_codes::STATE_ERROR, |
|
47 | 47 | Some("run 'hg tracked --update-working-copy'".into()), |
|
48 | 48 | ) |
|
49 | 49 | .into()); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | let config = sparse::parse_config( |
|
53 | 53 | &store_spec, |
|
54 | 54 | sparse::SparseConfigContext::Narrow, |
|
55 | 55 | )?; |
|
56 | 56 | |
|
57 | 57 | warnings.extend(config.warnings); |
|
58 | 58 | |
|
59 | 59 | if !config.profiles.is_empty() { |
|
60 | 60 | // TODO (from Python impl) maybe do something with profiles? |
|
61 | 61 | return Err(SparseConfigError::IncludesInNarrow); |
|
62 | 62 | } |
|
63 | 63 | validate_patterns(&config.includes)?; |
|
64 | 64 | validate_patterns(&config.excludes)?; |
|
65 | 65 | |
|
66 | 66 | if config.includes.is_empty() { |
|
67 | 67 | return Ok((Box::new(NeverMatcher), warnings)); |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | let (patterns, subwarnings) = parse_pattern_file_contents( |
|
71 | 71 | &config.includes, |
|
72 | 72 | Path::new(""), |
|
73 | 73 | None, |
|
74 | 74 | false, |
|
75 | 75 | )?; |
|
76 | 76 | warnings.extend(subwarnings.into_iter().map(From::from)); |
|
77 | 77 | |
|
78 | 78 | let mut m: Box<dyn Matcher + Sync> = |
|
79 | 79 | Box::new(IncludeMatcher::new(patterns)?); |
|
80 | 80 | |
|
81 | 81 | let (patterns, subwarnings) = parse_pattern_file_contents( |
|
82 | 82 | &config.excludes, |
|
83 | 83 | Path::new(""), |
|
84 | 84 | None, |
|
85 | 85 | false, |
|
86 | 86 | )?; |
|
87 | 87 | if !patterns.is_empty() { |
|
88 | 88 | warnings.extend(subwarnings.into_iter().map(From::from)); |
|
89 | 89 | let exclude_matcher = Box::new(IncludeMatcher::new(patterns)?); |
|
90 | 90 | m = Box::new(DifferenceMatcher::new(m, exclude_matcher)); |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | Ok((m, warnings)) |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | fn validate_patterns(patterns: &[u8]) -> Result<(), SparseConfigError> { |
|
97 | 97 | for pattern in patterns.split(|c| *c == b'\n') { |
|
98 | 98 | if pattern.is_empty() { |
|
99 | 99 | continue; |
|
100 | 100 | } |
|
101 | 101 | for prefix in VALID_PREFIXES.iter() { |
|
102 | 102 | if pattern.starts_with(prefix.as_bytes()) { |
|
103 |
|
|
|
103 | return Ok(()); | |
|
104 | 104 | } |
|
105 | return Err(SparseConfigError::InvalidNarrowPrefix( | |
|
106 | pattern.to_owned(), | |
|
107 | )); | |
|
108 | 105 | } |
|
106 | return Err(SparseConfigError::InvalidNarrowPrefix( | |
|
107 | pattern.to_owned(), | |
|
108 | )); | |
|
109 | 109 | } |
|
110 | 110 | Ok(()) |
|
111 | 111 | } |
@@ -1,299 +1,311 b'' | |||
|
1 | 1 | $ . "$TESTDIR/narrow-library.sh" |
|
2 | 2 | |
|
3 | 3 | $ hg init master |
|
4 | 4 | $ cd master |
|
5 | 5 | $ cat >> .hg/hgrc <<EOF |
|
6 | 6 | > [narrow] |
|
7 | 7 | > serveellipses=True |
|
8 | 8 | > EOF |
|
9 | 9 | $ mkdir dir |
|
10 | 10 | $ mkdir dir/src |
|
11 | 11 | $ cd dir/src |
|
12 | 12 | $ for x in `$TESTDIR/seq.py 20`; do echo $x > "f$x"; hg add "f$x"; hg commit -m "Commit src $x"; done |
|
13 | 13 | $ cd .. |
|
14 | 14 | $ mkdir tests |
|
15 | 15 | $ cd tests |
|
16 | 16 | $ for x in `$TESTDIR/seq.py 20`; do echo $x > "t$x"; hg add "t$x"; hg commit -m "Commit test $x"; done |
|
17 | 17 | $ cd ../../.. |
|
18 | 18 | |
|
19 | 19 | Only path: and rootfilesin: pattern prefixes are allowed |
|
20 | 20 | |
|
21 | 21 | $ hg clone --narrow ssh://user@dummy/master badnarrow --noupdate --include 'glob:**' |
|
22 | 22 | abort: invalid prefix on narrow pattern: glob:** |
|
23 | 23 | (narrow patterns must begin with one of the following: path:, rootfilesin:) |
|
24 | 24 | [255] |
|
25 | 25 | |
|
26 | 26 | $ hg clone --narrow ssh://user@dummy/master badnarrow --noupdate --exclude 'set:ignored' |
|
27 | 27 | abort: invalid prefix on narrow pattern: set:ignored |
|
28 | 28 | (narrow patterns must begin with one of the following: path:, rootfilesin:) |
|
29 | 29 | [255] |
|
30 | 30 | |
|
31 | rootfilesin: patterns work | |
|
32 | ||
|
33 | $ hg clone --narrow ssh://user@dummy/master rootfilesin --noupdate --include 'rootfilesin:dir' | |
|
34 | requesting all changes | |
|
35 | adding changesets | |
|
36 | adding manifests | |
|
37 | adding file changes | |
|
38 | added 1 changesets with 0 changes to 0 files | |
|
39 | new changesets 26ce255d5b5d | |
|
40 | $ hg tracked -R rootfilesin | |
|
41 | I rootfilesin:dir | |
|
42 | ||
|
31 | 43 | narrow clone a file, f10 |
|
32 | 44 | |
|
33 | 45 | $ hg clone --narrow ssh://user@dummy/master narrow --noupdate --include "dir/src/f10" |
|
34 | 46 | requesting all changes |
|
35 | 47 | adding changesets |
|
36 | 48 | adding manifests |
|
37 | 49 | adding file changes |
|
38 | 50 | added 3 changesets with 1 changes to 1 files |
|
39 | 51 | new changesets *:* (glob) |
|
40 | 52 | $ cd narrow |
|
41 | 53 | $ hg debugrequires | grep -v generaldelta |
|
42 | 54 | dotencode |
|
43 | 55 | dirstate-v2 (dirstate-v2 !) |
|
44 | 56 | fncache |
|
45 | 57 | narrowhg-experimental |
|
46 | 58 | persistent-nodemap (rust !) |
|
47 | 59 | revlog-compression-zstd (zstd !) |
|
48 | 60 | revlogv1 |
|
49 | 61 | share-safe |
|
50 | 62 | sparserevlog |
|
51 | 63 | store |
|
52 | 64 | testonly-simplestore (reposimplestore !) |
|
53 | 65 | |
|
54 | 66 | $ hg tracked |
|
55 | 67 | I path:dir/src/f10 |
|
56 | 68 | $ hg tracked |
|
57 | 69 | I path:dir/src/f10 |
|
58 | 70 | $ hg update |
|
59 | 71 | 1 files updated, 0 files merged, 0 files removed, 0 files unresolved |
|
60 | 72 | $ find * | sort |
|
61 | 73 | dir |
|
62 | 74 | dir/src |
|
63 | 75 | dir/src/f10 |
|
64 | 76 | $ cat dir/src/f10 |
|
65 | 77 | 10 |
|
66 | 78 | |
|
67 | 79 | $ cd .. |
|
68 | 80 | |
|
69 | 81 | local-to-local narrow clones work |
|
70 | 82 | |
|
71 | 83 | $ hg clone --narrow master narrow-via-localpeer --noupdate --include "dir/src/f10" |
|
72 | 84 | requesting all changes |
|
73 | 85 | adding changesets |
|
74 | 86 | adding manifests |
|
75 | 87 | adding file changes |
|
76 | 88 | added 3 changesets with 1 changes to 1 files |
|
77 | 89 | new changesets 5d21aaea77f8:26ce255d5b5d |
|
78 | 90 | $ hg tracked -R narrow-via-localpeer |
|
79 | 91 | I path:dir/src/f10 |
|
80 | 92 | $ rm -Rf narrow-via-localpeer |
|
81 | 93 | |
|
82 | 94 | narrow clone with a newline should fail |
|
83 | 95 | |
|
84 | 96 | $ hg clone --narrow ssh://user@dummy/master narrow_fail --noupdate --include 'dir/src/f10 |
|
85 | 97 | > ' |
|
86 | 98 | abort: newlines are not allowed in narrowspec paths |
|
87 | 99 | [255] |
|
88 | 100 | |
|
89 | 101 | narrow clone a directory, tests/, except tests/t19 |
|
90 | 102 | |
|
91 | 103 | $ hg clone --narrow ssh://user@dummy/master narrowdir --noupdate --include "dir/tests/" --exclude "dir/tests/t19" |
|
92 | 104 | requesting all changes |
|
93 | 105 | adding changesets |
|
94 | 106 | adding manifests |
|
95 | 107 | adding file changes |
|
96 | 108 | added 21 changesets with 19 changes to 19 files |
|
97 | 109 | new changesets *:* (glob) |
|
98 | 110 | $ cd narrowdir |
|
99 | 111 | $ hg tracked |
|
100 | 112 | I path:dir/tests |
|
101 | 113 | X path:dir/tests/t19 |
|
102 | 114 | $ hg tracked |
|
103 | 115 | I path:dir/tests |
|
104 | 116 | X path:dir/tests/t19 |
|
105 | 117 | $ hg update |
|
106 | 118 | 19 files updated, 0 files merged, 0 files removed, 0 files unresolved |
|
107 | 119 | $ find * | sort |
|
108 | 120 | dir |
|
109 | 121 | dir/tests |
|
110 | 122 | dir/tests/t1 |
|
111 | 123 | dir/tests/t10 |
|
112 | 124 | dir/tests/t11 |
|
113 | 125 | dir/tests/t12 |
|
114 | 126 | dir/tests/t13 |
|
115 | 127 | dir/tests/t14 |
|
116 | 128 | dir/tests/t15 |
|
117 | 129 | dir/tests/t16 |
|
118 | 130 | dir/tests/t17 |
|
119 | 131 | dir/tests/t18 |
|
120 | 132 | dir/tests/t2 |
|
121 | 133 | dir/tests/t20 |
|
122 | 134 | dir/tests/t3 |
|
123 | 135 | dir/tests/t4 |
|
124 | 136 | dir/tests/t5 |
|
125 | 137 | dir/tests/t6 |
|
126 | 138 | dir/tests/t7 |
|
127 | 139 | dir/tests/t8 |
|
128 | 140 | dir/tests/t9 |
|
129 | 141 | |
|
130 | 142 | $ cd .. |
|
131 | 143 | |
|
132 | 144 | narrow clone everything but a directory (tests/) |
|
133 | 145 | |
|
134 | 146 | $ hg clone --narrow ssh://user@dummy/master narrowroot --noupdate --exclude "dir/tests" |
|
135 | 147 | requesting all changes |
|
136 | 148 | adding changesets |
|
137 | 149 | adding manifests |
|
138 | 150 | adding file changes |
|
139 | 151 | added 21 changesets with 20 changes to 20 files |
|
140 | 152 | new changesets *:* (glob) |
|
141 | 153 | $ cd narrowroot |
|
142 | 154 | $ hg tracked |
|
143 | 155 | I path:. |
|
144 | 156 | X path:dir/tests |
|
145 | 157 | $ hg tracked |
|
146 | 158 | I path:. |
|
147 | 159 | X path:dir/tests |
|
148 | 160 | $ hg update |
|
149 | 161 | 20 files updated, 0 files merged, 0 files removed, 0 files unresolved |
|
150 | 162 | $ find * | sort |
|
151 | 163 | dir |
|
152 | 164 | dir/src |
|
153 | 165 | dir/src/f1 |
|
154 | 166 | dir/src/f10 |
|
155 | 167 | dir/src/f11 |
|
156 | 168 | dir/src/f12 |
|
157 | 169 | dir/src/f13 |
|
158 | 170 | dir/src/f14 |
|
159 | 171 | dir/src/f15 |
|
160 | 172 | dir/src/f16 |
|
161 | 173 | dir/src/f17 |
|
162 | 174 | dir/src/f18 |
|
163 | 175 | dir/src/f19 |
|
164 | 176 | dir/src/f2 |
|
165 | 177 | dir/src/f20 |
|
166 | 178 | dir/src/f3 |
|
167 | 179 | dir/src/f4 |
|
168 | 180 | dir/src/f5 |
|
169 | 181 | dir/src/f6 |
|
170 | 182 | dir/src/f7 |
|
171 | 183 | dir/src/f8 |
|
172 | 184 | dir/src/f9 |
|
173 | 185 | |
|
174 | 186 | $ cd .. |
|
175 | 187 | |
|
176 | 188 | narrow clone no paths at all |
|
177 | 189 | |
|
178 | 190 | $ hg clone --narrow ssh://user@dummy/master narrowempty --noupdate |
|
179 | 191 | requesting all changes |
|
180 | 192 | adding changesets |
|
181 | 193 | adding manifests |
|
182 | 194 | adding file changes |
|
183 | 195 | added 1 changesets with 0 changes to 0 files |
|
184 | 196 | new changesets * (glob) |
|
185 | 197 | $ cd narrowempty |
|
186 | 198 | $ hg tracked |
|
187 | 199 | $ hg update |
|
188 | 200 | 0 files updated, 0 files merged, 0 files removed, 0 files unresolved |
|
189 | 201 | $ ls -A |
|
190 | 202 | .hg |
|
191 | 203 | |
|
192 | 204 | $ cd .. |
|
193 | 205 | |
|
194 | 206 | simple clone |
|
195 | 207 | $ hg clone ssh://user@dummy/master simpleclone |
|
196 | 208 | requesting all changes |
|
197 | 209 | adding changesets |
|
198 | 210 | adding manifests |
|
199 | 211 | adding file changes |
|
200 | 212 | added 40 changesets with 40 changes to 40 files |
|
201 | 213 | new changesets * (glob) |
|
202 | 214 | updating to branch default |
|
203 | 215 | 40 files updated, 0 files merged, 0 files removed, 0 files unresolved |
|
204 | 216 | $ cd simpleclone |
|
205 | 217 | $ find * | sort |
|
206 | 218 | dir |
|
207 | 219 | dir/src |
|
208 | 220 | dir/src/f1 |
|
209 | 221 | dir/src/f10 |
|
210 | 222 | dir/src/f11 |
|
211 | 223 | dir/src/f12 |
|
212 | 224 | dir/src/f13 |
|
213 | 225 | dir/src/f14 |
|
214 | 226 | dir/src/f15 |
|
215 | 227 | dir/src/f16 |
|
216 | 228 | dir/src/f17 |
|
217 | 229 | dir/src/f18 |
|
218 | 230 | dir/src/f19 |
|
219 | 231 | dir/src/f2 |
|
220 | 232 | dir/src/f20 |
|
221 | 233 | dir/src/f3 |
|
222 | 234 | dir/src/f4 |
|
223 | 235 | dir/src/f5 |
|
224 | 236 | dir/src/f6 |
|
225 | 237 | dir/src/f7 |
|
226 | 238 | dir/src/f8 |
|
227 | 239 | dir/src/f9 |
|
228 | 240 | dir/tests |
|
229 | 241 | dir/tests/t1 |
|
230 | 242 | dir/tests/t10 |
|
231 | 243 | dir/tests/t11 |
|
232 | 244 | dir/tests/t12 |
|
233 | 245 | dir/tests/t13 |
|
234 | 246 | dir/tests/t14 |
|
235 | 247 | dir/tests/t15 |
|
236 | 248 | dir/tests/t16 |
|
237 | 249 | dir/tests/t17 |
|
238 | 250 | dir/tests/t18 |
|
239 | 251 | dir/tests/t19 |
|
240 | 252 | dir/tests/t2 |
|
241 | 253 | dir/tests/t20 |
|
242 | 254 | dir/tests/t3 |
|
243 | 255 | dir/tests/t4 |
|
244 | 256 | dir/tests/t5 |
|
245 | 257 | dir/tests/t6 |
|
246 | 258 | dir/tests/t7 |
|
247 | 259 | dir/tests/t8 |
|
248 | 260 | dir/tests/t9 |
|
249 | 261 | |
|
250 | 262 | $ cd .. |
|
251 | 263 | |
|
252 | 264 | Testing the --narrowspec flag to clone |
|
253 | 265 | |
|
254 | 266 | $ cat >> narrowspecs <<EOF |
|
255 | 267 | > %include foo |
|
256 | 268 | > [include] |
|
257 | 269 | > path:dir/tests/ |
|
258 | 270 | > path:dir/src/f12 |
|
259 | 271 | > EOF |
|
260 | 272 | |
|
261 | 273 | $ hg clone ssh://user@dummy/master specfile --narrowspec narrowspecs |
|
262 | 274 | reading narrowspec from '$TESTTMP/narrowspecs' |
|
263 | 275 | config error: cannot specify other files using '%include' in narrowspec |
|
264 | 276 | [30] |
|
265 | 277 | |
|
266 | 278 | $ cat > narrowspecs <<EOF |
|
267 | 279 | > [include] |
|
268 | 280 | > path:dir/tests/ |
|
269 | 281 | > path:dir/src/f12 |
|
270 | 282 | > EOF |
|
271 | 283 | |
|
272 | 284 | $ hg clone ssh://user@dummy/master specfile --narrowspec narrowspecs |
|
273 | 285 | reading narrowspec from '$TESTTMP/narrowspecs' |
|
274 | 286 | requesting all changes |
|
275 | 287 | adding changesets |
|
276 | 288 | adding manifests |
|
277 | 289 | adding file changes |
|
278 | 290 | added 23 changesets with 21 changes to 21 files |
|
279 | 291 | new changesets c13e3773edb4:26ce255d5b5d |
|
280 | 292 | updating to branch default |
|
281 | 293 | 21 files updated, 0 files merged, 0 files removed, 0 files unresolved |
|
282 | 294 | $ cd specfile |
|
283 | 295 | $ hg tracked |
|
284 | 296 | I path:dir/src/f12 |
|
285 | 297 | I path:dir/tests |
|
286 | 298 | $ cd .. |
|
287 | 299 | |
|
288 | 300 | Narrow spec with invalid patterns is rejected |
|
289 | 301 | |
|
290 | 302 | $ cat > narrowspecs <<EOF |
|
291 | 303 | > [include] |
|
292 | 304 | > glob:** |
|
293 | 305 | > EOF |
|
294 | 306 | |
|
295 | 307 | $ hg clone ssh://user@dummy/master badspecfile --narrowspec narrowspecs |
|
296 | 308 | reading narrowspec from '$TESTTMP/narrowspecs' |
|
297 | 309 | abort: invalid prefix on narrow pattern: glob:** |
|
298 | 310 | (narrow patterns must begin with one of the following: path:, rootfilesin:) |
|
299 | 311 | [255] |
General Comments 0
You need to be logged in to leave comments.
Login now