1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
|
// Copyright 2021 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use jj_lib::merge::Merge;
use jj_lib::op_store::RefTarget;
use jj_lib::refs::merge_ref_targets;
use jj_lib::repo::Repo as _;
use testutils::CommitGraphBuilder;
use testutils::TestWorkspace;
#[test]
fn test_merge_ref_targets() {
let test_workspace = TestWorkspace::init();
let repo = &test_workspace.repo;
// 6 7
// |/
// 5
// | 3 4
// | |/
// | 2
// |/
// 1
let mut tx = repo.start_transaction();
let mut graph_builder = CommitGraphBuilder::new(tx.repo_mut());
let commit1 = graph_builder.initial_commit();
let commit2 = graph_builder.commit_with_parents(&[&commit1]);
let commit3 = graph_builder.commit_with_parents(&[&commit2]);
let commit4 = graph_builder.commit_with_parents(&[&commit2]);
let commit5 = graph_builder.commit_with_parents(&[&commit1]);
let commit6 = graph_builder.commit_with_parents(&[&commit5]);
let commit7 = graph_builder.commit_with_parents(&[&commit5]);
let repo = tx.commit("test").unwrap();
let target1 = RefTarget::normal(commit1.id().clone());
let target2 = RefTarget::normal(commit2.id().clone());
let target3 = RefTarget::normal(commit3.id().clone());
let target4 = RefTarget::normal(commit4.id().clone());
let target5 = RefTarget::normal(commit5.id().clone());
let target6 = RefTarget::normal(commit6.id().clone());
let _target7 = RefTarget::normal(commit7.id().clone());
let index = repo.index();
// Left moved forward
assert_eq!(
merge_ref_targets(index, &target3, &target1, &target1),
target3
);
// Right moved forward
assert_eq!(
merge_ref_targets(index, &target1, &target1, &target3),
target3
);
// Left moved backward
assert_eq!(
merge_ref_targets(index, &target1, &target3, &target3),
target1
);
// Right moved backward
assert_eq!(
merge_ref_targets(index, &target3, &target3, &target1),
target1
);
// Left moved sideways
assert_eq!(
merge_ref_targets(index, &target4, &target3, &target3),
target4
);
// Right moved sideways
assert_eq!(
merge_ref_targets(index, &target3, &target3, &target4),
target4
);
// Both moved sideways ("A - B + A" - type conflict)
assert_eq!(
merge_ref_targets(index, &target4, &target3, &target4),
target4
);
// Both added same target ("A - B + A" - type conflict)
assert_eq!(
merge_ref_targets(index, &target3, RefTarget::absent_ref(), &target3),
target3
);
// Both removed ("A - B + A" - type conflict)
assert_eq!(
merge_ref_targets(
index,
RefTarget::absent_ref(),
&target3,
RefTarget::absent_ref()
),
RefTarget::absent()
);
// Left added target, right added descendant target
assert_eq!(
merge_ref_targets(index, &target2, RefTarget::absent_ref(), &target3),
target3
);
// Right added target, left added descendant target
assert_eq!(
merge_ref_targets(index, &target3, RefTarget::absent_ref(), &target2),
target3
);
// Both moved forward to same target
assert_eq!(
merge_ref_targets(index, &target3, &target1, &target3),
target3
);
// Both moved forward, left moved further
assert_eq!(
merge_ref_targets(index, &target3, &target1, &target2),
target3
);
// Both moved forward, right moved further
assert_eq!(
merge_ref_targets(index, &target2, &target1, &target3),
target3
);
// Left and right moved forward to divergent targets
assert_eq!(
merge_ref_targets(index, &target3, &target1, &target4),
RefTarget::from_legacy_form(
[commit1.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
)
);
// Left moved back, right moved forward
assert_eq!(
merge_ref_targets(index, &target1, &target2, &target3),
RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit1.id().clone(), commit3.id().clone()]
)
);
// Right moved back, left moved forward
assert_eq!(
merge_ref_targets(index, &target3, &target2, &target1),
RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit1.id().clone()]
)
);
// Left removed
assert_eq!(
merge_ref_targets(index, RefTarget::absent_ref(), &target3, &target3),
RefTarget::absent()
);
// Right removed
assert_eq!(
merge_ref_targets(index, &target3, &target3, RefTarget::absent_ref()),
RefTarget::absent()
);
// Left removed, right moved forward
assert_eq!(
merge_ref_targets(index, RefTarget::absent_ref(), &target1, &target3),
RefTarget::from_merge(Merge::from_vec(vec![
None,
Some(commit1.id().clone()),
Some(commit3.id().clone()),
]))
);
// Right removed, left moved forward
assert_eq!(
merge_ref_targets(index, &target3, &target1, RefTarget::absent_ref()),
RefTarget::from_merge(Merge::from_vec(vec![
Some(commit3.id().clone()),
Some(commit1.id().clone()),
None,
]))
);
// Left became conflicted, right moved forward
assert_eq!(
merge_ref_targets(
index,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
&target1,
&target3
),
// TODO: "removes" should have commit 2, just like it does in the next test case
RefTarget::from_legacy_form(
[commit1.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
)
);
// Right became conflicted, left moved forward
assert_eq!(
merge_ref_targets(
index,
&target3,
&target1,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
),
RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit4.id().clone(), commit3.id().clone()]
)
);
// Existing conflict on left, right moves an "add" sideways
//
// Under the hood, the conflict is simplified as below:
// ```
// 3 4 5 3 4 5 5 4
// 2 / => 2 3 => 2
// 3
// ```
assert_eq!(
merge_ref_targets(
index,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
&target3,
&target5
),
RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit5.id().clone(), commit4.id().clone()]
)
);
// Existing conflict on right, left moves an "add" sideways
//
// Under the hood, the conflict is simplified as below:
// ```
// 5 3 4 5 3 4 5 4
// \ 2 => 3 2 => 2
// 3
// ```
assert_eq!(
merge_ref_targets(
index,
&target5,
&target3,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
),
RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit5.id().clone(), commit4.id().clone()]
)
);
// Existing conflict on left, right moves an "add" backwards, past point of
// divergence
//
// Under the hood, the conflict is simplified as below:
// ```
// 3 4 1 3 4 1 1 4
// 2 / => 2 3 => 2
// 3
// ```
assert_eq!(
merge_ref_targets(
index,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
&target3,
&target1
),
RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit1.id().clone(), commit4.id().clone()]
)
);
// Existing conflict on right, left moves an "add" backwards, past point of
// divergence
//
// Under the hood, the conflict is simplified as below:
// ```
// 1 3 4 1 3 4 1 4
// \ 2 => 3 2 => 2
// 3
// ```
assert_eq!(
merge_ref_targets(
index,
&target1,
&target3,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
),
RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit1.id().clone(), commit4.id().clone()]
)
);
// Existing conflict on left, right undoes one side of conflict
assert_eq!(
merge_ref_targets(
index,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
&target3,
&target2
),
target4
);
// Existing conflict on right, left undoes one side of conflict
assert_eq!(
merge_ref_targets(
index,
&target2,
&target3,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
),
target4
);
// Existing conflict on left, right moves one side of conflict to the other
// side ("A - B + A" - type conflict)
assert_eq!(
merge_ref_targets(
index,
&RefTarget::from_legacy_form(
[commit5.id().clone()], // not an ancestor of commit3, 4
[commit3.id().clone(), commit4.id().clone()],
),
&target4,
&target3,
),
target3
);
// Existing conflict on right, left moves one side of conflict to the other
// side ("A - B + A" - type conflict)
assert_eq!(
merge_ref_targets(
index,
&target4,
&target3,
&RefTarget::from_legacy_form(
[commit5.id().clone()], // not an ancestor of commit3, 4
[commit3.id().clone(), commit4.id().clone()],
),
),
target4
);
// Existing conflict on left, right makes unrelated update
assert_eq!(
merge_ref_targets(
index,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
&target5,
&target6
),
RefTarget::from_legacy_form(
[commit2.id().clone(), commit5.id().clone()],
[
commit3.id().clone(),
commit4.id().clone(),
commit6.id().clone()
]
)
);
// Existing conflict on right, left makes unrelated update
assert_eq!(
merge_ref_targets(
index,
&target6,
&target5,
&RefTarget::from_legacy_form(
[commit2.id().clone()],
[commit3.id().clone(), commit4.id().clone()]
),
),
RefTarget::from_legacy_form(
[commit5.id().clone(), commit2.id().clone()],
[
commit6.id().clone(),
commit3.id().clone(),
commit4.id().clone()
]
)
);
}
|