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

Why `git am -3` apply patches that don't normally apply?

From
Yuri Kanivetsky <yuri.kanivetsky@gmail.com>
Date
Jul 3, 2022, 12:11 UTC
Message-ID
<CAMhVC3Z92hBsK0FbE4E_LV1CSxjR1zmu+MO8=iwR02BKqnwgUA@mail.gmail.com>
Hi,

From what I can see w/o `-3` `git am` follows the `patch`'es behavior. And the `patch`'es behavior is if there are lines in a patch that doesn't match the lines in the source file, it fails. For example, a source file:

11 2 3

A patch:
 1
 2
-3
+33

But `git am -3` will apply such a patch. That is, `patch` sees that the first line changed and that prevents it from applying the patch, but `git am -3` decides that it's okay. Why is that?

A script that reproduces the issue:
    set -eux
    mkdir a
    (cd a
    git init
    echo '1
    2
    3' > a
    git add a
    git commit -m 1,2,3
    sed -Ei 's/3/33/' a
    git add a
    git commit -m '3 -> 33'
    git format-patch -1 HEAD)
    mkdir b
    (cd b
    git init
    echo '11
    2
    3' > a
    git add a
    git commit -m 11,2,3
    git remote add a ../a
    git fetch a
    cat a
    cat ../a/0001-3-33.patch
    git --no-pager log --oneline --graph --all
    git am "$@" ../a/0001-3-33.patch  # try it w/ and w/o -3
    git --no-pager log --oneline --graph --all)

A couple of links, just in case, which have more information and show where it comes from:

https://stackoverflow.com/a/25847019/52499 https://gist.github.com/x-yuri/9907d5b9cbf29e84d510902a776741df https://gist.github.com/x-yuri/57d08f6afb10a89856d6e6a62abe30d4

Regards, Yuri

Next: Elijah Newren
Message 1 of 4 in “Why `git am -3` apply patches that don't normally apply?”
  1. Yuri KanivetskyJul 3, 2022
  2. Elijah NewrenJul 4, 2022
  3. Junio C HamanoJul 5, 2022
  4. Yuri KanivetskyJul 6, 2022

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.