Debian Codegraph @

lib/tests/test_local_working_copy_sparse.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use futures::StreamExt as _;
16 use itertools::Itertools as _;
17 use jj_lib::local_working_copy::LocalWorkingCopy;
18 use jj_lib::matchers::EverythingMatcher;
19 use jj_lib::repo::Repo as _;
20 use jj_lib::repo_path::RepoPath;
21 use jj_lib::repo_path::RepoPathBuf;
22 use jj_lib::working_copy::CheckoutStats;
23 use jj_lib::working_copy::WorkingCopy as _;
24 use pollster::FutureExt as _;
25 use testutils::TestResult;
26 use testutils::TestWorkspace;
27 use testutils::commit_with_tree;
28 use testutils::create_tree;
29 use testutils::repo_path;
30
31 fn to_owned_path_vec(paths: &[&RepoPath]) -> Vec<RepoPathBuf> {
32 paths.iter().map(|&path| path.to_owned()).collect()
33 }
34
35 #[test]
36 fn test_sparse_checkout() -> TestResult {
37 let mut test_workspace = TestWorkspace::init();
38 let repo = &test_workspace.repo;
39 let working_copy_path = test_workspace.workspace.workspace_root().to_owned();
40
41 let root_file1_path = repo_path("file1");
42 let root_file2_path = repo_path("file2");
43 let dir1_path = repo_path("dir1");
44 let dir1_file1_path = repo_path("dir1/file1");
45 let dir1_file2_path = repo_path("dir1/file2");
46 let dir1_subdir1_path = repo_path("dir1/subdir1");
47 let dir1_subdir1_file1_path = repo_path("dir1/subdir1/file1");
48 let dir2_path = repo_path("dir2");
49 let dir2_file1_path = repo_path("dir2/file1");
50
51 let tree = create_tree(
52 repo,
53 &[
54 (root_file1_path, "contents"),
55 (root_file2_path, "contents"),
56 (dir1_file1_path, "contents"),
57 (dir1_file2_path, "contents"),
58 (dir1_subdir1_file1_path, "contents"),
59 (dir2_file1_path, "contents"),
60 ],
61 );
62 let commit = commit_with_tree(repo.store(), tree);
63
64 test_workspace
65 .workspace
66 .check_out(repo.op_id().clone(), None, &commit)
67 .block_on()?;
68 let ws = &mut test_workspace.workspace;
69
70 // Set sparse patterns to only dir1/
71 let mut locked_ws = ws.start_working_copy_mutation().block_on()?;
72 let sparse_patterns = to_owned_path_vec(&[dir1_path]);
73 let stats = locked_ws
74 .locked_wc()
75 .set_sparse_patterns(sparse_patterns.clone())
76 .block_on()?;
77 assert_eq!(
78 stats,
79 CheckoutStats {
80 updated_files: 0,
81 added_files: 0,
82 removed_files: 3,
83 skipped_files: 0,
84 }
85 );
86 assert_eq!(locked_ws.locked_wc().sparse_patterns()?, sparse_patterns);
87 assert!(
88 !root_file1_path
89 .to_fs_path_unchecked(&working_copy_path)
90 .exists()
91 );
92 assert!(
93 !root_file2_path
94 .to_fs_path_unchecked(&working_copy_path)
95 .exists()
96 );
97 assert!(
98 dir1_file1_path
99 .to_fs_path_unchecked(&working_copy_path)
100 .exists()
101 );
102 assert!(
103 dir1_file2_path
104 .to_fs_path_unchecked(&working_copy_path)
105 .exists()
106 );
107 assert!(
108 dir1_subdir1_file1_path
109 .to_fs_path_unchecked(&working_copy_path)
110 .exists()
111 );
112 assert!(
113 !dir2_file1_path
114 .to_fs_path_unchecked(&working_copy_path)
115 .exists()
116 );
117
118 // Write the new state to disk
119 locked_ws.finish(repo.op_id().clone()).block_on()?;
120 let wc: &LocalWorkingCopy = ws.working_copy().downcast_ref().unwrap();
121 assert_eq!(
122 wc.file_states()?.paths().collect_vec(),
123 vec![dir1_file1_path, dir1_file2_path, dir1_subdir1_file1_path]
124 );
125 assert_eq!(wc.sparse_patterns()?, sparse_patterns);
126
127 // Reload the state to check that it was persisted
128 let wc = LocalWorkingCopy::load(
129 repo.store().clone(),
130 ws.workspace_root().to_path_buf(),
131 wc.state_path().to_path_buf(),
132 repo.settings(),
133 )?;
134 assert_eq!(
135 wc.file_states()?.paths().collect_vec(),
136 vec![dir1_file1_path, dir1_file2_path, dir1_subdir1_file1_path]
137 );
138 assert_eq!(wc.sparse_patterns()?, sparse_patterns);
139
140 // Set sparse patterns to file2, dir1/subdir1/ and dir2/
141 let mut locked_wc = wc.start_mutation().block_on()?;
142 let sparse_patterns = to_owned_path_vec(&[root_file1_path, dir1_subdir1_path, dir2_path]);
143 let stats = locked_wc
144 .set_sparse_patterns(sparse_patterns.clone())
145 .block_on()?;
146 assert_eq!(
147 stats,
148 CheckoutStats {
149 updated_files: 0,
150 added_files: 2,
151 removed_files: 2,
152 skipped_files: 0,
153 }
154 );
155 assert_eq!(locked_wc.sparse_patterns()?, sparse_patterns);
156 assert!(
157 root_file1_path
158 .to_fs_path_unchecked(&working_copy_path)
159 .exists()
160 );
161 assert!(
162 !root_file2_path
163 .to_fs_path_unchecked(&working_copy_path)
164 .exists()
165 );
166 assert!(
167 !dir1_file1_path
168 .to_fs_path_unchecked(&working_copy_path)
169 .exists()
170 );
171 assert!(
172 !dir1_file2_path
173 .to_fs_path_unchecked(&working_copy_path)
174 .exists()
175 );
176 assert!(
177 dir1_subdir1_file1_path
178 .to_fs_path_unchecked(&working_copy_path)
179 .exists()
180 );
181 assert!(
182 dir2_file1_path
183 .to_fs_path_unchecked(&working_copy_path)
184 .exists()
185 );
186 let wc = locked_wc.finish(repo.op_id().clone()).block_on()?;
187 let wc: &LocalWorkingCopy = wc.downcast_ref().unwrap();
188 assert_eq!(
189 wc.file_states()?.paths().collect_vec(),
190 vec![dir1_subdir1_file1_path, dir2_file1_path, root_file1_path]
191 );
192 Ok(())
193 }
194
195 /// Test that sparse patterns are respected on commit
196 #[test]
197 fn test_sparse_commit() -> TestResult {
198 let mut test_workspace = TestWorkspace::init();
199 let repo = &test_workspace.repo;
200 let op_id = repo.op_id().clone();
201 let working_copy_path = test_workspace.workspace.workspace_root().to_owned();
202
203 let root_file1_path = repo_path("file1");
204 let dir1_path = repo_path("dir1");
205 let dir1_file1_path = repo_path("dir1/file1");
206 let dir2_path = repo_path("dir2");
207 let dir2_file1_path = repo_path("dir2/file1");
208
209 let tree = create_tree(
210 repo,
211 &[
212 (root_file1_path, "contents"),
213 (dir1_file1_path, "contents"),
214 (dir2_file1_path, "contents"),
215 ],
216 );
217
218 let commit = commit_with_tree(repo.store(), tree.clone());
219 test_workspace
220 .workspace
221 .check_out(repo.op_id().clone(), None, &commit)
222 .block_on()?;
223
224 // Set sparse patterns to only dir1/
225 let mut locked_ws = test_workspace
226 .workspace
227 .start_working_copy_mutation()
228 .block_on()?;
229 let sparse_patterns = to_owned_path_vec(&[dir1_path]);
230 locked_ws
231 .locked_wc()
232 .set_sparse_patterns(sparse_patterns)
233 .block_on()?;
234 locked_ws.finish(repo.op_id().clone()).block_on()?;
235
236 // Write modified version of all files, including files that are not in the
237 // sparse patterns.
238 std::fs::write(
239 root_file1_path.to_fs_path_unchecked(&working_copy_path),
240 "modified",
241 )?;
242 std::fs::write(
243 dir1_file1_path.to_fs_path_unchecked(&working_copy_path),
244 "modified",
245 )?;
246 std::fs::create_dir(dir2_path.to_fs_path_unchecked(&working_copy_path))?;
247 std::fs::write(
248 dir2_file1_path.to_fs_path_unchecked(&working_copy_path),
249 "modified",
250 )?;
251
252 // Create a tree from the working copy. Only dir1/file1 should be updated in the
253 // tree.
254 let modified_tree = test_workspace.snapshot()?;
255 let diff: Vec<_> = tree
256 .diff_stream(&modified_tree, &EverythingMatcher)
257 .collect()
258 .block_on();
259 assert_eq!(diff.len(), 1);
260 assert_eq!(diff[0].path.as_ref(), dir1_file1_path);
261
262 // Set sparse patterns to also include dir2/
263 let mut locked_ws = test_workspace
264 .workspace
265 .start_working_copy_mutation()
266 .block_on()?;
267 let sparse_patterns = to_owned_path_vec(&[dir1_path, dir2_path]);
268 locked_ws
269 .locked_wc()
270 .set_sparse_patterns(sparse_patterns)
271 .block_on()?;
272 locked_ws.finish(op_id).block_on()?;
273
274 // Create a tree from the working copy. Only dir1/file1 and dir2/file1 should be
275 // updated in the tree.
276 let modified_tree = test_workspace.snapshot()?;
277 let diff: Vec<_> = tree
278 .diff_stream(&modified_tree, &EverythingMatcher)
279 .collect()
280 .block_on();
281 assert_eq!(diff.len(), 2);
282 assert_eq!(diff[0].path.as_ref(), dir1_file1_path);
283 assert_eq!(diff[1].path.as_ref(), dir2_file1_path);
284 Ok(())
285 }
286
287 #[test]
288 fn test_sparse_commit_gitignore() -> TestResult {
289 // Test that (untracked) .gitignore files in parent directories are respected
290 let mut test_workspace = TestWorkspace::init();
291 let repo = &test_workspace.repo;
292 let working_copy_path = test_workspace.workspace.workspace_root().to_owned();
293
294 let dir1_path = repo_path("dir1");
295 let dir1_file1_path = repo_path("dir1/file1");
296 let dir1_file2_path = repo_path("dir1/file2");
297
298 // Set sparse patterns to only dir1/
299 let mut locked_ws = test_workspace
300 .workspace
301 .start_working_copy_mutation()
302 .block_on()?;
303 let sparse_patterns = to_owned_path_vec(&[dir1_path]);
304 locked_ws
305 .locked_wc()
306 .set_sparse_patterns(sparse_patterns)
307 .block_on()?;
308 locked_ws.finish(repo.op_id().clone()).block_on()?;
309
310 // Write dir1/file1 and dir1/file2 and a .gitignore saying to ignore dir1/file1
311 std::fs::write(working_copy_path.join(".gitignore"), "dir1/file1")?;
312 std::fs::create_dir(dir1_path.to_fs_path_unchecked(&working_copy_path))?;
313 std::fs::write(
314 dir1_file1_path.to_fs_path_unchecked(&working_copy_path),
315 "contents",
316 )?;
317 std::fs::write(
318 dir1_file2_path.to_fs_path_unchecked(&working_copy_path),
319 "contents",
320 )?;
321
322 // Create a tree from the working copy. Only dir1/file2 should be updated in the
323 // tree because dir1/file1 is ignored.
324 let modified_tree = test_workspace.snapshot()?;
325 let entries = modified_tree.entries().collect_vec();
326 assert_eq!(entries.len(), 1);
327 assert_eq!(entries[0].0.as_ref(), dir1_file2_path);
328 Ok(())
329 }