mirror of
https://github.com/nushell/nushell.git
synced 2025-07-07 10:01:26 +02:00
add -u flag to cp, mv command (#9214)
# Description Closes: #7853 I found that I want this feature too... So I take over it, sorry for that @VincenzoCarlino # User-Facing Changes # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
This commit is contained in:
@ -57,6 +57,10 @@ impl Command for Cp {
|
||||
"show successful copies in addition to failed copies (default:false)",
|
||||
Some('v'),
|
||||
)
|
||||
.switch("update",
|
||||
"copy only when the SOURCE file is newer than the destination file or when the destination file is missing",
|
||||
Some('u')
|
||||
)
|
||||
// TODO: add back in additional features
|
||||
// .switch("force", "suppress error when no file", Some('f'))
|
||||
.switch("interactive", "ask user to confirm action", Some('i'))
|
||||
@ -88,6 +92,7 @@ impl Command for Cp {
|
||||
let verbose = call.has_flag("verbose");
|
||||
let interactive = call.has_flag("interactive");
|
||||
let progress = call.has_flag("progress");
|
||||
let update_mode = call.has_flag("update");
|
||||
|
||||
let current_dir_path = current_dir(engine_state, stack)?;
|
||||
let source = current_dir_path.join(src.item.as_str());
|
||||
@ -177,6 +182,12 @@ impl Command for Cp {
|
||||
if src.is_file() {
|
||||
let dst =
|
||||
canonicalize_with(dst.as_path(), ¤t_dir_path).unwrap_or(dst);
|
||||
|
||||
// ignore when source file is not newer than target file
|
||||
if update_mode && super::util::is_older(&src, &dst) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let res = if src == dst {
|
||||
let message = format!(
|
||||
"src {source:?} and dst {destination:?} are identical(not copied)"
|
||||
@ -361,6 +372,11 @@ impl Command for Cp {
|
||||
example: "cp *.txt dir_a",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Copy only if source file is newer than target file",
|
||||
example: "cp -u a b",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -53,6 +53,11 @@ impl Command for Mv {
|
||||
)
|
||||
.switch("force", "overwrite the destination.", Some('f'))
|
||||
.switch("interactive", "ask user to confirm action", Some('i'))
|
||||
.switch("update",
|
||||
"move only when the SOURCE file is newer than the destination file(with -f) or when the destination file is missing",
|
||||
Some('u')
|
||||
)
|
||||
// TODO: add back in additional features
|
||||
.category(Category::FileSystem)
|
||||
}
|
||||
|
||||
@ -75,6 +80,7 @@ impl Command for Mv {
|
||||
let verbose = call.has_flag("verbose");
|
||||
let interactive = call.has_flag("interactive");
|
||||
let force = call.has_flag("force");
|
||||
let update_mode = call.has_flag("update");
|
||||
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
|
||||
@ -191,6 +197,7 @@ impl Command for Mv {
|
||||
span: spanned_destination.span,
|
||||
},
|
||||
interactive,
|
||||
update_mode,
|
||||
);
|
||||
if let Err(error) = result {
|
||||
Some(Value::Error {
|
||||
@ -244,6 +251,7 @@ fn move_file(
|
||||
spanned_from: Spanned<PathBuf>,
|
||||
spanned_to: Spanned<PathBuf>,
|
||||
interactive: bool,
|
||||
update_mode: bool,
|
||||
) -> Result<bool, ShellError> {
|
||||
let Spanned {
|
||||
item: from,
|
||||
@ -305,9 +313,13 @@ fn move_file(
|
||||
}
|
||||
}
|
||||
|
||||
match move_item(&from, from_span, &to) {
|
||||
Ok(()) => Ok(true),
|
||||
Err(e) => Err(e),
|
||||
if update_mode && super::util::is_older(&from, &to) {
|
||||
Ok(false)
|
||||
} else {
|
||||
match move_item(&from, from_span, &to) {
|
||||
Ok(()) => Ok(true),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,3 +132,31 @@ fn get_interactive_confirmation(prompt: String) -> Result<bool, Box<dyn Error>>
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_older(src: &Path, dst: &Path) -> bool {
|
||||
if !dst.exists() {
|
||||
return true;
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let src_ctime = std::fs::metadata(src)
|
||||
.map(|m| m.ctime())
|
||||
.unwrap_or(i64::MIN);
|
||||
let dst_ctime = std::fs::metadata(dst)
|
||||
.map(|m| m.ctime())
|
||||
.unwrap_or(i64::MAX);
|
||||
src_ctime <= dst_ctime
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::fs::MetadataExt;
|
||||
let src_ctime = std::fs::metadata(src)
|
||||
.map(|m| m.last_write_time())
|
||||
.unwrap_or(u64::MIN);
|
||||
let dst_ctime = std::fs::metadata(dst)
|
||||
.map(|m| m.last_write_time())
|
||||
.unwrap_or(u64::MAX);
|
||||
src_ctime <= dst_ctime
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user