git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [BUG] git apply misplaces patch when similar code fragments exist in the same file
@ 2025-09-12 13:33 Guo Tingsheng
  2025-09-12 16:48 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Guo Tingsheng @ 2025-09-12 13:33 UTC (permalink / raw)
  To: git@vger.kernel.org

Hello Git developers,

I would like to report a potential issue in Git's patch application mechanism, where a patch can be incorrectly applied to a similar but unintended code location if line numbers have shifted.

Environment:
- git version: 2.43.0
- OS: Ubuntu 24.04 LTS

Steps to reproduce:
1. Start from Commit_1, containing the following file:

   function HeaderComponent() {
       return `
           <div class="layout-section">
               <button>Click Me</button>
           </div>
       `;
   }

   function FooterComponent() {
       return `
           <div class="layout-section">
               <button>Click Me</button>
           </div>
       `;
   }

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>
        `;
    }

3. Generate a patch from Commit_2.

4. Apply this patch on top of Commit_1 using:
   git apply commit2.patch

Expected result:
- The patch should apply the change to HeaderComponent, modifying its button text.

Actual result:
- The patch is incorrectly applied to FooterComponent instead, producing:

   function HeaderComponent() {
       return `
           <div class="layout-section">
               <button>Click Me</button>
           </div>
       `;
   }

   function FooterComponent() {
       return `
           <div class="layout-section">
               <button>点击</button>
           </div>
       `;
   }

Additional information:
- This issue occurs when a file contains multiple similar code fragments, and the patch context fails to match due to line number shifts between commits.
- As a result, git apply may incorrectly match against the wrong occurrence of the repeated code, introducing unintended changes.
- This can potentially lead to subtle bugs in real-world projects (e.g., configuration files, HTML/JS components).

Thanks,  
Cori

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-13  7:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-12 13:33 [BUG] git apply misplaces patch when similar code fragments exist in the same file Guo Tingsheng
2025-09-12 16:48 ` Junio C Hamano
2025-09-13  7:29   ` 回复: " Guo Tingsheng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).