`

svn kit 简单使用

阅读更多

1. util.DefaultSVNAnnotateHandler.java

package util;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.ISVNAnnotateHandler;

public class DefaultSVNAnnotateHandler implements ISVNAnnotateHandler {

	private Map<Integer,String> lineAuthorMap = new HashMap<Integer,String>();
	
	public void handleEOF() throws SVNException {
		// TODO Auto-generated method stub

	}

	public void handleLine(Date date, long revision, String author, String line)
			throws SVNException {

	}

	public void handleLine(Date date, long revision, String author,
			String line, Date mergedDate, long mergedRevision,
			String mergedAuthor, String mergedPath, int lineNumber)
			throws SVNException {
		lineAuthorMap.put(lineNumber, author);

	}

	public boolean handleRevision(Date date, long revision, String author,
			File contents) throws SVNException {
		// TODO Auto-generated method stub
		return false;
	}

	public Map<Integer, String> getLineAuthorMap() {
		return lineAuthorMap;
	}
	
	

}

 

2. util.SVNUtil.java

package util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.BasicAuthenticationManager;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;


public class SVNUtil {
	
	private static Logger logger = Logger.getLogger(SVNUtil.class.getName());
	
	private static SVNClientManager getSVNClientManager(){
		SVNClientManager svnClientManager = SVNClientManager.newInstance();
		
		ISVNAuthenticationManager authManager = new BasicAuthenticationManager("xiao", "xiao");
		
		svnClientManager.setAuthenticationManager(authManager );
		
		return svnClientManager;
	}
	
	
	public static File getFileFromSVN(String filePath,  
            long revision,String destFilePath) {  
		File file = new File(destFilePath);
		SVNURL reponsitoryURL = null;
		FileOutputStream fos = null;
		try {
			reponsitoryURL = SVNURL.parseURIEncoded(filePath);
			
			fos = new FileOutputStream(file);
			
			SVNRepository  reponsitory = getSVNClientManager().createRepository(reponsitoryURL, false);

			reponsitory.getFile("", revision, null, fos);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}finally{
			if(fos!=null){
				try {
					fos.close();
				} catch (IOException e) {
					logger.warning(e.getMessage());
				}
			}
		}
		return file;
    }  
	
	
	public static Map<Integer,String> getFileInfo(String filePath,long revision) throws SVNException{
		SVNURL url = SVNURL.parseURIEncoded(filePath);
		SVNRevision pegRevision = SVNRevision.create(revision);
		SVNRevision startRevision = SVNRevision.create(0);
		SVNRevision endRevision = SVNRevision.create(revision);
		DefaultSVNAnnotateHandler handler = new DefaultSVNAnnotateHandler();
		getSVNClientManager().getLogClient().doAnnotate(url, pegRevision, startRevision, endRevision, handler);
		
		return handler.getLineAuthorMap();
	}
}

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>svn</groupId>
	<artifactId>svn</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>svn</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.tmatesoft.svnkit</groupId>
			<artifactId>svnkit</artifactId>
			<version>1.8.5</version>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics