Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I apply rejected hunks after fixing them?

Tags:

I'm trying to apply a patch to a file using git apply. The overall patch failed, so I used git apply --reject.

Inspecting the generated .rej file showed me what's wrong, now I fixed the problem in the .rej file.

But trying to apply the .rej file fails with message

fatal: patch fragment without header at line 2: ...

Is there a way to re-apply the .rej file after fixing the problems there? Or do I have to modify the original patch and have to re-run git apply?

This would be a bit cumbersome in that case since the original patch contains patches for dozens of files and I don't want to git checkout the applied modifications in order to re-git apply the whole fixed patch file.

like image 762
eckes Avatar asked Jul 26 '13 11:07

eckes


People also ask

Why is a patch rejected?

A common cause of rejects is multiple additions to the beginning or end of something. Often, in these cases, the changes themselves don't really interfere with each other. But the change in text from one patch causes the patch program to be unable to match the context for a change from another patch.

What do I do with .REJ files?

rej files are rejected files (usually hunks patches but not only). When ever you get those files after merge you will manually have to edit them, fix them and then apply them back. Fix it manually and then apply the . rej files like a regular patch.

How do I open a Rej file?

If you cannot open your REJ file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a REJ file directly in the browser: Just drag the file onto this browser window and drop it.


2 Answers

To clarify what @julian-squires said, the problem is that the .rej files are missing some minor stuff between diff a/thefile... and @@ -line/columns....

ORIGINAL .rej file

diff a/the/original/file.cs b/the/original/file.cs    (rejected hunks)
@@ -27,9 +27,9 @@ whatever was on that line

You need to copy the a/b filenames from the diff line and add them with the change indicators below, like:

UPDATED .rej file

diff a/the/original/file.cs b/the/original/file.cs    (rejected hunks)
--- a/the/original/file.cs
+++ b/the/original/file.cs
@@ -27,9 +27,9 @@ whatever was on that line

Then you can apply the .rej files like a regular patch.

like image 198
drzaus Avatar answered Oct 03 '22 05:10

drzaus


I had this problem recently, while using git am --reject to apply a bunch of patches. The way I approached it was to massage the .rej file header into something patch(1) would recognize, with

sed -e 's/^diff a\/\(.*\) b\/\(.*\)[[:space:]].*rejected.*$/--- \1\n+++ \2/'

and modified them with emacs (whose diff-mode will update the line counts in the hunks as you modify the patch) and applied them with patch.

My workflow ended up looking like this:

$ (for i in $(find . -name \*.rej); do
     sed -e 's/^diff a\/\(.*\) b\/\(.*\)[[:space:]].*rejected.*$/--- \1\n+++ \2/' -i $i &&
     emacsclient $i &&
     patch -p0 < $i;
   done) && git add -u && git clean -xdf && git am --continue

with suitable macros setup in emacs for the recurring cases. Not the most elegant approach, but it worked.

like image 32
Julian Squires Avatar answered Oct 03 '22 05:10

Julian Squires