SVNkit - 基本使用


一、获取指定版本的文件内容

package com.xxx.patchgen.svn;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
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.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.ISVNDiffStatusHandler;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNDiffStatus;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatusType;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class SVNOperator {
	private String userName;
	private String passwd;
	private SVNClientManager ourClientManager;
	
	private SVNWCClient wcClient;
	private SVNDiffClient diffClient;
	private SVNUpdateClient updateClient;
	private List<SVNDiffStatus> changedFilePathsList = new ArrayList<SVNDiffStatus>();
	
	public SVNOperator(String userName, String passwd) {
		//setup
		DAVRepositoryFactory.setup();
		SVNRepositoryFactoryImpl.setup();
		FSRepositoryFactory.setup();
		//create clientManager
		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		ourClientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, passwd);
		this.userName = userName;
		this.passwd = passwd;
		//generate client
		wcClient = ourClientManager.getWCClient();
		diffClient = ourClientManager.getDiffClient();
		updateClient = ourClientManager.getUpdateClient();
	}

	/**
	 * 获取指定版本的文件并转换为字符串
	 */
	public String getRevisionFileContent(String url,SVNRevision revision) throws Exception {
		SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
		OutputStream contentStream = new ByteArrayOutputStream();
		wcClient.doGetFileContents(repositoryUrl, SVNRevision.HEAD, revision, false, contentStream);
		return contentStream.toString();
	}
	/**
	 * 获取指定版本的文件
	 */
	public File getRevisionFile(String url,String version) throws Exception {
		SVNRevision revision = SVNRevision.create(Long.parseLong(version));
		File file = File.createTempFile("patch-", ".tmp");
		SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
		OutputStream contentStream = new FileOutputStream(file);
		wcClient.doGetFileContents(repositoryUrl, SVNRevision.HEAD, revision, false, contentStream);
		return file;
	}

	/**
	 * 获取版本间差异文件
	 */
	public void getDiffFiles(String url,String startVersion,String endVersion) throws Exception{
		changedFilePathsList.clear();
		SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
//		authentication(repositoryUrl);
		SVNRevision start = SVNRevision.create(Long.parseLong(startVersion));
		SVNRevision end = SVNRevision.create(Long.parseLong(endVersion));
		diffClient.doDiffStatus(repositoryUrl,start,repositoryUrl,end,SVNDepth.INFINITY,false,new ISVNDiffStatusHandler() {
			public void handleDiffStatus(SVNDiffStatus status) throws SVNException {
				if(status.getKind() == SVNNodeKind.FILE
						&& (status.getModificationType() == SVNStatusType.STATUS_ADDED
								|| status.getModificationType() == SVNStatusType.STATUS_MODIFIED)){
					changedFilePathsList.add(status);
					System.out.println(status.getPath());
				}
			}
		});
	}
	
	/**
	 * 导出指定版本间差异文件
	 */
	public void doExport(String endVersion,String exportDir) throws Exception{
		SVNRevision end = SVNRevision.create(Long.parseLong(endVersion));
		for(SVNDiffStatus status: changedFilePathsList){
			File destination = new File(exportDir + "/" +status.getPath());
			updateClient.doExport(status.getURL(), destination, end, end, null, true, SVNDepth.getInfinityOrEmptyDepth(true));
		}
	}
	
	
	/**
	 * 导出最新版本文件
	 */
	public void doExportHead(String url,String exportDir) throws Exception{
		SVNURL repositoryUrl = SVNURL.parseURIEncoded(url);
		updateClient.doExport(repositoryUrl, new File(exportDir), SVNRevision.HEAD, SVNRevision.HEAD, null, true, SVNDepth.getInfinityOrEmptyDepth(true));
	}
	
	/**
	 * 认证
	 */
	public void authentication(SVNURL url) throws Exception{
		ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, passwd);
		SVNRepository repository = SVNRepositoryFactory.create(url);
		repository.setAuthenticationManager(authManager);
	}
	
}




二、取两版本间隔的日志文件


package com.xxx.pkgtool.utils;

import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNException;
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.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;


public class SVNFactory {
	
	private static SVNFactory SVNFactory = new SVNFactory();
	
	public static SVNFactory getInstance(){
		return SVNFactory;
	}
	
	private SVNFactory(){
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
    } 
    
	public void logCollecter(String url, String[] targetPaths, String userName, String password,
            long startRevision, long endRevision) throws Exception{
        SVNRepository repository = null;
        repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, password);
        repository.setAuthenticationManager(authManager);
        ISVNLogEntryHandler handler = new ISVNLogEntryHandler() {
            public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
            	for(SVNLogEntryPath path : logEntry.getChangedPaths().values()){
            		
            		if(path.toString().startsWith("A /")){
            			//TODO
            		}else if(path.toString().startsWith("M /")){
            			//TODO
            		}
            	}
            }
        };
        repository.log(targetPaths, startRevision, endRevision, true, true, handler);
    }
}





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值