diff+patch用法小试

背景

经常碰到使用开源工具的问题,比如SublimeText的插件,一些小需求,只有自己用到,做了一些修改,得小心翼翼记录下来,以防下次安装时出现各种蛋疼的问题.

今天尝试一下使用diff+patch来处理这个问题

diff

diff可以比较文件/目录,并记录区别,基本用法: > diff [选项] 源文件(夹) 目的文件(夹)

patch

patch的作用则是将diff记录的结果(即补丁)应用到相应文件(夹)上。最常见的用法为: > patch -pNUM

使用

给OmniMarkupPreviewer打个补丁

# Sublime3DIR=~/Library/Application\ Support/Sublime\ Text\ 3

# 制作文件补丁
diff $(Sublime3DIR)/Packages/OmniMarkupPreviewer/OmniMarkupLib/RendererManager.py ./RendererManager.py > RendererManager.py.patch
# 应用文件补丁
patch $(Sublime3DIR)/Packages/OmniMarkupPreviewer/OmniMarkupLib/RendererManager.py < RendererManager.py.patch
# 取消文件补丁
patch $(Sublime3DIR)/Packages/OmniMarkupPreviewer/OmniMarkupLib/RendererManager.py < RendererManager.py.patch -R

# 制作文件夹补丁
diff $(Sublime3DIR)/Packages/OmniMarkupPreviewer $(DIR)/SublimeText3/OmniMarkupPreviewer  > $(DIR)/SublimeText3/Patch/OmniMarkupPreviewer.patch -rupN
# 应用文件夹补丁
patch -d $(Sublime3DIR)/Packages/OmniMarkupPreviewer -p0 < $(DIR)/SublimeText3/Patch/OmniMarkupPreviewer.patch
# 取消文件夹补丁
patch -d $(Sublime3DIR)/Packages/OmniMarkupPreviewer -p0 < $(DIR)/SublimeText3/Patch/OmniMarkupPreviewer.patch -R

参考

补丁(patch)的制作与应用