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

2019-03-22 15:36发布

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 ?

4条回答
Evening l夕情丶
2楼-- · 2019-03-22 15:40

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

查看更多
戒情不戒烟
3楼-- · 2019-03-22 15:48

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.
查看更多
孤傲高冷的网名
4楼-- · 2019-03-22 15:57

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!

查看更多
叛逆
5楼-- · 2019-03-22 16:03

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.

查看更多
登录 后发表回答