|
| 1 | +use crate::blob::util::ObjectDb; |
| 2 | +use bstr::ByteSlice; |
| 3 | +use gix_merge::blob::pipeline::{self, DriverChoice, WorktreeRoots}; |
| 4 | +use gix_merge::blob::{BuiltinDriver, ResourceKind}; |
| 5 | +use gix_object::tree::EntryKind; |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn without_transformation() -> crate::Result { |
| 9 | + for mode in [pipeline::Mode::ToGit, pipeline::Mode::Renormalize] { |
| 10 | + let tmp = gix_testtools::tempfile::TempDir::new()?; |
| 11 | + let mut filter = gix_merge::blob::Pipeline::new( |
| 12 | + WorktreeRoots { |
| 13 | + common_ancestor_root: Some(tmp.path().to_owned()), |
| 14 | + current_root: None, |
| 15 | + other_root: None, |
| 16 | + }, |
| 17 | + gix_filter::Pipeline::default(), |
| 18 | + vec![], |
| 19 | + default_options(), |
| 20 | + ); |
| 21 | + |
| 22 | + let does_not_matter = gix_hash::Kind::Sha1.null(); |
| 23 | + let mut buf = Vec::new(); |
| 24 | + let a_name = "a"; |
| 25 | + let a_content = "a-content"; |
| 26 | + std::fs::write(tmp.path().join(a_name), a_content.as_bytes())?; |
| 27 | + let out = filter.convert_to_mergeable( |
| 28 | + &does_not_matter, |
| 29 | + EntryKind::Blob, |
| 30 | + a_name.into(), |
| 31 | + ResourceKind::CommonAncestorOrBase, |
| 32 | + &mut |_, _| {}, |
| 33 | + &gix_object::find::Never, |
| 34 | + mode, |
| 35 | + &mut buf, |
| 36 | + )?; |
| 37 | + assert_eq!( |
| 38 | + out.driver, |
| 39 | + default_driver(), |
| 40 | + "default driver is text, nothing else was configured" |
| 41 | + ); |
| 42 | + assert_eq!(out.data, Some(pipeline::Data::Buffer)); |
| 43 | + assert_eq!(buf.as_bstr(), a_content, "there is no transformations configured"); |
| 44 | + |
| 45 | + let link_name = "link"; |
| 46 | + gix_fs::symlink::create(a_name.as_ref(), &tmp.path().join(link_name))?; |
| 47 | + let err = filter |
| 48 | + .convert_to_mergeable( |
| 49 | + &does_not_matter, |
| 50 | + EntryKind::Link, |
| 51 | + link_name.into(), |
| 52 | + ResourceKind::CommonAncestorOrBase, |
| 53 | + &mut |_, _| {}, |
| 54 | + &gix_object::find::Never, |
| 55 | + mode, |
| 56 | + &mut buf, |
| 57 | + ) |
| 58 | + .unwrap_err(); |
| 59 | + |
| 60 | + assert!( |
| 61 | + matches!(err, pipeline::convert_to_mergeable::Error::InvalidEntryKind {rela_path,actual} |
| 62 | + if rela_path == link_name && actual == EntryKind::Link) |
| 63 | + ); |
| 64 | + assert_eq!( |
| 65 | + buf.len(), |
| 66 | + 9, |
| 67 | + "input buffers are cleared only if we think they are going to be used" |
| 68 | + ); |
| 69 | + drop(tmp); |
| 70 | + |
| 71 | + let mut db = ObjectDb::default(); |
| 72 | + let b_content = "b-content"; |
| 73 | + let id = db.insert(b_content); |
| 74 | + |
| 75 | + let out = filter.convert_to_mergeable( |
| 76 | + &id, |
| 77 | + EntryKind::Blob, |
| 78 | + a_name.into(), |
| 79 | + ResourceKind::CurrentOrOurs, |
| 80 | + &mut |_, _| {}, |
| 81 | + &db, |
| 82 | + mode, |
| 83 | + &mut buf, |
| 84 | + )?; |
| 85 | + |
| 86 | + assert_eq!(out.driver, default_driver()); |
| 87 | + assert_eq!(out.data, Some(pipeline::Data::Buffer)); |
| 88 | + assert_eq!( |
| 89 | + buf.as_bstr(), |
| 90 | + b_content, |
| 91 | + "there is no transformations configured, it fetched the data from the ODB" |
| 92 | + ); |
| 93 | + |
| 94 | + let out = filter.convert_to_mergeable( |
| 95 | + &does_not_matter, |
| 96 | + EntryKind::Blob, |
| 97 | + a_name.into(), |
| 98 | + ResourceKind::OtherOrTheirs, |
| 99 | + &mut |_, _| {}, |
| 100 | + &gix_object::find::Never, |
| 101 | + mode, |
| 102 | + &mut buf, |
| 103 | + )?; |
| 104 | + assert_eq!(out.driver, default_driver()); |
| 105 | + assert_eq!(out.data, None, "the lack of object in the database isn't a problem"); |
| 106 | + |
| 107 | + let out = filter.convert_to_mergeable( |
| 108 | + &does_not_matter, |
| 109 | + EntryKind::Blob, |
| 110 | + "does not exist on disk".into(), |
| 111 | + ResourceKind::CommonAncestorOrBase, |
| 112 | + &mut |_, _| {}, |
| 113 | + &gix_object::find::Never, |
| 114 | + mode, |
| 115 | + &mut buf, |
| 116 | + )?; |
| 117 | + assert_eq!(out.driver, default_driver()); |
| 118 | + assert_eq!(out.data, None, "the lack of file on disk is fine as well"); |
| 119 | + } |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
| 123 | + |
| 124 | +fn default_driver() -> DriverChoice { |
| 125 | + DriverChoice::BuiltIn(BuiltinDriver::Text) |
| 126 | +} |
| 127 | + |
| 128 | +fn default_options() -> pipeline::Options { |
| 129 | + pipeline::Options { |
| 130 | + large_file_threshold_bytes: 0, |
| 131 | + fs: gix_fs::Capabilities { |
| 132 | + precompose_unicode: false, |
| 133 | + ignore_case: false, |
| 134 | + executable_bit: true, |
| 135 | + symlink: true, |
| 136 | + }, |
| 137 | + default_driver: None, |
| 138 | + } |
| 139 | +} |
0 commit comments