pr-git-2356/ZamboniL/mv-detect-non-existing-target-folder-v1
tagged this
15 Jul 14:32
From: Lucas Zamboni Orioli <lucaszam0@gmail.com>
When moving a file to a destination whose leading directory does not
exist, "git mv" fails at the rename(2) syscall with ENOENT. Because
the error is reported via die_errno() using only the source path:
fatal: renaming 'src' failed: No such file or directory
the message misleadingly blames the source, even though it is the
destination's parent directory that is missing. A user who runs
git mv a/file b/does-not-exist/file
is told the problem is with 'a/file', which exists, giving no hint
that 'b/does-not-exist/' needs to be created first.
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 rename(2). As a result "git mv -n" also fails to detect the
problem, since the dry run never reaches the syscall and reports a
move that would not actually succeed.
Detect this during the checking phase instead: 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 gives a clear message and lets "git mv -n" report the failure.
Signed-off-by: Lucas Zamboni Orioli <lucaszam0@gmail.com>
Submitted-As: https://lore.kernel.org/git/pull.2356.git.git.1784125963694.gitgitgadget@gmail.com