一、获取指定版本的文件内容
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); } }