readd update flag to cp command (#10824)

# Description
- this PR should close #10819


# User-Facing Changes
Behaviour is similar to pre 0.86.0 behaviour of the cp command and
should as such not have a user-facing change, only compared to the
current version, were the option is readded.


# After Submitting
I guess the documentation will be automatically updated and as this
feature is no further highlighted, probably, no more work will be needed
here.

# Considerations
coreutils actually allows a third option:
```
pub enum UpdateMode {
    // --update=`all`,
    ReplaceAll,
    // --update=`none`
    ReplaceNone,
    // --update=`older`
    // -u
    ReplaceIfOlder,
}
```
namely `ReplaceNone`, which I have not added. Also I think that
specifying `--update 'abc'` is non functional.
This commit is contained in:
Ludwig Austermann 2023-10-25 11:30:13 +02:00 committed by GitHub
parent f043a8a8ff
commit 8429aec57f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -7,7 +7,7 @@ use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type,
}; };
use std::path::PathBuf; use std::path::PathBuf;
use uu_cp::{BackupMode, UpdateMode}; use uu_cp::{BackupMode, CopyMode, UpdateMode};
// TODO: related to uucore::error::set_exit_code(EXIT_ERR) // TODO: related to uucore::error::set_exit_code(EXIT_ERR)
// const EXIT_ERR: i32 = 1; // const EXIT_ERR: i32 = 1;
@ -46,6 +46,11 @@ impl Command for UCp {
Some('f'), Some('f'),
) )
.switch("interactive", "ask before overwriting files", Some('i')) .switch("interactive", "ask before overwriting files", Some('i'))
.switch(
"update",
"copy only when the SOURCE file is newer than the destination file or when the destination file is missing",
Some('u')
)
.switch("progress", "display a progress bar", Some('p')) .switch("progress", "display a progress bar", Some('p'))
.switch("no-clobber", "do not overwrite an existing file", Some('n')) .switch("no-clobber", "do not overwrite an existing file", Some('n'))
.switch("debug", "explain how a file is copied. Implies -v", None) .switch("debug", "explain how a file is copied. Implies -v", None)
@ -76,6 +81,11 @@ impl Command for UCp {
example: "cp *.txt dir_a", example: "cp *.txt dir_a",
result: None, result: None,
}, },
Example {
description: "Copy only if source file is newer than target file",
example: "cp -u a b",
result: None,
},
] ]
} }
@ -87,6 +97,11 @@ impl Command for UCp {
_input: PipelineData, _input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let interactive = call.has_flag("interactive"); let interactive = call.has_flag("interactive");
let (update, copy_mode) = if call.has_flag("update") {
(UpdateMode::ReplaceIfOlder, CopyMode::Update)
} else {
(UpdateMode::ReplaceAll, CopyMode::Copy)
};
let force = call.has_flag("force"); let force = call.has_flag("force");
let no_clobber = call.has_flag("no-clobber"); let no_clobber = call.has_flag("no-clobber");
let progress = call.has_flag("progress"); let progress = call.has_flag("progress");
@ -198,7 +213,7 @@ impl Command for UCp {
backup: BackupMode::NoBackup, backup: BackupMode::NoBackup,
copy_contents: false, copy_contents: false,
cli_dereference: false, cli_dereference: false,
copy_mode: uu_cp::CopyMode::Copy, copy_mode,
no_target_dir: false, no_target_dir: false,
one_file_system: false, one_file_system: false,
parents: false, parents: false,
@ -207,7 +222,7 @@ impl Command for UCp {
attributes: uu_cp::Attributes::NONE, attributes: uu_cp::Attributes::NONE,
backup_suffix: String::from("~"), backup_suffix: String::from("~"),
target_dir: None, target_dir: None,
update: UpdateMode::ReplaceAll, update,
}; };
if let Err(error) = uu_cp::copy(&sources, &target_path, &options) { if let Err(error) = uu_cp::copy(&sources, &target_path, &options) {

View File

@ -582,7 +582,6 @@ fn copy_file_with_read_permission_impl(progress: bool) {
}); });
} }
#[ignore = "not implemented with ucp"]
#[test] #[test]
fn copy_file_with_update_flag() { fn copy_file_with_update_flag() {
copy_file_with_update_flag_impl(false); copy_file_with_update_flag_impl(false);