痛苦的开端
近期手头上的项目是fork过来,经常需要merge上游的改动。由于上下游都有很多人同时在开发,因此每次merge都会有很多冲突,面对数十甚至数百个冲突,解决冲突是一件极其消耗脑细胞的事情。
解决冲突无外乎这几种方式:
- 采用上游的改动
- 采用本地的改动
- 同时采用上游和本地的改动
- 自定义的改动
对于前三种方式,很多IDE,例如Intelij IDEA,Visual Studio, Android Studio都有协助工具:

唯独Xcode没有!!!Shame on you!
自己动手,丰衣足食
好在Xcode 8之后,开发者可以开发Xcode Source Editor Extension,可以获取及修改当前文件的内容。
You build extensions to the source editor in Xcode using XcodeKit. Source editor extensions can read and modify the contents of a source file, as well as read and modify the current text selection within the editor.
来看一个冲突的示例:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> their_branch
实现起来也很简单,获取当前光标所在位置,如果被"<<<<<<< HEAD" 和 ">>>>>>>"所包围,即可识别为处于冲突区域。
插件提供三种选择:"Accept Theirs", "Accept Yours", "Keep Both",对应上述常见冲突解决方案。针对不同选择,删除相应代码即可。
成品如下:

独乐乐不如众乐乐
代码已开源,https://github.com/liaojinxing/ConflictResolver
已实现功能:
- 支持三个选项,'Accept Theirs', 'Accept Yours', 和'Keep Both',一键解决冲突,无需手动删除代码
- 支持跳转到下一个冲突的地方
- 支持 git diff3 style
带着这个工具,开始愉快的merge旅程吧!
网友评论