SVNKit (JavaSVN) 是一个纯 Java 的 SVN 客户端库,使用 SVNKit 无需安装任何 SVN 的客户端,支持各种操作系统。不是开源软件,但是可以免费使用。
其实还有一个众所周知的API JavaHL。特别是在svn相关的一些工具和插件上,两个API都被广泛使用。最经典的就是eclipse IDE上的Subclipse插件。在window->Prepfences->SVN->SVN接口一栏中,就有两个选项可供选择:JavaHL和SVNKIt。这里就不得不将他们之间不得不说的故事了。
直白说,SVNKit就是JavaHL的加强版,一个高阶的API。目前网上有SVNKit开发相关的入门资料《SVNKit开发指南》,这里就不在累赘了。这里只记录下资料内没谈到的冲突解决方案。
在利用SVNKit进行代码update、svn branches switch、merge的时候经常会出现因为小组合作而出现的代码冲突的情况。在默认情况下,SVNUpdateClient、SVNDiffClient在碰到代码update 或者merge冲突的时候,直接跳过中断,并跳过其他文件处理,并不提示任何异常。这样就给使用者造成了极大的困惑。目前提供一个方案解决下这样的尴尬。
以SVNDiffClient进行merge操作为例:
- //获取SVNDiffClient
- SVNDiffClient diffClient = getSVNClientManager(svnModel).getDiffClient();
- diffClient.setIgnoreExternals(false);
- DefaultSVNOptions options = (DefaultSVNOptions) diffClient.getOptions();
- //配置一个 ConflictResolverHandler
- options.setConflictHandler(new ConflictResolverHandler())
ConflictResolverHandler这里模拟SVN在命令行终端的实行方式。当代码merge产生冲突的时候,弹出窗口,让用户选择Select: (p) postpone, (mf) mine-full, (tf) theirs-full 三种不同方式后,再进行merge。
- public class ConflictResolverHandler implements ISVNConflictHandler {
- /*
- * (non-Javadoc)
- *
- * @see
- * org.tmatesoft.svn.core.wc.ISVNConflictHandler#handleConflict(org.tmatesoft
- * .svn.core.wc.SVNConflictDescription)
- */
- @Override
- public SVNConflictResult handleConflict(
- SVNConflictDescription conflictDescription) throws SVNException {
- SVNConflictReason reason = conflictDescription.getConflictReason();
- SVNMergeFileSet mergeFiles = conflictDescription.getMergeFiles();
- System.out.println("Conflict discovered in:" + mergeFiles.getWCFile());
- // System.out.println(reason);
- System.out
- .print("Select: (p) postpone, (mf) mine-full, (tf) theirs-full ");
- SVNConflictChoice choice = SVNConflictChoice.POSTPONE;
- Scanner reader = new Scanner(System.in);
- if (reader.hasNextLine()) {
- String sVNConflictChoice = reader.nextLine();
- if (sVNConflictChoice.equalsIgnoreCase("mf")) {
- choice = SVNConflictChoice.MINE_FULL;
- } else if (sVNConflictChoice.equalsIgnoreCase("tf")) {
- choice = SVNConflictChoice.THEIRS_FULL;
- }
- }
- return new SVNConflictResult(choice, mergeFiles.getResultFile());
- }
- }
自此,SVNKit中的冲突解决问题就借助handler方式顺利解决了。