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>