48

Scenario: I have a patch file that applies cleanly to my working files, but I do not want all the changes from the patch.

Usually, I do vim example.patch, remove unwanted changes and them apply patch -p0 -i example.patch but at times the patch does not apply cleanly, and I have to start over again.

Is there a patch file editor that allows users to edit and delete part of the patch and still can apply cleanly ?

sudo
  • 548
  • 1
  • 4
  • 16
My_patch_question
  • 481
  • 1
  • 4
  • 3
  • 1
    Why not create a second patch? – johnsyweb Sep 28 '11 at 10:34
  • 2
    See http://stackoverflow.com/questions/242646/use-vimdiff-with-a-diff-file – dmedvinsky Sep 28 '11 at 10:44
  • I have need here. I need to edit patch files before applying and there is no way of generating other patch. So: Is there a patch file editor that allows users to edit and/or delete part of the patch and still is able to apply cleanly ? – Peter Nov 05 '12 at 16:44

4 Answers4

30

If you are looking for a non-interactive solution, rediff from patchutils is of help.

Here's its man description:

You can use rediff to correct a hand-edited unified diff. Take a copy of the diff you want to edit, and edit it without changing any offsets or counts (the lines that begin “@@”). Then run rediff, telling it the name of the original diff file and the name of the one you have edited, and it will output the edited diff file but withcorrected offsets and counts.

A small script, editdiff, is provided for editing a diff file in-place.

The types of changes that are currently handled are:

  • Modifying the text of any file content line (of course).
  • Adding new line insertions or deletions.
  • Adding, changing or removing context lines. Lines at the context horizon are dealt with by adjusting the offset and/or count.
  • Adding a single hunk (@@-prefixed section).
  • Removing multiple hunk (@@-prefixed sections).

Based on its description, recountdiff could also be a potential candidate to fix unified diffs.

slv
  • 666
  • 6
  • 12
29

If you open a diff file in emacs and put the editor in "diff" mode you can actually edit patches and it will update the hunk markers in a smart way. Works really well for me!

jkp
  • 78,960
  • 28
  • 103
  • 104
  • 2
    Emacs can also _apply_ the hunks one by one—you need not remove the ones you don’t want. – Davis Herring Aug 25 '18 at 06:21
  • @DavisHerring can you explain how? – young_souvlaki Nov 01 '20 at 19:33
  • @young_souvlaki: `C-c C-a`; `C-u` to reverse-apply. You can also `M-x diff-tell-file-name` to apply them to a different file. – Davis Herring Nov 01 '20 at 21:35
  • This worked well for me. The only issue I encountered was with changes in the middle of a hunk (ie, remove a line delete on line 3 of the hunk but keep the rest), which caused the auto-updating to fail. In order to solve this I need to call `M-x diff-split-hunk` to split the hunk at the line I wanted to remove (in this case line 3 of the hunk). I then was able to remove the line I wanted to from the new split and the auto-updating worked correctly. – Cole Feb 22 '21 at 00:52
  • Good enough, I can `C-x C-s` `C-x C-c` after 2min google. Well, does anyone know if there is a vim equivalent of emacs Diff Mode? – Weekend Feb 11 '22 at 02:06
  • Note: as of latest Emacs, it updates on-the-fly, [which doesn't work well with find-and-replace feature](https://debbugs.gnu.org/cgi/bugreport.cgi?bug=50762), so I recommend disabling it with `(setq diff-update-on-the-fly nil)`. This will make Emacs only update diff headers upon saving the file. – Hi-Angel Aug 04 '22 at 12:35
2

What SCM do you use? if using Git you can:

  • Before generating the actual patch use git add -p to only add parts of your changes. It is good practice to generate smaller commits with only related changes (however some organizations don't like this and only allow a mega commit).

  • If you already have the patch apply it then use git add -p to add the parts of the code you want to keep to your index. You can commit and throw away the rest (git co .) or stash it (git stash).

edit (based on the git add -p comment)

  • git add -p allows you to split a hunk into smaller pieces using the s option, in cases when you need more detail you need to use the e option to edit, that will take you to your gitconfig editor and it will have the instructions on how to edit the hunk.
DavidGamba
  • 3,503
  • 2
  • 30
  • 46
  • I have this issue exactly when using `git add -p` (i.e. in "Manual hunk edit mode") as sometimes it's not possible to split the diff into clean hunks and some manual diff editing is needed. – Jawa Dec 05 '12 at 13:01
  • The problem with `git add -p` is that it only offers small part of the functionality that `git diff` (and its underlying driver) offers. For instance, if I want to make sense of the changes I have made before committing, using `-w` flag to `git diff` to ignore the white-space, that helps me nil if I want to `git add -p` some of these changes interactively, because there is no `-w` for `git add`. What people do is that they generate the patch file with `git diff -w ...` and then use `git apply`, but that again brings them back to a patch editor -- since `git add` is not there to help them. – Armen Michaeli Jul 20 '16 at 13:47
0

Don't edit patch files manually. In your case, you can try some interactive tool to apply your patch hunk by hunk, like ipatch

Lifu Tang
  • 1,271
  • 9
  • 11
  • http://web.archive.org/web/20191210180318/https://www.joachim-breitner.de/blog/425-ipatch,_the_interactive_patch_editor here should be mentioned files still... – Jan Jan 23 '22 at 20:56
  • https://hackage.haskell.org/package/ipatch or what about discussion here https://github.com/microsoft/vscode/issues/43887 and also Meld looks interesting. – Jan Jan 24 '22 at 19:52