From: Dominik Maier <dmnk@google.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Lucas Zamboni Orioli <lucaszam0@gmail.com>,
Derrick Stolee <stolee@gmail.com>,
Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>,
dmnk+artist@google.com, Dominik Maier <dmnk@google.com>
Subject: [PATCH] mv: check symlink leading path on force overwrite
Date: Thu, 19 Nov 2026 20:22:52 +0100 [thread overview]
Message-ID: <20261119192252.2674835-1-dmnk@google.com> (raw)
Commit 062d90b2a8 (mv: reject a destination whose leading path is
missing or a symlink, 2026-07-30) taught cmd_mv() to reject destinations
whose leading path contains a symbolic link via
has_symlink_leading_path(). Its "mv -f" test used a dangling symlink, so
lstat(dst, &st) failed with ENOENT and fell through to the new check.
When the target file beyond the symlinked directory already exists on
disk, lstat(dst, &st) succeeds, "-f" clears "bad = NULL", and the
unconditional "goto act_on_entry" skips src_for_dst,
has_symlink_leading_path(), and the sparse-checkout checks, overwriting
the file through the symlink.
Only jump to act_on_entry when "bad" is non-NULL so forced overwrites
still run the remaining destination checks.
Signed-off-by: Dominik Maier <dmnk@google.com>
---
builtin/mv.c | 3 ++-
t/t7001-mv.sh | 14 ++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/builtin/mv.c b/builtin/mv.c
index 373d4aeba3..9c801e9cbb 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -440,7 +440,8 @@ int cmd_mv(int argc,
} else
bad = _("Cannot overwrite");
}
- goto act_on_entry;
+ if (bad)
+ goto act_on_entry;
}
if (string_list_has_string(&src_for_dst, dst)) {
bad = _("multiple sources for the same target");
diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh
index 719562a118..37815aadd1 100755
--- a/t/t7001-mv.sh
+++ b/t/t7001-mv.sh
@@ -207,6 +207,20 @@ test_expect_success SYMLINKS 'mv -f does not follow a symlinked leading path' '
test_grep "destination is beyond a symbolic link" actual
'
+test_expect_success SYMLINKS 'mv -f does not overwrite existing target beyond a symlinked leading path' '
+ git reset --hard &&
+ rm -rf from target_dir &&
+ mkdir from target_dir &&
+ echo contents >from/src &&
+ echo keep >target_dir/dst &&
+ ln -s ../target_dir from/link &&
+ git add from/src from/link &&
+ test_must_fail git mv -f from/src from/link/dst 2>actual &&
+ test_grep "destination is beyond a symbolic link" actual &&
+ echo keep >expect &&
+ test_cmp expect target_dir/dst
+'
+
test_expect_success 'mv --dry-run detects non-existent destination parent directory' '
git reset --hard &&
rm -rf from && mkdir from &&
--
2.56.1.750.gf70dc682cf-goog
reply other threads:[~2026-11-19 19:22 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119192252.2674835-1-dmnk@google.com \
--to=dmnk@google.com \
--cc=dmnk+artist@google.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=lucaszam0@gmail.com \
--cc=shaoxuan.yuan02@gmail.com \
--cc=stolee@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox