From 7ca4ebea08ed6c5fc093934c1052f6619d636e42 Mon Sep 17 00:00:00 2001 From: Lucas Zamboni Orioli Date: Thu, 23 Jul 2026 13:13:09 +0000 Subject: [PATCH 1/2] mv: name both source and destination when rename fails When "git mv" fails at the rename(2) syscall, the error is reported with die_errno() using only the source path: fatal: renaming 'src' failed: No such file or directory rename(2) returns ENOENT both when the source does not exist and when a directory component of the destination does not exist, and errno does not distinguish the two. Reporting only the source therefore misleads the user in the latter case: for git mv a/file b/no-such-dir/file the message blames 'a/file', which exists, and gives no hint that 'b/no-such-dir/' is the missing part. Inspecting the paths again after the failure to determine which one is at fault would be racy, since either could appear or disappear between the rename(2) and the follow-up check. Instead, simply name both the source and the destination in the message and let the reader see which one is wrong: fatal: renaming 'a/file' to 'b/no-such-dir/file' failed: No such file or directory Signed-off-by: Lucas Zamboni Orioli Signed-off-by: Junio C Hamano --- builtin/mv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/mv.c b/builtin/mv.c index e03823370c..a414f3621f 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -549,7 +549,7 @@ remove_entry: rename(src, dst) < 0) { if (ignore_errors) continue; - die_errno(_("renaming '%s' failed"), src); + die_errno(_("renaming '%s' to '%s' failed"), src, dst); } if (submodule_gitfiles[i]) { if (!update_path_in_gitmodules(src, dst)) From f9f00ae5d778dd30860794938776ab20cad28cc2 Mon Sep 17 00:00:00 2001 From: Lucas Zamboni Orioli Date: Thu, 23 Jul 2026 13:13:10 +0000 Subject: [PATCH 2/2] mv: check for missing destination directory before renaming Moving a file into a directory that does not exist fails at rename(2) with ENOENT. The checking phase already rejects a missing destination directory when the destination ends in a slash, but a destination that names a file inside a non-existent directory is not caught and only fails later at the syscall. As a consequence "git mv -n" does not detect the problem either: the dry run never reaches rename(2) and reports a move that would not actually succeed. Detect this during the checking phase. For entries that will be renamed on disk, stat the destination's leading directory and, if it is missing, fail with the existing "destination directory does not exist" message. Guard the check with the same condition under which rename(2) is invoked, so that directory moves, whose child entries are expanded to paths under a not-yet-created directory, and sparse or out-of-cone destinations, which are not written to the worktree, are not flagged incorrectly. This is a best-effort diagnostic rather than a guarantee: the destination directory can still disappear between the check and the rename(2). It fixes the common case and, unlike the syscall path, lets "git mv -n" report the failure. Add tests covering both the error path and the dry-run detection. Signed-off-by: Lucas Zamboni Orioli Signed-off-by: Junio C Hamano --- builtin/mv.c | 21 +++++++++++++++++++++ t/t7001-mv.sh | 14 ++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/builtin/mv.c b/builtin/mv.c index a414f3621f..ad704dc2d6 100644 --- a/builtin/mv.c +++ b/builtin/mv.c @@ -444,6 +444,27 @@ dir_check: goto act_on_entry; } + /* + * If we are going to move SRC to DST on disk, DST's leading + * directories must already exist. + */ + if (!(modes[i] & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) && + !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE))) { + char *dst_dir = xstrdup(dst); + char *slash = strrchr(dst_dir, '/'); + + if (slash) { + struct stat dir_st; + *slash = '\0'; + if (lstat(dst_dir, &dir_st) < 0 && errno == ENOENT) { + free(dst_dir); + bad = _("destination directory does not exist"); + goto act_on_entry; + } + } + free(dst_dir); + } + if (ignore_sparse && (dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) && index_entry_exists(the_repository->index, dst, strlen(dst))) { diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh index 920479e925..8a45997b33 100755 --- a/t/t7001-mv.sh +++ b/t/t7001-mv.sh @@ -114,6 +114,20 @@ test_expect_success 'clean up' ' git reset --hard ' +test_expect_success 'moving to non-existent destination parent directory' ' + git reset --hard && + mkdir -p from && + echo content >from/file && + git add from/file && + test_must_fail git mv from/file no-such-dir/file 2>actual && + test_grep "destination directory does not exist" actual +' + +test_expect_success 'mv --dry-run detects non-existent destination parent directory' ' + test_must_fail git mv -n from/file no-such-dir/file 2>actual && + test_grep "destination directory does not exist" actual +' + test_expect_success 'moving to existing untracked target with trailing slash' ' mkdir path1 && git mv path0/ path1/ &&