mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 06:58:36 +02:00
Make default for mv safer, require -f
to overwrite (#6904)
* fix: ✨ "saner" default for mv fixes #6747 As highlighted in the issue, the default behavior of nu currently is to overwrite the destination file without notice. This is not a "standard" expectation users that want this behavior can create a dedicated alias. * fix: 📝 edit the comment * fix: ✨ updated the tests * fix: 🚧 use --force for case test
This commit is contained in:
@ -50,8 +50,8 @@ impl Command for Mv {
|
||||
"make mv to be verbose, showing files been moved.",
|
||||
Some('v'),
|
||||
)
|
||||
.switch("force", "overwrite the destination.", Some('f'))
|
||||
.switch("interactive", "ask user to confirm action", Some('i'))
|
||||
// .switch("force", "suppress error when no file", Some('f'))
|
||||
.category(Category::FileSystem)
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ impl Command for Mv {
|
||||
let spanned_destination: Spanned<String> = call.req(engine_state, stack, 1)?;
|
||||
let verbose = call.has_flag("verbose");
|
||||
let interactive = call.has_flag("interactive");
|
||||
// let force = call.has_flag("force");
|
||||
let force = call.has_flag("force");
|
||||
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
|
||||
@ -101,12 +101,22 @@ impl Command for Mv {
|
||||
//
|
||||
// First, the destination exists.
|
||||
// - If a directory, move everything into that directory, otherwise
|
||||
// - if only a single source, overwrite the file, otherwise
|
||||
// - error.
|
||||
// - if only a single source, and --force (or -f) is provided overwrite the file,
|
||||
// - otherwise error.
|
||||
//
|
||||
// Second, the destination doesn't exist, so we can only rename a single source. Otherwise
|
||||
// it's an error.
|
||||
|
||||
if destination.exists() && !force && !destination.is_dir() && !source.is_dir() {
|
||||
return Err(ShellError::GenericError(
|
||||
"Destination file already exists".into(),
|
||||
"you can use -f, --force to force overwriting the destination".into(),
|
||||
Some(spanned_destination.span),
|
||||
None,
|
||||
Vec::new(),
|
||||
));
|
||||
}
|
||||
|
||||
if (destination.exists() && !destination.is_dir() && sources.len() > 1)
|
||||
|| (!destination.exists() && sources.len() > 1)
|
||||
{
|
||||
|
Reference in New Issue
Block a user