-
Notifications
You must be signed in to change notification settings - Fork 2k
Add hardlink flag to cp #14365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add hardlink flag to cp #14365
Conversation
| .switch("link", "hard-link files instead of copying", Some('l')) | ||
| .switch("symbolic-link", "make symbolic links instead of copying", Some('s')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flag names and descriptions are copied directly from the uutils.cp docs:
https://uutils.github.io/coreutils/docs/utils/cp.html
--link, -l
hard-link files instead of copying
...
--symbolic-link, -s
make symbolic links instead of copying
| let (update, copy_mode) = if call.has_flag(engine_state, stack, "update")? { | ||
| (UpdateMode::ReplaceIfOlder, CopyMode::Update) | ||
| let update = if call.has_flag(engine_state, stack, "update")? { | ||
| UpdateMode::ReplaceIfOlder | ||
| } else { | ||
| (UpdateMode::ReplaceAll, CopyMode::Copy) | ||
| UpdateMode::ReplaceAll | ||
| }; | ||
| let copy_mode = if call.has_flag(engine_state, stack, "link")? { | ||
| CopyMode::Link | ||
| } else if call.has_flag(engine_state, stack, "symbolic-link")? { | ||
| CopyMode::SymLink | ||
| } else if call.has_flag(engine_state, stack, "update")? { | ||
| CopyMode::Update | ||
| } else { | ||
| CopyMode::Copy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to directly port the flag behavior from the uu_cp crate:
https://github.com/uutils/coreutils/blob/7e4ef50c82a7503cba05b97c1c37970b9b4c8346/src/uu/cp/src/cp.rs#L760-L782
impl CopyMode {
fn from_matches(matches: &ArgMatches) -> Self {
if matches.get_flag(options::LINK) {
Self::Link
} else if matches.get_flag(options::SYMBOLIC_LINK) {
Self::SymLink
} else if matches
.get_one::<String>(update_control::arguments::OPT_UPDATE)
.is_some()
|| matches.get_flag(update_control::arguments::OPT_UPDATE_NO_ARG)
{
Self::Update
} else if matches.get_flag(options::ATTRIBUTES_ONLY) {
if matches.get_flag(options::REMOVE_DESTINATION) {
Self::Copy
} else {
Self::AttrOnly
}
} else {
Self::Copy
}
}
}I left out the logic regarding ATTRIBUTES_ONLY and REMOVE_DESTINATION because nushell's cp does not have those flags (yet).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My opinion is that, if nushell's cp continues to implement more flags with passthrough to uutils cp, then we should find a way to try and delegate the above sorts of switch logic to uutils's cp instead of reimplementing it. Copying the switch logic from uutils to nushell's codebase is more error prone and requires writing more tests.
Add flag to
cpbuiltin to enable creating hardlinks.This make use of the
-l/--linksflag from coreutils' cp command.Links:
User-Facing Changes
Should add extra flag to
cp.Default behavior of
cpshould not changeTests + Formatting
Added some unit tests
TODO:
cargo fmt --all -- --checkto check standard code formatting (cargo fmt --allapplies these changes)cargo clippy --workspace -- -D warnings -D clippy::unwrap_usedto check that you're using the standard code stylecargo test --workspaceto check that all tests pass (on Windows make sure to enable developer mode)cargo run -- -c "use toolkit.nu; toolkit test stdlib"to run the tests for the standard library-->
After Submitting