How to edit a diff/patch file cleanly ? Are there any patch file editors?

29,193

Solution 1

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!

Solution 2

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.

Solution 3

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.

Solution 4

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

Share:
29,193

Related videos on Youtube

My_patch_question
Author by

My_patch_question

Updated on July 09, 2022

Comments

  • My_patch_question
    My_patch_question almost 2 years

    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 ?

    • johnsyweb
      johnsyweb over 12 years
      Why not create a second patch?
    • dmedvinsky
      dmedvinsky over 12 years
    • Peter
      Peter over 11 years
      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
    Peter over 11 years
    Yes! Thanks! This does it perfectly!
  • Jawa
    Jawa over 11 years
    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.
  • Armen Michaeli
    Armen Michaeli almost 8 years
    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.
  • Davis Herring
    Davis Herring over 5 years
    Emacs can also apply the hunks one by one—you need not remove the ones you don’t want.
  • young_souvlaki
    young_souvlaki over 3 years
    @DavisHerring can you explain how?
  • Davis Herring
    Davis Herring over 3 years
    @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.
  • Cole
    Cole about 3 years
    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.
  • Tom
    Tom over 2 years
    web.archive.org/web/20191210180318/https://… here should be mentioned files still...
  • Tom
    Tom over 2 years
    hackage.haskell.org/package/ipatch or what about discussion here github.com/microsoft/vscode/issues/43887 and also Meld looks interesting.
  • Weekend
    Weekend over 2 years
    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?