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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
|
use gix_worktree::stack::state::attributes;
use gix_merge::blob::Platform;
mod merge {
use crate::blob::platform::new_platform;
use crate::blob::util::ObjectDb;
use crate::hex_to_id;
use bstr::{BStr, ByteSlice};
use gix_merge::blob::builtin_driver::text::ConflictStyle;
use gix_merge::blob::platform::builtin_merge::Pick;
use gix_merge::blob::platform::DriverChoice;
use gix_merge::blob::{builtin_driver, pipeline, platform, BuiltinDriver, Resolution, ResourceKind};
use gix_object::tree::EntryKind;
use std::convert::Infallible;
use std::process::Stdio;
#[test]
fn builtin_text_uses_binary_if_needed() -> crate::Result {
let mut platform = new_platform(None, pipeline::Mode::ToGit);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"a".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
let mut db = ObjectDb::default();
for (content, kind) in [
("ours", ResourceKind::CurrentOrOurs),
("theirs\0", ResourceKind::OtherOrTheirs),
] {
let id = db.insert(content);
platform.set_resource(
id,
EntryKind::Blob,
"path matters only for attribute lookup".into(),
kind,
&db,
)?;
}
let mut platform_ref = platform.prepare_merge(&db, Default::default())?;
assert_eq!(
platform_ref.driver,
DriverChoice::BuiltIn(BuiltinDriver::Text),
"it starts out at the default text driver"
);
let mut buf = Vec::new();
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(
res,
(Pick::Ours, Resolution::Conflict),
"it detected the binary buffer, ran the binary merge with default conflict resolution"
);
platform_ref.options.resolve_binary_with = Some(builtin_driver::binary::ResolveWith::Theirs);
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(
res,
(Pick::Theirs, Resolution::CompleteWithAutoResolvedConflict),
"the auto-binary driver respects its own options"
);
Ok(())
}
#[test]
fn same_binaries_do_not_count_as_conflicted() -> crate::Result {
let mut platform = new_platform(None, pipeline::Mode::ToGit);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"a".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
let mut db = ObjectDb::default();
for (content, kind) in [
("any\0", ResourceKind::CurrentOrOurs),
("any\0", ResourceKind::OtherOrTheirs),
] {
let id = db.insert(content);
platform.set_resource(
id,
EntryKind::Blob,
"path matters only for attribute lookup".into(),
kind,
&db,
)?;
}
let platform_ref = platform.prepare_merge(&db, Default::default())?;
assert_eq!(
platform_ref.driver,
DriverChoice::BuiltIn(BuiltinDriver::Text),
"it starts out at the default text driver"
);
let mut buf = Vec::new();
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(
res,
(Pick::Ours, Resolution::Complete),
"as both are the same, it just picks ours, declaring it non-conflicting"
);
let mut input = imara_diff::intern::InternedInput::new(&[][..], &[]);
assert_eq!(
platform_ref.builtin_merge(BuiltinDriver::Binary, &mut buf, &mut input, default_labels()),
res,
"no way to work around it - calling the built-in binary merge is the same"
);
Ok(())
}
#[test]
fn builtin_with_conflict() -> crate::Result {
let mut platform = new_platform(None, pipeline::Mode::ToGit);
let non_existing_ancestor_id = hex_to_id("ffffffffffffffffffffffffffffffffffffffff");
platform.set_resource(
non_existing_ancestor_id,
EntryKind::Blob,
"b".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
let mut db = ObjectDb::default();
for (content, kind) in [
("ours", ResourceKind::CurrentOrOurs),
("theirs", ResourceKind::OtherOrTheirs),
] {
let id = db.insert(content);
platform.set_resource(id, EntryKind::Blob, "b".into(), kind, &db)?;
}
let mut platform_ref = platform.prepare_merge(&db, Default::default())?;
assert_eq!(platform_ref.driver, DriverChoice::BuiltIn(BuiltinDriver::Text));
let mut buf = Vec::new();
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(res, (Pick::Buffer, Resolution::Conflict));
assert_eq!(
buf.as_bstr(),
r#"<<<<<<< current label
ours
=======
theirs
>>>>>>> other label
"#,
"default options apply, hence the 'merge' style conflict"
);
platform_ref.options.text.conflict = builtin_driver::text::Conflict::Keep {
style: ConflictStyle::Diff3,
marker_size: 3.try_into().unwrap(),
};
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(res, (Pick::Buffer, Resolution::Conflict));
assert_eq!(
buf.as_bstr(),
r#"<<< current label
ours
||| ancestor label
b
===
theirs
>>> other label
"#,
"options apply correctly"
);
platform_ref.options.text.conflict = builtin_driver::text::Conflict::ResolveWithOurs;
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(
res,
(Pick::Buffer, Resolution::CompleteWithAutoResolvedConflict),
"we can determine that there was a conflict, despite the resolution being complete"
);
assert_eq!(buf.as_bstr(), "ours");
platform_ref.options.text.conflict = builtin_driver::text::Conflict::ResolveWithTheirs;
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(res, (Pick::Buffer, Resolution::CompleteWithAutoResolvedConflict));
assert_eq!(buf.as_bstr(), "theirs");
platform_ref.options.text.conflict = builtin_driver::text::Conflict::ResolveWithUnion;
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(res, (Pick::Buffer, Resolution::CompleteWithAutoResolvedConflict));
assert_eq!(buf.as_bstr(), "ours\ntheirs");
platform_ref.driver = DriverChoice::BuiltIn(BuiltinDriver::Union);
platform_ref.options.text.conflict = builtin_driver::text::Conflict::default();
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(res, (Pick::Buffer, Resolution::CompleteWithAutoResolvedConflict));
assert_eq!(buf.as_bstr(), "ours\ntheirs");
platform_ref.driver = DriverChoice::BuiltIn(BuiltinDriver::Binary);
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(
res,
(Pick::Ours, Resolution::Conflict),
"binary merges choose ours but conflict by default"
);
assert!(buf.is_empty(), "it tells us where to get the content from");
assert_eq!(
platform_ref.buffer_by_pick(res.0).unwrap().unwrap().as_bstr(),
"ours",
"getting access to the content is simplified"
);
assert_eq!(
platform_ref
.id_by_pick(res.0, &buf, |_buf| -> Result<_, Infallible> {
panic!("no need to write buffer")
})
.unwrap()
.unwrap(),
hex_to_id("424860eef4edb9f5a2dacbbd6dc8c2d2e7645035"),
"there is no need to write a buffer here, it just returns one of our inputs"
);
for (expected, expected_pick, resolve, expected_id) in [
(
"ours",
Pick::Ours,
builtin_driver::binary::ResolveWith::Ours,
hex_to_id("424860eef4edb9f5a2dacbbd6dc8c2d2e7645035"),
),
(
"theirs",
Pick::Theirs,
builtin_driver::binary::ResolveWith::Theirs,
hex_to_id("228068dbe790983c15535164cd483eb77ade97e4"),
),
(
"b\n",
Pick::Ancestor,
builtin_driver::binary::ResolveWith::Ancestor,
non_existing_ancestor_id,
),
] {
platform_ref.options.resolve_binary_with = Some(resolve);
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(res, (expected_pick, Resolution::CompleteWithAutoResolvedConflict));
assert_eq!(platform_ref.buffer_by_pick(res.0).unwrap().unwrap().as_bstr(), expected);
assert_eq!(
platform_ref
.id_by_pick(res.0, &buf, |_buf| -> Result<_, Infallible> {
panic!("no need to write buffer")
})
.unwrap()
.unwrap(),
expected_id,
"{expected}: each input has an id, so it's just returned as is without handling buffers"
);
}
Ok(())
}
#[test]
fn with_external() -> crate::Result {
let mut platform = new_platform(
[gix_merge::blob::Driver {
name: "b".into(),
command:
"for arg in %O %A %B %L %P %S %X %Y %F; do echo $arg >> \"%A\"; done; cat \"%O\" \"%B\" >> \"%A\""
.into(),
..Default::default()
}],
pipeline::Mode::ToGit,
);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"b".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
let mut db = ObjectDb::default();
for (content, kind) in [
("ours", ResourceKind::CurrentOrOurs),
("theirs", ResourceKind::OtherOrTheirs),
] {
let id = db.insert(content);
platform.set_resource(id, EntryKind::Blob, "b".into(), kind, &db)?;
}
let platform_ref = platform.prepare_merge(&db, Default::default())?;
let mut buf = Vec::new();
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(res, (Pick::Buffer, Resolution::Complete), "merge drivers always merge ");
let mut lines = cleaned_driver_lines(&buf)?;
for tmp_file in lines.by_ref().take(3) {
assert!(tmp_file.contains_str(&b".tmp"[..]), "{tmp_file}");
}
let lines: Vec<_> = lines.collect();
assert_eq!(
lines,
[
"7",
"b",
"ancestor label",
"current label",
"other label",
"%F",
"b",
"theirs"
],
"we handle word-splitting and definitely pick-up what's written into the %A buffer"
);
let id = db.insert("binary\0");
platform.set_resource(id, EntryKind::Blob, "b".into(), ResourceKind::OtherOrTheirs, &db)?;
let platform_ref = platform.prepare_merge(&db, Default::default())?;
let res = platform_ref.merge(&mut buf, default_labels(), &Default::default())?;
assert_eq!(
res,
(Pick::Buffer, Resolution::Complete),
"merge drivers deal with binary themselves"
);
assert_eq!(
platform_ref.buffer_by_pick(res.0),
Ok(None),
"This indicates the buffer must be read"
);
let marker = hex_to_id("ffffffffffffffffffffffffffffffffffffffff");
assert_eq!(
platform_ref.id_by_pick(res.0, &buf, |_buf| Ok::<_, Infallible>(marker)),
Ok(Some(marker)),
"the id is created by hashing the merge buffer"
);
let mut lines = cleaned_driver_lines(&buf)?;
for tmp_file in lines.by_ref().take(3) {
assert!(tmp_file.contains_str(&b".tmp"[..]), "{tmp_file}");
}
let lines: Vec<_> = lines.collect();
assert_eq!(
lines,
[
"7",
"b",
"ancestor label",
"current label",
"other label",
"%F",
"b",
"binary\0"
],
"in this case, the binary lines are just taken verbatim"
);
Ok(())
}
#[test]
fn missing_buffers_are_empty_buffers() -> crate::Result {
let mut platform = new_platform(None, pipeline::Mode::ToGit);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"just-set".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
// Two deletions
for kind in [ResourceKind::CurrentOrOurs, ResourceKind::OtherOrTheirs] {
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"does not matter for driver".into(),
kind,
&gix_object::find::Never,
)?;
}
let platform_ref = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
let mut buf = Vec::new();
let res = platform_ref.merge(&mut buf, Default::default(), &Default::default())?;
assert_eq!(
res,
(Pick::Buffer, Resolution::Complete),
"both versions are deleted, an actual merge happened"
);
assert!(
buf.is_empty(),
"the new buffer is considered empty, both sides were deleted, too"
);
let mut input = imara_diff::intern::InternedInput::new(&[][..], &[]);
let res = platform_ref.builtin_merge(BuiltinDriver::Text, &mut buf, &mut input, Default::default());
assert_eq!(res, (Pick::Buffer, Resolution::Complete), "both versions are deleted");
assert!(buf.is_empty(), "the result is the same on direct invocation");
let print_all = "for arg in $@ %O %A %B %L %P %S %X %Y %F; do echo $arg; done";
let mut cmd = platform_ref.prepare_external_driver(print_all.into(), default_labels(), Default::default())?;
let stdout = cmd.stdout(Stdio::piped()).output()?.stdout;
let mut lines = cleaned_driver_lines(&stdout)?;
for tmp_file in lines.by_ref().take(3) {
assert!(tmp_file.contains_str(&b".tmp"[..]), "{tmp_file}");
}
let lines: Vec<_> = lines.collect();
assert_eq!(
lines,
[
"7",
"does not matter for driver",
"ancestor label",
"current label",
"other label",
"%F"
],
"word splitting is prevented thanks to proper quoting"
);
Ok(())
}
#[test]
fn one_buffer_too_large() -> crate::Result {
let mut platform = new_platform(None, pipeline::Mode::ToGit);
platform.filter.options.large_file_threshold_bytes = 9;
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"just-set".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
platform.filter.roots.other_root = platform.filter.roots.common_ancestor_root.clone();
platform.filter.roots.current_root = platform.filter.roots.common_ancestor_root.clone();
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"b".into(),
ResourceKind::CurrentOrOurs,
&gix_object::find::Never,
)?;
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"unspecified".into(),
ResourceKind::OtherOrTheirs,
&gix_object::find::Never,
)?;
let platform_ref = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
assert_eq!(platform_ref.other.data, platform::resource::Data::TooLarge { size: 12 });
let mut out = Vec::new();
let res = platform_ref.merge(&mut out, Default::default(), &Default::default())?;
assert_eq!(
res,
(Pick::Ours, Resolution::Conflict),
"this is the default for binary merges, which are used in this case"
);
let mut input = imara_diff::intern::InternedInput::new(&[][..], &[]);
assert_eq!(
platform_ref.builtin_merge(BuiltinDriver::Text, &mut out, &mut input, Default::default(),),
res,
"we can't enforce it, it will just default to using binary"
);
let err = platform_ref
.prepare_external_driver("bogus".into(), Default::default(), Default::default())
.unwrap_err();
assert!(
matches!(err, platform::prepare_external_driver::Error::ResourceTooLarge { .. }),
"however, for external drivers, resources can still be too much to handle, until we learn how to stream them"
);
Ok(())
}
fn cleaned_driver_lines(buf: &[u8]) -> std::io::Result<impl Iterator<Item = &BStr>> {
let current_dir = gix_path::into_bstr(std::env::current_dir()?);
Ok(buf
.lines()
.map(move |line| line.strip_prefix(current_dir.as_bytes()).unwrap_or(line).as_bstr()))
}
fn default_labels() -> builtin_driver::text::Labels<'static> {
builtin_driver::text::Labels {
ancestor: Some("ancestor label".into()),
current: Some("current label".into()),
other: Some("other label".into()),
}
}
}
mod prepare_merge {
use crate::blob::platform::new_platform;
use gix_merge::blob::platform::{resource, DriverChoice};
use gix_merge::blob::{builtin_driver, pipeline, BuiltinDriver, ResourceKind};
use gix_object::tree::EntryKind;
#[test]
fn ancestor_and_current_and_other_do_not_exist() -> crate::Result {
let mut platform = new_platform(None, pipeline::Mode::ToGit);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"also-missing".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"can't-be-found-in-odb".into(),
ResourceKind::CurrentOrOurs,
&gix_object::find::Never,
)?;
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::BlobExecutable,
"can't-be-found-in-odb".into(),
ResourceKind::OtherOrTheirs,
&gix_object::find::Never,
)?;
let state = platform
.prepare_merge(&gix_object::find::Never, Default::default())
.expect("no validation is done here, let the caller inspect");
assert_eq!(state.ancestor.data, resource::Data::Missing);
assert_eq!(state.current.data, resource::Data::Missing);
assert_eq!(state.other.data, resource::Data::Missing);
Ok(())
}
#[test]
fn driver_selection() -> crate::Result {
let mut platform = new_platform(
[
gix_merge::blob::Driver {
name: "union".into(),
..Default::default()
},
gix_merge::blob::Driver {
name: "to proof it will be sorted".into(),
..Default::default()
},
gix_merge::blob::Driver {
name: "b".into(),
recursive: Some("for-recursion".into()),
..Default::default()
},
gix_merge::blob::Driver {
name: "for-recursion".into(),
recursive: Some("should not be looked up".into()),
..Default::default()
},
],
pipeline::Mode::ToGit,
);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"ancestor does not matter for attributes".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"just-set".into(),
ResourceKind::CurrentOrOurs,
&gix_object::find::Never,
)?;
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::BlobExecutable,
"also does not matter for driver".into(),
ResourceKind::OtherOrTheirs,
&gix_object::find::Never,
)?;
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
assert_eq!(
prepared.driver,
DriverChoice::BuiltIn(BuiltinDriver::Text),
"`merge` attribute means text"
);
assert_eq!(
prepared.options.text.conflict.marker_size(),
Some(32),
"marker sizes are picked up from attributes as well"
);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"unset".into(),
ResourceKind::CommonAncestorOrBase,
&gix_object::find::Never,
)?;
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
assert_eq!(
prepared.driver,
DriverChoice::BuiltIn(BuiltinDriver::Text),
"`-merge` attribute means binary, but it looked up 'current' which is still at some bogus worktree path"
);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"unset".into(),
ResourceKind::CurrentOrOurs,
&gix_object::find::Never,
)?;
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
assert_eq!(
prepared.driver,
DriverChoice::BuiltIn(BuiltinDriver::Binary),
"`-merge` attribute means binary"
);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"unspecified".into(),
ResourceKind::CurrentOrOurs,
&gix_object::find::Never,
)?;
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
assert_eq!(
prepared.driver,
DriverChoice::BuiltIn(BuiltinDriver::Text),
"`!merge` attribute means the hardcoded default"
);
platform.options.default_driver = Some("union".into());
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
let expected_idx = 3;
assert_eq!(
prepared.driver,
DriverChoice::Index(expected_idx),
"`!merge` attribute will also pick up the 'merge.default' configuration, and find the name in passed drivers first.\
Note that the index is 1, even though it was 0 when passing the drivers - they are sorted by name."
);
assert_eq!(platform.drivers()[expected_idx].name, "union");
platform.options.default_driver = Some("binary".into());
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
assert_eq!(
prepared.driver,
DriverChoice::BuiltIn(BuiltinDriver::Binary),
"`!merge` attribute will also pick up the 'merge.default' configuration, non-overridden builtin filters work as well"
);
platform.options.default_driver = Some("Binary".into());
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
assert_eq!(
prepared.driver,
DriverChoice::BuiltIn(BuiltinDriver::Text),
"'merge.default' is case-sensitive"
);
platform.set_resource(
gix_hash::Kind::Sha1.null(),
EntryKind::Blob,
"b".into(),
ResourceKind::CurrentOrOurs,
&gix_object::find::Never,
)?;
let prepared = platform.prepare_merge(&gix_object::find::Never, Default::default())?;
let expected_idx = 0;
assert_eq!(prepared.driver, DriverChoice::Index(expected_idx));
assert_eq!(
platform.drivers()[expected_idx].name,
"b",
"by default, even if recursive is specified, it doesn't look it up"
);
let prepared = platform.prepare_merge(
&gix_object::find::Never,
gix_merge::blob::platform::merge::Options {
is_virtual_ancestor: true,
resolve_binary_with: None,
..Default::default()
},
)?;
let expected_idx = 1;
assert_eq!(prepared.driver, DriverChoice::Index(expected_idx),);
assert_eq!(
prepared.options.resolve_binary_with,
Some(builtin_driver::binary::ResolveWith::Ours),
"it automatically adjusts the merge mode for binary operations to work for bases"
);
assert_eq!(
platform.drivers()[expected_idx].name,
"for-recursion",
"It looks up the final driver, including recursion, it only looks it up once though"
);
Ok(())
}
}
mod set_resource {
use crate::blob::platform::new_platform;
use gix_merge::blob::{pipeline, ResourceKind};
use gix_object::tree::EntryKind;
#[test]
fn invalid_resource_types() {
let mut platform = new_platform(None, pipeline::Mode::ToGit);
for (mode, name) in [(EntryKind::Commit, "Commit"), (EntryKind::Tree, "Tree")] {
assert_eq!(
platform
.set_resource(
gix_hash::Kind::Sha1.null(),
mode,
"a".into(),
ResourceKind::OtherOrTheirs,
&gix_object::find::Never,
)
.unwrap_err()
.to_string(),
format!("Can only diff blobs, not {name}")
);
}
}
}
fn new_platform(
drivers: impl IntoIterator<Item = gix_merge::blob::Driver>,
filter_mode: gix_merge::blob::pipeline::Mode,
) -> Platform {
let root = gix_testtools::scripted_fixture_read_only("make_blob_repo.sh").expect("valid fixture");
let attributes = gix_worktree::Stack::new(
&root,
gix_worktree::stack::State::AttributesStack(gix_worktree::stack::state::Attributes::new(
Default::default(),
None,
attributes::Source::WorktreeThenIdMapping,
Default::default(),
)),
gix_worktree::glob::pattern::Case::Sensitive,
Vec::new(),
Vec::new(),
);
let filter = gix_merge::blob::Pipeline::new(
gix_merge::blob::pipeline::WorktreeRoots {
common_ancestor_root: Some(root.clone()),
..Default::default()
},
gix_filter::Pipeline::default(),
Default::default(),
);
Platform::new(
filter,
filter_mode,
attributes,
drivers.into_iter().collect(),
Default::default(),
)
}
|