Skip to content

Commit 527bb6f

Browse files
authored
Merge pull request #5788 from cakebaker/mv_same_file
mv: show "same file" error for `mv d/f d`
2 parents 2799b28 + 2e4b7c8 commit 527bb6f

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
@@ -314,6 +314,7 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
314314
)
315315
.into());
316316
}
317+
317318
if source.symlink_metadata().is_err() {
318319
return Err(if path_ends_with_terminator(source) {
319320
MvError::CannotStatNotADirectory(source.quote().to_string()).into()
@@ -336,6 +337,13 @@ fn handle_two_paths(source: &Path, target: &Path, opts: &Options) -> UResult<()>
336337
}
337338
}
338339

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

tests/by-util/test_mv.rs

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

408424
#[test]

0 commit comments

Comments
 (0)