# [BUG] Apply patches failed without any changes

2 messages from 2024-11-17 to 2024-11-18. Participants: Ylarod, Jeff King.
Thread: https://gitlist.dev/t/62508

## Ylarod, 2024-11-17 12:40

Subject: [BUG] Apply patches failed without any changes
Message-ID: <CAGtHOOFutv6wHgoFnkZoETJwq6FDkbbBgGqzSXT9V7ESXQswCg@mail.gmail.com>
URL: https://gitlist.dev/e/CAGtHOOFutv6wHgoFnkZoETJwq6FDkbbBgGqzSXT9V7ESXQswCg%40mail.gmail.com

```
Thank you for filling out a Git bug report!
Please answer the following questions to help us understand your issue.

What did you do before the bug happened? (Steps to reproduce your issue)

1. Clone the repo

ˋˋˋbash
git clone https://github.com/Ylarod/frida-core
ˋˋˋ

2. Generate patches from 36a8ffbb to ed039c9f

ed039c9f Florida: memfd-name-jit-cache
f5f61669 Florida: pool-frida
7d4ada2d Florida: update python script
bbe4ebb1 Florida: protocol_unexpected_command
bedd2337 Florida: thread_gmain
e251174c Florida: thread_gum_js_loop
357374cf Florida: symbol_frida_agent_main
da9110f5 Florida: frida_agent_so
351d9c1e Florida: string_frida_rpc
36a8ffbb subprojects: Prepare for release

ˋˋˋbash
git format-patch -N 36a8ffbb
ˋˋˋ

There will be 9 patches:

0001-Florida-string_frida_rpc.patch
0002-Florida-frida_agent_so.patch
0003-Florida-symbol_frida_agent_main.patch
0004-Florida-thread_gum_js_loop.patch
0005-Florida-thread_gmain.patch
0006-Florida-protocol_unexpected_command.patch
0007-Florida-update-python-script.patch
0008-Florida-pool-frida.patch
0009-Florida-memfd-name-jit-cache.patch

3. Apply these patches to 36a8ffbb

ˋˋˋbash
git reset --hard 36a8ffbb

git am 000*
ˋˋˋ

The bug happens:

ˋˋˋ
Applying: Florida: string_frida_rpc
Applying: Florida: frida_agent_so
Applying: Florida: symbol_frida_agent_main
.git/rebase-apply/patch:126: trailing whitespace.

error: patch failed: lib/agent/frida-agent.def:1
error: lib/agent/frida-agent.def: patch does not apply
error: patch failed: tests/labrats/agent.def:1
error: tests/labrats/agent.def: patch does not apply
Patch failed at 0003 Florida: symbol_frida_agent_main
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config advice.mergeConflict false"
ˋˋˋ

What did you expect to happen? (Expected behavior)

Apply these patches correctly

What happened instead? (Actual behavior)

Apply patch failed

What's different between what you expected and what actually happened?

Anything else you want to add:

Please review the rest of the bug report below.
You can delete any lines you don't wish to share.


[System Info]
git version:
git version 2.47.0
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
libcurl: 8.10.1
OpenSSL: OpenSSL 3.3.2 3 Sep 2024
zlib: 1.3.1
uname: Linux 6.11.6-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 01 Nov 2024
03:30:41 +0000 x86_64
compiler info: gnuc: 14.2
libc info: glibc: 2.40
$SHELL (typically, interactive shell): /usr/bin/zsh


[Enabled Hooks]

```

## Jeff King, 2024-11-18 10:15

Subject: Re: [BUG] Apply patches failed without any changes
Message-ID: <20241118101521.GB3991628@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20241118101521.GB3991628%40coredump.intra.peff.net
In-Reply-To: <CAGtHOOFutv6wHgoFnkZoETJwq6FDkbbBgGqzSXT9V7ESXQswCg@mail.gmail.com>

```
On Sun, Nov 17, 2024 at 08:40:06PM +0800, Ylarod wrote:

> ˋˋˋ
> Applying: Florida: string_frida_rpc
> Applying: Florida: frida_agent_so
> Applying: Florida: symbol_frida_agent_main
> .git/rebase-apply/patch:126: trailing whitespace.
> 
> error: patch failed: lib/agent/frida-agent.def:1
> error: lib/agent/frida-agent.def: patch does not apply
> error: patch failed: tests/labrats/agent.def:1
> error: tests/labrats/agent.def: patch does not apply
> Patch failed at 0003 Florida: symbol_frida_agent_main
> hint: Use 'git am --show-current-patch=diff' to see the failed patch
> hint: When you have resolved this problem, run "git am --continue".
> hint: If you prefer to skip this patch, run "git am --skip" instead.
> hint: To restore the original branch and stop patching, run "git am --abort".
> hint: Disable this message with "git config advice.mergeConflict false"
> ˋˋˋ

Your .def files have CRLF line endings:

  $ git show --oneline 357374cfbbcb '*/frida-agent.def' | cat -A
  357374cf Florida: symbol_frida_agent_main$
  diff --git a/lib/agent/frida-agent.def b/lib/agent/frida-agent.def$
  index 7634e3fd..dc297d6f 100644$
  --- a/lib/agent/frida-agent.def$
  +++ b/lib/agent/frida-agent.def$
  @@ -1,2 +1,2 @@$
   EXPORTS^M$
  -^Ifrida_agent_main^M$
  +^Imain^M$

The email format created by format-patch puts the diff inline in a
text/plain email part. And since emails use CRLF as the line terminator,
they are stripped when it is read back in by git-am. So it tries to
apply the change above _without_ the extra CR, and we fail to find the
context.

You can use "git am --keep-cr" to retain the literal line endings on the
diff. This would work for the commands you showed above, but I suspect
would be a problem if you were actually sending the emails to somebody
else (since the mail transport would probably normalize everything to
CRLF).

To robustly send them you'd either have to:

  - send a binary patch. I don't think there's an easy way to ask for
    this for a particular format-patch invocation. You'd probably have
    to mark them with .gitattributes which would also suppress diffs,
    etc.

  - store the files in the repository with regular LF endings. If it's
    important that they have CRLF in the working tree, you can use
    attributes to convert them on checkout. See "End-of-line conversion"
    in "git help attributes".

-Peff

```
