git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [BUG] git apply misplaces patch when similar code fragments exist in the same file

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 12, 2025, 16:48 UTC
Message-ID
<xmqqfrcrmxan.fsf@gitster.g>
In-Reply-To
<SN6PR03MB428591529FBE9413B427B030AE08A@SN6PR03MB4285.namprd03.prod.outlook.com>
Guo Tingsheng <CoriCraft16@outlook.com> writes:
Show 11 quoted lines
> 2. In another branch, Commit_2 introduces additional import statements before HeaderComponent, shifting its return statement further down (around line 10).  
>    In Commit_2, the button text in HeaderComponent is modified as follows:
>
>    @@ -10,6 +10,6 @@
>         return `
>             <div class="layout-section">
>    -            <button>Click Me</button>
>    +            <button>点击</button>
>             </div>
>         `;
>     }

This hunk shown here may be a fabrication (it only has two pre-context but three post-context lines, which is unusual unless the shorter one is at the end of the file), but in any case, a patch hunk above is applied to a location that has exactly these lines:

        return `
            <div class="layout-section">
                <button>Click Me</button>
            </div>
        `;
    }
that is the closest to line #10.

If there are more than one places in the target file that the preimage (i.e. the context lines that are shown with " " at the beginning, and the preimage lines that are shown with "-" at the beginning) would match, the patch is ambiguous.

It is very much expected, depending on what other changes have happened to the target file since they diverged to make the matching places move from the original place, it would be applied to a "wrong" place by chance, as the preimage does not uniquely identify where the patch hunk should to be applied in such a situation.

You can generate a patch with wider context if you can _anticipate_ the issue (for example, you may _know_ that commit-1 already had multiple lines that match the preimage in the hunk before running "git diff" or "git format-patch") to give it a better chance to be unambiguous, e.g. "git diff -U8". But in general it is impossible to guarantee that your preimage in the hunk will be and stay to be unambiguous. After all, the receiving end can make independent changes that happen to match the preimage the patch is looking for, no matter how wide the context you pick when you generate your patch.

It might be a good starter project for aspiring Git developers to teach "git apply" to notice this situation and warn about it. The tool cannot by definition to always pick the right place to patch, but the tool should be able to recognise a situation where a patch hunk is ambiguous and can apply to multiple places in the target and let you know about it.

Previous: Guo TingshengNext: Guo Tingsheng
Message 2 of 3 in “[BUG] git apply misplaces patch when similar code fragments exist in the same file”
  1. Guo TingshengSep 12, 2025
  2. Junio C HamanoSep 12, 2025
  3. 回复: [BUG] git apply misplaces patch when similar code fragments exist in the same fileGuo Tingsheng, Sep 13, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.