Skip to content

Commit c5a2393

Browse files
committed
mv: show "same file" error for "mv d/f d"
1 parent 2ba9501 commit c5a2393

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/uu/mv/src/mv.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
312312
)
313313
.into());
314314
}
315+
315316
if source.symlink_metadata().is_err() {
316317
return Err(if path_ends_with_terminator(source) {
317318
MvError::CannotStatNotADirectory(source.quote().to_string()).into()
@@ -334,6 +335,13 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
334335
}
335336
}
336337

338+
if source.parent() == Some(target) {
339+
return Err(
340+
// use source twice to match GNU's error message
341+
MvError::SameFile(source.quote().to_string(), source.quote().to_string()).into(),
342+
);
343+
}
344+
337345
let target_is_dir = target.is_dir();
338346

339347
if path_ends_with_terminator(target)

tests/by-util/test_mv.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,23 @@ fn test_mv_same_file() {
403403
ucmd.arg(file_a)
404404
.arg(file_a)
405405
.fails()
406-
.stderr_is(format!("mv: '{file_a}' and '{file_a}' are the same file\n",));
406+
.stderr_is(format!("mv: '{file_a}' and '{file_a}' are the same file\n"));
407+
}
408+
409+
#[test]
410+
fn test_mv_file_to_same_dir() {
411+
let (at, mut ucmd) = at_and_ucmd!();
412+
let file = "a";
413+
let dir = "dir";
414+
let path = &format!("{dir}/{file}");
415+
416+
at.mkdir(dir);
417+
at.touch(path);
418+
419+
ucmd.arg(path)
420+
.arg(dir)
421+
.fails()
422+
.stderr_is(format!("mv: '{path}' and '{path}' are the same file\n"));
407423
}
408424

409425
#[test]

0 commit comments

Comments
 (0)