{"thread":{"id":"66884","subject":"[PATCH] mv: check symlink leading path on force overwrite","startedAt":"2026-11-19T19:22:52Z","lastAt":"2026-11-19T19:22:52Z","messageCount":1,"participants":["Dominik Maier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558877","messageId":"20261119192252.2674835-1-dmnk@google.com","threadId":"66884","inReplyTo":null,"subject":"[PATCH] mv: check symlink leading path on force overwrite","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:22:52Z","receivedAt":"2026-11-19T19:22:52Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"Commit 062d90b2a8 (mv: reject a destination whose leading path is\nmissing or a symlink, 2026-07-30) taught cmd_mv() to reject destinations\nwhose leading path contains a symbolic link via\nhas_symlink_leading_path(). Its \"mv -f\" test used a dangling symlink, so\nlstat(dst, &st) failed with ENOENT and fell through to the new check.\n\nWhen the target file beyond the symlinked directory already exists on\ndisk, lstat(dst, &st) succeeds, \"-f\" clears \"bad = NULL\", and the\nunconditional \"goto act_on_entry\" skips src_for_dst,\nhas_symlink_leading_path(), and the sparse-checkout checks, overwriting\nthe file through the symlink.\n\nOnly jump to act_on_entry when \"bad\" is non-NULL so forced overwrites\nstill run the remaining destination checks.\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n builtin/mv.c  |  3 ++-\n t/t7001-mv.sh | 14 ++++++++++++++\n 2 files changed, 16 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/mv.c b/builtin/mv.c\nindex 373d4aeba3..9c801e9cbb 100644\n--- a/builtin/mv.c\n+++ b/builtin/mv.c\n@@ -440,7 +440,8 @@ int cmd_mv(int argc,\n \t\t\t\t} else\n \t\t\t\t\tbad = _(\"Cannot overwrite\");\n \t\t\t}\n-\t\t\tgoto act_on_entry;\n+\t\t\tif (bad)\n+\t\t\t\tgoto act_on_entry;\n \t\t}\n \t\tif (string_list_has_string(&src_for_dst, dst)) {\n \t\t\tbad = _(\"multiple sources for the same target\");\ndiff --git a/t/t7001-mv.sh b/t/t7001-mv.sh\nindex 719562a118..37815aadd1 100755\n--- a/t/t7001-mv.sh\n+++ b/t/t7001-mv.sh\n@@ -207,6 +207,20 @@ test_expect_success SYMLINKS 'mv -f does not follow a symlinked leading path' '\n \ttest_grep \"destination is beyond a symbolic link\" actual\n '\n \n+test_expect_success SYMLINKS 'mv -f does not overwrite existing target beyond a symlinked leading path' '\n+\tgit reset --hard &&\n+\trm -rf from target_dir &&\n+\tmkdir from target_dir &&\n+\techo contents >from/src &&\n+\techo keep >target_dir/dst &&\n+\tln -s ../target_dir from/link &&\n+\tgit add from/src from/link &&\n+\ttest_must_fail git mv -f from/src from/link/dst 2>actual &&\n+\ttest_grep \"destination is beyond a symbolic link\" actual &&\n+\techo keep >expect &&\n+\ttest_cmp expect target_dir/dst\n+'\n+\n test_expect_success 'mv --dry-run detects non-existent destination parent directory' '\n \tgit reset --hard &&\n \trm -rf from && mkdir from &&\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"}]}