SVNKit学习——基于Repository的操作之print repository tree、file content、repository history(四)

  此篇文章同样是参考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
原文地址:https://www.cnblogs.com/douJiangYouTiao888/p/6138660.html