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
|
use crate::blob::util::ObjectDb;
use bstr::ByteSlice;
use gix_filter::eol;
use gix_filter::eol::AutoCrlf;
use gix_merge::blob::pipeline::{self, Mode, WorktreeRoots};
use gix_merge::blob::{Pipeline, ResourceKind};
use gix_object::tree::EntryKind;
const ALL_MODES: [pipeline::Mode; 2] = [pipeline::Mode::ToGit, pipeline::Mode::Renormalize];
#[test]
fn without_transformation() -> crate::Result {
for mode in ALL_MODES {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let mut filter = Pipeline::new(
WorktreeRoots {
common_ancestor_root: Some(tmp.path().to_owned()),
..Default::default()
},
gix_filter::Pipeline::default(),
default_options(),
);
let does_not_matter = gix_hash::Kind::Sha1.null();
let mut buf = Vec::new();
let a_name = "a";
let a_content = "a-content";
std::fs::write(tmp.path().join(a_name), a_content.as_bytes())?;
let out = filter.convert_to_mergeable(
&does_not_matter,
EntryKind::Blob,
a_name.into(),
ResourceKind::CommonAncestorOrBase,
&mut |_, _| {},
&gix_object::find::Never,
mode,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer));
assert_eq!(buf.as_bstr(), a_content, "there is no transformations configured");
let link_name = "link";
gix_fs::symlink::create(a_name.as_ref(), &tmp.path().join(link_name))?;
let err = filter
.convert_to_mergeable(
&does_not_matter,
EntryKind::Link,
link_name.into(),
ResourceKind::CommonAncestorOrBase,
&mut |_, _| {},
&gix_object::find::Never,
mode,
&mut buf,
)
.unwrap_err();
assert!(
matches!(err, pipeline::convert_to_mergeable::Error::InvalidEntryKind {rela_path,actual}
if rela_path == link_name && actual == EntryKind::Link)
);
assert_eq!(
buf.len(),
9,
"input buffers are cleared only if we think they are going to be used"
);
drop(tmp);
let mut db = ObjectDb::default();
let b_content = "b-content";
let id = db.insert(b_content);
let out = filter.convert_to_mergeable(
&id,
EntryKind::Blob,
a_name.into(),
ResourceKind::CurrentOrOurs,
&mut |_, _| {},
&db,
mode,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer));
assert_eq!(
buf.as_bstr(),
b_content,
"there is no transformations configured, it fetched the data from the ODB"
);
let out = filter.convert_to_mergeable(
&does_not_matter,
EntryKind::Blob,
a_name.into(),
ResourceKind::OtherOrTheirs,
&mut |_, _| {},
&gix_object::find::Never,
mode,
&mut buf,
)?;
assert_eq!(out, None, "the lack of object in the database isn't a problem");
let out = filter.convert_to_mergeable(
&does_not_matter,
EntryKind::Blob,
"does not exist on disk".into(),
ResourceKind::CommonAncestorOrBase,
&mut |_, _| {},
&gix_object::find::Never,
mode,
&mut buf,
)?;
assert_eq!(out, None, "the lack of file on disk is fine as well");
}
Ok(())
}
#[test]
fn binary_below_large_file_threshold() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let mut filter = Pipeline::new(
WorktreeRoots {
current_root: Some(tmp.path().to_owned()),
..Default::default()
},
gix_filter::Pipeline::default(),
pipeline::Options {
large_file_threshold_bytes: 5,
},
);
let does_not_matter = gix_hash::Kind::Sha1.null();
let mut buf = Vec::new();
let a_name = "a";
let binary_content = "a\0b";
std::fs::write(tmp.path().join(a_name), binary_content.as_bytes())?;
let out = filter.convert_to_mergeable(
&does_not_matter,
EntryKind::BlobExecutable,
a_name.into(),
ResourceKind::CurrentOrOurs,
&mut |_, _| {},
&gix_object::find::Never,
pipeline::Mode::ToGit,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer), "binary data can still be merged");
assert_eq!(buf.as_bstr(), binary_content);
let mut db = ObjectDb::default();
let id = db.insert(binary_content);
let out = filter.convert_to_mergeable(
&id,
EntryKind::Blob,
a_name.into(),
ResourceKind::OtherOrTheirs,
&mut |_, _| {},
&db,
pipeline::Mode::ToGit,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer));
assert_eq!(buf.as_bstr(), binary_content);
Ok(())
}
#[test]
fn above_large_file_threshold() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let mut filter = gix_merge::blob::Pipeline::new(
WorktreeRoots {
current_root: Some(tmp.path().to_owned()),
..Default::default()
},
gix_filter::Pipeline::default(),
pipeline::Options {
large_file_threshold_bytes: 4,
},
);
let does_not_matter = gix_hash::Kind::Sha1.null();
let mut buf = Vec::new();
let a_name = "a";
let large_content = "hello";
std::fs::write(tmp.path().join(a_name), large_content.as_bytes())?;
let out = filter.convert_to_mergeable(
&does_not_matter,
EntryKind::BlobExecutable,
a_name.into(),
ResourceKind::CurrentOrOurs,
&mut |_, _| {},
&gix_object::find::Never,
pipeline::Mode::ToGit,
&mut buf,
)?;
assert_eq!(
out,
Some(pipeline::Data::TooLarge { size: 5 }),
"it indicates that the file is too large"
);
assert_eq!(buf.len(), 0, "it should avoid querying that data in the first place");
drop(tmp);
let mut db = ObjectDb::default();
let id = db.insert(large_content);
let out = filter.convert_to_mergeable(
&id,
EntryKind::Blob,
a_name.into(),
ResourceKind::CommonAncestorOrBase,
&mut |_, _| {},
&db,
pipeline::Mode::ToGit,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::TooLarge { size: 5 }));
assert_eq!(
buf.len(),
0,
"it won't have queried the blob, first it checks the header"
);
Ok(())
}
#[test]
fn non_existing() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let mut filter = Pipeline::new(
WorktreeRoots {
common_ancestor_root: Some(tmp.path().to_owned()),
..Default::default()
},
gix_filter::Pipeline::default(),
default_options(),
);
let null = gix_hash::Kind::Sha1.null();
let mut buf = vec![1];
let a_name = "a";
assert!(
!tmp.path().join(a_name).exists(),
"precondition: worktree file doesn't exist"
);
let out = filter.convert_to_mergeable(
&null,
EntryKind::Blob,
a_name.into(),
ResourceKind::CommonAncestorOrBase,
&mut |_, _| {},
&gix_object::find::Never,
pipeline::Mode::ToGit,
&mut buf,
)?;
assert_eq!(
out, None,
"it's OK for a resource to not exist on disk - they'd then count as deleted"
);
assert_eq!(buf.len(), 0, "always cleared");
drop(tmp);
buf.push(1);
let out = filter.convert_to_mergeable(
&null,
EntryKind::Blob,
a_name.into(),
ResourceKind::OtherOrTheirs,
&mut |_, _| {},
&gix_object::find::Never,
pipeline::Mode::ToGit,
&mut buf,
)?;
assert_eq!(
out, None,
"the root path isn't configured and the object database returns nothing"
);
assert_eq!(buf.len(), 0, "it's always cleared before any potential use");
let some_id = gix_hash::ObjectId::from_hex(b"45c160c35c17ad264b96431cceb9793160396e99")?;
let err = filter
.convert_to_mergeable(
&some_id,
EntryKind::Blob,
a_name.into(),
ResourceKind::OtherOrTheirs,
&mut |_, _| {},
&gix_object::find::Never,
pipeline::Mode::ToGit,
&mut buf,
)
.unwrap_err();
assert!(
matches!(
err,
gix_merge::blob::pipeline::convert_to_mergeable::Error::FindObject(
gix_object::find::existing_object::Error::NotFound { .. }
),
),
"missing object database ids are always an error (even though missing objects on disk are allowed)"
);
Ok(())
}
#[test]
fn worktree_filter() -> crate::Result {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let filter = gix_filter::Pipeline::new(
Default::default(),
gix_filter::pipeline::Options {
eol_config: eol::Configuration {
auto_crlf: AutoCrlf::Enabled,
..Default::default()
},
..Default::default()
},
);
let mut filter = gix_merge::blob::Pipeline::new(
WorktreeRoots {
common_ancestor_root: Some(tmp.path().to_owned()),
..Default::default()
},
filter,
default_options(),
);
let mut db = ObjectDb::default();
let a_name = "a";
let mut buf = Vec::new();
let a_content = "a-content\r\n";
std::fs::write(tmp.path().join(a_name), a_content.as_bytes())?;
for mode in ALL_MODES {
let does_not_matter = gix_hash::Kind::Sha1.null();
let out = filter.convert_to_mergeable(
&does_not_matter,
EntryKind::Blob,
a_name.into(),
ResourceKind::CommonAncestorOrBase,
&mut |_, _| {},
&gix_object::find::Never,
mode,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer));
assert_eq!(
buf.as_bstr(),
"a-content\n",
"worktree files need to be converted back to what's stored in Git"
);
let id = db.insert(a_content);
let out = filter.convert_to_mergeable(
&id,
EntryKind::Blob,
a_name.into(),
ResourceKind::CommonAncestorOrBase,
&mut |_, _| {},
&db,
mode,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer));
match mode {
Mode::ToGit => {
assert_eq!(
buf.as_bstr(),
"a-content\r\n",
"if an object with CRLF already exists, we don't 'renormalize' it, it's a feature"
);
}
Mode::Renormalize => {
assert_eq!(
buf.as_bstr(),
"a-content\n",
"we can also do it if the file exists both on disk and is known to the ODB"
);
}
}
}
drop(tmp);
let b_content = "b-content\n";
let id = db.insert(b_content);
let out = filter.convert_to_mergeable(
&id,
EntryKind::Blob,
a_name.into(),
ResourceKind::CurrentOrOurs,
&mut |_, _| {},
&db,
pipeline::Mode::ToGit,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer));
assert_eq!(buf.as_bstr(), b_content, "no work is done for what's already in Git");
let mut db = ObjectDb::default();
let b_content = "b-content\r\n";
let id = db.insert(b_content);
let out = filter.convert_to_mergeable(
&id,
EntryKind::Blob,
a_name.into(),
ResourceKind::OtherOrTheirs,
&mut |_, _| {},
&db,
pipeline::Mode::Renormalize,
&mut buf,
)?;
assert_eq!(out, Some(pipeline::Data::Buffer));
assert_eq!(
buf.as_bstr(),
"b-content\n",
"we see what would have been stored if the file was checked out and checked in again.\
It explicitly ignores what's in Git already (or it wouldn't do anyting)"
);
Ok(())
}
fn default_options() -> pipeline::Options {
pipeline::Options {
large_file_threshold_bytes: 0,
}
}
|