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

心已入冬 提交于 2019-12-03 01:12:10

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!

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.

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!