| 1 |
// Copyright 2020 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 jj_lib::backend::TreeValue; |
| 16 |
use jj_lib::config::ConfigLayer; |
| 17 |
use jj_lib::config::ConfigSource; |
| 18 |
use jj_lib::merge::Merge; |
| 19 |
use jj_lib::merge::SameChange; |
| 20 |
use jj_lib::repo::Repo as _; |
| 21 |
use jj_lib::rewrite::rebase_commit; |
| 22 |
use jj_lib::settings::UserSettings; |
| 23 |
use pollster::FutureExt as _; |
| 24 |
use test_case::test_case; |
| 25 |
use testutils::CommitBuilderExt as _; |
| 26 |
use testutils::TestRepo; |
| 27 |
use testutils::TestResult; |
| 28 |
use testutils::assert_tree_eq; |
| 29 |
use testutils::create_tree; |
| 30 |
use testutils::repo_path; |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn test_simplify_conflict_after_resolving_parent() -> TestResult { |
| 34 |
let test_repo = TestRepo::init(); |
| 35 |
let repo = &test_repo.repo; |
| 36 |
|
| 37 |
// Set up a repo like this: |
| 38 |
// D |
| 39 |
// | C |
| 40 |
// | B |
| 41 |
// |/ |
| 42 |
// A |
| 43 |
// |
| 44 |
// Commit A has a file with 3 lines. B and D make conflicting changes to the |
| 45 |
// first line. C changes the third line. We then rebase B and C onto D, |
| 46 |
// which creates a conflict. We resolve the conflict in the first line and |
| 47 |
// rebase C2 (the rebased C) onto the resolved conflict. C3 should not have |
| 48 |
// a conflict since it changed an unrelated line. |
| 49 |
let path = repo_path("dir/file"); |
| 50 |
let mut tx = repo.start_transaction(); |
| 51 |
let tree_a = create_tree(repo, &[(path, "abc\ndef\nghi\n")]); |
| 52 |
let commit_a = tx |
| 53 |
.repo_mut() |
| 54 |
.new_commit(vec![repo.store().root_commit_id().clone()], tree_a) |
| 55 |
.write_unwrap(); |
| 56 |
let tree_b = create_tree(repo, &[(path, "Abc\ndef\nghi\n")]); |
| 57 |
let commit_b = tx |
| 58 |
.repo_mut() |
| 59 |
.new_commit(vec![commit_a.id().clone()], tree_b) |
| 60 |
.write_unwrap(); |
| 61 |
let tree_c = create_tree(repo, &[(path, "Abc\ndef\nGhi\n")]); |
| 62 |
let commit_c = tx |
| 63 |
.repo_mut() |
| 64 |
.new_commit(vec![commit_b.id().clone()], tree_c) |
| 65 |
.write_unwrap(); |
| 66 |
let tree_d = create_tree(repo, &[(path, "abC\ndef\nghi\n")]); |
| 67 |
let commit_d = tx |
| 68 |
.repo_mut() |
| 69 |
.new_commit(vec![commit_a.id().clone()], tree_d) |
| 70 |
.write_unwrap(); |
| 71 |
|
| 72 |
let commit_b2 = |
| 73 |
rebase_commit(tx.repo_mut(), commit_b, vec![commit_d.id().clone()]).block_on()?; |
| 74 |
let commit_c2 = |
| 75 |
rebase_commit(tx.repo_mut(), commit_c, vec![commit_b2.id().clone()]).block_on()?; |
| 76 |
|
| 77 |
// Test the setup: Both B and C should have conflicts. |
| 78 |
let tree_b2 = commit_b2.tree(); |
| 79 |
let tree_c2 = commit_b2.tree(); |
| 80 |
assert!(!tree_b2.path_value(path).block_on()?.is_resolved()); |
| 81 |
assert!(!tree_c2.path_value(path).block_on()?.is_resolved()); |
| 82 |
|
| 83 |
// Create the resolved B and rebase C on top. |
| 84 |
let tree_b3 = create_tree(repo, &[(path, "AbC\ndef\nghi\n")]); |
| 85 |
let commit_b3 = tx |
| 86 |
.repo_mut() |
| 87 |
.rewrite_commit(&commit_b2) |
| 88 |
.set_tree(tree_b3) |
| 89 |
.write_unwrap(); |
| 90 |
let commit_c3 = |
| 91 |
rebase_commit(tx.repo_mut(), commit_c2, vec![commit_b3.id().clone()]).block_on()?; |
| 92 |
tx.repo_mut().rebase_descendants().block_on()?; |
| 93 |
let repo = tx.commit("test").block_on()?; |
| 94 |
|
| 95 |
// The conflict should now be resolved. |
| 96 |
let tree_c2 = commit_c3.tree(); |
| 97 |
let resolved_value = tree_c2.path_value(path).block_on()?; |
| 98 |
match resolved_value.into_resolved() { |
| 99 |
Ok(Some(TreeValue::File { |
| 100 |
id, |
| 101 |
executable: false, |
| 102 |
copy_id: _, |
| 103 |
})) => { |
| 104 |
assert_eq!( |
| 105 |
testutils::read_file(repo.store(), path, &id), |
| 106 |
b"AbC\ndef\nGhi\n" |
| 107 |
); |
| 108 |
} |
| 109 |
other => { |
| 110 |
panic!("unexpected value: {other:#?}"); |
| 111 |
} |
| 112 |
} |
| 113 |
Ok(()) |
| 114 |
} |
| 115 |
|
| 116 |
// TODO: Add tests for simplification of multi-way conflicts. Both the content |
| 117 |
// and the executable bit need testing. |
| 118 |
|
| 119 |
#[test_case(SameChange::Keep)] |
| 120 |
#[test_case(SameChange::Accept)] |
| 121 |
fn test_rebase_linearize_lossy_merge(same_change: SameChange) -> TestResult { |
| 122 |
let settings = settings_with_same_change(same_change); |
| 123 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 124 |
let repo = &test_repo.repo; |
| 125 |
|
| 126 |
// Test this rebase: |
| 127 |
// D foo=2 D' foo=1 or 2 |
| 128 |
// |\ | |
| 129 |
// | C foo=2 | |
| 130 |
// | | => B foo=2 |
| 131 |
// B | foo=2 | |
| 132 |
// |/ | |
| 133 |
// A foo=1 A foo=1 |
| 134 |
// |
| 135 |
// Since both B and C changed "1" to "2" but only one "2" remains in D, it |
| 136 |
// effectively discarded a change from "1" to "2". With `SameChange::Keep`, |
| 137 |
// D' is therefore "1". However, with `SameChange::Accept`, `jj show D` etc. |
| 138 |
// currently don't tell the user about the discarded change, so it's |
| 139 |
// surprising that the change in commit D is interpreted that way. |
| 140 |
let path = repo_path("foo"); |
| 141 |
let mut tx = repo.start_transaction(); |
| 142 |
let repo_mut = tx.repo_mut(); |
| 143 |
let tree_1 = create_tree(repo, &[(path, "1")]); |
| 144 |
let tree_2 = create_tree(repo, &[(path, "2")]); |
| 145 |
let commit_a = repo_mut |
| 146 |
.new_commit(vec![repo.store().root_commit_id().clone()], tree_1.clone()) |
| 147 |
.write_unwrap(); |
| 148 |
let commit_b = repo_mut |
| 149 |
.new_commit(vec![commit_a.id().clone()], tree_2.clone()) |
| 150 |
.write_unwrap(); |
| 151 |
let commit_c = repo_mut |
| 152 |
.new_commit(vec![commit_a.id().clone()], tree_2.clone()) |
| 153 |
.write_unwrap(); |
| 154 |
let commit_d = repo_mut |
| 155 |
.new_commit( |
| 156 |
vec![commit_b.id().clone(), commit_c.id().clone()], |
| 157 |
tree_2.clone(), |
| 158 |
) |
| 159 |
.write_unwrap(); |
| 160 |
|
| 161 |
match same_change { |
| 162 |
SameChange::Keep => assert!(!commit_d.is_empty(repo_mut).block_on()?), |
| 163 |
SameChange::Accept => assert!(commit_d.is_empty(repo_mut).block_on()?), |
| 164 |
} |
| 165 |
|
| 166 |
let commit_d2 = rebase_commit(repo_mut, commit_d, vec![commit_b.id().clone()]).block_on()?; |
| 167 |
|
| 168 |
match same_change { |
| 169 |
SameChange::Keep => assert_tree_eq!(commit_d2.tree(), tree_1), |
| 170 |
SameChange::Accept => assert_tree_eq!(commit_d2.tree(), tree_2), |
| 171 |
} |
| 172 |
Ok(()) |
| 173 |
} |
| 174 |
|
| 175 |
#[test_case(SameChange::Keep)] |
| 176 |
#[test_case(SameChange::Accept)] |
| 177 |
fn test_rebase_on_lossy_merge(same_change: SameChange) -> TestResult { |
| 178 |
let settings = settings_with_same_change(same_change); |
| 179 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 180 |
let repo = &test_repo.repo; |
| 181 |
|
| 182 |
// Test this rebase: |
| 183 |
// D foo=2 D' foo=3 or 2+(3-1) (conflict) |
| 184 |
// |\ |\ |
| 185 |
// | C foo=2 | C' foo=3 |
| 186 |
// | | => | | |
| 187 |
// B | foo=2 B | foo=2 |
| 188 |
// |/ |/ |
| 189 |
// A foo=1 A foo=1 |
| 190 |
// |
| 191 |
// Commit D effectively discarded a change from "1" to "2", so one |
| 192 |
// reasonable result in D' is "3". That's the result with |
| 193 |
// `SameChange::Keep`. However, with `SameChange::Accept`, we resolve the |
| 194 |
// auto-merged parents to just "2" before the rebase in order to be |
| 195 |
// consistent with `jj show D` and other commands for inspecting the commit, |
| 196 |
// so we instead get a conflict after the rebase. |
| 197 |
let path = repo_path("foo"); |
| 198 |
let mut tx = repo.start_transaction(); |
| 199 |
let repo_mut = tx.repo_mut(); |
| 200 |
let tree_1 = create_tree(repo, &[(path, "1")]); |
| 201 |
let tree_2 = create_tree(repo, &[(path, "2")]); |
| 202 |
let tree_3 = create_tree(repo, &[(path, "3")]); |
| 203 |
let commit_a = repo_mut |
| 204 |
.new_commit(vec![repo.store().root_commit_id().clone()], tree_1.clone()) |
| 205 |
.write_unwrap(); |
| 206 |
let commit_b = repo_mut |
| 207 |
.new_commit(vec![commit_a.id().clone()], tree_2.clone()) |
| 208 |
.write_unwrap(); |
| 209 |
let commit_c = repo_mut |
| 210 |
.new_commit(vec![commit_a.id().clone()], tree_2.clone()) |
| 211 |
.write_unwrap(); |
| 212 |
let commit_d = repo_mut |
| 213 |
.new_commit( |
| 214 |
vec![commit_b.id().clone(), commit_c.id().clone()], |
| 215 |
tree_2.clone(), |
| 216 |
) |
| 217 |
.write_unwrap(); |
| 218 |
|
| 219 |
match same_change { |
| 220 |
SameChange::Keep => assert!(!commit_d.is_empty(repo_mut).block_on()?), |
| 221 |
SameChange::Accept => assert!(commit_d.is_empty(repo_mut).block_on()?), |
| 222 |
} |
| 223 |
|
| 224 |
let commit_c2 = repo_mut |
| 225 |
.new_commit(vec![commit_a.id().clone()], tree_3.clone()) |
| 226 |
.write_unwrap(); |
| 227 |
let commit_d2 = rebase_commit( |
| 228 |
repo_mut, |
| 229 |
commit_d, |
| 230 |
vec![commit_b.id().clone(), commit_c2.id().clone()], |
| 231 |
) |
| 232 |
.block_on()?; |
| 233 |
|
| 234 |
match same_change { |
| 235 |
SameChange::Keep => assert_tree_eq!(commit_d2.tree(), tree_3), |
| 236 |
SameChange::Accept => { |
| 237 |
let expected_tree_id = Merge::from_vec(vec![ |
| 238 |
tree_2.into_tree_ids(), |
| 239 |
tree_1.into_tree_ids(), |
| 240 |
tree_3.into_tree_ids(), |
| 241 |
]) |
| 242 |
.flatten(); |
| 243 |
assert_eq!(*commit_d2.tree_ids(), expected_tree_id); |
| 244 |
} |
| 245 |
} |
| 246 |
Ok(()) |
| 247 |
} |
| 248 |
|
| 249 |
fn settings_with_same_change(same_change: SameChange) -> UserSettings { |
| 250 |
let mut config = testutils::base_user_config(); |
| 251 |
let mut layer = ConfigLayer::empty(ConfigSource::User); |
| 252 |
let same_change_str = match same_change { |
| 253 |
SameChange::Keep => "keep", |
| 254 |
SameChange::Accept => "accept", |
| 255 |
}; |
| 256 |
layer |
| 257 |
.set_value("merge.same-change", same_change_str) |
| 258 |
.unwrap(); |
| 259 |
config.add_layer(layer); |
| 260 |
UserSettings::from_config(config).unwrap() |
| 261 |
} |