Skip to content

Commit

Permalink
Move file mode transitions into SelectedContents
Browse files Browse the repository at this point in the history
  • Loading branch information
jgilchrist committed Jan 27, 2025
1 parent c23e319 commit be138a3
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 382 deletions.
107 changes: 55 additions & 52 deletions scm-diff-editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use thiserror::Error;
use walkdir::WalkDir;

use scm_record::helpers::CrosstermInput;
use scm_record::{File, FileMode, RecordError, RecordState, Recorder, SelectedContents};
use scm_record::{File, FileMode, FileState, RecordError, RecordState, Recorder, SelectedContents};

/// Render a partial commit selector for use as a difftool or mergetool.
///
Expand Down Expand Up @@ -440,24 +440,28 @@ fn print_dry_run(write_root: &Path, state: RecordState) {
let file_path = write_root.join(file.path.clone());
let (selected_contents, _unselected_contents) = file.get_selected_contents();
match selected_contents {
SelectedContents::Absent => {
FileState::Absent => {
println!("Would delete file: {}", file_path.display())
}
SelectedContents::Unchanged => {
println!("Would leave file unchanged: {}", file_path.display())
}
SelectedContents::Binary {
old_description,
new_description,
} => {
println!("Would update binary file: {}", file_path.display());
println!(" Old: {:?}", old_description);
println!(" New: {:?}", new_description);
}
SelectedContents::Present { contents } => {
println!("Would update text file: {}", file_path.display());
for line in contents.lines() {
println!(" {line}");
FileState::Present { contents, mode: _ } => {
match contents {
SelectedContents::Unchanged => {
println!("Would leave file unchanged: {}", file_path.display())
}
SelectedContents::Binary {
old_description,
new_description,
} => {
println!("Would update binary file: {}", file_path.display());
println!(" Old: {:?}", old_description);
println!(" New: {:?}", new_description);
}
SelectedContents::Text { contents } => {
println!("Would update text file: {}", file_path.display());
for line in contents.lines() {
println!(" {line}");
}
}
}
}
}
Expand All @@ -483,28 +487,32 @@ pub fn apply_changes(
let file_path = write_root.join(file.path.clone());
let (selected_contents, _unselected_contents) = file.get_selected_contents();
match selected_contents {
SelectedContents::Absent => {
FileState::Absent => {
filesystem.remove_file(&file_path)?;
}
SelectedContents::Unchanged => {
// Do nothing.
}
SelectedContents::Binary {
old_description: _,
new_description: _,
} => {
let new_path = file_path;
let old_path = match &file.old_path {
Some(old_path) => old_path.clone(),
None => Cow::Borrowed(new_path.as_path()),
};
filesystem.copy_file(&old_path, &new_path)?;
}
SelectedContents::Present { contents } => {
if let Some(parent_dir) = file_path.parent() {
filesystem.create_dir_all(parent_dir)?;
},
FileState::Present { contents, mode: _ } => {
match contents {
SelectedContents::Unchanged => {
// Do nothing.
}
SelectedContents::Binary {
old_description: _,
new_description: _,
} => {
let new_path = file_path;
let old_path = match &file.old_path {
Some(old_path) => old_path.clone(),
None => Cow::Borrowed(new_path.as_path()),
};
filesystem.copy_file(&old_path, &new_path)?;
}
SelectedContents::Text { contents } => {
if let Some(parent_dir) = file_path.parent() {
filesystem.create_dir_all(parent_dir)?;
}
filesystem.write_file(&file_path, &contents)?;
}
}
filesystem.write_file(&file_path, &contents)?;
}
}
}
Expand Down Expand Up @@ -680,14 +688,13 @@ qux2
dry_run: false,
},
)?;
assert_debug_snapshot!(files, @r###"
assert_debug_snapshot!(files, @r#"
[
File {
old_path: Some(
"left",
),
path: "right",
file_mode: None,
sections: [
Changed {
lines: [
Expand Down Expand Up @@ -726,7 +733,7 @@ qux2
],
},
]
"###);
"#);

select_all(&mut files);
apply_changes(
Expand Down Expand Up @@ -862,14 +869,13 @@ qux2
dry_run: false,
},
)?;
assert_debug_snapshot!(files, @r###"
assert_debug_snapshot!(files, @r#"
[
File {
old_path: Some(
"left",
),
path: "right",
file_mode: None,
sections: [
Changed {
lines: [
Expand All @@ -883,7 +889,7 @@ qux2
],
},
]
"###);
"#);

select_all(&mut files);
apply_changes(
Expand Down Expand Up @@ -938,14 +944,13 @@ qux2
dry_run: false,
},
)?;
assert_debug_snapshot!(files, @r###"
assert_debug_snapshot!(files, @r#"
[
File {
old_path: Some(
"left",
),
path: "right",
file_mode: None,
sections: [
Changed {
lines: [
Expand All @@ -959,7 +964,7 @@ qux2
],
},
]
"###);
"#);

select_all(&mut files);
apply_changes(
Expand Down Expand Up @@ -1196,14 +1201,13 @@ Hello world 4
output: Some("output".into()),
},
)?;
insta::assert_debug_snapshot!(files, @r###"
insta::assert_debug_snapshot!(files, @r#"
[
File {
old_path: Some(
"base",
),
path: "output",
file_mode: None,
sections: [
Unchanged {
lines: [
Expand Down Expand Up @@ -1238,7 +1242,7 @@ Hello world 4
],
},
]
"###);
"#);

select_all(&mut files);
apply_changes(
Expand Down Expand Up @@ -1329,14 +1333,13 @@ Hello world 2
output: None,
},
)?;
insta::assert_debug_snapshot!(files, @r###"
insta::assert_debug_snapshot!(files, @r#"
[
File {
old_path: Some(
"left",
),
path: "right",
file_mode: None,
sections: [
Changed {
lines: [
Expand All @@ -1355,7 +1358,7 @@ Hello world 2
],
},
]
"###);
"#);

// Select no changes from new file.
apply_changes(
Expand Down
2 changes: 0 additions & 2 deletions scm-diff-editor/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ pub fn create_file(
None
},
path: Cow::Owned(right_display_path),
file_mode: None, // TODO
sections,
})
}
Expand Down Expand Up @@ -211,7 +210,6 @@ pub fn create_merge_file(
Ok(File {
old_path: Some(Cow::Owned(base_path)),
path: Cow::Owned(output_path),
file_mode: None,
sections,
})
}
Expand Down
Loading

0 comments on commit be138a3

Please sign in to comment.