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 <lucaszam0@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lucas Zamboni Orioli committed Jul 23, 2026 at 13:13 UTC f9f00ae5d778dd30860794938776ab20cad28cc2
2 files changed +35
builtin/mv.c
+21
@@ -444,6 +444,27 @@ dir_check:
444 goto act_on_entry;
445 }
446
447 + /*
448 + * If we are going to move SRC to DST on disk, DST's leading
449 + * directories must already exist.
450 + */
451 + if (!(modes[i] & (INDEX | SPARSE | SKIP_WORKTREE_DIR)) &&
452 + !(dst_mode & (SKIP_WORKTREE_DIR | SPARSE))) {
453 + char *dst_dir = xstrdup(dst);
454 + char *slash = strrchr(dst_dir, '/');
455 +
456 + if (slash) {
457 + struct stat dir_st;
458 + *slash = '\0';
459 + if (lstat(dst_dir, &dir_st) < 0 && errno == ENOENT) {
460 + free(dst_dir);
461 + bad = _("destination directory does not exist");
462 + goto act_on_entry;
463 + }
464 + }
465 + free(dst_dir);
466 + }
467 +
468 if (ignore_sparse &&
469 (dst_mode & (SKIP_WORKTREE_DIR | SPARSE)) &&
470 index_entry_exists(the_repository->index, dst, strlen(dst))) {
t/t7001-mv.sh
+14
@@ -114,6 +114,20 @@ test_expect_success 'clean up' '
114 git reset --hard
115 '
116
117 +test_expect_success 'moving to non-existent destination parent directory' '
118 + git reset --hard &&
119 + mkdir -p from &&
120 + echo content >from/file &&
121 + git add from/file &&
122 + test_must_fail git mv from/file no-such-dir/file 2>actual &&
123 + test_grep "destination directory does not exist" actual
124 +'
125 +
126 +test_expect_success 'mv --dry-run detects non-existent destination parent directory' '
127 + test_must_fail git mv -n from/file no-such-dir/file 2>actual &&
128 + test_grep "destination directory does not exist" actual
129 +'
130 +
131 test_expect_success 'moving to existing untracked target with trailing slash' '
132 mkdir path1 &&
133 git mv path0/ path1/ &&