Debian Codegraph @

lib/tests/test_annotate.rs

raw
1 // Copyright 2024 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 std::fmt::Write as _;
16 use std::sync::Arc;
17
18 use itertools::Itertools as _;
19 use jj_lib::annotate::FileAnnotation;
20 use jj_lib::annotate::FileAnnotator;
21 use jj_lib::backend::CommitId;
22 use jj_lib::backend::MillisSinceEpoch;
23 use jj_lib::backend::Signature;
24 use jj_lib::backend::Timestamp;
25 use jj_lib::backend::TreeValue;
26 use jj_lib::commit::Commit;
27 use jj_lib::merged_tree::MergedTree;
28 use jj_lib::repo::MutableRepo;
29 use jj_lib::repo::Repo;
30 use jj_lib::repo_path::RepoPath;
31 use jj_lib::revset::ResolvedRevsetExpression;
32 use jj_lib::revset::RevsetExpression;
33 use pollster::FutureExt as _;
34 use testutils::CommitBuilderExt as _;
35 use testutils::TestRepo;
36 use testutils::TestResult;
37 use testutils::create_tree;
38 use testutils::read_file;
39 use testutils::repo_path;
40
41 fn create_commit_fn(
42 mut_repo: &mut MutableRepo,
43 ) -> impl FnMut(&str, &[&CommitId], MergedTree) -> Commit {
44 // stabilize commit IDs for ease of debugging
45 let signature = Signature {
46 name: "Some One".to_owned(),
47 email: "[email protected]".to_owned(),
48 timestamp: Timestamp {
49 timestamp: MillisSinceEpoch(0),
50 tz_offset: 0,
51 },
52 };
53 move |description, parent_ids, tree| {
54 let parent_ids = parent_ids.iter().map(|&id| id.clone()).collect();
55 mut_repo
56 .new_commit(parent_ids, tree)
57 .set_author(signature.clone())
58 .set_committer(signature.clone())
59 .set_description(description)
60 .write_unwrap()
61 }
62 }
63
64 fn annotate(repo: &dyn Repo, commit: &Commit, file_path: &RepoPath) -> String {
65 let domain = RevsetExpression::all();
66 annotate_within(repo, commit, &domain, file_path)
67 }
68
69 fn annotate_within(
70 repo: &dyn Repo,
71 commit: &Commit,
72 domain: &Arc<ResolvedRevsetExpression>,
73 file_path: &RepoPath,
74 ) -> String {
75 let mut annotator = FileAnnotator::from_commit(commit, file_path)
76 .block_on()
77 .unwrap();
78 annotator.compute(repo, domain).block_on().unwrap();
79 format_annotation(repo, &annotator.to_annotation())
80 }
81
82 fn annotate_parent_tree(repo: &dyn Repo, commit: &Commit, file_path: &RepoPath) -> String {
83 let tree = commit.parent_tree(repo).block_on().unwrap();
84 let text = match tree
85 .path_value(file_path)
86 .block_on()
87 .unwrap()
88 .into_resolved()
89 .unwrap()
90 {
91 Some(TreeValue::File { id, .. }) => read_file(repo.store(), file_path, &id),
92 value => panic!("unexpected path value: {value:?}"),
93 };
94 let mut annotator = FileAnnotator::with_file_content(commit.id(), file_path, text);
95 annotator
96 .compute(repo, &RevsetExpression::all())
97 .block_on()
98 .unwrap();
99 format_annotation(repo, &annotator.to_annotation())
100 }
101
102 fn format_annotation(repo: &dyn Repo, annotation: &FileAnnotation) -> String {
103 let mut output = String::new();
104 for (origin, line) in annotation.line_origins() {
105 let line_origin = origin.unwrap_or_else(|line_origin| line_origin);
106 let line_number = line_origin.line_number + 1;
107 let commit = repo.store().get_commit(&line_origin.commit_id).unwrap();
108 let desc = commit.description().trim_end();
109 let sigil = if origin.is_err() { '*' } else { ' ' };
110 write!(output, "{desc}:{line_number}{sigil}: {line}").unwrap();
111 }
112 output
113 }
114
115 #[test]
116 fn test_annotate_linear() -> TestResult {
117 let test_repo = TestRepo::init();
118 let repo = &test_repo.repo;
119
120 let root_commit_id = repo.store().root_commit_id();
121 let file_path = repo_path("file");
122
123 let mut tx = repo.start_transaction();
124 let mut create_commit = create_commit_fn(tx.repo_mut());
125 let content1 = "";
126 let content2 = "2a\n2b\n";
127 let content3 = "2b\n3\n";
128 let tree1 = create_tree(repo, &[(file_path, content1)]);
129 let tree2 = create_tree(repo, &[(file_path, content2)]);
130 let tree3 = create_tree(repo, &[(file_path, content3)]);
131 let commit1 = create_commit("commit1", &[root_commit_id], tree1);
132 let commit2 = create_commit("commit2", &[commit1.id()], tree2);
133 let commit3 = create_commit("commit3", &[commit2.id()], tree3.clone());
134 let commit4 = create_commit("commit4", &[commit3.id()], tree3); // empty commit
135 drop(create_commit);
136
137 insta::assert_snapshot!(annotate(tx.repo(), &commit1, file_path), @"");
138 insta::assert_snapshot!(annotate(tx.repo(), &commit2, file_path), @"
139 commit2:1 : 2a
140 commit2:2 : 2b
141 ");
142 insta::assert_snapshot!(annotate(tx.repo(), &commit3, file_path), @"
143 commit2:2 : 2b
144 commit3:2 : 3
145 ");
146 insta::assert_snapshot!(annotate(tx.repo(), &commit4, file_path), @"
147 commit2:2 : 2b
148 commit3:2 : 3
149 ");
150 Ok(())
151 }
152
153 #[test]
154 fn test_annotate_merge_simple() -> TestResult {
155 let test_repo = TestRepo::init();
156 let repo = &test_repo.repo;
157
158 let root_commit_id = repo.store().root_commit_id();
159 let file_path = repo_path("file");
160
161 // 4 "2 1 3"
162 // |\
163 // | 3 "1 3"
164 // | |
165 // 2 | "2 1"
166 // |/
167 // 1 "1"
168 let mut tx = repo.start_transaction();
169 let mut create_commit = create_commit_fn(tx.repo_mut());
170 let content1 = "1\n";
171 let content2 = "2\n1\n";
172 let content3 = "1\n3\n";
173 let content4 = "2\n1\n3\n";
174 let tree1 = create_tree(repo, &[(file_path, content1)]);
175 let tree2 = create_tree(repo, &[(file_path, content2)]);
176 let tree3 = create_tree(repo, &[(file_path, content3)]);
177 let tree4 = create_tree(repo, &[(file_path, content4)]);
178 let commit1 = create_commit("commit1", &[root_commit_id], tree1);
179 let commit2 = create_commit("commit2", &[commit1.id()], tree2);
180 let commit3 = create_commit("commit3", &[commit1.id()], tree3);
181 let commit4 = create_commit("commit4", &[commit2.id(), commit3.id()], tree4);
182 drop(create_commit);
183
184 insta::assert_snapshot!(annotate(tx.repo(), &commit4, file_path), @"
185 commit2:1 : 2
186 commit1:1 : 1
187 commit3:2 : 3
188 ");
189
190 // Exclude the fork commit and its ancestors.
191 let domain = RevsetExpression::commit(commit1.id().clone())
192 .ancestors()
193 .negated();
194 insta::assert_snapshot!(annotate_within(tx.repo(), &commit4, &domain, file_path), @"
195 commit2:1 : 2
196 commit1:1*: 1
197 commit3:2 : 3
198 ");
199
200 // Exclude one side of the merge and its ancestors.
201 let domain = RevsetExpression::commit(commit2.id().clone())
202 .ancestors()
203 .negated();
204 insta::assert_snapshot!(annotate_within(tx.repo(), &commit4, &domain, file_path), @"
205 commit2:1*: 2
206 commit2:2*: 1
207 commit3:2 : 3
208 ");
209
210 // Exclude both sides of the merge and their ancestors.
211 let domain = RevsetExpression::commit(commit4.id().clone());
212 insta::assert_snapshot!(annotate_within(tx.repo(), &commit4, &domain, file_path), @"
213 commit2:1*: 2
214 commit2:2*: 1
215 commit3:2*: 3
216 ");
217
218 // Exclude intermediate commit, which is useless but works.
219 let domain = RevsetExpression::commit(commit3.id().clone()).negated();
220 insta::assert_snapshot!(annotate_within(tx.repo(), &commit4, &domain, file_path), @"
221 commit2:1 : 2
222 commit1:1 : 1
223 commit4:3 : 3
224 ");
225
226 // Calculate incrementally
227 let mut annotator = FileAnnotator::from_commit(&commit4, file_path).block_on()?;
228 assert_eq!(annotator.pending_commits().collect_vec(), [commit4.id()]);
229 insta::assert_snapshot!(format_annotation(tx.repo(), &annotator.to_annotation()), @"
230 commit4:1*: 2
231 commit4:2*: 1
232 commit4:3*: 3
233 ");
234 annotator
235 .compute(
236 tx.repo(),
237 &RevsetExpression::commits(vec![
238 commit4.id().clone(),
239 commit3.id().clone(),
240 commit2.id().clone(),
241 ]),
242 )
243 .block_on()?;
244 assert_eq!(annotator.pending_commits().collect_vec(), [commit1.id()]);
245 insta::assert_snapshot!(format_annotation(tx.repo(), &annotator.to_annotation()), @"
246 commit2:1 : 2
247 commit1:1*: 1
248 commit3:2 : 3
249 ");
250 annotator
251 .compute(
252 tx.repo(),
253 &RevsetExpression::commits(vec![commit1.id().clone()]),
254 )
255 .block_on()?;
256 assert!(annotator.pending_commits().next().is_none());
257 insta::assert_snapshot!(format_annotation(tx.repo(), &annotator.to_annotation()), @"
258 commit2:1 : 2
259 commit1:1 : 1
260 commit3:2 : 3
261 ");
262 Ok(())
263 }
264
265 #[test]
266 fn test_annotate_merge_split() -> TestResult {
267 let test_repo = TestRepo::init();
268 let repo = &test_repo.repo;
269
270 let root_commit_id = repo.store().root_commit_id();
271 let file_path = repo_path("file");
272
273 // 4 "2 1a 1b 3 4"
274 // |\
275 // | 3 "1b 3"
276 // | |
277 // 2 | "2 1a"
278 // |/
279 // 1 "1a 1b"
280 let mut tx = repo.start_transaction();
281 let mut create_commit = create_commit_fn(tx.repo_mut());
282 let content1 = "1a\n1b\n";
283 let content2 = "2\n1a\n";
284 let content3 = "1b\n3\n";
285 let content4 = "2\n1a\n1b\n3\n4\n";
286 let tree1 = create_tree(repo, &[(file_path, content1)]);
287 let tree2 = create_tree(repo, &[(file_path, content2)]);
288 let tree3 = create_tree(repo, &[(file_path, content3)]);
289 let tree4 = create_tree(repo, &[(file_path, content4)]);
290 let commit1 = create_commit("commit1", &[root_commit_id], tree1);
291 let commit2 = create_commit("commit2", &[commit1.id()], tree2);
292 let commit3 = create_commit("commit3", &[commit1.id()], tree3);
293 let commit4 = create_commit("commit4", &[commit2.id(), commit3.id()], tree4);
294 drop(create_commit);
295
296 insta::assert_snapshot!(annotate(tx.repo(), &commit4, file_path), @"
297 commit2:1 : 2
298 commit1:1 : 1a
299 commit1:2 : 1b
300 commit3:2 : 3
301 commit4:5 : 4
302 ");
303 Ok(())
304 }
305
306 #[test]
307 fn test_annotate_merge_split_interleaved() -> TestResult {
308 let test_repo = TestRepo::init();
309 let repo = &test_repo.repo;
310
311 let root_commit_id = repo.store().root_commit_id();
312 let file_path = repo_path("file");
313
314 // 6 "1a 4 1b 6 2a 5 2b"
315 // |\
316 // | 5 "1b 5 2b"
317 // | |
318 // 4 | "1a 4 2a"
319 // |/
320 // 3 "1a 1b 2a 2b"
321 // |\
322 // | 2 "2a 2b"
323 // |
324 // 1 "1a 1b"
325 let mut tx = repo.start_transaction();
326 let mut create_commit = create_commit_fn(tx.repo_mut());
327 let content1 = "1a\n1b\n";
328 let content2 = "2a\n2b\n";
329 let content3 = "1a\n1b\n2a\n2b\n";
330 let content4 = "1a\n4\n2a\n";
331 let content5 = "1b\n5\n2b\n";
332 let content6 = "1a\n4\n1b\n6\n2a\n5\n2b\n";
333 let tree1 = create_tree(repo, &[(file_path, content1)]);
334 let tree2 = create_tree(repo, &[(file_path, content2)]);
335 let tree3 = create_tree(repo, &[(file_path, content3)]);
336 let tree4 = create_tree(repo, &[(file_path, content4)]);
337 let tree5 = create_tree(repo, &[(file_path, content5)]);
338 let tree6 = create_tree(repo, &[(file_path, content6)]);
339 let commit1 = create_commit("commit1", &[root_commit_id], tree1);
340 let commit2 = create_commit("commit2", &[root_commit_id], tree2);
341 let commit3 = create_commit("commit3", &[commit1.id(), commit2.id()], tree3);
342 let commit4 = create_commit("commit4", &[commit3.id()], tree4);
343 let commit5 = create_commit("commit5", &[commit3.id()], tree5);
344 let commit6 = create_commit("commit6", &[commit4.id(), commit5.id()], tree6);
345 drop(create_commit);
346
347 insta::assert_snapshot!(annotate(tx.repo(), &commit6, file_path), @"
348 commit1:1 : 1a
349 commit4:2 : 4
350 commit1:2 : 1b
351 commit6:4 : 6
352 commit2:1 : 2a
353 commit5:2 : 5
354 commit2:2 : 2b
355 ");
356 Ok(())
357 }
358
359 #[test]
360 fn test_annotate_merge_dup() -> TestResult {
361 let test_repo = TestRepo::init();
362 let repo = &test_repo.repo;
363
364 let root_commit_id = repo.store().root_commit_id();
365 let file_path = repo_path("file");
366
367 // 4 "2 1 1 3 4"
368 // |\
369 // | 3 "1 3"
370 // | |
371 // 2 | "2 1"
372 // |/
373 // 1 "1"
374 let mut tx = repo.start_transaction();
375 let mut create_commit = create_commit_fn(tx.repo_mut());
376 let content1 = "1\n";
377 let content2 = "2\n1\n";
378 let content3 = "1\n3\n";
379 let content4 = "2\n1\n1\n3\n4\n";
380 let tree1 = create_tree(repo, &[(file_path, content1)]);
381 let tree2 = create_tree(repo, &[(file_path, content2)]);
382 let tree3 = create_tree(repo, &[(file_path, content3)]);
383 let tree4 = create_tree(repo, &[(file_path, content4)]);
384 let commit1 = create_commit("commit1", &[root_commit_id], tree1);
385 let commit2 = create_commit("commit2", &[commit1.id()], tree2);
386 let commit3 = create_commit("commit3", &[commit1.id()], tree3);
387 let commit4 = create_commit("commit4", &[commit2.id(), commit3.id()], tree4);
388 drop(create_commit);
389
390 // Both "1"s can be propagated to commit1 through commit2 and commit3.
391 // Alternatively, it's also good to interpret that one of the "1"s was
392 // produced at commit2, commit3, or commit4.
393 insta::assert_snapshot!(annotate(tx.repo(), &commit4, file_path), @"
394 commit2:1 : 2
395 commit1:1 : 1
396 commit1:1 : 1
397 commit3:2 : 3
398 commit4:5 : 4
399 ");
400
401 // For example, the parent tree of commit4 doesn't contain multiple "1"s.
402 // If annotation were computed compared to the parent tree, not trees of the
403 // parent commits, "1" would be inserted at commit4.
404 insta::assert_snapshot!(annotate_parent_tree(tx.repo(), &commit4, file_path), @"
405 commit2:1 : 2
406 commit1:1 : 1
407 commit3:2 : 3
408 ");
409 Ok(())
410 }
411
412 #[test]
413 fn test_annotate_file_directory_transition() -> TestResult {
414 let test_repo = TestRepo::init();
415 let repo = &test_repo.repo;
416
417 let root_commit_id = repo.store().root_commit_id();
418 let file_path1 = repo_path("file/was_dir");
419 let file_path2 = repo_path("file");
420
421 let mut tx = repo.start_transaction();
422 let mut create_commit = create_commit_fn(tx.repo_mut());
423 let tree1 = create_tree(repo, &[(file_path1, "1\n")]);
424 let tree2 = create_tree(repo, &[(file_path2, "2\n")]);
425 let commit1 = create_commit("commit1", &[root_commit_id], tree1);
426 let commit2 = create_commit("commit2", &[commit1.id()], tree2);
427 drop(create_commit);
428
429 insta::assert_snapshot!(annotate(tx.repo(), &commit2, file_path2), @"commit2:1 : 2");
430 Ok(())
431 }