Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with git format-patch/am (patch does not apply)

I am trying to apply a few patches to my repo, and getting message patch does not apply unless I specify params --ignore-space-change --ignore-whitespace. Some patches can't be applied even with these keys, it says there are conflicts to be solved manually. (but actually nothing conflicts there, auto merge must had solved this)

I have done an experiment: created a patch from a commit in my repo, reset master to the previous commit, tried to apply patch from the file. The same error message.

Any ideas, why this might happen?

UPD the commands are very simple:

git format-patch -o ../_patches 0f3bf7874c32b22256ae2d9dc00b1afd6464e43c
git reset --hard 0f3bf7874c32b22256ae2d9dc00b1afd6464e43c
git am ../_patches/0001-test2.patch

(this id refers to the first commit before last)

like image 776
fithu Avatar asked Apr 22 '13 06:04

fithu


People also ask

How do I fix a failed patch?

hint: Use 'git am --show-current-patch' to see the failed patch Patch failed at 0001 b1 Resolve all conflicts manually, mark them as resolved with "git add/rm <conflicted_files>", then run "git rebase --continue". You can instead skip this commit: run "git rebase --skip".

How do I format a patch in git?

The first rule takes precedence in the case of a single <commit>. To apply the second rule, i.e., format everything since the beginning of history up until <commit>, use the --root option: git format-patch --root <commit> . If you want to format only <commit> itself, you can do this with git format-patch -1 <commit> .

How do I manually install a git patch?

To properly apply a Git patch in the terminal, you will need to perform the following steps: Git checkout the associated commit or branch you want the patch applied to. Run the command: git apply <. patch file>

Why are patches skipped?

A patch is usually skipped when the changes it contains were already applied in the past. There are many possible reasons for this: a merge, a cherry-pick, changes operated manually or using another patch etc.


2 Answers

You need to pass the --keep-cr flag to git am. It's unfortunate, but because of competing standards (email workflow versus local), there's really not much choice.

You may want to try setting up a .gitattributes file as well. Trying to recreate your issue, I was able to get things to work when I specified the file as requiring CRLF. Note, without normalizing the files first, it did show the whole file as modified. I commonly use a .gitattributes with this:

.gitattributes  export-ignore
.gitignore      export-ignore

*.txt           text
*.C             text trailing-space space-before-tab -indent-with-non-tab
*.rst           text trailing-space space-before-tab -indent-with-non-tab
*.clj           text trailing-space space-before-tab -indent-with-non-tab

*.c             text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.cpp           text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.h             text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.hpp           text diff=cpp trailing-space space-before-tab -indent-with-non-tab
*.py            text diff=python trailing-space space-before-tab -indent-with-non-tab
*.tex           text diff=tex
*.java          text diff=java trailing-space space-before-tab -indent-with-non-tab
*.pl            text diff=perl trailing-space space-before-tab -indent-with-non-tab
*.php           text diff=php
*.rb            text diff=ruby trailing-space space-before-tab -indent-with-non-tab

*.vcproj        eol=crlf
*.dsp           eol=crlf
*.dsw           eol=crlf

*.sh            eol=lf

*.jpg           binary
*.png           binary
*.gif           binary
*.tiff          binary

You'll want to normalize your line ending according to the gitattributes man page. Another SO user ended up turning off core.autocrlf as well to get clean commits and patches.

Trying to reproduce your bug

$ git init repo
Initialized empty Git repository in c:/tmp/git-eol/repo/.git/
$ cd repo
$ git config --local core.autocrlf false
$ vim foo.txt
$ git add foo.txt
$ git commit -m "Add foo."
[master (root-commit) 3903abd] Add foo.
 1 file changed, 3 insertions(+)
 create mode 100644 foo.txt
$ vim foo.txt
$ git st
## master
 M foo.txt
$ git commit -m "Add more foo." -a
[master 03e991a] Add more foo.
 1 file changed, 2 insertions(+)
$ git format-patch HEAD~1
0001-Add-more-foo.patch
$ vim 0001-Add-more-foo.patch

Looking at the patch file that was produced, I see this:

As you can see, the carriage returns (^Ms) were kept in the patch. This is with core.autocrlf=false. Continuing on, I see:

$ git reset --hard HEAD~1
HEAD is now at 3903abd Add foo.
$ git am 0001-Add-more-foo.patch
Applying: Add more foo.
error: patch failed: foo.txt:1
error: foo.txt: patch does not apply
Patch failed at 0001 Add more foo.
The copy of the patch that failed is found in:
   c:/tmp/git-eol/repo/.git/rebase-apply/patch
When you have resolved this problem, run "git am --resolved".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
$ git am --abort

So the patch doesn't apply well out-of-the-box. Using --keep-cr was as expected though:

$ git am --keep-cr 0001-Add-more-foo.patch
Applying: Add more foo.
$

Okay. So let's try this with core.autocrlf=true (in a different repository):

# Removed the initial commands...
$ git format-patch HEAD~1
0001-Add-more-foo.patch
$ git reset --hard HEAD~1
HEAD is now at 525b5aa Initial commit.
$ git am 0001-Add-more-foo.patch
Applying: Add more foo.
$ git config --get core.autocrlf
true

The patch in this case had LF endings everywhere.

like image 119
John Szakmeister Avatar answered Sep 29 '22 15:09

John Szakmeister


Aha! Looks like I've found the reason. The repository uses CRLF line endings, but format-patch creates patch files with LF's. When I create a repo from scratch with autocrlf=false and make the same experiment, I have the same problem. (looks like a bug in Git)
Any advise how to fix this?

UPD my solution was to create a fresh repository with autocrlf=true and re-import all changes from both repositories into it.

like image 31
fithu Avatar answered Sep 29 '22 14:09

fithu