| 1 |
use std::borrow::Cow; |
| 2 |
use std::path::Path; |
| 3 |
use std::sync::Arc; |
| 4 |
|
| 5 |
use futures::StreamExt as _; |
| 6 |
use futures::stream::BoxStream; |
| 7 |
use itertools::Itertools as _; |
| 8 |
use jj_lib::backend::BackendResult; |
| 9 |
use jj_lib::backend::CopyId; |
| 10 |
use jj_lib::backend::MergedTreeValue; |
| 11 |
use jj_lib::backend::MergedTreeValueExt as _; |
| 12 |
use jj_lib::backend::TreeValue; |
| 13 |
use jj_lib::conflicts; |
| 14 |
use jj_lib::conflicts::ConflictMarkerStyle; |
| 15 |
use jj_lib::conflicts::ConflictMaterializeOptions; |
| 16 |
use jj_lib::conflicts::MIN_CONFLICT_MARKER_LEN; |
| 17 |
use jj_lib::conflicts::MaterializedTreeValue; |
| 18 |
use jj_lib::conflicts::materialize_merge_result_to_bytes; |
| 19 |
use jj_lib::conflicts::materialized_diff_stream; |
| 20 |
use jj_lib::copies::CopiesTreeDiffEntry; |
| 21 |
use jj_lib::copies::CopyRecords; |
| 22 |
use jj_lib::diff::ContentDiff; |
| 23 |
use jj_lib::diff::DiffHunkKind; |
| 24 |
use jj_lib::files; |
| 25 |
use jj_lib::files::MergeResult; |
| 26 |
use jj_lib::matchers::Matcher; |
| 27 |
use jj_lib::merge::Diff; |
| 28 |
use jj_lib::merge::Merge; |
| 29 |
use jj_lib::merge::SameChange; |
| 30 |
use jj_lib::merged_tree::MergedTree; |
| 31 |
use jj_lib::merged_tree_builder::MergedTreeBuilder; |
| 32 |
use jj_lib::object_id::ObjectId as _; |
| 33 |
use jj_lib::repo_path::RepoPath; |
| 34 |
use jj_lib::repo_path::RepoPathBuf; |
| 35 |
use jj_lib::store::Store; |
| 36 |
use jj_lib::tree_merge::MergeOptions; |
| 37 |
use thiserror::Error; |
| 38 |
|
| 39 |
use super::MergeToolFile; |
| 40 |
|
| 41 |
#[derive(Debug, Error)] |
| 42 |
pub enum BuiltinToolError { |
| 43 |
#[error("Failed to record changes")] |
| 44 |
Record(#[from] scm_record::RecordError), |
| 45 |
#[error("Failed to decode UTF-8 text for item {item} (this should not happen)")] |
| 46 |
DecodeUtf8 { |
| 47 |
source: std::str::Utf8Error, |
| 48 |
item: &'static str, |
| 49 |
}, |
| 50 |
#[error("Rendering {item} {id} is unimplemented for the builtin difftool/mergetool")] |
| 51 |
Unimplemented { item: &'static str, id: String }, |
| 52 |
#[error("Backend error")] |
| 53 |
BackendError(#[from] jj_lib::backend::BackendError), |
| 54 |
} |
| 55 |
|
| 56 |
#[derive(Clone, Debug)] |
| 57 |
enum FileContents { |
| 58 |
Absent, |
| 59 |
Text { |
| 60 |
contents: String, |
| 61 |
hash: Option<String>, |
| 62 |
num_bytes: u64, |
| 63 |
}, |
| 64 |
Binary { |
| 65 |
hash: Option<String>, |
| 66 |
num_bytes: u64, |
| 67 |
}, |
| 68 |
} |
| 69 |
|
| 70 |
impl FileContents { |
| 71 |
fn describe(&self) -> Option<String> { |
| 72 |
match self { |
| 73 |
Self::Absent => None, |
| 74 |
Self::Text { |
| 75 |
contents: _, |
| 76 |
hash, |
| 77 |
num_bytes, |
| 78 |
} |
| 79 |
| Self::Binary { hash, num_bytes } => match hash { |
| 80 |
Some(hash) => Some(format!("{hash} ({num_bytes}B)")), |
| 81 |
None => Some(format!("({num_bytes}B)")), |
| 82 |
}, |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/// Information about a file that was read from disk. Note that the file may not |
| 88 |
/// have existed, in which case its contents will be marked as absent. |
| 89 |
#[derive(Clone, Debug)] |
| 90 |
pub struct FileInfo { |
| 91 |
file_mode: scm_record::FileMode, |
| 92 |
contents: FileContents, |
| 93 |
} |
| 94 |
|
| 95 |
/// File modes according to the Git file mode conventions. used for display |
| 96 |
/// purposes and equality comparison. |
| 97 |
/// |
| 98 |
/// TODO: let `scm-record` accept strings instead of numbers for file modes? Or |
| 99 |
/// figure out some other way to represent file mode changes in a jj-compatible |
| 100 |
/// manner? |
| 101 |
mod mode { |
| 102 |
pub const ABSENT: scm_record::FileMode = scm_record::FileMode::Absent; |
| 103 |
pub const NORMAL: scm_record::FileMode = scm_record::FileMode::Unix(0o100644); |
| 104 |
pub const EXECUTABLE: scm_record::FileMode = scm_record::FileMode::Unix(0o100755); |
| 105 |
pub const SYMLINK: scm_record::FileMode = scm_record::FileMode::Unix(0o120000); |
| 106 |
} |
| 107 |
|
| 108 |
fn buf_to_file_contents(hash: Option<String>, buf: Vec<u8>) -> FileContents { |
| 109 |
let num_bytes: u64 = buf.len().try_into().unwrap(); |
| 110 |
let text = if buf.contains(&0) { |
| 111 |
None |
| 112 |
} else { |
| 113 |
String::from_utf8(buf).ok() |
| 114 |
}; |
| 115 |
match text { |
| 116 |
Some(text) => FileContents::Text { |
| 117 |
contents: text, |
| 118 |
hash, |
| 119 |
num_bytes, |
| 120 |
}, |
| 121 |
None => FileContents::Binary { hash, num_bytes }, |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
async fn read_file_contents( |
| 126 |
materialized_value: MaterializedTreeValue, |
| 127 |
path: &RepoPath, |
| 128 |
materialize_options: &ConflictMaterializeOptions, |
| 129 |
) -> Result<FileInfo, BuiltinToolError> { |
| 130 |
match materialized_value { |
| 131 |
MaterializedTreeValue::Absent => Ok(FileInfo { |
| 132 |
file_mode: mode::ABSENT, |
| 133 |
contents: FileContents::Absent, |
| 134 |
}), |
| 135 |
MaterializedTreeValue::AccessDenied(err) => Ok(FileInfo { |
| 136 |
file_mode: mode::NORMAL, |
| 137 |
contents: FileContents::Text { |
| 138 |
contents: format!("Access denied: {err}"), |
| 139 |
hash: None, |
| 140 |
num_bytes: 0, |
| 141 |
}, |
| 142 |
}), |
| 143 |
|
| 144 |
MaterializedTreeValue::File(mut file) => { |
| 145 |
let buf = file.read_all(path).await?; |
| 146 |
let file_mode = if file.executable { |
| 147 |
mode::EXECUTABLE |
| 148 |
} else { |
| 149 |
mode::NORMAL |
| 150 |
}; |
| 151 |
let contents = buf_to_file_contents(Some(file.id.hex()), buf); |
| 152 |
Ok(FileInfo { |
| 153 |
file_mode, |
| 154 |
contents, |
| 155 |
}) |
| 156 |
} |
| 157 |
|
| 158 |
MaterializedTreeValue::Symlink { id, target } => { |
| 159 |
let file_mode = mode::SYMLINK; |
| 160 |
let num_bytes = target.len().try_into().unwrap(); |
| 161 |
Ok(FileInfo { |
| 162 |
file_mode, |
| 163 |
contents: FileContents::Text { |
| 164 |
contents: target, |
| 165 |
hash: Some(id.hex()), |
| 166 |
num_bytes, |
| 167 |
}, |
| 168 |
}) |
| 169 |
} |
| 170 |
|
| 171 |
MaterializedTreeValue::Tree(tree_id) => { |
| 172 |
unreachable!("list of changed files included a tree: {tree_id:?}"); |
| 173 |
} |
| 174 |
MaterializedTreeValue::GitSubmodule(id) => Err(BuiltinToolError::Unimplemented { |
| 175 |
item: "git submodule", |
| 176 |
id: id.hex(), |
| 177 |
}), |
| 178 |
MaterializedTreeValue::FileConflict(file) => { |
| 179 |
// Since scm_record doesn't support diffs of conflicts, file |
| 180 |
// conflicts are compared in materialized form. The UI would look |
| 181 |
// scary, but it can at least allow squashing resolved hunks. |
| 182 |
let buf = materialize_merge_result_to_bytes( |
| 183 |
&file.contents, |
| 184 |
&file.labels, |
| 185 |
materialize_options, |
| 186 |
) |
| 187 |
.into(); |
| 188 |
// TODO: Render the ID somehow? |
| 189 |
let contents = buf_to_file_contents(None, buf); |
| 190 |
Ok(FileInfo { |
| 191 |
file_mode: if file.executable == Some(true) { |
| 192 |
mode::EXECUTABLE |
| 193 |
} else { |
| 194 |
mode::NORMAL |
| 195 |
}, |
| 196 |
contents, |
| 197 |
}) |
| 198 |
} |
| 199 |
MaterializedTreeValue::OtherConflict { id, labels } => { |
| 200 |
// TODO: Non-file conflict shouldn't be rendered as a normal file |
| 201 |
// TODO: Render the ID somehow? |
| 202 |
let contents = buf_to_file_contents(None, id.describe(&labels).into_bytes()); |
| 203 |
Ok(FileInfo { |
| 204 |
file_mode: mode::NORMAL, |
| 205 |
contents, |
| 206 |
}) |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
fn make_section_changed_lines( |
| 212 |
contents: &str, |
| 213 |
change_type: scm_record::ChangeType, |
| 214 |
) -> Vec<scm_record::SectionChangedLine<'static>> { |
| 215 |
contents |
| 216 |
.split_inclusive('\n') |
| 217 |
.map(|line| scm_record::SectionChangedLine { |
| 218 |
is_checked: false, |
| 219 |
change_type, |
| 220 |
line: Cow::Owned(line.to_owned()), |
| 221 |
}) |
| 222 |
.collect() |
| 223 |
} |
| 224 |
|
| 225 |
fn make_diff_sections( |
| 226 |
left_contents: &str, |
| 227 |
right_contents: &str, |
| 228 |
) -> Result<Vec<scm_record::Section<'static>>, BuiltinToolError> { |
| 229 |
let diff = ContentDiff::by_line([left_contents.as_bytes(), right_contents.as_bytes()]); |
| 230 |
let mut sections = Vec::new(); |
| 231 |
for hunk in diff.hunks() { |
| 232 |
match hunk.kind { |
| 233 |
DiffHunkKind::Matching => { |
| 234 |
debug_assert!(hunk.contents.iter().all_equal()); |
| 235 |
let text = hunk.contents[0]; |
| 236 |
let text = str::from_utf8(text).map_err(|err| BuiltinToolError::DecodeUtf8 { |
| 237 |
source: err, |
| 238 |
item: "matching text in diff hunk", |
| 239 |
})?; |
| 240 |
sections.push(scm_record::Section::Unchanged { |
| 241 |
lines: text |
| 242 |
.split_inclusive('\n') |
| 243 |
.map(|line| Cow::Owned(line.to_owned())) |
| 244 |
.collect(), |
| 245 |
}); |
| 246 |
} |
| 247 |
DiffHunkKind::Different => { |
| 248 |
let sides = &hunk.contents; |
| 249 |
assert_eq!(sides.len(), 2, "only two inputs were provided to the diff"); |
| 250 |
let left_side = |
| 251 |
str::from_utf8(sides[0]).map_err(|err| BuiltinToolError::DecodeUtf8 { |
| 252 |
source: err, |
| 253 |
item: "left side of diff hunk", |
| 254 |
})?; |
| 255 |
let right_side = |
| 256 |
str::from_utf8(sides[1]).map_err(|err| BuiltinToolError::DecodeUtf8 { |
| 257 |
source: err, |
| 258 |
item: "right side of diff hunk", |
| 259 |
})?; |
| 260 |
sections.push(scm_record::Section::Changed { |
| 261 |
lines: [ |
| 262 |
make_section_changed_lines(left_side, scm_record::ChangeType::Removed), |
| 263 |
make_section_changed_lines(right_side, scm_record::ChangeType::Added), |
| 264 |
] |
| 265 |
.concat(), |
| 266 |
}); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
Ok(sections) |
| 271 |
} |
| 272 |
|
| 273 |
async fn make_diff_files( |
| 274 |
store: &Arc<Store>, |
| 275 |
trees: Diff<&MergedTree>, |
| 276 |
tree_diff: BoxStream<'_, CopiesTreeDiffEntry>, |
| 277 |
marker_style: ConflictMarkerStyle, |
| 278 |
) -> Result<(Vec<RepoPathBuf>, Vec<scm_record::File<'static>>), BuiltinToolError> { |
| 279 |
let materialize_options = ConflictMaterializeOptions { |
| 280 |
marker_style, |
| 281 |
marker_len: None, |
| 282 |
merge: store.merge_options().clone(), |
| 283 |
}; |
| 284 |
let conflict_labels = trees.map(MergedTree::labels); |
| 285 |
let mut diff_stream = materialized_diff_stream(store, tree_diff, conflict_labels); |
| 286 |
let mut changed_files = Vec::new(); |
| 287 |
let mut files = Vec::new(); |
| 288 |
while let Some(entry) = diff_stream.next().await { |
| 289 |
let left_path = entry.path.source(); |
| 290 |
let right_path = entry.path.target(); |
| 291 |
let values = entry.values?; |
| 292 |
let left_info = read_file_contents(values.before, left_path, &materialize_options).await?; |
| 293 |
let right_info = read_file_contents(values.after, right_path, &materialize_options).await?; |
| 294 |
let mut sections = Vec::new(); |
| 295 |
|
| 296 |
if left_info.file_mode != right_info.file_mode { |
| 297 |
sections.push(scm_record::Section::FileMode { |
| 298 |
is_checked: false, |
| 299 |
mode: right_info.file_mode, |
| 300 |
}); |
| 301 |
} |
| 302 |
|
| 303 |
match (left_info.contents, right_info.contents) { |
| 304 |
(FileContents::Absent, FileContents::Absent) => {} |
| 305 |
// In this context, `Absent` means the file doesn't exist. If it only |
| 306 |
// exists on one side, we will render a mode change section above. |
| 307 |
// The next two patterns are to avoid also rendering an empty |
| 308 |
// changed lines section that clutters the UI. |
| 309 |
( |
| 310 |
FileContents::Absent, |
| 311 |
FileContents::Text { |
| 312 |
contents: _, |
| 313 |
hash: _, |
| 314 |
num_bytes: 0, |
| 315 |
}, |
| 316 |
) => {} |
| 317 |
( |
| 318 |
FileContents::Text { |
| 319 |
contents: _, |
| 320 |
hash: _, |
| 321 |
num_bytes: 0, |
| 322 |
}, |
| 323 |
FileContents::Absent, |
| 324 |
) => {} |
| 325 |
( |
| 326 |
FileContents::Absent, |
| 327 |
FileContents::Text { |
| 328 |
contents, |
| 329 |
hash: _, |
| 330 |
num_bytes: _, |
| 331 |
}, |
| 332 |
) => sections.push(scm_record::Section::Changed { |
| 333 |
lines: make_section_changed_lines(&contents, scm_record::ChangeType::Added), |
| 334 |
}), |
| 335 |
|
| 336 |
( |
| 337 |
FileContents::Text { |
| 338 |
contents, |
| 339 |
hash: _, |
| 340 |
num_bytes: _, |
| 341 |
}, |
| 342 |
FileContents::Absent, |
| 343 |
) => sections.push(scm_record::Section::Changed { |
| 344 |
lines: make_section_changed_lines(&contents, scm_record::ChangeType::Removed), |
| 345 |
}), |
| 346 |
|
| 347 |
( |
| 348 |
FileContents::Text { |
| 349 |
contents: old_contents, |
| 350 |
hash: _, |
| 351 |
num_bytes: _, |
| 352 |
}, |
| 353 |
FileContents::Text { |
| 354 |
contents: new_contents, |
| 355 |
hash: _, |
| 356 |
num_bytes: _, |
| 357 |
}, |
| 358 |
) => { |
| 359 |
sections.extend(make_diff_sections(&old_contents, &new_contents)?); |
| 360 |
} |
| 361 |
|
| 362 |
( |
| 363 |
FileContents::Binary { |
| 364 |
hash: Some(left_hash), |
| 365 |
.. |
| 366 |
}, |
| 367 |
FileContents::Binary { |
| 368 |
hash: Some(right_hash), |
| 369 |
.. |
| 370 |
}, |
| 371 |
) if left_hash == right_hash => { |
| 372 |
// Binary file contents have not changed. |
| 373 |
} |
| 374 |
(left, right @ FileContents::Binary { .. }) |
| 375 |
| (left @ FileContents::Binary { .. }, right) => { |
| 376 |
sections.push(scm_record::Section::Binary { |
| 377 |
is_checked: false, |
| 378 |
old_description: left.describe().map(Cow::Owned), |
| 379 |
new_description: right.describe().map(Cow::Owned), |
| 380 |
}); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
files.push(scm_record::File { |
| 385 |
old_path: None, |
| 386 |
// Path for displaying purposes, not for file access. |
| 387 |
path: Cow::Owned(right_path.to_fs_path_unchecked(Path::new(""))), |
| 388 |
file_mode: left_info.file_mode, |
| 389 |
sections, |
| 390 |
}); |
| 391 |
changed_files.push(entry.path.target); |
| 392 |
} |
| 393 |
Ok((changed_files, files)) |
| 394 |
} |
| 395 |
|
| 396 |
fn resolve_file_copy_id(tree_value: &MergedTreeValue) -> Option<CopyId> { |
| 397 |
tree_value.to_copy_id_merge().and_then(|copy_id| { |
| 398 |
copy_id |
| 399 |
.resolve_trivial(SameChange::Accept) |
| 400 |
.cloned() |
| 401 |
.flatten() |
| 402 |
}) |
| 403 |
} |
| 404 |
|
| 405 |
async fn apply_diff_builtin( |
| 406 |
store: &Arc<Store>, |
| 407 |
left_tree: &MergedTree, |
| 408 |
right_tree: &MergedTree, |
| 409 |
changed_files: Vec<RepoPathBuf>, |
| 410 |
files: &[scm_record::File<'_>], |
| 411 |
) -> BackendResult<MergedTree> { |
| 412 |
// Start with the right tree to match external tool behavior. |
| 413 |
// This ensures unmatched paths keep their values from the right tree. |
| 414 |
let mut tree_builder = MergedTreeBuilder::new(right_tree.clone()); |
| 415 |
|
| 416 |
// First, revert all changed files to their left versions |
| 417 |
for path in &changed_files { |
| 418 |
let left_value = left_tree.path_value(path).await?; |
| 419 |
tree_builder.set_or_remove(path.clone(), left_value); |
| 420 |
} |
| 421 |
|
| 422 |
// Then apply only the selected changes |
| 423 |
apply_changes( |
| 424 |
&mut tree_builder, |
| 425 |
changed_files, |
| 426 |
files, |
| 427 |
async |path| left_tree.path_value(path).await, |
| 428 |
async |path| right_tree.path_value(path).await, |
| 429 |
async |path, contents, executable| { |
| 430 |
let old_value = left_tree.path_value(path).await?; |
| 431 |
let copy_id = resolve_file_copy_id(&old_value).unwrap_or_else(CopyId::placeholder); |
| 432 |
let new_value = if old_value.is_resolved() { |
| 433 |
let id = store.write_file(path, &mut &contents[..]).await?; |
| 434 |
Merge::normal(TreeValue::File { |
| 435 |
id, |
| 436 |
executable, |
| 437 |
copy_id, |
| 438 |
}) |
| 439 |
} else if let Some(old_file_ids) = old_value.to_file_merge() { |
| 440 |
// TODO: should error out if conflicts couldn't be parsed? |
| 441 |
let new_file_ids = conflicts::update_from_content( |
| 442 |
&old_file_ids, |
| 443 |
store, |
| 444 |
path, |
| 445 |
contents, |
| 446 |
MIN_CONFLICT_MARKER_LEN, // TODO: use the materialization parameter |
| 447 |
) |
| 448 |
.await?; |
| 449 |
match new_file_ids.into_resolved() { |
| 450 |
Ok(id) => Merge::resolved(id.map(|id| TreeValue::File { |
| 451 |
id, |
| 452 |
executable, |
| 453 |
copy_id, |
| 454 |
})), |
| 455 |
Err(file_ids) => old_value.with_new_file_ids(&file_ids), |
| 456 |
} |
| 457 |
} else { |
| 458 |
panic!("unexpected content change at {path:?}: {old_value:?}"); |
| 459 |
}; |
| 460 |
Ok(new_value) |
| 461 |
}, |
| 462 |
) |
| 463 |
.await?; |
| 464 |
tree_builder.write_tree().await |
| 465 |
} |
| 466 |
|
| 467 |
async fn apply_changes( |
| 468 |
tree_builder: &mut MergedTreeBuilder, |
| 469 |
changed_files: Vec<RepoPathBuf>, |
| 470 |
files: &[scm_record::File<'_>], |
| 471 |
select_left: impl AsyncFn(&RepoPath) -> BackendResult<MergedTreeValue>, |
| 472 |
select_right: impl AsyncFn(&RepoPath) -> BackendResult<MergedTreeValue>, |
| 473 |
write_file: impl AsyncFn(&RepoPath, &[u8], bool) -> BackendResult<MergedTreeValue>, |
| 474 |
) -> BackendResult<()> { |
| 475 |
assert_eq!( |
| 476 |
changed_files.len(), |
| 477 |
files.len(), |
| 478 |
"result had a different number of files" |
| 479 |
); |
| 480 |
// TODO: Write files concurrently |
| 481 |
for (path, file) in changed_files.into_iter().zip(files) { |
| 482 |
let file_mode_change_selected = file |
| 483 |
.sections |
| 484 |
.iter() |
| 485 |
.find_map(|sec| match sec { |
| 486 |
scm_record::Section::FileMode { is_checked, .. } => Some(*is_checked), |
| 487 |
_ => None, |
| 488 |
}) |
| 489 |
.unwrap_or(false); |
| 490 |
|
| 491 |
let ( |
| 492 |
scm_record::SelectedChanges { |
| 493 |
contents, |
| 494 |
file_mode, |
| 495 |
}, |
| 496 |
_unselected, |
| 497 |
) = file.get_selected_contents(); |
| 498 |
|
| 499 |
if file_mode == mode::ABSENT { |
| 500 |
// The file is not present in the selected changes. |
| 501 |
// Either a file mode change was selected to delete an existing file, so we |
| 502 |
// should remove it from the tree, |
| 503 |
if file_mode_change_selected { |
| 504 |
tree_builder.set_or_remove(path, Merge::absent()); |
| 505 |
} |
| 506 |
// or the file's creation has been split out of the change, in which case we |
| 507 |
// don't need to change the tree. |
| 508 |
// In either case, we're done with this file afterwards. |
| 509 |
continue; |
| 510 |
} |
| 511 |
|
| 512 |
let executable = file_mode == mode::EXECUTABLE; |
| 513 |
match contents { |
| 514 |
scm_record::SelectedContents::Unchanged => { |
| 515 |
if file_mode_change_selected { |
| 516 |
// File contents haven't changed, but file mode needs to be updated on the tree. |
| 517 |
let value = override_file_executable_bit(select_left(&path).await?, executable); |
| 518 |
tree_builder.set_or_remove(path, value); |
| 519 |
} else { |
| 520 |
// Neither file mode, nor contents changed => Do nothing. |
| 521 |
} |
| 522 |
} |
| 523 |
scm_record::SelectedContents::Binary { |
| 524 |
old_description: _, |
| 525 |
new_description: Some(_), |
| 526 |
} => { |
| 527 |
let value = override_file_executable_bit(select_right(&path).await?, executable); |
| 528 |
tree_builder.set_or_remove(path, value); |
| 529 |
} |
| 530 |
scm_record::SelectedContents::Binary { |
| 531 |
old_description: _, |
| 532 |
new_description: None, |
| 533 |
} => { |
| 534 |
// File contents emptied out, but file mode is not absent => write empty file. |
| 535 |
let value = write_file(&path, &[], executable).await?; |
| 536 |
tree_builder.set_or_remove(path, value); |
| 537 |
} |
| 538 |
scm_record::SelectedContents::Text { contents } => { |
| 539 |
let value = write_file(&path, contents.as_bytes(), executable).await?; |
| 540 |
tree_builder.set_or_remove(path, value); |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
Ok(()) |
| 545 |
} |
| 546 |
|
| 547 |
fn override_file_executable_bit( |
| 548 |
mut merged_tree_value: MergedTreeValue, |
| 549 |
new_executable_bit: bool, |
| 550 |
) -> MergedTreeValue { |
| 551 |
for tree_value in merged_tree_value.iter_mut().flatten() { |
| 552 |
let TreeValue::File { executable, .. } = tree_value else { |
| 553 |
panic!("incompatible update: expected a TreeValue::File, got {tree_value:?}"); |
| 554 |
}; |
| 555 |
*executable = new_executable_bit; |
| 556 |
} |
| 557 |
merged_tree_value |
| 558 |
} |
| 559 |
|
| 560 |
pub async fn edit_diff_builtin( |
| 561 |
trees: Diff<&MergedTree>, |
| 562 |
matcher: &dyn Matcher, |
| 563 |
conflict_marker_style: ConflictMarkerStyle, |
| 564 |
) -> Result<MergedTree, BuiltinToolError> { |
| 565 |
let store = trees.before.store().clone(); |
| 566 |
// TODO: handle copy tracking |
| 567 |
let copy_records = CopyRecords::default(); |
| 568 |
let tree_diff = trees |
| 569 |
.before |
| 570 |
.diff_stream_with_copies(trees.after, matcher, ©_records); |
| 571 |
let (changed_files, files) = |
| 572 |
make_diff_files(&store, trees, tree_diff, conflict_marker_style).await?; |
| 573 |
let mut input = scm_record::helpers::CrosstermInput; |
| 574 |
let recorder = scm_record::Recorder::new( |
| 575 |
scm_record::RecordState { |
| 576 |
is_read_only: false, |
| 577 |
files, |
| 578 |
commits: Default::default(), |
| 579 |
}, |
| 580 |
&mut input, |
| 581 |
); |
| 582 |
let result = recorder.run().map_err(BuiltinToolError::Record)?; |
| 583 |
apply_diff_builtin( |
| 584 |
&store, |
| 585 |
trees.before, |
| 586 |
trees.after, |
| 587 |
changed_files, |
| 588 |
&result.files, |
| 589 |
) |
| 590 |
.await |
| 591 |
.map_err(BuiltinToolError::BackendError) |
| 592 |
} |
| 593 |
|
| 594 |
fn make_merge_sections( |
| 595 |
merge_result: MergeResult, |
| 596 |
) -> Result<Vec<scm_record::Section<'static>>, BuiltinToolError> { |
| 597 |
let mut sections = Vec::new(); |
| 598 |
match merge_result { |
| 599 |
MergeResult::Resolved(buf) => { |
| 600 |
let contents = buf_to_file_contents(None, buf.into()); |
| 601 |
let section = match contents { |
| 602 |
FileContents::Absent => None, |
| 603 |
FileContents::Text { |
| 604 |
contents, |
| 605 |
hash: _, |
| 606 |
num_bytes: _, |
| 607 |
} => Some(scm_record::Section::Unchanged { |
| 608 |
lines: contents |
| 609 |
.split_inclusive('\n') |
| 610 |
.map(|line| Cow::Owned(line.to_owned())) |
| 611 |
.collect(), |
| 612 |
}), |
| 613 |
FileContents::Binary { .. } => Some(scm_record::Section::Binary { |
| 614 |
// TODO: Perhaps, this should be an "unchanged" section? |
| 615 |
is_checked: false, |
| 616 |
old_description: None, |
| 617 |
new_description: contents.describe().map(Cow::Owned), |
| 618 |
}), |
| 619 |
}; |
| 620 |
if let Some(section) = section { |
| 621 |
sections.push(section); |
| 622 |
} |
| 623 |
} |
| 624 |
MergeResult::Conflict(hunks) => { |
| 625 |
for hunk in hunks { |
| 626 |
let section = match hunk.into_resolved() { |
| 627 |
Ok(contents) => { |
| 628 |
let contents = str::from_utf8(&contents).map_err(|err| { |
| 629 |
BuiltinToolError::DecodeUtf8 { |
| 630 |
source: err, |
| 631 |
item: "unchanged hunk", |
| 632 |
} |
| 633 |
})?; |
| 634 |
scm_record::Section::Unchanged { |
| 635 |
lines: contents |
| 636 |
.split_inclusive('\n') |
| 637 |
.map(|line| Cow::Owned(line.to_owned())) |
| 638 |
.collect(), |
| 639 |
} |
| 640 |
} |
| 641 |
Err(merge) => { |
| 642 |
let lines: Vec<scm_record::SectionChangedLine> = merge |
| 643 |
.iter() |
| 644 |
.zip( |
| 645 |
[ |
| 646 |
scm_record::ChangeType::Added, |
| 647 |
scm_record::ChangeType::Removed, |
| 648 |
] |
| 649 |
.into_iter() |
| 650 |
.cycle(), |
| 651 |
) |
| 652 |
.map(|(contents, change_type)| -> Result<_, BuiltinToolError> { |
| 653 |
let contents = str::from_utf8(contents).map_err(|err| { |
| 654 |
BuiltinToolError::DecodeUtf8 { |
| 655 |
source: err, |
| 656 |
item: "conflicting hunk", |
| 657 |
} |
| 658 |
})?; |
| 659 |
let changed_lines = |
| 660 |
make_section_changed_lines(contents, change_type); |
| 661 |
Ok(changed_lines) |
| 662 |
}) |
| 663 |
.flatten_ok() |
| 664 |
.try_collect()?; |
| 665 |
scm_record::Section::Changed { lines } |
| 666 |
} |
| 667 |
}; |
| 668 |
sections.push(section); |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
Ok(sections) |
| 673 |
} |
| 674 |
|
| 675 |
fn make_merge_file( |
| 676 |
merge_tool_file: &MergeToolFile, |
| 677 |
options: &MergeOptions, |
| 678 |
) -> Result<scm_record::File<'static>, BuiltinToolError> { |
| 679 |
let file = &merge_tool_file.file; |
| 680 |
let file_mode = if file.executable.expect("should have been resolved") { |
| 681 |
mode::EXECUTABLE |
| 682 |
} else { |
| 683 |
mode::NORMAL |
| 684 |
}; |
| 685 |
// TODO: Maybe we should test binary contents here, and generate per-file |
| 686 |
// Binary section to select either "our" or "their" file. |
| 687 |
let merge_result = files::merge_hunks(&file.contents, options); |
| 688 |
let sections = make_merge_sections(merge_result)?; |
| 689 |
Ok(scm_record::File { |
| 690 |
old_path: None, |
| 691 |
// Path for displaying purposes, not for file access. |
| 692 |
path: Cow::Owned( |
| 693 |
merge_tool_file |
| 694 |
.repo_path |
| 695 |
.to_fs_path_unchecked(Path::new("")), |
| 696 |
), |
| 697 |
file_mode, |
| 698 |
sections, |
| 699 |
}) |
| 700 |
} |
| 701 |
|
| 702 |
pub async fn edit_merge_builtin( |
| 703 |
tree: &MergedTree, |
| 704 |
merge_tool_files: &[MergeToolFile], |
| 705 |
) -> Result<MergedTree, BuiltinToolError> { |
| 706 |
let store = tree.store(); |
| 707 |
let mut input = scm_record::helpers::CrosstermInput; |
| 708 |
let recorder = scm_record::Recorder::new( |
| 709 |
scm_record::RecordState { |
| 710 |
is_read_only: false, |
| 711 |
files: merge_tool_files |
| 712 |
.iter() |
| 713 |
.map(|f| make_merge_file(f, store.merge_options())) |
| 714 |
.try_collect()?, |
| 715 |
commits: Default::default(), |
| 716 |
}, |
| 717 |
&mut input, |
| 718 |
); |
| 719 |
let state = recorder.run()?; |
| 720 |
apply_merge_builtin( |
| 721 |
store, |
| 722 |
tree, |
| 723 |
merge_tool_files |
| 724 |
.iter() |
| 725 |
.map(|file| file.repo_path.clone()) |
| 726 |
.collect_vec(), |
| 727 |
&state.files, |
| 728 |
) |
| 729 |
.await |
| 730 |
.map_err(BuiltinToolError::BackendError) |
| 731 |
} |
| 732 |
|
| 733 |
async fn apply_merge_builtin( |
| 734 |
store: &Arc<Store>, |
| 735 |
tree: &MergedTree, |
| 736 |
changed_files: Vec<RepoPathBuf>, |
| 737 |
files: &[scm_record::File<'_>], |
| 738 |
) -> BackendResult<MergedTree> { |
| 739 |
let mut tree_builder = MergedTreeBuilder::new(tree.clone()); |
| 740 |
apply_changes( |
| 741 |
&mut tree_builder, |
| 742 |
changed_files, |
| 743 |
files, |
| 744 |
async |path| tree.path_value(path).await, |
| 745 |
// FIXME: It doesn't make sense to select a new value from the source tree. |
| 746 |
// Presently, `select_right` is never actually called, since it is used to select binary |
| 747 |
// sections, but `make_merge_file` does not produce `Binary` sections for conflicted files. |
| 748 |
// This needs to be revisited when the UI becomes capable of representing binary conflicts. |
| 749 |
async |path| tree.path_value(path).await, |
| 750 |
async |path, contents, executable| { |
| 751 |
let id = store.write_file(path, &mut &contents[..]).await?; |
| 752 |
let tree_value = tree.path_value(path).await?; |
| 753 |
let copy_id = resolve_file_copy_id(&tree_value).unwrap_or_else(CopyId::placeholder); |
| 754 |
Ok(Merge::normal(TreeValue::File { |
| 755 |
id, |
| 756 |
executable, |
| 757 |
copy_id, |
| 758 |
})) |
| 759 |
}, |
| 760 |
) |
| 761 |
.await?; |
| 762 |
tree_builder.write_tree().await |
| 763 |
} |
| 764 |
|
| 765 |
#[cfg(test)] |
| 766 |
mod tests { |
| 767 |
use std::collections::BTreeSet; |
| 768 |
|
| 769 |
use jj_lib::backend::FileId; |
| 770 |
use jj_lib::conflict_labels::ConflictLabels; |
| 771 |
use jj_lib::conflicts::extract_as_single_hunk; |
| 772 |
use jj_lib::matchers::EverythingMatcher; |
| 773 |
use jj_lib::matchers::FilesMatcher; |
| 774 |
use jj_lib::repo::Repo as _; |
| 775 |
use pollster::FutureExt as _; |
| 776 |
use proptest::prelude::*; |
| 777 |
use proptest_state_machine::ReferenceStateMachine; |
| 778 |
use proptest_state_machine::StateMachineTest; |
| 779 |
use proptest_state_machine::prop_state_machine; |
| 780 |
use test_case::test_matrix; |
| 781 |
use testutils::TestRepo; |
| 782 |
use testutils::TestResult; |
| 783 |
use testutils::assert_tree_eq; |
| 784 |
use testutils::dump_tree; |
| 785 |
use testutils::proptest::Transition; |
| 786 |
use testutils::proptest::WorkingCopyReferenceStateMachine; |
| 787 |
use testutils::repo_path; |
| 788 |
use testutils::repo_path_component; |
| 789 |
|
| 790 |
use super::*; |
| 791 |
|
| 792 |
fn make_diff( |
| 793 |
store: &Arc<Store>, |
| 794 |
left_tree: &MergedTree, |
| 795 |
right_tree: &MergedTree, |
| 796 |
) -> (Vec<RepoPathBuf>, Vec<scm_record::File<'static>>) { |
| 797 |
make_diff_with_matcher(store, left_tree, right_tree, &EverythingMatcher) |
| 798 |
} |
| 799 |
|
| 800 |
fn make_diff_with_matcher( |
| 801 |
store: &Arc<Store>, |
| 802 |
left_tree: &MergedTree, |
| 803 |
right_tree: &MergedTree, |
| 804 |
matcher: &dyn Matcher, |
| 805 |
) -> (Vec<RepoPathBuf>, Vec<scm_record::File<'static>>) { |
| 806 |
let copy_records = CopyRecords::default(); |
| 807 |
let tree_diff = left_tree.diff_stream_with_copies(right_tree, matcher, ©_records); |
| 808 |
make_diff_files( |
| 809 |
store, |
| 810 |
Diff::new(left_tree, right_tree), |
| 811 |
tree_diff, |
| 812 |
ConflictMarkerStyle::Diff, |
| 813 |
) |
| 814 |
.block_on() |
| 815 |
.unwrap() |
| 816 |
} |
| 817 |
|
| 818 |
fn apply_diff( |
| 819 |
store: &Arc<Store>, |
| 820 |
left_tree: &MergedTree, |
| 821 |
right_tree: &MergedTree, |
| 822 |
changed_files: &[RepoPathBuf], |
| 823 |
files: &[scm_record::File], |
| 824 |
) -> MergedTree { |
| 825 |
apply_diff_builtin(store, left_tree, right_tree, changed_files.to_vec(), files) |
| 826 |
.block_on() |
| 827 |
.unwrap() |
| 828 |
} |
| 829 |
|
| 830 |
#[test] |
| 831 |
fn test_edit_diff_builtin() { |
| 832 |
let test_repo = TestRepo::init(); |
| 833 |
let store = test_repo.repo.store(); |
| 834 |
|
| 835 |
let unchanged = repo_path("unchanged"); |
| 836 |
let changed_path = repo_path("changed"); |
| 837 |
let added_path = repo_path("added"); |
| 838 |
let left_tree = testutils::create_tree( |
| 839 |
&test_repo.repo, |
| 840 |
&[ |
| 841 |
(unchanged, "unchanged\n"), |
| 842 |
(changed_path, "line1\nline2\nline3\n"), |
| 843 |
], |
| 844 |
); |
| 845 |
let right_tree = testutils::create_tree( |
| 846 |
&test_repo.repo, |
| 847 |
&[ |
| 848 |
(unchanged, "unchanged\n"), |
| 849 |
(changed_path, "line1\nchanged1\nchanged2\nline3\nadded1\n"), |
| 850 |
(added_path, "added\n"), |
| 851 |
], |
| 852 |
); |
| 853 |
|
| 854 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 855 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 856 |
[ |
| 857 |
"added", |
| 858 |
"changed", |
| 859 |
] |
| 860 |
"#); |
| 861 |
insta::assert_debug_snapshot!(files, @r#" |
| 862 |
[ |
| 863 |
File { |
| 864 |
old_path: None, |
| 865 |
path: "added", |
| 866 |
file_mode: Absent, |
| 867 |
sections: [ |
| 868 |
FileMode { |
| 869 |
is_checked: false, |
| 870 |
mode: Unix( |
| 871 |
33188, |
| 872 |
), |
| 873 |
}, |
| 874 |
Changed { |
| 875 |
lines: [ |
| 876 |
SectionChangedLine { |
| 877 |
is_checked: false, |
| 878 |
change_type: Added, |
| 879 |
line: "added\n", |
| 880 |
}, |
| 881 |
], |
| 882 |
}, |
| 883 |
], |
| 884 |
}, |
| 885 |
File { |
| 886 |
old_path: None, |
| 887 |
path: "changed", |
| 888 |
file_mode: Unix( |
| 889 |
33188, |
| 890 |
), |
| 891 |
sections: [ |
| 892 |
Unchanged { |
| 893 |
lines: [ |
| 894 |
"line1\n", |
| 895 |
], |
| 896 |
}, |
| 897 |
Changed { |
| 898 |
lines: [ |
| 899 |
SectionChangedLine { |
| 900 |
is_checked: false, |
| 901 |
change_type: Removed, |
| 902 |
line: "line2\n", |
| 903 |
}, |
| 904 |
SectionChangedLine { |
| 905 |
is_checked: false, |
| 906 |
change_type: Added, |
| 907 |
line: "changed1\n", |
| 908 |
}, |
| 909 |
SectionChangedLine { |
| 910 |
is_checked: false, |
| 911 |
change_type: Added, |
| 912 |
line: "changed2\n", |
| 913 |
}, |
| 914 |
], |
| 915 |
}, |
| 916 |
Unchanged { |
| 917 |
lines: [ |
| 918 |
"line3\n", |
| 919 |
], |
| 920 |
}, |
| 921 |
Changed { |
| 922 |
lines: [ |
| 923 |
SectionChangedLine { |
| 924 |
is_checked: false, |
| 925 |
change_type: Added, |
| 926 |
line: "added1\n", |
| 927 |
}, |
| 928 |
], |
| 929 |
}, |
| 930 |
], |
| 931 |
}, |
| 932 |
] |
| 933 |
"#); |
| 934 |
|
| 935 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 936 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 937 |
|
| 938 |
let mut files = files; |
| 939 |
for file in &mut files { |
| 940 |
file.toggle_all(); |
| 941 |
} |
| 942 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 943 |
assert_tree_eq!( |
| 944 |
right_tree, |
| 945 |
all_changes_tree, |
| 946 |
"all-changes tree was different", |
| 947 |
); |
| 948 |
} |
| 949 |
|
| 950 |
fn create_tree_with_executable( |
| 951 |
store: &Arc<Store>, |
| 952 |
file_path: &RepoPath, |
| 953 |
contents: &[&str], |
| 954 |
executable: bool, |
| 955 |
) -> MergedTree { |
| 956 |
let tree_ids = contents |
| 957 |
.iter() |
| 958 |
.map(|contents| { |
| 959 |
let mut tree_builder = testutils::TestTreeBuilder::new(store.clone()); |
| 960 |
tree_builder |
| 961 |
.file(file_path, *contents) |
| 962 |
.executable(executable); |
| 963 |
tree_builder.write_single_tree().id().clone() |
| 964 |
}) |
| 965 |
.collect_vec(); |
| 966 |
MergedTree::new( |
| 967 |
store.clone(), |
| 968 |
Merge::from_vec(tree_ids), |
| 969 |
ConflictLabels::unlabeled(), |
| 970 |
) |
| 971 |
} |
| 972 |
|
| 973 |
fn create_resolved_left_tree_with_executable( |
| 974 |
store: &Arc<Store>, |
| 975 |
file_path: &RepoPath, |
| 976 |
executable: bool, |
| 977 |
) -> MergedTree { |
| 978 |
let contents = ["left\n"]; |
| 979 |
create_tree_with_executable(store, file_path, &contents, executable) |
| 980 |
} |
| 981 |
|
| 982 |
fn create_conflict_left_tree_with_executable( |
| 983 |
store: &Arc<Store>, |
| 984 |
file_path: &RepoPath, |
| 985 |
executable: bool, |
| 986 |
) -> MergedTree { |
| 987 |
let contents = ["left parent1\n", "", "left parent2\n"]; |
| 988 |
create_tree_with_executable(store, file_path, &contents, executable) |
| 989 |
} |
| 990 |
|
| 991 |
fn create_resolved_right_tree_with_executable( |
| 992 |
store: &Arc<Store>, |
| 993 |
file_path: &RepoPath, |
| 994 |
executable: bool, |
| 995 |
) -> MergedTree { |
| 996 |
let contents = ["right\n"]; |
| 997 |
create_tree_with_executable(store, file_path, &contents, executable) |
| 998 |
} |
| 999 |
|
| 1000 |
fn create_conflict_right_tree_with_executable( |
| 1001 |
store: &Arc<Store>, |
| 1002 |
file_path: &RepoPath, |
| 1003 |
executable: bool, |
| 1004 |
) -> MergedTree { |
| 1005 |
let contents = ["right parent1\n", "", "right parent2\n"]; |
| 1006 |
create_tree_with_executable(store, file_path, &contents, executable) |
| 1007 |
} |
| 1008 |
|
| 1009 |
#[test_matrix( |
| 1010 |
[true, false], |
| 1011 |
[create_resolved_left_tree_with_executable, create_conflict_left_tree_with_executable], |
| 1012 |
[create_resolved_right_tree_with_executable, create_conflict_right_tree_with_executable] |
| 1013 |
)] |
| 1014 |
fn test_edit_diff_builtin_no_file_mode_section_when_file_modes_are_the_same( |
| 1015 |
executable: bool, |
| 1016 |
create_left_tree: impl FnOnce(&Arc<Store>, &RepoPath, bool) -> MergedTree, |
| 1017 |
create_right_tree: impl FnOnce(&Arc<Store>, &RepoPath, bool) -> MergedTree, |
| 1018 |
) { |
| 1019 |
// In this test, we create 2 trees that consist of only one file under the same |
| 1020 |
// path. The executable bits are the same. Either of the left tree and the right |
| 1021 |
// tree can be resolved or have conflicts. Regardless of whether the file is |
| 1022 |
// resolved or have conflicts, the file has the same executable bit in both |
| 1023 |
// trees, so we don't expect a file mode section in the diff. |
| 1024 |
|
| 1025 |
let test_repo = TestRepo::init(); |
| 1026 |
let store = test_repo.repo.store(); |
| 1027 |
|
| 1028 |
let file_path = repo_path("file"); |
| 1029 |
|
| 1030 |
let left_tree = create_left_tree(store, file_path, executable); |
| 1031 |
let right_tree = create_right_tree(store, file_path, executable); |
| 1032 |
|
| 1033 |
let (changed_files, mut files) = make_diff(store, &left_tree, &right_tree); |
| 1034 |
assert_eq!(files.len(), 1); |
| 1035 |
for section in &files[0].sections { |
| 1036 |
let is_file_mode_section = matches!(section, scm_record::Section::FileMode { .. }); |
| 1037 |
assert!( |
| 1038 |
!is_file_mode_section, |
| 1039 |
"Expect the diff to not contain a file mode section, but got: {:#?}", |
| 1040 |
files[0] |
| 1041 |
); |
| 1042 |
} |
| 1043 |
|
| 1044 |
let get_actual_executables = |tree: &MergedTree| { |
| 1045 |
tree.path_value(file_path) |
| 1046 |
.block_on() |
| 1047 |
.unwrap() |
| 1048 |
.to_executable_merge() |
| 1049 |
.expect("The path should point to an existing file.") |
| 1050 |
}; |
| 1051 |
|
| 1052 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1053 |
let actual_executables = get_actual_executables(&no_changes_tree); |
| 1054 |
assert_eq!( |
| 1055 |
actual_executables.resolve_trivial(SameChange::Accept), |
| 1056 |
Some(&Some(executable)), |
| 1057 |
"Expect the executable bit before applying any diffs to be trivially resolved to \ |
| 1058 |
{executable}, but got {actual_executables:?}", |
| 1059 |
); |
| 1060 |
|
| 1061 |
for file in &mut files { |
| 1062 |
file.toggle_all(); |
| 1063 |
} |
| 1064 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1065 |
let actual_executables = get_actual_executables(&all_changes_tree); |
| 1066 |
assert_eq!( |
| 1067 |
actual_executables.resolve_trivial(SameChange::Accept), |
| 1068 |
Some(&Some(executable)), |
| 1069 |
"Expect the executable bit after applying all diffs to be trivially resolved to \ |
| 1070 |
{executable}, but got {actual_executables:?}", |
| 1071 |
); |
| 1072 |
} |
| 1073 |
|
| 1074 |
fn create_tree_with_copy_id( |
| 1075 |
store: &Arc<Store>, |
| 1076 |
file_path: &RepoPath, |
| 1077 |
contents: &[&str], |
| 1078 |
copy_id: &CopyId, |
| 1079 |
) -> MergedTree { |
| 1080 |
let tree_ids = contents |
| 1081 |
.iter() |
| 1082 |
.map(|contents| { |
| 1083 |
let mut tree_builder = testutils::TestTreeBuilder::new(store.clone()); |
| 1084 |
tree_builder |
| 1085 |
.file(file_path, *contents) |
| 1086 |
.copy_id(copy_id.clone()); |
| 1087 |
tree_builder.write_single_tree().id().clone() |
| 1088 |
}) |
| 1089 |
.collect_vec(); |
| 1090 |
MergedTree::new( |
| 1091 |
store.clone(), |
| 1092 |
Merge::from_vec(tree_ids), |
| 1093 |
ConflictLabels::unlabeled(), |
| 1094 |
) |
| 1095 |
} |
| 1096 |
|
| 1097 |
fn create_resolved_left_tree_with_copy_id( |
| 1098 |
store: &Arc<Store>, |
| 1099 |
file_path: &RepoPath, |
| 1100 |
copy_id: &CopyId, |
| 1101 |
) -> MergedTree { |
| 1102 |
let contents = ["left\n"]; |
| 1103 |
create_tree_with_copy_id(store, file_path, &contents, copy_id) |
| 1104 |
} |
| 1105 |
|
| 1106 |
fn create_conflict_left_tree_with_copy_id( |
| 1107 |
store: &Arc<Store>, |
| 1108 |
file_path: &RepoPath, |
| 1109 |
copy_id: &CopyId, |
| 1110 |
) -> MergedTree { |
| 1111 |
let contents = ["left parent1\n", "", "left parent2\n"]; |
| 1112 |
create_tree_with_copy_id(store, file_path, &contents, copy_id) |
| 1113 |
} |
| 1114 |
|
| 1115 |
fn create_resolved_right_tree_with_copy_id( |
| 1116 |
store: &Arc<Store>, |
| 1117 |
file_path: &RepoPath, |
| 1118 |
copy_id: &CopyId, |
| 1119 |
) -> MergedTree { |
| 1120 |
let contents = ["right\n"]; |
| 1121 |
create_tree_with_copy_id(store, file_path, &contents, copy_id) |
| 1122 |
} |
| 1123 |
|
| 1124 |
fn create_conflict_right_tree_with_copy_id( |
| 1125 |
store: &Arc<Store>, |
| 1126 |
file_path: &RepoPath, |
| 1127 |
copy_id: &CopyId, |
| 1128 |
) -> MergedTree { |
| 1129 |
let contents = ["right parent1\n", "", "right parent2\n"]; |
| 1130 |
create_tree_with_copy_id(store, file_path, &contents, copy_id) |
| 1131 |
} |
| 1132 |
|
| 1133 |
#[test_matrix( |
| 1134 |
[create_resolved_left_tree_with_copy_id, create_conflict_left_tree_with_copy_id], |
| 1135 |
[create_resolved_right_tree_with_copy_id, create_conflict_right_tree_with_copy_id] |
| 1136 |
)] |
| 1137 |
fn test_edit_diff_builtin_apply_diff_should_preserve_copy_id( |
| 1138 |
create_left_tree: impl FnOnce(&Arc<Store>, &RepoPath, &CopyId) -> MergedTree, |
| 1139 |
create_right_tree: impl FnOnce(&Arc<Store>, &RepoPath, &CopyId) -> MergedTree, |
| 1140 |
) -> TestResult { |
| 1141 |
let test_repo = TestRepo::init(); |
| 1142 |
let store = test_repo.repo.store(); |
| 1143 |
|
| 1144 |
// A random unique copy id that is unlikely to collide. |
| 1145 |
let copy_id = CopyId::from_bytes(b"\xe6\x64\x40\x71\x52\x94\x4d\xd9\x2d\xb1"); |
| 1146 |
let file_path = repo_path("file"); |
| 1147 |
let left_tree = create_left_tree(store, file_path, ©_id); |
| 1148 |
let right_tree = create_right_tree( |
| 1149 |
store, |
| 1150 |
file_path, |
| 1151 |
// Another random unique copy id that is unlikely to collide. |
| 1152 |
&CopyId::from_bytes(b"\x02\x91\xeb\xd4\xc0\xed\x71\x33\xeb\xaf"), |
| 1153 |
); |
| 1154 |
|
| 1155 |
let (changed_files, mut files) = make_diff(store, &left_tree, &right_tree); |
| 1156 |
assert_eq!(files.len(), 1); |
| 1157 |
for section in &mut files[0].sections { |
| 1158 |
// We only modify the contents of the file. |
| 1159 |
if matches!(section, scm_record::Section::Changed { .. }) { |
| 1160 |
section.toggle_all(); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
let tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1164 |
let actual_copy_ids = tree.path_value(file_path).block_on()?.map(|tree_value| { |
| 1165 |
let Some(TreeValue::File { copy_id, .. }) = tree_value else { |
| 1166 |
panic!("The path should point to an existing file."); |
| 1167 |
}; |
| 1168 |
copy_id.clone() |
| 1169 |
}); |
| 1170 |
assert_eq!( |
| 1171 |
actual_copy_ids.resolve_trivial(SameChange::Accept), |
| 1172 |
Some(©_id), |
| 1173 |
"Expect the copy id of the file to be resolved to {copy_id:?}, but got \ |
| 1174 |
{actual_copy_ids:?}." |
| 1175 |
); |
| 1176 |
Ok(()) |
| 1177 |
} |
| 1178 |
|
| 1179 |
#[test] |
| 1180 |
fn test_edit_merge_builtin_should_preserve_copy_id() -> TestResult { |
| 1181 |
let test_repo = TestRepo::init(); |
| 1182 |
let store = test_repo.repo.store(); |
| 1183 |
|
| 1184 |
let file_path = repo_path("test-file.txt"); |
| 1185 |
// A random unique copy id that is unlikely to collide. |
| 1186 |
let new_copy_id = CopyId::from_bytes(b"\xa3\x2b\x24\x3b\xda\x42\x6b\xe3\x14\x33"); |
| 1187 |
let mut tree_builder = testutils::TestThreeWayMergeTreeBuilder::new(Arc::clone(store)); |
| 1188 |
tree_builder |
| 1189 |
.base() |
| 1190 |
.file(file_path, "") |
| 1191 |
// Another random unique copy id that is unlikely to collide. |
| 1192 |
.copy_id(CopyId::from_bytes( |
| 1193 |
b"\x10\xda\x3e\x78\x6f\x62\x83\x97\x0b\x0f", |
| 1194 |
)); |
| 1195 |
tree_builder |
| 1196 |
.parent1() |
| 1197 |
.file(file_path, "left parent1\n") |
| 1198 |
.copy_id(new_copy_id.clone()); |
| 1199 |
tree_builder |
| 1200 |
.parent2() |
| 1201 |
.file(file_path, "left parent2\n") |
| 1202 |
.copy_id(new_copy_id.clone()); |
| 1203 |
let tree = tree_builder.write_merged_tree(); |
| 1204 |
|
| 1205 |
let merge_tool_file = MergeToolFile::from_tree_and_path(&tree, file_path).block_on()?; |
| 1206 |
let merge_file = make_merge_file(&merge_tool_file, store.merge_options())?; |
| 1207 |
let tree = apply_merge_builtin(store, &tree, vec![file_path.to_owned()], &[merge_file]) |
| 1208 |
.block_on()?; |
| 1209 |
|
| 1210 |
let actual_copy_ids = tree |
| 1211 |
.path_value(file_path) |
| 1212 |
.block_on()? |
| 1213 |
.into_map(|tree_value| { |
| 1214 |
let Some(TreeValue::File { copy_id, .. }) = tree_value else { |
| 1215 |
panic!("The path should point to an existing file."); |
| 1216 |
}; |
| 1217 |
copy_id |
| 1218 |
}); |
| 1219 |
assert_eq!( |
| 1220 |
actual_copy_ids.resolve_trivial(SameChange::Accept), |
| 1221 |
Some(&new_copy_id), |
| 1222 |
"Expect the copy id of the file to be resolved to {new_copy_id:?}, but got \ |
| 1223 |
{actual_copy_ids:?}." |
| 1224 |
); |
| 1225 |
Ok(()) |
| 1226 |
} |
| 1227 |
|
| 1228 |
#[test] |
| 1229 |
fn test_edit_diff_builtin_add_empty_file() { |
| 1230 |
let test_repo = TestRepo::init(); |
| 1231 |
let store = test_repo.repo.store(); |
| 1232 |
|
| 1233 |
let added_empty_file_path = repo_path("empty_file"); |
| 1234 |
let left_tree = testutils::create_tree(&test_repo.repo, &[]); |
| 1235 |
let right_tree = testutils::create_tree(&test_repo.repo, &[(added_empty_file_path, "")]); |
| 1236 |
|
| 1237 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1238 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1239 |
[ |
| 1240 |
"empty_file", |
| 1241 |
] |
| 1242 |
"#); |
| 1243 |
insta::assert_debug_snapshot!(files, @r#" |
| 1244 |
[ |
| 1245 |
File { |
| 1246 |
old_path: None, |
| 1247 |
path: "empty_file", |
| 1248 |
file_mode: Absent, |
| 1249 |
sections: [ |
| 1250 |
FileMode { |
| 1251 |
is_checked: false, |
| 1252 |
mode: Unix( |
| 1253 |
33188, |
| 1254 |
), |
| 1255 |
}, |
| 1256 |
], |
| 1257 |
}, |
| 1258 |
] |
| 1259 |
"#); |
| 1260 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1261 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1262 |
|
| 1263 |
let mut files = files; |
| 1264 |
for file in &mut files { |
| 1265 |
file.toggle_all(); |
| 1266 |
} |
| 1267 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1268 |
assert_tree_eq!( |
| 1269 |
right_tree, |
| 1270 |
all_changes_tree, |
| 1271 |
"all-changes tree was different", |
| 1272 |
); |
| 1273 |
} |
| 1274 |
|
| 1275 |
#[test] |
| 1276 |
fn test_edit_diff_builtin_add_executable_file() { |
| 1277 |
let test_repo = TestRepo::init(); |
| 1278 |
let store = test_repo.repo.store(); |
| 1279 |
|
| 1280 |
let added_executable_file_path = repo_path("executable_file"); |
| 1281 |
let left_tree = testutils::create_tree(&test_repo.repo, &[]); |
| 1282 |
let right_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1283 |
builder |
| 1284 |
.file(added_executable_file_path, "executable") |
| 1285 |
.executable(true); |
| 1286 |
}); |
| 1287 |
|
| 1288 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1289 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1290 |
[ |
| 1291 |
"executable_file", |
| 1292 |
] |
| 1293 |
"#); |
| 1294 |
insta::assert_debug_snapshot!(files, @r#" |
| 1295 |
[ |
| 1296 |
File { |
| 1297 |
old_path: None, |
| 1298 |
path: "executable_file", |
| 1299 |
file_mode: Absent, |
| 1300 |
sections: [ |
| 1301 |
FileMode { |
| 1302 |
is_checked: false, |
| 1303 |
mode: Unix( |
| 1304 |
33261, |
| 1305 |
), |
| 1306 |
}, |
| 1307 |
Changed { |
| 1308 |
lines: [ |
| 1309 |
SectionChangedLine { |
| 1310 |
is_checked: false, |
| 1311 |
change_type: Added, |
| 1312 |
line: "executable", |
| 1313 |
}, |
| 1314 |
], |
| 1315 |
}, |
| 1316 |
], |
| 1317 |
}, |
| 1318 |
] |
| 1319 |
"#); |
| 1320 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1321 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1322 |
|
| 1323 |
let mut files = files; |
| 1324 |
for file in &mut files { |
| 1325 |
file.toggle_all(); |
| 1326 |
} |
| 1327 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1328 |
assert_tree_eq!( |
| 1329 |
right_tree, |
| 1330 |
all_changes_tree, |
| 1331 |
"all-changes tree was different", |
| 1332 |
); |
| 1333 |
} |
| 1334 |
|
| 1335 |
#[test] |
| 1336 |
fn test_edit_diff_builtin_empty_file_mode_change() { |
| 1337 |
let test_repo = TestRepo::init(); |
| 1338 |
let store = test_repo.repo.store(); |
| 1339 |
|
| 1340 |
let empty_file_path = repo_path("empty_file"); |
| 1341 |
let left_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1342 |
builder.file(empty_file_path, vec![]).executable(false); |
| 1343 |
}); |
| 1344 |
let right_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1345 |
builder.file(empty_file_path, vec![]).executable(true); |
| 1346 |
}); |
| 1347 |
|
| 1348 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1349 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1350 |
[ |
| 1351 |
"empty_file", |
| 1352 |
] |
| 1353 |
"#); |
| 1354 |
insta::assert_debug_snapshot!(files, @r#" |
| 1355 |
[ |
| 1356 |
File { |
| 1357 |
old_path: None, |
| 1358 |
path: "empty_file", |
| 1359 |
file_mode: Unix( |
| 1360 |
33188, |
| 1361 |
), |
| 1362 |
sections: [ |
| 1363 |
FileMode { |
| 1364 |
is_checked: false, |
| 1365 |
mode: Unix( |
| 1366 |
33261, |
| 1367 |
), |
| 1368 |
}, |
| 1369 |
], |
| 1370 |
}, |
| 1371 |
] |
| 1372 |
"#); |
| 1373 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1374 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1375 |
|
| 1376 |
let mut files = files; |
| 1377 |
for file in &mut files { |
| 1378 |
file.toggle_all(); |
| 1379 |
} |
| 1380 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1381 |
assert_tree_eq!( |
| 1382 |
right_tree, |
| 1383 |
all_changes_tree, |
| 1384 |
"all-changes tree was different", |
| 1385 |
); |
| 1386 |
} |
| 1387 |
|
| 1388 |
#[test] |
| 1389 |
fn test_edit_diff_builtin_text_file_mode_change() { |
| 1390 |
let test_repo = TestRepo::init(); |
| 1391 |
let store = test_repo.repo.store(); |
| 1392 |
|
| 1393 |
let text_file_path = repo_path("text_file"); |
| 1394 |
let left_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1395 |
builder.file(text_file_path, "text").executable(false); |
| 1396 |
}); |
| 1397 |
let right_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1398 |
builder.file(text_file_path, "text").executable(true); |
| 1399 |
}); |
| 1400 |
|
| 1401 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1402 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1403 |
[ |
| 1404 |
"text_file", |
| 1405 |
] |
| 1406 |
"#); |
| 1407 |
insta::assert_debug_snapshot!(files, @r#" |
| 1408 |
[ |
| 1409 |
File { |
| 1410 |
old_path: None, |
| 1411 |
path: "text_file", |
| 1412 |
file_mode: Unix( |
| 1413 |
33188, |
| 1414 |
), |
| 1415 |
sections: [ |
| 1416 |
FileMode { |
| 1417 |
is_checked: false, |
| 1418 |
mode: Unix( |
| 1419 |
33261, |
| 1420 |
), |
| 1421 |
}, |
| 1422 |
Unchanged { |
| 1423 |
lines: [ |
| 1424 |
"text", |
| 1425 |
], |
| 1426 |
}, |
| 1427 |
], |
| 1428 |
}, |
| 1429 |
] |
| 1430 |
"#); |
| 1431 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1432 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1433 |
|
| 1434 |
let mut files = files; |
| 1435 |
for file in &mut files { |
| 1436 |
file.toggle_all(); |
| 1437 |
} |
| 1438 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1439 |
assert_tree_eq!( |
| 1440 |
right_tree, |
| 1441 |
all_changes_tree, |
| 1442 |
"all-changes tree was different", |
| 1443 |
); |
| 1444 |
} |
| 1445 |
|
| 1446 |
#[test] |
| 1447 |
fn test_edit_diff_builtin_binary_file_mode_change() { |
| 1448 |
let test_repo = TestRepo::init(); |
| 1449 |
let store = test_repo.repo.store(); |
| 1450 |
|
| 1451 |
let binary_file_path = repo_path("binary_file"); |
| 1452 |
let left_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1453 |
builder |
| 1454 |
.file(binary_file_path, vec![0xff, 0x00]) |
| 1455 |
.executable(false); |
| 1456 |
}); |
| 1457 |
let right_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1458 |
builder |
| 1459 |
.file(binary_file_path, vec![0xff, 0x00]) |
| 1460 |
.executable(true); |
| 1461 |
}); |
| 1462 |
|
| 1463 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1464 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1465 |
[ |
| 1466 |
"binary_file", |
| 1467 |
] |
| 1468 |
"#); |
| 1469 |
insta::assert_debug_snapshot!(files, @r#" |
| 1470 |
[ |
| 1471 |
File { |
| 1472 |
old_path: None, |
| 1473 |
path: "binary_file", |
| 1474 |
file_mode: Unix( |
| 1475 |
33188, |
| 1476 |
), |
| 1477 |
sections: [ |
| 1478 |
FileMode { |
| 1479 |
is_checked: false, |
| 1480 |
mode: Unix( |
| 1481 |
33261, |
| 1482 |
), |
| 1483 |
}, |
| 1484 |
], |
| 1485 |
}, |
| 1486 |
] |
| 1487 |
"#); |
| 1488 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1489 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1490 |
|
| 1491 |
let mut files = files; |
| 1492 |
for file in &mut files { |
| 1493 |
file.toggle_all(); |
| 1494 |
} |
| 1495 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1496 |
assert_tree_eq!( |
| 1497 |
right_tree, |
| 1498 |
all_changes_tree, |
| 1499 |
"all-changes tree was different", |
| 1500 |
); |
| 1501 |
} |
| 1502 |
|
| 1503 |
#[test] |
| 1504 |
fn test_edit_diff_builtin_change_binary_file_with_unselected_file_mode_change() { |
| 1505 |
let test_repo = TestRepo::init(); |
| 1506 |
let store = test_repo.repo.store(); |
| 1507 |
|
| 1508 |
let binary_file_path = repo_path("binary_file"); |
| 1509 |
let left_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1510 |
builder |
| 1511 |
.file(binary_file_path, vec![0xff, 0x00]) |
| 1512 |
.executable(false); |
| 1513 |
}); |
| 1514 |
let right_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1515 |
builder |
| 1516 |
.file(binary_file_path, vec![0xff, 0x01]) |
| 1517 |
.executable(true); |
| 1518 |
}); |
| 1519 |
|
| 1520 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1521 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1522 |
[ |
| 1523 |
"binary_file", |
| 1524 |
] |
| 1525 |
"#); |
| 1526 |
insta::assert_debug_snapshot!(files, @r#" |
| 1527 |
[ |
| 1528 |
File { |
| 1529 |
old_path: None, |
| 1530 |
path: "binary_file", |
| 1531 |
file_mode: Unix( |
| 1532 |
33188, |
| 1533 |
), |
| 1534 |
sections: [ |
| 1535 |
FileMode { |
| 1536 |
is_checked: false, |
| 1537 |
mode: Unix( |
| 1538 |
33261, |
| 1539 |
), |
| 1540 |
}, |
| 1541 |
Binary { |
| 1542 |
is_checked: false, |
| 1543 |
old_description: Some( |
| 1544 |
"fb296c879f1852c0dca0 (2B)", |
| 1545 |
), |
| 1546 |
new_description: Some( |
| 1547 |
"cc429d26cbaec338223b (2B)", |
| 1548 |
), |
| 1549 |
}, |
| 1550 |
], |
| 1551 |
}, |
| 1552 |
] |
| 1553 |
"#); |
| 1554 |
|
| 1555 |
// Select only the binary change |
| 1556 |
let mut files = files; |
| 1557 |
for file in &mut files { |
| 1558 |
for section in &mut file.sections { |
| 1559 |
if let scm_record::Section::Binary { is_checked, .. } = section { |
| 1560 |
*is_checked = true; |
| 1561 |
} |
| 1562 |
} |
| 1563 |
} |
| 1564 |
|
| 1565 |
let expected_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1566 |
builder |
| 1567 |
.file(binary_file_path, vec![0xff, 0x01]) |
| 1568 |
.executable(false); |
| 1569 |
}); |
| 1570 |
let actual_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1571 |
assert_tree_eq!(expected_tree, actual_tree); |
| 1572 |
} |
| 1573 |
|
| 1574 |
#[test] |
| 1575 |
fn test_edit_diff_builtin_delete_binary_file_with_unselected_file_mode_change() { |
| 1576 |
let test_repo = TestRepo::init(); |
| 1577 |
let store = test_repo.repo.store(); |
| 1578 |
|
| 1579 |
let binary_file_path = repo_path("binary_file"); |
| 1580 |
let left_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1581 |
builder.file(binary_file_path, vec![0xff, 0x00]); |
| 1582 |
}); |
| 1583 |
let right_tree = testutils::create_tree(&test_repo.repo, &[]); |
| 1584 |
|
| 1585 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1586 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1587 |
[ |
| 1588 |
"binary_file", |
| 1589 |
] |
| 1590 |
"#); |
| 1591 |
insta::assert_debug_snapshot!(files, @r#" |
| 1592 |
[ |
| 1593 |
File { |
| 1594 |
old_path: None, |
| 1595 |
path: "binary_file", |
| 1596 |
file_mode: Unix( |
| 1597 |
33188, |
| 1598 |
), |
| 1599 |
sections: [ |
| 1600 |
FileMode { |
| 1601 |
is_checked: false, |
| 1602 |
mode: Absent, |
| 1603 |
}, |
| 1604 |
Binary { |
| 1605 |
is_checked: false, |
| 1606 |
old_description: Some( |
| 1607 |
"fb296c879f1852c0dca0 (2B)", |
| 1608 |
), |
| 1609 |
new_description: None, |
| 1610 |
}, |
| 1611 |
], |
| 1612 |
}, |
| 1613 |
] |
| 1614 |
"#); |
| 1615 |
|
| 1616 |
// Select only the binary change |
| 1617 |
let mut files = files; |
| 1618 |
for file in &mut files { |
| 1619 |
for section in &mut file.sections { |
| 1620 |
if let scm_record::Section::Binary { is_checked, .. } = section { |
| 1621 |
*is_checked = true; |
| 1622 |
} |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
let expected_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1627 |
builder.file(binary_file_path, vec![]); |
| 1628 |
}); |
| 1629 |
let actual_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1630 |
assert_tree_eq!(expected_tree, actual_tree); |
| 1631 |
} |
| 1632 |
|
| 1633 |
#[test] |
| 1634 |
fn test_edit_diff_builtin_delete_file() { |
| 1635 |
let test_repo = TestRepo::init(); |
| 1636 |
let store = test_repo.repo.store(); |
| 1637 |
|
| 1638 |
let file_path = repo_path("file_with_content"); |
| 1639 |
let left_tree = testutils::create_tree(&test_repo.repo, &[(file_path, "content\n")]); |
| 1640 |
let right_tree = testutils::create_tree(&test_repo.repo, &[]); |
| 1641 |
|
| 1642 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1643 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1644 |
[ |
| 1645 |
"file_with_content", |
| 1646 |
] |
| 1647 |
"#); |
| 1648 |
insta::assert_debug_snapshot!(files, @r#" |
| 1649 |
[ |
| 1650 |
File { |
| 1651 |
old_path: None, |
| 1652 |
path: "file_with_content", |
| 1653 |
file_mode: Unix( |
| 1654 |
33188, |
| 1655 |
), |
| 1656 |
sections: [ |
| 1657 |
FileMode { |
| 1658 |
is_checked: false, |
| 1659 |
mode: Absent, |
| 1660 |
}, |
| 1661 |
Changed { |
| 1662 |
lines: [ |
| 1663 |
SectionChangedLine { |
| 1664 |
is_checked: false, |
| 1665 |
change_type: Removed, |
| 1666 |
line: "content\n", |
| 1667 |
}, |
| 1668 |
], |
| 1669 |
}, |
| 1670 |
], |
| 1671 |
}, |
| 1672 |
] |
| 1673 |
"#); |
| 1674 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1675 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1676 |
|
| 1677 |
let mut files = files; |
| 1678 |
for file in &mut files { |
| 1679 |
file.toggle_all(); |
| 1680 |
} |
| 1681 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1682 |
assert_tree_eq!( |
| 1683 |
right_tree, |
| 1684 |
all_changes_tree, |
| 1685 |
"all-changes tree was different", |
| 1686 |
); |
| 1687 |
} |
| 1688 |
|
| 1689 |
#[test] |
| 1690 |
fn test_edit_diff_builtin_delete_empty_file() { |
| 1691 |
let test_repo = TestRepo::init(); |
| 1692 |
let store = test_repo.repo.store(); |
| 1693 |
|
| 1694 |
let added_empty_file_path = repo_path("empty_file"); |
| 1695 |
let left_tree = testutils::create_tree(&test_repo.repo, &[(added_empty_file_path, "")]); |
| 1696 |
let right_tree = testutils::create_tree(&test_repo.repo, &[]); |
| 1697 |
|
| 1698 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1699 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1700 |
[ |
| 1701 |
"empty_file", |
| 1702 |
] |
| 1703 |
"#); |
| 1704 |
insta::assert_debug_snapshot!(files, @r#" |
| 1705 |
[ |
| 1706 |
File { |
| 1707 |
old_path: None, |
| 1708 |
path: "empty_file", |
| 1709 |
file_mode: Unix( |
| 1710 |
33188, |
| 1711 |
), |
| 1712 |
sections: [ |
| 1713 |
FileMode { |
| 1714 |
is_checked: false, |
| 1715 |
mode: Absent, |
| 1716 |
}, |
| 1717 |
], |
| 1718 |
}, |
| 1719 |
] |
| 1720 |
"#); |
| 1721 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1722 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1723 |
|
| 1724 |
let mut files = files; |
| 1725 |
for file in &mut files { |
| 1726 |
file.toggle_all(); |
| 1727 |
} |
| 1728 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1729 |
assert_tree_eq!( |
| 1730 |
right_tree, |
| 1731 |
all_changes_tree, |
| 1732 |
"all-changes tree was different", |
| 1733 |
); |
| 1734 |
} |
| 1735 |
|
| 1736 |
#[test] |
| 1737 |
fn test_edit_diff_builtin_modify_empty_file() { |
| 1738 |
let test_repo = TestRepo::init(); |
| 1739 |
let store = test_repo.repo.store(); |
| 1740 |
|
| 1741 |
let empty_file_path = repo_path("empty_file"); |
| 1742 |
let left_tree = testutils::create_tree(&test_repo.repo, &[(empty_file_path, "")]); |
| 1743 |
let right_tree = |
| 1744 |
testutils::create_tree(&test_repo.repo, &[(empty_file_path, "modified\n")]); |
| 1745 |
|
| 1746 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1747 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1748 |
[ |
| 1749 |
"empty_file", |
| 1750 |
] |
| 1751 |
"#); |
| 1752 |
insta::assert_debug_snapshot!(files, @r#" |
| 1753 |
[ |
| 1754 |
File { |
| 1755 |
old_path: None, |
| 1756 |
path: "empty_file", |
| 1757 |
file_mode: Unix( |
| 1758 |
33188, |
| 1759 |
), |
| 1760 |
sections: [ |
| 1761 |
Changed { |
| 1762 |
lines: [ |
| 1763 |
SectionChangedLine { |
| 1764 |
is_checked: false, |
| 1765 |
change_type: Added, |
| 1766 |
line: "modified\n", |
| 1767 |
}, |
| 1768 |
], |
| 1769 |
}, |
| 1770 |
], |
| 1771 |
}, |
| 1772 |
] |
| 1773 |
"#); |
| 1774 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1775 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1776 |
|
| 1777 |
let mut files = files; |
| 1778 |
for file in &mut files { |
| 1779 |
file.toggle_all(); |
| 1780 |
} |
| 1781 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1782 |
assert_tree_eq!( |
| 1783 |
right_tree, |
| 1784 |
all_changes_tree, |
| 1785 |
"all-changes tree was different", |
| 1786 |
); |
| 1787 |
} |
| 1788 |
|
| 1789 |
#[test] |
| 1790 |
fn test_edit_diff_builtin_make_file_empty() { |
| 1791 |
let test_repo = TestRepo::init(); |
| 1792 |
let store = test_repo.repo.store(); |
| 1793 |
|
| 1794 |
let file_path = repo_path("file_with_content"); |
| 1795 |
let left_tree = testutils::create_tree(&test_repo.repo, &[(file_path, "content\n")]); |
| 1796 |
let right_tree = testutils::create_tree(&test_repo.repo, &[(file_path, "")]); |
| 1797 |
|
| 1798 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1799 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1800 |
[ |
| 1801 |
"file_with_content", |
| 1802 |
] |
| 1803 |
"#); |
| 1804 |
insta::assert_debug_snapshot!(files, @r#" |
| 1805 |
[ |
| 1806 |
File { |
| 1807 |
old_path: None, |
| 1808 |
path: "file_with_content", |
| 1809 |
file_mode: Unix( |
| 1810 |
33188, |
| 1811 |
), |
| 1812 |
sections: [ |
| 1813 |
Changed { |
| 1814 |
lines: [ |
| 1815 |
SectionChangedLine { |
| 1816 |
is_checked: false, |
| 1817 |
change_type: Removed, |
| 1818 |
line: "content\n", |
| 1819 |
}, |
| 1820 |
], |
| 1821 |
}, |
| 1822 |
], |
| 1823 |
}, |
| 1824 |
] |
| 1825 |
"#); |
| 1826 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1827 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 1828 |
|
| 1829 |
let mut files = files; |
| 1830 |
for file in &mut files { |
| 1831 |
file.toggle_all(); |
| 1832 |
} |
| 1833 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1834 |
assert_tree_eq!( |
| 1835 |
right_tree, |
| 1836 |
all_changes_tree, |
| 1837 |
"all-changes tree was different", |
| 1838 |
); |
| 1839 |
} |
| 1840 |
|
| 1841 |
#[test] |
| 1842 |
fn test_edit_diff_builtin_conflict_file() { |
| 1843 |
let test_repo = TestRepo::init(); |
| 1844 |
let store = test_repo.repo.store(); |
| 1845 |
|
| 1846 |
let file_path = repo_path("file"); |
| 1847 |
let left_tree = { |
| 1848 |
let base = testutils::create_single_tree(&test_repo.repo, &[(file_path, "")]); |
| 1849 |
let left = testutils::create_single_tree(&test_repo.repo, &[(file_path, "1\n")]); |
| 1850 |
let right = testutils::create_single_tree(&test_repo.repo, &[(file_path, "2\n")]); |
| 1851 |
MergedTree::new( |
| 1852 |
store.clone(), |
| 1853 |
Merge::from_vec(vec![ |
| 1854 |
left.id().clone(), |
| 1855 |
base.id().clone(), |
| 1856 |
right.id().clone(), |
| 1857 |
]), |
| 1858 |
ConflictLabels::from_vec(vec!["left".into(), "base".into(), "right".into()]), |
| 1859 |
) |
| 1860 |
}; |
| 1861 |
let right_tree = testutils::create_tree(&test_repo.repo, &[(file_path, "resolved\n")]); |
| 1862 |
|
| 1863 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1864 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1865 |
[ |
| 1866 |
"file", |
| 1867 |
] |
| 1868 |
"#); |
| 1869 |
insta::assert_debug_snapshot!(files, @r#" |
| 1870 |
[ |
| 1871 |
File { |
| 1872 |
old_path: None, |
| 1873 |
path: "file", |
| 1874 |
file_mode: Unix( |
| 1875 |
33188, |
| 1876 |
), |
| 1877 |
sections: [ |
| 1878 |
Changed { |
| 1879 |
lines: [ |
| 1880 |
SectionChangedLine { |
| 1881 |
is_checked: false, |
| 1882 |
change_type: Removed, |
| 1883 |
line: "<<<<<<< conflict 1 of 1\n", |
| 1884 |
}, |
| 1885 |
SectionChangedLine { |
| 1886 |
is_checked: false, |
| 1887 |
change_type: Removed, |
| 1888 |
line: "%%%%%%% diff from: base\n", |
| 1889 |
}, |
| 1890 |
SectionChangedLine { |
| 1891 |
is_checked: false, |
| 1892 |
change_type: Removed, |
| 1893 |
line: "\\\\\\\\\\\\\\ to: left\n", |
| 1894 |
}, |
| 1895 |
SectionChangedLine { |
| 1896 |
is_checked: false, |
| 1897 |
change_type: Removed, |
| 1898 |
line: "+1\n", |
| 1899 |
}, |
| 1900 |
SectionChangedLine { |
| 1901 |
is_checked: false, |
| 1902 |
change_type: Removed, |
| 1903 |
line: "+++++++ right\n", |
| 1904 |
}, |
| 1905 |
SectionChangedLine { |
| 1906 |
is_checked: false, |
| 1907 |
change_type: Removed, |
| 1908 |
line: "2\n", |
| 1909 |
}, |
| 1910 |
SectionChangedLine { |
| 1911 |
is_checked: false, |
| 1912 |
change_type: Removed, |
| 1913 |
line: ">>>>>>> conflict 1 of 1 ends\n", |
| 1914 |
}, |
| 1915 |
SectionChangedLine { |
| 1916 |
is_checked: false, |
| 1917 |
change_type: Added, |
| 1918 |
line: "resolved\n", |
| 1919 |
}, |
| 1920 |
], |
| 1921 |
}, |
| 1922 |
], |
| 1923 |
}, |
| 1924 |
] |
| 1925 |
"#); |
| 1926 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1927 |
// TODO: we should ensure that `jj diffedit` preserves labels when restoring a |
| 1928 |
// conflict. It also should work when restoring conflicts of differing arities. |
| 1929 |
let left_tree_without_labels = MergedTree::new( |
| 1930 |
left_tree.store().clone(), |
| 1931 |
left_tree.tree_ids().clone(), |
| 1932 |
ConflictLabels::unlabeled(), |
| 1933 |
); |
| 1934 |
assert_tree_eq!( |
| 1935 |
left_tree_without_labels, |
| 1936 |
no_changes_tree, |
| 1937 |
"no-changes tree was different" |
| 1938 |
); |
| 1939 |
|
| 1940 |
let mut files = files; |
| 1941 |
for file in &mut files { |
| 1942 |
file.toggle_all(); |
| 1943 |
} |
| 1944 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 1945 |
assert_tree_eq!( |
| 1946 |
right_tree, |
| 1947 |
all_changes_tree, |
| 1948 |
"all-changes tree was different", |
| 1949 |
); |
| 1950 |
} |
| 1951 |
|
| 1952 |
#[test] |
| 1953 |
fn test_edit_diff_builtin_replace_directory_with_file() { |
| 1954 |
let test_repo = TestRepo::init(); |
| 1955 |
let store = test_repo.repo.store(); |
| 1956 |
|
| 1957 |
let folder_path = repo_path("folder"); |
| 1958 |
let file_in_folder_path = folder_path.join(repo_path_component("file_in_folder")); |
| 1959 |
let left_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1960 |
builder.file(&file_in_folder_path, vec![]); |
| 1961 |
}); |
| 1962 |
let right_tree = testutils::create_tree_with(&test_repo.repo, |builder| { |
| 1963 |
builder.file(folder_path, vec![]); |
| 1964 |
}); |
| 1965 |
|
| 1966 |
let (changed_files, files) = make_diff(store, &left_tree, &right_tree); |
| 1967 |
insta::assert_debug_snapshot!(changed_files, @r#" |
| 1968 |
[ |
| 1969 |
"folder", |
| 1970 |
"folder/file_in_folder", |
| 1971 |
] |
| 1972 |
"#); |
| 1973 |
insta::with_settings!({filters => vec![(r"\\\\", "/")]}, { |
| 1974 |
insta::assert_debug_snapshot!(files, @r#" |
| 1975 |
[ |
| 1976 |
File { |
| 1977 |
old_path: None, |
| 1978 |
path: "folder", |
| 1979 |
file_mode: Absent, |
| 1980 |
sections: [ |
| 1981 |
FileMode { |
| 1982 |
is_checked: false, |
| 1983 |
mode: Unix( |
| 1984 |
33188, |
| 1985 |
), |
| 1986 |
}, |
| 1987 |
], |
| 1988 |
}, |
| 1989 |
File { |
| 1990 |
old_path: None, |
| 1991 |
path: "folder/file_in_folder", |
| 1992 |
file_mode: Unix( |
| 1993 |
33188, |
| 1994 |
), |
| 1995 |
sections: [ |
| 1996 |
FileMode { |
| 1997 |
is_checked: false, |
| 1998 |
mode: Absent, |
| 1999 |
}, |
| 2000 |
], |
| 2001 |
}, |
| 2002 |
] |
| 2003 |
"#); |
| 2004 |
}); |
| 2005 |
let no_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 2006 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 2007 |
|
| 2008 |
let mut files = files; |
| 2009 |
for file in &mut files { |
| 2010 |
file.toggle_all(); |
| 2011 |
} |
| 2012 |
let all_changes_tree = apply_diff(store, &left_tree, &right_tree, &changed_files, &files); |
| 2013 |
assert_tree_eq!( |
| 2014 |
right_tree, |
| 2015 |
all_changes_tree, |
| 2016 |
"all-changes tree was different", |
| 2017 |
); |
| 2018 |
} |
| 2019 |
|
| 2020 |
#[test] |
| 2021 |
fn test_edit_diff_builtin_with_matcher() -> TestResult { |
| 2022 |
let test_repo = TestRepo::init(); |
| 2023 |
let store = test_repo.repo.store(); |
| 2024 |
|
| 2025 |
let matched_path = repo_path("matched"); |
| 2026 |
let unmatched_path = repo_path("unmatched"); |
| 2027 |
let left_tree = testutils::create_tree( |
| 2028 |
&test_repo.repo, |
| 2029 |
&[ |
| 2030 |
(matched_path, "left matched\n"), |
| 2031 |
(unmatched_path, "left unmatched\n"), |
| 2032 |
], |
| 2033 |
); |
| 2034 |
let right_tree = testutils::create_tree( |
| 2035 |
&test_repo.repo, |
| 2036 |
&[ |
| 2037 |
(matched_path, "right matched\n"), |
| 2038 |
(unmatched_path, "right unmatched\n"), |
| 2039 |
], |
| 2040 |
); |
| 2041 |
|
| 2042 |
let matcher = FilesMatcher::new([matched_path]); |
| 2043 |
|
| 2044 |
let (changed_files, files) = |
| 2045 |
make_diff_with_matcher(store, &left_tree, &right_tree, &matcher); |
| 2046 |
|
| 2047 |
assert_eq!(changed_files, vec![matched_path.to_owned()]); |
| 2048 |
|
| 2049 |
let result_tree = |
| 2050 |
apply_diff_builtin(store, &left_tree, &right_tree, changed_files, &files).block_on()?; |
| 2051 |
|
| 2052 |
assert_eq!( |
| 2053 |
result_tree.path_value(matched_path).block_on()?, |
| 2054 |
left_tree.path_value(matched_path).block_on()? |
| 2055 |
); |
| 2056 |
assert_eq!( |
| 2057 |
result_tree.path_value(unmatched_path).block_on()?, |
| 2058 |
right_tree.path_value(unmatched_path).block_on()? |
| 2059 |
); |
| 2060 |
Ok(()) |
| 2061 |
} |
| 2062 |
|
| 2063 |
#[test] |
| 2064 |
fn test_make_merge_sections() -> TestResult { |
| 2065 |
let test_repo = TestRepo::init(); |
| 2066 |
let store = test_repo.repo.store(); |
| 2067 |
|
| 2068 |
let path = repo_path("file"); |
| 2069 |
let base_tree = testutils::create_tree( |
| 2070 |
&test_repo.repo, |
| 2071 |
&[(path, "base 1\nbase 2\nbase 3\nbase 4\nbase 5\n")], |
| 2072 |
); |
| 2073 |
let left_tree = testutils::create_tree( |
| 2074 |
&test_repo.repo, |
| 2075 |
&[(path, "left 1\nbase 2\nbase 3\nbase 4\nleft 5\n")], |
| 2076 |
); |
| 2077 |
let right_tree = testutils::create_tree( |
| 2078 |
&test_repo.repo, |
| 2079 |
&[(path, "right 1\nbase 2\nbase 3\nbase 4\nright 5\n")], |
| 2080 |
); |
| 2081 |
|
| 2082 |
fn to_file_id(tree_value: MergedTreeValue) -> Option<FileId> { |
| 2083 |
match tree_value.into_resolved() { |
| 2084 |
Ok(Some(TreeValue::File { |
| 2085 |
id, |
| 2086 |
executable: _, |
| 2087 |
copy_id: _, |
| 2088 |
})) => Some(id.clone()), |
| 2089 |
other => { |
| 2090 |
panic!("merge should have been a FileId: {other:?}") |
| 2091 |
} |
| 2092 |
} |
| 2093 |
} |
| 2094 |
|
| 2095 |
let merge = Merge::from_vec(vec![ |
| 2096 |
to_file_id(left_tree.path_value(path).block_on()?), |
| 2097 |
to_file_id(base_tree.path_value(path).block_on()?), |
| 2098 |
to_file_id(right_tree.path_value(path).block_on()?), |
| 2099 |
]); |
| 2100 |
let content = extract_as_single_hunk(&merge, store, path).block_on()?; |
| 2101 |
let merge_result = files::merge_hunks(&content, store.merge_options()); |
| 2102 |
let sections = make_merge_sections(merge_result)?; |
| 2103 |
insta::assert_debug_snapshot!(sections, @r#" |
| 2104 |
[ |
| 2105 |
Changed { |
| 2106 |
lines: [ |
| 2107 |
SectionChangedLine { |
| 2108 |
is_checked: false, |
| 2109 |
change_type: Added, |
| 2110 |
line: "left 1\n", |
| 2111 |
}, |
| 2112 |
SectionChangedLine { |
| 2113 |
is_checked: false, |
| 2114 |
change_type: Removed, |
| 2115 |
line: "base 1\n", |
| 2116 |
}, |
| 2117 |
SectionChangedLine { |
| 2118 |
is_checked: false, |
| 2119 |
change_type: Added, |
| 2120 |
line: "right 1\n", |
| 2121 |
}, |
| 2122 |
], |
| 2123 |
}, |
| 2124 |
Unchanged { |
| 2125 |
lines: [ |
| 2126 |
"base 2\n", |
| 2127 |
"base 3\n", |
| 2128 |
"base 4\n", |
| 2129 |
], |
| 2130 |
}, |
| 2131 |
Changed { |
| 2132 |
lines: [ |
| 2133 |
SectionChangedLine { |
| 2134 |
is_checked: false, |
| 2135 |
change_type: Added, |
| 2136 |
line: "left 5\n", |
| 2137 |
}, |
| 2138 |
SectionChangedLine { |
| 2139 |
is_checked: false, |
| 2140 |
change_type: Removed, |
| 2141 |
line: "base 5\n", |
| 2142 |
}, |
| 2143 |
SectionChangedLine { |
| 2144 |
is_checked: false, |
| 2145 |
change_type: Added, |
| 2146 |
line: "right 5\n", |
| 2147 |
}, |
| 2148 |
], |
| 2149 |
}, |
| 2150 |
] |
| 2151 |
"#); |
| 2152 |
Ok(()) |
| 2153 |
} |
| 2154 |
|
| 2155 |
prop_state_machine! { |
| 2156 |
#[test] |
| 2157 |
fn test_edit_diff_builtin_all_or_nothing_proptest( |
| 2158 |
sequential 1..20 => EditDiffBuiltinAllOrNothingPropTest |
| 2159 |
); |
| 2160 |
|
| 2161 |
#[test] |
| 2162 |
fn test_edit_diff_builtin_partial_selection_proptest( |
| 2163 |
sequential 1..20 => EditDiffBuiltinPartialSelectionPropTest |
| 2164 |
); |
| 2165 |
} |
| 2166 |
|
| 2167 |
/// SUT for property-based test to check that selecting all or none of the |
| 2168 |
/// changes in the diff between two working copy states reproduces the right |
| 2169 |
/// or the left tree, respectively. |
| 2170 |
struct EditDiffBuiltinAllOrNothingPropTest { |
| 2171 |
test_repo: TestRepo, |
| 2172 |
prev_tree: MergedTree, |
| 2173 |
} |
| 2174 |
|
| 2175 |
impl StateMachineTest for EditDiffBuiltinAllOrNothingPropTest { |
| 2176 |
type SystemUnderTest = Self; |
| 2177 |
|
| 2178 |
type Reference = WorkingCopyReferenceStateMachine; |
| 2179 |
|
| 2180 |
fn init_test(ref_state: &Self::Reference) -> Self::SystemUnderTest { |
| 2181 |
let test_repo = TestRepo::init(); |
| 2182 |
let initial_tree = ref_state.create_tree(&test_repo.repo); |
| 2183 |
Self { |
| 2184 |
test_repo, |
| 2185 |
prev_tree: initial_tree, |
| 2186 |
} |
| 2187 |
} |
| 2188 |
|
| 2189 |
fn apply( |
| 2190 |
state: Self::SystemUnderTest, |
| 2191 |
ref_state: &Self::Reference, |
| 2192 |
transition: <Self::Reference as ReferenceStateMachine>::Transition, |
| 2193 |
) -> Self::SystemUnderTest { |
| 2194 |
match transition { |
| 2195 |
Transition::Commit => { |
| 2196 |
let prev_tree = ref_state.create_tree(&state.test_repo.repo); |
| 2197 |
Self { |
| 2198 |
test_repo: state.test_repo, |
| 2199 |
prev_tree, |
| 2200 |
} |
| 2201 |
} |
| 2202 |
|
| 2203 |
Transition::SetDirEntry { .. } => { |
| 2204 |
// Do nothing; this is handled by the reference state machine. |
| 2205 |
state |
| 2206 |
} |
| 2207 |
} |
| 2208 |
} |
| 2209 |
|
| 2210 |
fn check_invariants(state: &Self::SystemUnderTest, ref_state: &Self::Reference) { |
| 2211 |
let store = state.test_repo.repo.store(); |
| 2212 |
let left_tree = &state.prev_tree; |
| 2213 |
let right_tree = ref_state.create_tree(&state.test_repo.repo); |
| 2214 |
|
| 2215 |
let (changed_files, files) = make_diff(store, left_tree, &right_tree); |
| 2216 |
let no_changes_tree = apply_diff(store, left_tree, &right_tree, &changed_files, &files); |
| 2217 |
assert_tree_eq!(left_tree, no_changes_tree, "no-changes tree was different"); |
| 2218 |
|
| 2219 |
let mut files = files; |
| 2220 |
for file in &mut files { |
| 2221 |
file.toggle_all(); |
| 2222 |
} |
| 2223 |
let all_changes_tree = |
| 2224 |
apply_diff(store, left_tree, &right_tree, &changed_files, &files); |
| 2225 |
assert_tree_eq!( |
| 2226 |
right_tree, |
| 2227 |
all_changes_tree, |
| 2228 |
"all-changes tree was different", |
| 2229 |
); |
| 2230 |
} |
| 2231 |
} |
| 2232 |
|
| 2233 |
/// SUT for property-based test to check that after selecting some of the |
| 2234 |
/// changes in a diff, applying the remaining changes to the intermediate |
| 2235 |
/// tree reproduces the right tree. |
| 2236 |
/// |
| 2237 |
/// This "roundtrip" property only holds if none of the selected changes |
| 2238 |
/// implicitly incurs changes that would conflict with the unselected |
| 2239 |
/// ones. An example of this is the deletion of a non-empty file which |
| 2240 |
/// is represented by a file mode change to `Absent` and a text or |
| 2241 |
/// binary change deleting the contents. When only the file mode change |
| 2242 |
/// is selected, the file is still entirely removed. scm_record should |
| 2243 |
/// ensure that selecting the file mode change implies the content |
| 2244 |
/// change, but we cannot rely on this. |
| 2245 |
/// |
| 2246 |
/// Another situation arises when a file, e.g. "a", is replaced by a |
| 2247 |
/// directory containing another file, e.g. "a/b". Selecting the creation of |
| 2248 |
/// "a/b" but not the deletion of "a" will still implicitly replace the file |
| 2249 |
/// "a" directory. scm_record currently does not group or enforce selections |
| 2250 |
/// of changes in a way to prevent this. |
| 2251 |
/// |
| 2252 |
/// This test does not allow selections of changes that violate the |
| 2253 |
/// "roundtrip" property for the above reasons. Otherwise, it sources its |
| 2254 |
/// selection from a bit mask that is part of the reference state and is |
| 2255 |
/// subject to random generation and shrinking but otherwise does not affect |
| 2256 |
/// the state machine's transition, nor is it affected by any of the |
| 2257 |
/// transitions. |
| 2258 |
struct EditDiffBuiltinPartialSelectionPropTest { |
| 2259 |
test_repo: TestRepo, |
| 2260 |
prev_tree: MergedTree, |
| 2261 |
prev_file_list: BTreeSet<RepoPathBuf>, |
| 2262 |
} |
| 2263 |
|
| 2264 |
impl StateMachineTest for EditDiffBuiltinPartialSelectionPropTest { |
| 2265 |
type SystemUnderTest = Self; |
| 2266 |
type Reference = WorkingCopyWithSelectionStateMachine; |
| 2267 |
|
| 2268 |
fn init_test(ref_state: &Self::Reference) -> Self::SystemUnderTest { |
| 2269 |
let test_repo = TestRepo::init(); |
| 2270 |
let initial_tree = ref_state.working_copy.create_tree(&test_repo.repo); |
| 2271 |
Self { |
| 2272 |
test_repo, |
| 2273 |
prev_tree: initial_tree, |
| 2274 |
prev_file_list: BTreeSet::new(), |
| 2275 |
} |
| 2276 |
} |
| 2277 |
|
| 2278 |
fn apply( |
| 2279 |
state: Self::SystemUnderTest, |
| 2280 |
ref_state: &Self::Reference, |
| 2281 |
transition: <Self::Reference as ReferenceStateMachine>::Transition, |
| 2282 |
) -> Self::SystemUnderTest { |
| 2283 |
match transition { |
| 2284 |
Transition::Commit => { |
| 2285 |
let prev_tree = ref_state.working_copy.create_tree(&state.test_repo.repo); |
| 2286 |
let prev_file_list = ref_state |
| 2287 |
.working_copy |
| 2288 |
.paths() |
| 2289 |
.map(ToOwned::to_owned) |
| 2290 |
.collect(); |
| 2291 |
Self { |
| 2292 |
test_repo: state.test_repo, |
| 2293 |
prev_tree, |
| 2294 |
prev_file_list, |
| 2295 |
} |
| 2296 |
} |
| 2297 |
|
| 2298 |
Transition::SetDirEntry { .. } => { |
| 2299 |
// Do nothing; this is handled by the reference state machine. |
| 2300 |
state |
| 2301 |
} |
| 2302 |
} |
| 2303 |
} |
| 2304 |
|
| 2305 |
fn check_invariants(state: &Self::SystemUnderTest, ref_state: &Self::Reference) { |
| 2306 |
let store = state.test_repo.repo.store(); |
| 2307 |
let left_tree = &state.prev_tree; |
| 2308 |
let right_tree = ref_state.working_copy.create_tree(&state.test_repo.repo); |
| 2309 |
|
| 2310 |
let (changed_files, files) = make_diff(store, left_tree, &right_tree); |
| 2311 |
|
| 2312 |
let mut files = files; |
| 2313 |
for (path, file) in changed_files.iter().zip(&mut files) { |
| 2314 |
for (section, selected) in file.sections.iter_mut().zip(&ref_state.selection_mask) { |
| 2315 |
section.set_checked(*selected); |
| 2316 |
} |
| 2317 |
|
| 2318 |
// Sanity checks: Does the partial selection make sense on its own? |
| 2319 |
if let Some(scm_record::Section::FileMode { is_checked, mode }) = |
| 2320 |
file.sections.first() |
| 2321 |
{ |
| 2322 |
let did_file_exist = state.prev_file_list.contains(path); |
| 2323 |
let is_anything_selected = file.sections.iter().any(|sec| match sec { |
| 2324 |
scm_record::Section::FileMode { is_checked, .. } |
| 2325 |
| scm_record::Section::Binary { is_checked, .. } => *is_checked, |
| 2326 |
scm_record::Section::Changed { lines } => { |
| 2327 |
lines.iter().any(|line| line.is_checked) |
| 2328 |
} |
| 2329 |
scm_record::Section::Unchanged { .. } => false, |
| 2330 |
}); |
| 2331 |
|
| 2332 |
if did_file_exist && *mode == scm_record::FileMode::Absent && *is_checked { |
| 2333 |
// File was removed, so all sections need to be checked. |
| 2334 |
file.set_checked(true); |
| 2335 |
} |
| 2336 |
if !did_file_exist && is_anything_selected { |
| 2337 |
// File was created, so if any changes are selected, then so must the file |
| 2338 |
// mode change. |
| 2339 |
file.sections[0].set_checked(true); |
| 2340 |
} |
| 2341 |
} |
| 2342 |
|
| 2343 |
if state |
| 2344 |
.prev_file_list |
| 2345 |
.iter() |
| 2346 |
.any(|f| f.ancestors().skip(1).contains(path.as_ref())) |
| 2347 |
{ |
| 2348 |
// Do not create files which would overwrite directories. |
| 2349 |
file.set_checked(false); |
| 2350 |
} |
| 2351 |
|
| 2352 |
if path |
| 2353 |
.ancestors() |
| 2354 |
.skip(1) |
| 2355 |
.any(|dir| state.prev_file_list.contains(dir)) |
| 2356 |
{ |
| 2357 |
// Do not create files which would create directories overwriting files. |
| 2358 |
file.set_checked(false); |
| 2359 |
} |
| 2360 |
} |
| 2361 |
|
| 2362 |
eprintln!("selected changes: {files:#?}"); |
| 2363 |
|
| 2364 |
let selected_changes_tree = |
| 2365 |
apply_diff(store, left_tree, &right_tree, &changed_files, &files); |
| 2366 |
|
| 2367 |
eprintln!( |
| 2368 |
"selected changes intermediate tree:\n{}", |
| 2369 |
dump_tree(&selected_changes_tree) |
| 2370 |
); |
| 2371 |
|
| 2372 |
// Transform `files` to create the complementary set of changes: |
| 2373 |
for file in &mut files { |
| 2374 |
// If a file mode change was applied, update the base mode. |
| 2375 |
if let Some(scm_record::Section::FileMode { is_checked, mode }) = |
| 2376 |
file.sections.first() |
| 2377 |
&& *is_checked |
| 2378 |
{ |
| 2379 |
file.file_mode = *mode; |
| 2380 |
} |
| 2381 |
|
| 2382 |
// If the file has been renamed, it's now in its new position. |
| 2383 |
file.old_path = None; |
| 2384 |
|
| 2385 |
// Only keep sections which weren't selected previously. For text files, |
| 2386 |
// transform additions which have already been applied into `Unchanged` hunks. |
| 2387 |
file.sections = std::mem::take(&mut file.sections) |
| 2388 |
.into_iter() |
| 2389 |
.flat_map(|sec| { |
| 2390 |
use scm_record::ChangeType::*; |
| 2391 |
use scm_record::Section::*; |
| 2392 |
use scm_record::SectionChangedLine; |
| 2393 |
match sec { |
| 2394 |
Changed { lines } => lines |
| 2395 |
.into_iter() |
| 2396 |
.filter_map(|line_change| match line_change { |
| 2397 |
SectionChangedLine { |
| 2398 |
is_checked: true, |
| 2399 |
change_type: Added, |
| 2400 |
line, |
| 2401 |
} => Some(Unchanged { lines: vec![line] }), |
| 2402 |
SectionChangedLine { |
| 2403 |
is_checked: true, |
| 2404 |
change_type: Removed, |
| 2405 |
line: _, |
| 2406 |
} => None, |
| 2407 |
SectionChangedLine { |
| 2408 |
is_checked: false, .. |
| 2409 |
} => Some(Changed { |
| 2410 |
lines: vec![line_change], |
| 2411 |
}), |
| 2412 |
}) |
| 2413 |
.collect(), |
| 2414 |
|
| 2415 |
Unchanged { .. } |
| 2416 |
| FileMode { |
| 2417 |
is_checked: false, .. |
| 2418 |
} |
| 2419 |
| Binary { |
| 2420 |
is_checked: false, .. |
| 2421 |
} => vec![sec], |
| 2422 |
|
| 2423 |
FileMode { |
| 2424 |
is_checked: true, .. |
| 2425 |
} |
| 2426 |
| Binary { |
| 2427 |
is_checked: true, .. |
| 2428 |
} => { |
| 2429 |
vec![] |
| 2430 |
} |
| 2431 |
} |
| 2432 |
}) |
| 2433 |
.collect(); |
| 2434 |
|
| 2435 |
// We want to select all of the remaining changes this time. |
| 2436 |
file.set_checked(true); |
| 2437 |
} |
| 2438 |
|
| 2439 |
eprintln!("remaining changes: {files:#?}"); |
| 2440 |
|
| 2441 |
let all_changes_tree = apply_diff( |
| 2442 |
store, |
| 2443 |
&selected_changes_tree, |
| 2444 |
&right_tree, |
| 2445 |
&changed_files, |
| 2446 |
&files, |
| 2447 |
); |
| 2448 |
assert_tree_eq!( |
| 2449 |
right_tree, |
| 2450 |
all_changes_tree, |
| 2451 |
"all-changes tree was different", |
| 2452 |
); |
| 2453 |
} |
| 2454 |
} |
| 2455 |
|
| 2456 |
#[derive(Debug, Clone, Default)] |
| 2457 |
struct WorkingCopyWithSelectionStateMachine { |
| 2458 |
working_copy: WorkingCopyReferenceStateMachine, |
| 2459 |
selection_mask: Vec<bool>, |
| 2460 |
} |
| 2461 |
|
| 2462 |
impl ReferenceStateMachine for WorkingCopyWithSelectionStateMachine { |
| 2463 |
type State = Self; |
| 2464 |
type Transition = <WorkingCopyReferenceStateMachine as ReferenceStateMachine>::Transition; |
| 2465 |
|
| 2466 |
fn init_state() -> BoxedStrategy<Self::State> { |
| 2467 |
( |
| 2468 |
WorkingCopyReferenceStateMachine::init_state(), |
| 2469 |
proptest::collection::vec(any::<bool>(), 20), |
| 2470 |
) |
| 2471 |
.prop_map(|(working_copy, selection_mask)| Self { |
| 2472 |
working_copy, |
| 2473 |
selection_mask, |
| 2474 |
}) |
| 2475 |
.boxed() |
| 2476 |
} |
| 2477 |
|
| 2478 |
fn transitions(state: &Self::State) -> BoxedStrategy<Self::Transition> { |
| 2479 |
WorkingCopyReferenceStateMachine::transitions(&state.working_copy) |
| 2480 |
} |
| 2481 |
|
| 2482 |
fn apply(mut state: Self::State, transition: &Self::Transition) -> Self::State { |
| 2483 |
state.working_copy = |
| 2484 |
WorkingCopyReferenceStateMachine::apply(state.working_copy, transition); |
| 2485 |
state |
| 2486 |
} |
| 2487 |
} |
| 2488 |
} |