SVNKit學習——基於Repository的操作之print repository tree、file content、repository history(四)
阿新 • • 發佈:2018-02-10
oge rgs 新版本 val 技術分享 array class and 解析
此篇文章同樣是參考SVNKit在wiki的官方文檔做的demo,每個類都可以單獨運行。具體的細節都寫到註釋裏了~
開發背景:
SVNKit版本:1.7.14 附上官網下載鏈接:https://www.svnkit.com/org.tmatesoft.svn_1.7.14.standalone.zip
jdk版本要求:我試了1.6版本是不行的,1.7版本的jdk沒有問題。
操作:①.在官網下載SVNKit1.7.14後將lib/*.jar全部復制到工程中 ②.導入google的Gson的包,這裏我用的是gson-2.2.4.jar
倉庫目錄結構:
工程結構圖:
具體代碼:
一、顯示svn倉庫的樹結構
package com.demo; import org.tmatesoft.svn.core.SVNDirEntry; import org.tmatesoft.svn.core.SVNNodeKind; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; import org.tmatesoft.svn.core.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; import org.tmatesoft.svn.core.wc.SVNWCUtil; import java.io.File; import java.util.Collection; import java.util.Iterator; /** * 顯示svn倉庫的樹結構 */ public class PrintRepositoryTree { public static void main(String[] args) throws Exception{ //1.根據訪問協議初始化工廠 DAVRepositoryFactory.setup();; //2.初始化倉庫 String url = "https://wlyfree-PC:8443/svn/svnkitRepository1/trunk"; SVNRepository svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); //3.創建一個訪問的權限 String username = "wly"; String password = "wly"; char[] pwd = password.toCharArray(); ISVNAuthenticationManager authenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,pwd); svnRepository.setAuthenticationManager(authenticationManager); /*輸出倉庫的根目錄和UUID*/ System.out.println("Repository Root:" + svnRepository.getRepositoryRoot(true)); System.out.println("Repository UUID:" + svnRepository.getRepositoryUUID(true)); /** * 檢驗某個URL(可以是文件、目錄)是否在倉庫歷史的修訂版本中存在,參數:被檢驗的URL,修訂版本,這裏我們想要打印出目錄樹,所以要求必須是目錄 * SVNNodeKind的枚舉值有以下四種: * SVNNodeKind.NONE 這個node已經丟失(可能是已被刪除) * SVNNodeKind.FILE 文件 * SVNNodeKind.DIR 目錄 * SVNNodeKind.UNKNOW 未知,無法解析 * */ /* * 被檢驗的URL,本例有兩種等價的寫法。 * 1.不是以"/"開頭的是相對於倉庫驅動目錄的相對目錄,即svnRepository的url,在本例中是:空字符串(url目錄是:https://wlyfree-PC:8443/svn/svnkitRepository1/trunk) * 2.以"/"開頭的是相對於svnRepository root目錄的相對目錄,即svnRepository的rootUrl,在本例中是:/trunk(root目錄是https://wlyfree-pc:8443/svn/svnkitRepository1) */ String checkUrl = ""; //修訂版本號,-1代表一個無效的修訂版本號,代表必須是最新的修訂版 long revisionNum = -1; SVNNodeKind svnNodeKind = svnRepository.checkPath(checkUrl,revisionNum); if(svnNodeKind == SVNNodeKind.NONE){ System.err.println("This is no entry at " + checkUrl); System.exit(1); }else if(svnNodeKind == SVNNodeKind.FILE){ System.err.println("The entry at ‘" + checkUrl + "‘ is a file while a directory was expected."); System.exit(1); }else{ System.err.println("SVNNodeKind的值:" + svnNodeKind); } //打印出目錄樹結構 listEntries(svnRepository,checkUrl); //打印最新修訂版的版本號 System.err.println("最新修訂版版本號:" + svnRepository.getLatestRevision()); } private static void listEntries(SVNRepository svnRepository,String path) throws Exception{ System.err.println("path:" + path); Collection entry = svnRepository.getDir(path, -1 ,null,(Collection)null); Iterator iterator = entry.iterator(); while(iterator.hasNext()){ SVNDirEntry svnDirEntry = (SVNDirEntry)iterator.next(); System.out.println("path:" + "/" + (path.equals("") ? "" : path + "/") + svnDirEntry.getName() + ",(author:" + svnDirEntry.getAuthor() + ",revision:" + svnDirEntry.getRevision() + ",date:" + svnDirEntry.getDate() + ")"); if(svnDirEntry.getKind() == SVNNodeKind.DIR){ String tempPath = (path.equals("") ? svnDirEntry.getName() : path + "/" + svnDirEntry.getName()) ; listEntries(svnRepository,tempPath); } } } }
運行效果:
Repository Root:https://wlyfree-pc:8443/svn/svnkitRepository1 Repository UUID:62e76a57-4b9a-d34b-92c0-4551f8669da5 SVNNodeKind的值:dir path: path:test path:/init1.txt,(author:wly,revision:8,date:Tue Nov 29 15:36:47 CST 2016) path:/init2.txt,(author:wly,revision:8,date:Tue Nov 29 15:36:47 CST 2016) path:/test,(author:wly,revision:10,date:Tue Dec 06 13:50:53 CST 2016) path:/test/init11.txt,(author:wly,revision:10,date:Tue Dec 06 13:50:53 CST 2016) path:/test/init22.txt,(author:wly,revision:9,date:Tue Dec 06 12:13:42 CST 2016) path:/test/test2,(author:wly,revision:9,date:Tue Dec 06 12:13:42 CST 2016) path:test/test2 path:/test/test2/init111.txt,(author:wly,revision:9,date:Tue Dec 06 12:13:42 CST 2016) path:/test/test2/init222.txt,(author:wly,revision:9,date:Tue Dec 06 12:13:42 CST 2016) 最新修訂版版本號:10 Process finished with exit code 0
二、打印文件內容
獲取文件的類型,如果文件是二進制文件,則只輸出文件屬性;如果文件是一個文本文件,輸出文件屬性和文件內容
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.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; import org.tmatesoft.svn.core.wc.SVNWCUtil; import java.io.ByteArrayOutputStream; import java.util.Iterator; import java.util.Map; /** * 獲取文件的類型,如果文件是二進制文件,則只輸出文件屬性;如果文件是一個文本文件,輸出文件屬性和文件內容 */ public class PrintFileContent { public static void main(String[] args) throws Exception { //===========================前面幾步和打印樹是一樣的START=================================== //1.根據訪問協議初始化工廠 DAVRepositoryFactory.setup();; //2.初始化倉庫 String url = "https://wlyfree-PC:8443/svn/svnkitRepository1/trunk"; SVNRepository svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); //3.創建一個訪問的權限 String username = "wly"; String password = "wly"; char[] pwd = password.toCharArray(); ISVNAuthenticationManager authenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,pwd); svnRepository.setAuthenticationManager(authenticationManager); //===========================前面幾步和打印樹是一樣的END=================================== //這裏我們要讀取的是其中的一個文件 String filePath = "test/init11.txt"; //修訂版本號,-1代表一個無效的修訂版本號,代表必須是最新的修訂版 long revisionNum = -1; SVNNodeKind svnNodeKind = svnRepository.checkPath(filePath,revisionNum); if(svnNodeKind == SVNNodeKind.NONE){ System.err.println("This is no entry at " + filePath); System.exit(1); }else if(svnNodeKind == SVNNodeKind.DIR){ System.err.println("The entry at ‘" + filePath + "‘ is a directory while a file was expected."); System.exit(1); }else{ System.err.println("SVNNodeKind的值:" + svnNodeKind); } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); SVNProperties svnProperties = new SVNProperties(); //若svnProperties對象非空,使用vnProperties屬性接收文件的屬性 svnRepository.getFile(filePath,-1,svnProperties ,byteArrayOutputStream); /* * 輸出文件屬性 */ System.err.println("文件屬性:"); Map<String,SVNPropertyValue> svnPropertiesMap = svnProperties.asMap(); Iterator<String> it = svnPropertiesMap.keySet().iterator(); while(it.hasNext()){ String key = it.next(); System.err.println(key + " : " + svnPropertiesMap.get(key)); } //序列化看下svnProperrties中的數據 Gson gson = new Gson(); System.err.println(gson.toJson(svnProperties)); /* * 文件是否是文本類型的文件,文本類型文件輸出文件內容 */ System.err.println("文件內容:"); String mimeType = svnProperties.getStringValue(SVNProperty.MIME_TYPE); System.err.println("mimeType is :" + mimeType); boolean isTextType = SVNProperty.isTextMimeType(mimeType); if(isTextType){ System.err.println("The file is a text file,this is contents:"); byteArrayOutputStream.writeTo(System.err); }else{ System.err.println("The file is not a text file,we can‘t read content of it."); } } }
運行效果:
SVNNodeKind的值:file 文件屬性: svn:entry:uuid : 62e76a57-4b9a-d34b-92c0-4551f8669da5 svn:entry:revision : 10 svn:entry:committed-date : 2016-12-06T05:50:53.160008Z svn:wc:ra_dav:version-url : /svn/svnkitRepository1/!svn/ver/10/trunk/test/init11.txt svn:entry:checksum : 8217e71c38f5c42e3fd4e8ac8dc75c4f svn:entry:committed-rev : 10 svn:entry:last-author : wly {"myProperties":{"svn:entry:uuid":{"myValue":"62e76a57-4b9a-d34b-92c0-4551f8669da5"},"svn:entry:revision":{"myValue":"10"},"svn:entry:committed-date":{"myValue":"2016-12-06T05:50:53.160008Z"},"svn:wc:ra_dav:version-url":{"myValue":"/svn/svnkitRepository1/!svn/ver/10/trunk/test/init11.txt"},"svn:entry:checksum":{"myValue":"8217e71c38f5c42e3fd4e8ac8dc75c4f"},"svn:entry:committed-rev":{"myValue":"10"},"svn:entry:last-author":{"myValue":"wly"}}} 文件內容: mimeType is :null The file is a text file,this is contents: init aa bb cc dd 11 22 33 44 Process finished with exit code 0
三、打印歷史記錄
package com.demo; import com.google.gson.Gson; import org.tmatesoft.svn.core.SVNLogEntry; import org.tmatesoft.svn.core.SVNLogEntryPath; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; import org.tmatesoft.svn.core.io.SVNRepository; import org.tmatesoft.svn.core.io.SVNRepositoryFactory; import org.tmatesoft.svn.core.wc.SVNWCUtil; import java.util.Collection; import java.util.Iterator; import java.util.Set; /** * 打印歷史記錄 */ public class PrintRepositoryHistory { public static void main(String[] args) throws Exception{ //===========================前面幾步和打印樹是一樣的START=================================== //1.根據訪問協議初始化工廠 DAVRepositoryFactory.setup();; //2.初始化倉庫 String url = "https://wlyfree-PC:8443/svn/svnkitRepository1/trunk"; SVNRepository svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url)); //3.創建一個訪問的權限 String username = "wly"; String password = "wly"; char[] pwd = password.toCharArray(); ISVNAuthenticationManager authenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,pwd); svnRepository.setAuthenticationManager(authenticationManager); //===========================前面幾步和打印樹是一樣的END=================================== long startVersion = 10; long endVersion = 8; /* * 參數: * 1.接收日誌 * 2.接收history對象,每個修訂版的信息都代表一個SVNLogEntry對象。如果我們不需要傳入一個已經存在的history對象,就傳入null值 * 3.開始修訂版本號,0、-1代表最新版本 * 4.結束修訂版本號,0、-1代表最新版本 * 5.如果需要打印改變路徑的信息,則設置為true。會使用每個SVNLogEntry對象記錄改變路徑的信息getchangedpaths()會返回一個Map<String改變路徑,SVNLogEntryPath> 對象 * 6.strictNode設置為true,復制history的時候不會跳過每個path的修訂版日誌 */ Collection logEntries = svnRepository.log(new String[]{""}, null,8,8,true,true); Gson gson = new Gson(); if(logEntries != null){ Iterator it = logEntries.iterator(); while (it.hasNext()){ SVNLogEntry svnLogEntry = (SVNLogEntry)it.next(); System.err.println("序列化數據:" + gson.toJson(svnLogEntry)); if(svnLogEntry.getChangedPaths().size() > 0){ System.err.println("Change path:"); Set changePathSet = svnLogEntry.getChangedPaths().keySet(); if(changePathSet != null && changePathSet.size() > 0){ for(Iterator changePaths = changePathSet.iterator();changePaths.hasNext();){ SVNLogEntryPath svnLogEntryPath = svnLogEntry.getChangedPaths().get(changePaths.next()); System.err.println(gson.toJson(svnLogEntryPath)); } } } } } } }
運行效果:
序列化數據:{"myRevision":8,"myChangedPaths":{"/trunk/init1.txt":{"myPath":"/trunk/init1.txt","myType":"A","myCopyRevision":-1,"myNodeKind":{"myID":1}},"/trunk/init2.txt":{"myPath":"/trunk/init2.txt","myType":"A","myCopyRevision":-1,"myNodeKind":{"myID":1}}},"myRevisionProperties":{"myProperties":{"svn:log":{"myValue":"初始化導入目錄-myRepository1"},"svn:author":{"myValue":"wly"},"svn:date":{"myValue":"2016-11-29T07:36:47.737654Z"}}},"myHasChildren":false,"myIsSubtractiveMerge":false,"myIsNonInheritable":false} Change path: {"myPath":"/trunk/init1.txt","myType":"A","myCopyRevision":-1,"myNodeKind":{"myID":1}} {"myPath":"/trunk/init2.txt","myType":"A","myCopyRevision":-1,"myNodeKind":{"myID":1}} Process finished with exit code 0
SVNKit學習——基於Repository的操作之print repository tree、file content、repository history(四)