git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Guo Tingsheng <CoriCraft16@outlook.com>
Cc: "git@vger.kernel.org" <git@vger.kernel.org>
Subject: Re: [BUG] git apply misplaces patch when similar code fragments exist in the same file
Date: Fri, 12 Sep 2025 09:48:48 -0700	[thread overview]
Message-ID: <xmqqfrcrmxan.fsf@gitster.g> (raw)
In-Reply-To: <SN6PR03MB428591529FBE9413B427B030AE08A@SN6PR03MB4285.namprd03.prod.outlook.com> (Guo Tingsheng's message of "Fri, 12 Sep 2025 13:33:20 +0000")

Guo Tingsheng <CoriCraft16@outlook.com> writes:

> 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.


  reply	other threads:[~2025-09-12 16:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2025-09-13  7:29   ` 回复: " Guo Tingsheng

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=xmqqfrcrmxan.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=CoriCraft16@outlook.com \
    --cc=git@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).