{"thread":{"id":"66644","subject":"[BUG] git apply: pure rename followed by edit fails with core.fileMode=false","startedAt":"2026-10-23T13:53:30Z","lastAt":"2026-11-09T08:13:48Z","messageCount":8,"participants":["Hannes Brandstätter-Müller","Aniket Gupta","Junio C Hamano","Patrick Steinhardt"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"556036","messageId":"CANuH12niCO_tU+=dikanZ9Wf30NFsz4jpnZBXW6OyeQfD26jtg@mail.gmail.com","threadId":"66644","inReplyTo":null,"subject":"[BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Hannes Brandstätter-Müller","fromEmail":"hannes.mueller@gmail.com","sentAt":"2026-10-23T13:53:30Z","receivedAt":"2026-10-23T13:53:30Z","isPatch":false,"sender":{"key":"hannes.mueller@gmail.com","avatar":null},"body":"Hi Git developers,\n\nI would like to report a possible regression in git apply and ask\nfor your help with a fix.\n\nWith core.fileMode=false, applying a patch series containing a pure\nrename followed by an edit to the renamed file fails with:\n\n    error: renamed: wrong type\n\nI encountered this while checking a series generated by\ngit format-patch. Here is a small reproduction, to run in a temporary\ndirectory with a Git author identity configured:\n\n    git init -q repro && cd repro\n    git config core.fileMode false\n    printf 'a\\nb\\nc\\n' >file\n    git add file && git commit -q -m base\n    git mv file renamed && git commit -q -m rename\n    sed s/b/B/ renamed >tmp && mv tmp renamed\n    git commit -q -a -m edit\n    git format-patch --stdout HEAD~2 >series.patch\n    git checkout -q HEAD~2\n    git apply --check series.patch\n\nI expected the check to succeed, since the series renames the file\nand then edits it. Instead, it reports the error above. Applying the\npatches separately succeeds, as does checking the combined series\nwith core.fileMode=true.\n\nOur testing reproduced the failure with Git for Windows 2.55.0,\nsource-built v2.45.0, and master at 7e34d8174\n(2.56.0.236.g7e34d8174). Source-built v2.44.0 and Ubuntu's Git 2.34.1\ndid not show the problem.\n\nThe behavior appears related to 0482c32c33 (\"apply: ignore working\ntree filemode when !core.filemode, 2023-12-26\"), although we have not\nconfirmed that with a bisect.\n\nOur reading of check_preimage() suggests that the pure rename has\nno mode information, leaving previous->new_mode as zero. The later\nedit then uses that zero mode and fails the file-type comparison.\n\nThe attached patch is a tentative fix, with a regression test. It\nfalls back to patch->old_mode when previous->new_mode is zero.\nI am not familiar enough with this code to be confident that this\nis the right approach, so I would appreciate a review of both the\ndiagnosis and the proposed change.\n\nWith this change, our tests passed for both a regular file and an\nexecutable file, without a mode warning. The new regression test\nfailed without the fix and passed with it. The t41* and t42* tests\nalso completed with 1,524 passed, no failures, and 12 known\nbreakages.\n\nCould someone please take a look and help get an appropriate fix\nupstream? If I have misunderstood the expected behavior or missed\nan existing fix, I would appreciate being pointed in the right\ndirection.\n\nThank you for your time and for maintaining Git.\n\nBest regards,\nHannes\n\n\ndiff --git a/apply.c b/apply.c\nindex a0b7119c6..ecd489391 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -3873,6 +3873,13 @@ static int check_preimage(struct apply_state *state,\n \t\treturn error(_(\"path %s has been renamed/deleted\"), old_name);\n \tif (previous) {\n \t\tst_mode = previous->new_mode;\n+\t\t/*\n+\t\t * Without core.fileMode a pure rename may not know its mode\n+\t\t * (see below); take the one this patch expects, as we do for\n+\t\t * a path read from the working tree.\n+\t\t */\n+\t\tif (!st_mode)\n+\t\t\tst_mode = patch->old_mode;\n \t} else if (!state->cached) {\n \t\tstat_ret = lstat(old_name, st);\n \t\tif (stat_ret && errno != ENOENT)\ndiff --git a/t/t4129-apply-samemode.sh b/t/t4129-apply-samemode.sh\nindex 1d6317bd7..45dee717a 100755\n--- a/t/t4129-apply-samemode.sh\n+++ b/t/t4129-apply-samemode.sh\n@@ -412,4 +412,27 @@ test_expect_success POSIXPERM 'patch mode for mode change is canonicalized' '\n \ttest_grep \"^-rwxrwxrwx\" worktree\n '\n\n+test_expect_success 'apply a pure rename followed by an edit with core.fileMode=false' '\n+\ttest_when_finished \"rm -rf rename-then-edit series.patch\" &&\n+\tgit init rename-then-edit &&\n+\t(\n+\t\tcd rename-then-edit &&\n+\t\tgit config core.fileMode false &&\n+\t\ttest_write_lines a b c >file &&\n+\t\tgit add file &&\n+\t\ttest_tick && git commit -m base &&\n+\t\tgit mv file renamed &&\n+\t\ttest_tick && git commit -m rename &&\n+\t\ttest_write_lines a B c >renamed &&\n+\t\ttest_tick && git commit -a -m edit &&\n+\t\tgit format-patch --stdout HEAD~2 >../series.patch &&\n+\t\tgit checkout HEAD~2 &&\n+\t\tgit apply --check ../series.patch &&\n+\t\tgit apply ../series.patch &&\n+\t\ttest_write_lines a B c >expect &&\n+\t\ttest_cmp expect renamed &&\n+\t\ttest_path_is_missing file\n+\t)\n+'\n+\n test_done\n"},{"id":"557721","messageId":"20261106181145.1070831-1-aniketgupta3001@gmail.com","threadId":"66644","inReplyTo":"CANuH12niCO_tU+=dikanZ9Wf30NFsz4jpnZBXW6OyeQfD26jtg@mail.gmail.com","subject":"Re: [BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-06T18:11:45Z","receivedAt":"2026-11-06T18:11:45Z","isPatch":false,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Hi Hannes,\n\nI was able to repro this. One thing I noticed: taking the whole mode\nfrom patch->old_mode also takes the file type, so the \"wrong type\"\ncheck can no longer fail. With a pure rename of a regular file followed\nby a hand-made patch that says it is a symlink, core.fileMode=true\nrejects it but =false applies it and turns the file into a symlink.\n\nWould S_IFREG | (patch->old_mode & 07777) be better? That passed your\ntest and the other apply tests for me.\n\nThanks,\nAniket\n\n"},{"id":"557734","messageId":"xmqqwlqpg0js.fsf@gitster.g","threadId":"66644","inReplyTo":"20261106181145.1070831-1-aniketgupta3001@gmail.com","subject":"Re: [BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-06T20:36:07Z","receivedAt":"2026-11-06T20:36:07Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Aniket Gupta <aniketgupta3001@gmail.com> writes:\n\n> I was able to repro this. One thing I noticed: taking the whole mode\n> from patch->old_mode also takes the file type, so the \"wrong type\"\n> check can no longer fail. With a pure rename of a regular file followed\n> by a hand-made patch that says it is a symlink, core.fileMode=true\n> rejects it but =false applies it and turns the file into a symlink.\n\nWhy does the input patch talk about exactly the same path twice in\nthe first place, though?  Are we talking about a valid patch input,\nor are we bending the tool to cope better with corrupt input?\n\n> Would S_IFREG | (patch->old_mode & 07777) be better? That passed your\n> test and the other apply tests for me.\n\nWould it work equally well when applying the patch in reverse?\n\n"},{"id":"557763","messageId":"20261107040715.436-1-aniketgupta3001@gmail.com","threadId":"66644","inReplyTo":"xmqqwlqpg0js.fsf@gitster.g","subject":"Re: [BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-07T04:07:15Z","receivedAt":"2026-11-07T04:07:15Z","isPatch":false,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Hi Junio,\n\nThe series in Hannes's report is valid input which is what format-patch\nmakes for a commit that renames a file followed by one which edits it.\nMy symlink example was hand made and not valid. I only wanted to\ndemonstrate that the type check no longer fires. This is a minor point.\n\nI also tried undoing the series with -R (the edit first, then the\nrename) for a plain and an exe file. It works the same with and without\nthe change.\n\nThanks,\nAniket\n\n"},{"id":"557767","messageId":"xmqqfqxd9pru.fsf@gitster.g","threadId":"66644","inReplyTo":"20261107040715.436-1-aniketgupta3001@gmail.com","subject":"Re: [BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-07T05:25:25Z","receivedAt":"2026-11-07T05:25:25Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Aniket Gupta <aniketgupta3001@gmail.com> writes:\n\n> The series in Hannes's report is valid input which is what format-patch\n> makes for a commit that renames a file followed by one which edits it.\n\nDo you mean these are two commits, and format-patch created two\nmessages?\n\nIf so, applying these two patches in one invocation of \"git apply\"\nis exactly what I was questioning.  I would understand two separate\ninvocations of \"git apply\", or \"git am\" fed two input files, though.\n\n"},{"id":"557768","messageId":"20261107054701.298961-1-aniketgupta3001@gmail.com","threadId":"66644","inReplyTo":"xmqqfqxd9pru.fsf@gitster.g","subject":"Re: [BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-07T05:47:01Z","receivedAt":"2026-11-07T05:47:01Z","isPatch":false,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Hey Junio. Yup. Two commits. Hannes feeds the whole format-patch output\nto a single git apply and separate invocations and git am work for him.\nThough applying several patches that touch the same path in one\ninvocation is something git apply does on purpose. A pure rename\nfollowed by an edit worked that way before 0482c32c33 and still does\nwith core.fileMode=true. It only breaks with =false.\n\nThanks,\nAniket\n\n"},{"id":"557886","messageId":"avF86vESF26lpNv2@pks.im","threadId":"66644","inReplyTo":"20261107054701.298961-1-aniketgupta3001@gmail.com","subject":"Re: [BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-11-09T07:57:30Z","receivedAt":"2026-11-09T07:57:30Z","isPatch":false,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"Hi Aniket,\n\nOn Sat, Nov 07, 2026 at 05:47:01AM +0000, Aniket Gupta wrote:\n> Hey Junio. Yup. Two commits. Hannes feeds the whole format-patch output\n> to a single git apply and separate invocations and git am work for him.\n> Though applying several patches that touch the same path in one\n> invocation is something git apply does on purpose. A pure rename\n> followed by an edit worked that way before 0482c32c33 and still does\n> with core.fileMode=true. It only breaks with =false.\n\none ask from my side for mailing list etiquette: could you please keep\nthe relevant parts of the mail that you're replying to? Otherwise it\nbecomes very hard to follow the discussion as one has no idea what parts\nof the mail you reply to specifically.\n\nThanks!\n\nPatrick\n\n"},{"id":"557897","messageId":"20261109081348.352831-1-aniketgupta3001@gmail.com","threadId":"66644","inReplyTo":"avF86vESF26lpNv2@pks.im","subject":"Re: [BUG] git apply: pure rename followed by edit fails with core.fileMode=false","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-09T08:13:48Z","receivedAt":"2026-11-09T08:13:48Z","isPatch":false,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"> could you please keep the relevant parts of the mail that you're\n> replying to?\n\nMy bad. I'll take care of this from now on.\n\n"}]}