SVN 根据条件导出指定文件

阅读更多

工作需要,写了个SVN 根据条件导出指定文件的工具。可自行扩展优化,如定制ANT任务什么的,只实现了核心功能。依赖svnkit 。

/**
 * 
 */
package com.elotter.common.svn;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNDepth;
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.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

/**
 * @author liuhx
 *
 */
public class SvnExportTool {
	private static Log logger = LogFactory.getLog(SvnExportTool.class);

	public static final Pattern  PROTOCOL = Pattern.compile("(\\w+://[\\w|\\d|.]+)");	
    private static volatile SvnExportTool svnExportTool ;
    
    private SvnExportTool(){
		SVNRepositoryFactoryImpl.setup();		
		FSRepositoryFactory.setup();  
	    DAVRepositoryFactory.setup();
    }
    
    public static SvnExportTool getInstance(){
    	if(svnExportTool==null){
    		svnExportTool = new SvnExportTool();
    	}
    	return svnExportTool;
    }
	/**
	 * 比较版本导出
	 * @param repositoryURL	
	 * @param dist	导出的目录文件夹
	 * @param userName	用户名
	 * @param password	密码
	 * @param startRevision	起始版本
	 * @param endRevision	结束版本
	 * @throws SVNException
	 * @throws IOException
	 */
	public void export(String svnHost,String project,String dist,String userName,String password,long startRevision ,long endRevision,EnableExportHandler enableExport) throws SVNException, IOException{		
		 export(svnHost,project, dist, userName, password, startRevision , endRevision,false, enableExport) ;
	}
	/**
	 * 比较版本导出
	 * @param repositoryURL	
	 * @param dist	导出的目录文件夹
	 * @param userName	用户名
	 * @param password	密码
	 * @param startRevision	起始版本
	 * @param endRevision	结束版本
	 * @param lastVersion	是否导出最新版本
	 * @throws SVNException
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked", "deprecation" })
	public synchronized void export(String svnHost,String project,String dist,String userName,String password,long startRevision ,long endRevision,boolean lastVersion,EnableExportHandler enableExport) throws SVNException, IOException{		
	    ISVNAuthenticationManager authManager = SVNWCUtil .createDefaultAuthenticationManager(userName, password);  

		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		//创建SVNClientManager的实例
		SVNClientManager clientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, authManager);
		
		ExportLogEntryHandler handler = new ExportLogEntryHandler();
		
//		Matcher matcher = PROTOCOL.matcher(repositoryURL);
//		String svnHost = null;
//		if(matcher.find()){
//			svnHost = matcher.group(1);
//		}else
//			svnHost = repositoryURL;
		//获得历史记录
		getHistoryEntrys(clientManager,svnHost+project,startRevision ,endRevision,handler);
		
		List history =	compareRevision(handler.history);
		
		StringBuilder log = new StringBuilder();
		for(SVNLogEntry entry:history){
			if(enableExport!=null){
				//判断能否导出,若不能导出继续
				if(!enableExport.enableExport(entry)){
					continue;
				}
			}
			log.append(entry.getRevision()).append(" ").append(entry.getDate().toLocaleString()).append(" ").append(entry.getAuthor()).append(" ").append(entry.getMessage()).append("\r\n");
			Collection paths = entry.getChangedPaths().values();
			logger.info("export "+svnHost);
			for (SVNLogEntryPath path : paths) {
				
				log.append(path).append("\r\n");
				if(path.getType()=='D'){//删除
					File file = new File(dist,path.getPath());
					if(file.exists()){
						SvnExportTool.logger.info(" 删除 "+file.getAbsolutePath());
						file.delete();
					}else{
						SvnExportTool.logger.info(file.getAbsolutePath()+"不存在!!");
					}
					continue;
				}
				//添加或更新
				File file = new File(dist,path.getPath());
				if(!file.getParentFile().exists()){
					file.getParentFile().mkdirs();
				}
				SvnExportTool.logger.info(path.getType()+" "+path.getPath());
				
				SVNRevision fileVersion = null;
				if(lastVersion){
					fileVersion = SVNRevision.HEAD;
				}else{
					fileVersion = SVNRevision.create(entry.getRevision());
				}
				try{
					//导出
					clientManager.getUpdateClient().doExport(SVNURL.parseURIEncoded(svnHost +path.getPath()),file, SVNRevision.HEAD,fileVersion,"native", true,SVNDepth.INFINITY);
				}catch(Exception e){
					System.out.println( e.getMessage());
				}
			}
			log.append("\r\n");
		}
		FileUtils.writeStringToFile(new File(dist,"logger.log"), log.toString(), "UTF-8");
	}
	/**
	 * 比较版本
	 * @param list
	 * @return
	 */
	private  List  compareRevision(List list){
		return list;		
	}
	/**
	 * 获得版本区间的历史记录
	 * @param repositoryURL
	 * @param start
	 * @param end
	 * @param handler
	 * @throws SVNException
	 */
	private void getHistoryEntrys (SVNClientManager clientManager ,String repositoryURL,long start ,long end,ISVNLogEntryHandler handler) throws SVNException{
		SVNLogClient logClient = clientManager.getLogClient();
		SVNRevision startRevision = SVNRevision.create(start);
		SVNRevision endRevision = null;
		if(end==-1)
			endRevision = SVNRevision.HEAD;
		else
			endRevision = SVNRevision.create(end);
		
		logClient.doLog(SVNURL.parseURIEncoded(repositoryURL),new String[] {},SVNRevision.HEAD, startRevision, endRevision,false, true, Long.MAX_VALUE,handler);
	}
	
	class ExportLogEntryHandler implements ISVNLogEntryHandler {
		private List history = new ArrayList();
		
		public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
				//存储历史提交记录
				history.add(logEntry);
		}
	};
	
	public interface EnableExportHandler{
		public boolean enableExport(SVNLogEntry logEntry);
	}
	
}

 调用的时候

	public static void main(String[] args) throws SVNException, IOException{
		String svnHost = "svn://192.168.20.224/";
		SvnExportTool.getInstance().export(svnHost,"trunk/sdlfwap","c:/temp1","liuhongxu","111111",3114 ,-1,true,new EnableExportHandler(){
			@Override
			public boolean enableExport(SVNLogEntry logEntry) {
				if(logEntry.getAuthor().equals("liuhongxu")){
					return true;
				}
				return false;
			}
		});
	}
 

 

你可能感兴趣的:(SVN 根据条件导出指定文件)