本篇內容是基於SVNKit High-Level API實現的針對Working copy的操作,操作內容與SVN圖形化界面、命令行類似。
High-Level API類圖:

核心思想:
所有操作由各種SVN*Client來管理,而SVNClientManager類封裝了所有的SVN*Client,所以我們通常有兩種做法(更傾向於后一種):
1.可以分別實例化各種SVN*Client類。2.實例化一個SVNClientManager類,實際上在首次請求SVNClient類的時候,它就已經實例化了每個SVN*Client類了。
示例編碼:
package com.demo; import com.google.gson.Gson; import org.tmatesoft.svn.core.*; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; import org.tmatesoft.svn.core.wc.*; import java.io.File; /** * 使用Hign-Level API來操作Working Copy,由於所有的操作比較貼近SVN命令行、客戶端的操作,所以理解起來相對容易一些。 * 對於Working Copy的不同操作被分類封裝到不同的SVN*Client中,SVNClientManager類包含這些SVN*Client,所以通常使用SVNClientManager類可以很方便的管理這些SVN*Client * 所以我們在操作Working Copy時有兩種選擇:1.可以分別實例化各種SVN*Client類。2.實例化一個SVNClientManager類,實際上在首次請求SVNClient類的時候,它就已經實例化了每個SVN*Client類了。 * 具體結構詳見類圖 */ public class ManageWorkingCopy { private static SVNClientManager svnClientManager; private static final Gson gson = new Gson(); public static void main(String[] args) throws Exception{ //1.根據訪問協議初始化工廠 DAVRepositoryFactory.setup(); //2.使用默認選項 ISVNOptions isvnOptions = SVNWCUtil.createDefaultOptions(true); //3.初始化權限 String username = "wly"; String password = "wly"; char[] pwd = password.toCharArray(); ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,pwd); //4.創建SVNClientManager的實例 svnClientManager = SVNClientManager.newInstance(isvnOptions,isvnAuthenticationManager); //=========================================demo start======================================= //1.導入-import // doImport(); //2.檢出-checkout // doCheckout(); //3.創建目錄-mkdir // doMkDir(); //4.提交-commit // doCommit(); //5.更新-update // doUpdate(); //6.切換分支-switch //7.添加-add,在commit中有體現,提交前,沒有加入版本控制的文件需要加入版本控制 //8.鎖定-lock // doLock(); //9.刪除-delete // doDelete(); //10.拷貝-copy // doCopy(); //11.狀態-status // doStatus(); //12.信息-info // doInfo(); } /* * 導入-import * @throws SVNException */ private static void doImport() throws SVNException{ SVNProperties svnProperties = new SVNProperties(); boolean useGlobalIgnores = true; boolean ignoreUnknownNodeTypes = true; SVNCommitInfo svnCommitInfo = svnClientManager.getCommitClient().doImport(new File("E:\\svnWorkspace\\commonProject"),SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk"),"初始化導入",svnProperties,useGlobalIgnores,ignoreUnknownNodeTypes,SVNDepth.INFINITY); System.out.println("執行import操作成功,導入結果:" + gson.toJson(svnCommitInfo)); } /** * 檢出-checkout * 參數: * 倉庫地址 * 本地Working Copy地址 * Peg Revision * Revision * 檢出深度,一般遞歸檢出 * 是否允許沒有版本的障礙物,true的話允許,false不允許,false在checkout的時候如果有障礙物就會停止檢出,所以一般是true * 返回值:long 當前版本號 * @throws SVNException */ private static void doCheckout() throws SVNException{ long nowRevision = svnClientManager.getUpdateClient().doCheckout(SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk"),new File("E:\\svnWorkspace\\projectWorkingCopy"), SVNRevision.HEAD,SVNRevision.HEAD,SVNDepth.INFINITY,true); System.out.println("執行checkout操作成功,當前檢出的版本號是:" + nowRevision); } /** * 創建目錄-mkdir * @throws SVNException */ private static void doMkDir() throws SVNException{ String commitMessage = "創建一個目錄"; SVNCommitInfo svnCommitInfo = svnClientManager.getCommitClient().doMkDir(new SVNURL[]{SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk/aaa"),SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk/bbb")},commitMessage); System.out.println("執行mkdir操作成功,操作結果:" + gson.toJson(svnCommitInfo)); } /** * 提交-commit * commit更改一個文件時,如果文件本身存在,則需要提交其父目錄 * @throws SVNException */ private static void doCommit() throws SVNException{ File[] files = new File[]{new File("E:\\svnWorkspace\\projectWorkingCopy"),new File("E:\\svnWorkspace\\projectWorkingCopy\\ccc")}; //沒有版本號的先執行add操作 for(File tempFile : files){ SVNStatus status = svnClientManager.getStatusClient().doStatus(tempFile,true); System.err.println(status); if(status == null || status.getContentsStatus() == SVNStatusType.STATUS_UNVERSIONED) { System.out.println("文件" + tempFile.getName() + "無版本號"); svnClientManager.getWCClient().doAdd(tempFile, false, false, false, SVNDepth.INFINITY, false, false); } } //執行commit操作 svnClientManager.getCommitClient().setIgnoreExternals(false); SVNProperties svnProperties = new SVNProperties(); String[] changeLists = new String[]{}; SVNCommitInfo svnCommitInfo = svnClientManager.getCommitClient().doCommit(files,false,"提交操作",svnProperties,changeLists,false,false,SVNDepth.fromRecurse(true)); System.out.println("執行commit操作成功,操作結果:" + gson.toJson(svnCommitInfo)); } /** * 更新-update * @throws SVNException */ private static void doUpdate() throws SVNException{ long nowRevision = svnClientManager.getUpdateClient().doUpdate(new File("E:\\svnWorkspace\\projectWorkingCopy"),SVNRevision.HEAD, SVNDepth.INFINITY,true,false); System.out.println("執行update操作成功,當前版本號:" + nowRevision); } /** * 鎖定-lock * @throws SVNException */ private static void doLock() throws SVNException{ // svnClientManager.getWCClient().doLock(new SVNURL[]{SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk/bbb/aa.txt")},true,"給文件加鎖"); svnClientManager.getWCClient().doLock(new File[]{new File("E:\\svnWorkspace\\projectWorkingCopy\\bbb\\aa.txt")},true,"給文件加鎖"); System.out.println("給文件加鎖成功"); } /** * 刪除-delete * @throws SVNException */ private static void doDelete() throws SVNException{ SVNCommitInfo svnCommitInfo = svnClientManager.getCommitClient().doDelete(new SVNURL[]{SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk/bbb"),SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk/b.txt")},"執行刪除操作,刪除一個目錄bbb一個文件b.txt"); System.out.println("執行delete操作成功,操作結果:" + gson.toJson(svnCommitInfo)); } /** * 復制-copy * @throws SVNException */ private static void doCopy() throws SVNException{ SVNCopySource svnCopySource1 = new SVNCopySource(SVNRevision.HEAD,SVNRevision.HEAD,SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk/aaa/aa.txt")); SVNCopySource svnCopySource2 = new SVNCopySource(SVNRevision.HEAD,SVNRevision.HEAD,SVNURL.parseURIEncoded("https://wlyfree-PC:8443/svn/testRepository/trunk/aaa/bb.txt")); svnClientManager.getCopyClient().doCopy(new SVNCopySource[]{svnCopySource1,svnCopySource2},new File("E:\\svnWorkspace\\projectWorkingCopy\\bbb"),false,false,true); System.out.println("執行copy操作成功"); } /** * 狀態-status * @throws SVNException */ private static void doStatus() throws SVNException{ SVNStatus svnStatus = svnClientManager.getStatusClient().doStatus(new File("E:\\svnWorkspace\\projectWorkingCopy\\a.txt"),true); System.out.println("執行status操作成功,操作結果:" + gson.toJson(svnStatus)); } /** * 信息-info * @throws SVNException */ private static void doInfo() throws SVNException{ SVNInfo svnInfo = svnClientManager.getWCClient().doInfo(new File("E:\\svnWorkspace\\projectWorkingCopy\\a.txt"),SVNRevision.HEAD); System.out.println("執行info操作成功,操作結果:" + gson.toJson(svnInfo)); } }
