SVNKit使用冲突解决方案

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操作为例:

Java代码   收藏代码
  1. //获取SVNDiffClient       
  2. SVNDiffClient diffClient = getSVNClientManager(svnModel).getDiffClient();  
  3. diffClient.setIgnoreExternals(false);  
  4. DefaultSVNOptions options = (DefaultSVNOptions) diffClient.getOptions();  
  5. //配置一个 ConflictResolverHandler  
  6. options.setConflictHandler(new ConflictResolverHandler())  

 

ConflictResolverHandler这里模拟SVN在命令行终端的实行方式。当代码merge产生冲突的时候,弹出窗口,让用户选择Select: (p) postpone, (mf) mine-full, (tf) theirs-full 三种不同方式后,再进行merge。

 

Conflictresolverhandler代码   收藏代码
  1. public class ConflictResolverHandler implements ISVNConflictHandler {  
  2.   
  3.     /*  
  4.      * (non-Javadoc)  
  5.      *   
  6.      * @see  
  7.      * org.tmatesoft.svn.core.wc.ISVNConflictHandler#handleConflict(org.tmatesoft  
  8.      * .svn.core.wc.SVNConflictDescription)  
  9.      */  
  10.     @Override  
  11.     public SVNConflictResult handleConflict(  
  12.             SVNConflictDescription conflictDescription) throws SVNException {  
  13.   
  14.         SVNConflictReason reason = conflictDescription.getConflictReason();  
  15.         SVNMergeFileSet mergeFiles = conflictDescription.getMergeFiles();  
  16.   
  17.         System.out.println("Conflict discovered in:" + mergeFiles.getWCFile());  
  18.         // System.out.println(reason);  
  19.         System.out  
  20.                 .print("Select: (p) postpone, (mf) mine-full, (tf) theirs-full     ");  
  21.   
  22.         SVNConflictChoice choice = SVNConflictChoice.POSTPONE;  
  23.   
  24.         Scanner reader = new Scanner(System.in);  
  25.         if (reader.hasNextLine()) {  
  26.             String sVNConflictChoice = reader.nextLine();  
  27.             if (sVNConflictChoice.equalsIgnoreCase("mf")) {  
  28.                 choice = SVNConflictChoice.MINE_FULL;  
  29.             } else if (sVNConflictChoice.equalsIgnoreCase("tf")) {  
  30.                 choice = SVNConflictChoice.THEIRS_FULL;  
  31.             }  
  32.         }  
  33.   
  34.         return new SVNConflictResult(choice, mergeFiles.getResultFile());  
  35.   
  36.     }  
  37. }  

 

自此,SVNKit中的冲突解决问题就借助handler方式顺利解决了。


猜你喜欢

转载自blog.csdn.net/feiren127/article/details/7551659