Debian Codegraph @

lib/tests/test_merge_operations.rs

raw
1 // Copyright 2026 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::process::Command;
16
17 use jj_lib::op_store;
18 use jj_lib::operation::Operation;
19 use jj_lib::ref_name::WorkspaceName;
20 use pollster::FutureExt as _;
21 use testutils::CommitBuilderExt as _;
22 use testutils::TestRepo;
23 use testutils::TestResult;
24 use testutils::create_random_commit;
25 use testutils::write_random_commit;
26
27 // This test is about checking that we don't run out of stack space.
28 #[test]
29 fn test_merge_operations_deep_criss_cross() -> TestResult {
30 const CHILD_ENV: &str = "JJ_LIB_TEST_MERGE_OPS_DEEP_CRISS_CROSS_CHILD";
31 const LEVELS_ENV: &str = "JJ_LIB_TEST_MERGE_OPS_DEEP_CRISS_CROSS_LEVELS";
32 const CHILD_STACK_SIZE: usize = 512 * 1024;
33 const DEFAULT_NUM_LEVELS: usize = 14;
34
35 if std::env::var_os(CHILD_ENV).is_some() {
36 let num_levels = std::env::var(LEVELS_ENV)
37 .ok()
38 .and_then(|value| value.parse().ok())
39 .unwrap_or(DEFAULT_NUM_LEVELS);
40 return run_merge_operations_deep_criss_cross(num_levels);
41 }
42
43 let num_levels = std::env::var(LEVELS_ENV)
44 .ok()
45 .and_then(|value| value.parse().ok())
46 .unwrap_or(DEFAULT_NUM_LEVELS);
47 let output = Command::new(std::env::current_exe()?)
48 .arg("--exact")
49 .arg("test_merge_operations::test_merge_operations_deep_criss_cross")
50 .arg("--nocapture")
51 .env(CHILD_ENV, "1")
52 .env("RUST_MIN_STACK", CHILD_STACK_SIZE.to_string())
53 .output()?;
54 assert!(
55 output.status.success(),
56 "deep criss-cross merge failed with {num_levels} levels and {CHILD_STACK_SIZE} byte child \
57 stack\nstdout:\n{}\nstderr:\n{}",
58 String::from_utf8_lossy(&output.stdout),
59 String::from_utf8_lossy(&output.stderr),
60 );
61 Ok(())
62 }
63
64 fn run_merge_operations_deep_criss_cross(num_levels: usize) -> TestResult {
65 let test_repo = TestRepo::init();
66 let repo_loader = test_repo.repo.loader();
67
68 let mut tx_a = test_repo.repo.start_transaction();
69 let commit_k = write_random_commit(tx_a.repo_mut());
70 tx_a.repo_mut()
71 .set_wc_commit(WorkspaceName::DEFAULT.to_owned(), commit_k.id().clone())?;
72 let repo_a = tx_a.commit("K").block_on()?;
73
74 let mut tx_b = repo_a.start_transaction();
75 let _commit_k2 = tx_b
76 .repo_mut()
77 .rewrite_commit(&commit_k)
78 .set_description("K2")
79 .write_unwrap();
80 tx_b.repo_mut().rebase_descendants().block_on()?;
81
82 let mut tx_c = repo_a.start_transaction();
83 let commit_l = create_random_commit(tx_c.repo_mut())
84 .set_description("L")
85 .set_parents(vec![commit_k.id().clone()])
86 .write_unwrap();
87 tx_c.repo_mut()
88 .set_wc_commit(WorkspaceName::DEFAULT.to_owned(), commit_l.id().clone())?;
89
90 let repo_b = tx_b.commit("B").block_on()?;
91 let repo_c = tx_c.commit("C").block_on()?;
92
93 let mut left_op = repo_b.operation().clone();
94 let mut right_op = repo_c.operation().clone();
95 let op_store = repo_loader.op_store();
96 for _ in 0..num_levels {
97 let next_left_op = op_store::Operation {
98 view_id: left_op.view_id().clone(),
99 metadata: left_op.metadata().clone(),
100 parents: vec![left_op.id().clone(), right_op.id().clone()],
101 commit_predecessors: None,
102 };
103 let next_left_op_id = op_store.write_operation(&next_left_op).block_on()?;
104
105 let next_right_op = op_store::Operation {
106 view_id: right_op.view_id().clone(),
107 metadata: right_op.metadata().clone(),
108 parents: vec![left_op.id().clone(), right_op.id().clone()],
109 commit_predecessors: None,
110 };
111 let next_right_op_id = op_store.write_operation(&next_right_op).block_on()?;
112
113 left_op = Operation::new(op_store.clone(), next_left_op_id, next_left_op);
114 right_op = Operation::new(op_store.clone(), next_right_op_id, next_right_op);
115 }
116
117 let workspace_name = None;
118 let transaction_attributes = [];
119 let (merged_repo, _num_rebased) = repo_loader
120 .merge_operations(
121 vec![left_op.clone(), right_op.clone()],
122 workspace_name,
123 Some("merge deep criss-cross"),
124 transaction_attributes,
125 )
126 .block_on()?;
127 assert!(!merged_repo.view().heads().is_empty());
128 Ok(())
129 }