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

Re: `git diff`/`git apply` can generate/apply ambiguous hunks (ie. in the wrong place) (just like gnu diff/patch)

From
ECEmanuel Czirai <correabuscar+gitml@gmail.com>
Date
Jul 4, 2024, 21:38 UTC
Message-ID
<CAFjaU5vC--bGWBBPP=6YW43sXR6rDgLA7sTL4G81xVeBKsgFrg@mail.gmail.com>
In-Reply-To
<CABPp-BGVdQZCr=0NzY9vpUJqaH+5yxJdpvfUqqhtWB4V=nkwDw@mail.gmail.com>
On Thu, Jul 4, 2024 at 10:08 PM Elijah Newren <newren@gmail.com> wrote:
Show 43 quoted lines
>
> On Wed, Jul 3, 2024 at 8:25 AM Emanuel Czirai
> <correabuscar+gitML@gmail.com> wrote:
> >
> > Subject: `git diff`/`git apply` can generate/apply ambiguous hunks (ie. in the wrong place) (just like gnu diff/patch)
>
> Yes, this is already known.  In fact, it was one of the big reasons we
> changed the default backend in rebase from apply to merge.  From the
> git-rebase manpage:
>
> ```
>    Context
>        The apply backend works by creating a sequence of patches (by calling
>        format-patch internally), and then applying the patches in sequence
>        (calling am internally). Patches are composed of multiple hunks, each
>        with line numbers, a context region, and the actual changes. The line
>        numbers have to be taken with some fuzz, since the other side will
>        likely have inserted or deleted lines earlier in the file. The context
>        region is meant to help find how to adjust the line numbers in order to
>        apply the changes to the right lines. However, if multiple areas of the
>        code have the same surrounding lines of context, the wrong one can be
>        picked. There are real-world cases where this has caused commits to be
>        reapplied incorrectly with no conflicts reported. Setting diff.context
>        to a larger value may prevent such types of problems, but increases the
>        chance of spurious conflicts (since it will require more lines of
>        matching context to apply).
>
>        The merge backend works with a full copy of each relevant file,
>        insulating it from these types of problems.
> ```
>
> > This doesn't affect `git rebase` as it's way more robust than simply
> > extracting the commits as patches and re-applying them. (I haven't looked
> > into `git merge` though, but I doubt it's affected)
>
> This was not always true; and, in fact, rebase is actually still
> partially affected today -- if you pick the `apply` backend or pick
> arguments that imply that backend, then you can still run into this
> problem.  The merge backend (the default) is unaffected, and this
> problem was one of the big reasons for us switching to make the merge
> backend the default instead of the apply backend.
>
> git merge is unaffected.

Thank you very much, I really appreciate knowing this very interesting info! Cheers!

This also tells me that I shouldn't expect a solution for `git diff`/`git apply` any time soon, else it would've been done at that time, I suppose. It's all good, I'll find some kind of workaround for my use case. (probably something based on `diffy` as it's simpler for me to grasp than the C code)

Thanks everyone. All the best!
Previous: Elijah NewrenNext: Junio C Hamano
Message 7 of 8 in “`git diff`/`git apply` can generate/apply ambiguous hunks (ie. in the wrong place) (just like gnu diff/patch)”
  1. Emanuel CziraiJul 3, 2024
  2. rsbecker@nexbridge.comJul 3, 2024
  3. Emanuel Attila CziraiJul 3, 2024
  4. Johannes SixtJul 3, 2024
  5. Emanuel CziraiJul 4, 2024
  6. Elijah NewrenJul 4, 2024
  7. Emanuel CziraiJul 4, 2024
  8. Junio C HamanoJul 6, 2024

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.