Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/uu/mv/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ fn assert_not_same_file(
|| source.ends_with("/.")
|| source.is_file())
{
// GNU mv skips the same-file check when it would not replace the
// target anyway (-n, --update=none), so this is a silent no-op.
if (opts.overwrite == OverwriteMode::NoClobber || opts.update == UpdateMode::None)
&& source.file_name().is_some()
{
return Ok(());
}
return Err(MvError::SameFile(source.quote().to_string(), target_display()).into());
} else if (same_file || canonicalized_target.starts_with(canonicalized_source))
// don't error if we're moving a symlink of a directory into itself
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,31 @@ fn test_mv_same_file() {
.stderr_only(format!("mv: '{file_a}' and '{file_a}' are the same file\n"));
}

#[test]
fn test_mv_same_file_no_clobber() {
// GNU mv does not report "are the same file" when it would not
// replace the target anyway; it exits 0 and leaves the file alone.
let file_a = "test_mv_same_file_a";
for args in [["-n"], ["--update=none"]] {
let (at, mut ucmd) = at_and_ucmd!();
at.write(file_a, "a");
ucmd.args(&args)
.arg(file_a)
.arg(file_a)
.succeeds()
.no_output();
assert_eq!(at.read(file_a), "a");
}

// --update=none-fail still fails, like GNU
let (at, mut ucmd) = at_and_ucmd!();
at.touch(file_a);
ucmd.arg("--update=none-fail")
.arg(file_a)
.arg(file_a)
.fails_with_code(1);
}

#[test]
#[cfg(all(unix, not(target_os = "android")))]
#[cfg_attr(
Expand Down
Loading