简体   繁体   中英

Convert a context diff to unified diff format

I have received a patch in the context diff format, and I need to apply it in Git. As far as I know, Git can only apply patches that are in the unified diff format.

Is there any way to convert a context diff into unified diff format so that I can then git apply the modified patch?

由于git diff只能配置为生成上下文差异 (或者被过滤以生成一个 ),一种可能的简单方法是使用patch手动应用上下文差异,然后使用git add来检测更改。

Here's a solution I've recently found when looking for a solution to the same issue. Using quilt :

dev-util/quilt-0.65::gentoo was built with the following:
USE="-emacs -graphviz" ABI_X86="(64)"

from gentoo, and the following command line session, I was able to painlessly convert a context diff into a unified diff, and adjust the strip level (option -p in patch) from -p0 to -p1 (always use -p1 guys, it will make your and others' lives much easier!)

$ tar xf SDL2-2.0.8.tar.gz
$ cd SDL2-2.0.8
$ quilt new SDL2-2.0.8.unified.patch
$ quilt --quiltrc - fold -p 0 < ../SDL2-2.0.8.context.patch # arbitrary -p0 context diff I created for this exercise
$ quilt refresh
# your new -p1 unified diff can be found at SDL2-2.0.8/patches/SDL2-2.0.8.unified.patch

Answering this here as this is one of the highest results in google for queries related to converting a context diff to a unified one.

Should work in any distro, I'm just reporting exactly what I have for posterity's sake.

Just found a 'better' way, but requires its own sort of prep work. For this you just need the patch file itself. You will require patchutils

dev-util/patchutils-0.3.4::gentoo was built with the following:
USE="-test" ABI_X86="(64)"

$ $EDITOR SDL2-2.0.8.context.patch
# remove all lines like: 1   diff -cr SDL2-2.0.8/src/SDL.c SDL2-2.0.8.new/src/SDL.c (apparently not needed in current git)
# save and quit
$ filterdiff --format=unified < SDL2-2.0.8.context.patch > SDL2-2.0.8.unified.patch
# you can even do this inside vim with
:%!filterdiff --format=unified

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM