当前位置: 首页 > 工具软件 > SVNKit > 使用案例 >

使用svnkit对svn版本信息

訾凯歌
2023-12-01
package svnkitdemo.svnkit;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

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.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNInfo;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNPropertyData;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import org.tmatesoft.svn.core.wc2.SvnInfo;

/**
 * SvnKit日志的处理类
 * 
 * @author yanggguang
 *
 */
public class SvnKitUtil {

	private String userName = "yangguang";
	private String password = "yangguang";
	private String urlString = "http://192.168.50.56:81/svn/demo/demo/";
	private String urlStringfile = "http://192.168.50.56:81/svn/demo/demo/pom.xml";
	boolean readonly = true;
	private DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(readonly);

	private SVNRepository repos;
	private ISVNAuthenticationManager authManager;
	private static SVNClientManager clientManager;

	public SvnKitUtil() {
		try {
			init();
		} catch (SVNException e) {
			System.out.println("初始化失败");
			e.printStackTrace();
		}
	}

	public void init() throws SVNException {
		authManager = SVNWCUtil.createDefaultAuthenticationManager(userName, password.toCharArray());
		options.setDiffCommand("-x -w");
		repos = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(urlString));
		repos.setAuthenticationManager(authManager);
		clientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, authManager);

	}

	/**
	 * 统计一段时间修改的版本
	 * 
	 * @param st
	 * @param et
	 * @return
	 * @throws Exception
	 */
	public int staticticsCodeAddByTime(Date st, Date et) throws Exception {
		int sum = 0;
		// 获得在 st 和et时间段里面共有多少次提交
		SVNLogEntry[] logs = getLogByTime(st, et);
		if (logs.length > 0) {
			for (SVNLogEntry log : logs) {
				System.out.println("----------" + "版本号为" + log.getRevision() + "信息" + "----------");
				System.out.println("作者:" + log.getAuthor() + "----" + "提交信息" + log.getMessage());
				System.out.println("----修改的路径--");
				getChangepahtsByLogEntry(log).forEach(p -> System.out.println(p.getPath()));
			}
		}
		return sum;
	}

	/**
	 * 通过SVNLogEntry
	 * 
	 * @param log
	 * @return
	 */
	public List<SVNLogEntryPath> getChangepahtsByLogEntry(SVNLogEntry log) {
		List<SVNLogEntryPath> result = new ArrayList<>();
		Map<String, SVNLogEntryPath> maps = log.getChangedPaths();
		Set<Map.Entry<String, SVNLogEntryPath>> entries = maps.entrySet();
		for (Map.Entry<String, SVNLogEntryPath> entry : entries) {
			// System.out.println(entry.getKey());
			SVNLogEntryPath entryPath = entry.getValue();
			result.add(entryPath);
			// System.out.println(entryPath.getType()+" "+entryPath.getPath());
		}
		return result;
	}

	/**
	 * 获取一段时间内,所有的commit记录
	 * 
	 * @param st
	 *            开始时间
	 * @param et
	 *            结束时间
	 * @return
	 * @throws SVNException
	 */
	public SVNLogEntry[] getLogByTime(Date st, Date et) throws SVNException {
		// long startRevision = 39;
		// long endRevision = -1;//为-1时 表示最后一个版本
		long startRevision = repos.getDatedRevision(st);
		long endRevision = repos.getDatedRevision(et);
		@SuppressWarnings("unchecked")
		Collection<SVNLogEntry> logEntries = repos.log(new String[] { "" }, null, startRevision, endRevision, true,
				true);
		SVNLogEntry[] svnLogEntries = logEntries.toArray(new SVNLogEntry[0]);
		return svnLogEntries;
	}

	/**
	 * 获取某一版本有变动的文件路径
	 * 
	 * @param version
	 * @return
	 * @throws SVNException
	 */
	public List<SVNLogEntryPath> getChangeFileList(long version) throws SVNException {

		List<SVNLogEntryPath> result = new ArrayList<>();
		SVNLogClient logClient = new SVNLogClient(authManager, options);
		SVNURL url = SVNURL.parseURIEncoded(urlString);
		// 相对于目标URL的路径数组
		String[] paths = { "." };
		// PegRevision表示在这个版本下定位那个元素,这一定是唯一的
		SVNRevision pegRevision = SVNRevision.create(version);
		SVNRevision startRevision = SVNRevision.create(version);
		SVNRevision endRevision = SVNRevision.create(version);
		// 遍历历史记录时不交叉复制,否则副本历史记录也将包括在处理中。
		boolean stopOnCopy = false;
		// 报告正在处理的每个修订的所有更改路径(这些路径将通过调用org.tmateSoft.svn.core.svnlogentry.getChangedPaths()调用)
		boolean discoverChangedPaths = true;
		// 限制最大处理的日志数
		long limit = 9999l;

		ISVNLogEntryHandler handler = new ISVNLogEntryHandler() {

			/**
			 * This method will process when doLog() is done
			 */
			@Override
			public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
				System.out.println("提交者: " + logEntry.getAuthor());
				System.out.println("提交时间: " + logEntry.getDate());
				System.out.println("提交信息: " + logEntry.getMessage());
				System.out.println("版本: " + logEntry.getRevision());
				System.out.println("---------版本为" + logEntry.getRevision() + "的修改记录start--------");
				Map<String, SVNLogEntryPath> maps = logEntry.getChangedPaths();
				Set<Map.Entry<String, SVNLogEntryPath>> entries = maps.entrySet();
				for (Map.Entry<String, SVNLogEntryPath> entry : entries) {
					// System.out.println(entry.getKey());
					SVNLogEntryPath entryPath = entry.getValue();
					result.add(entryPath);
					System.out.println(entryPath.getType() + " " + entryPath.getPath());
				}
				System.out.println("---------版本为" + logEntry.getRevision() + "的修改记录end--------");

			}
		};
		try {
			logClient.doLog(url, paths, pegRevision, startRevision, endRevision, stopOnCopy, discoverChangedPaths,
					limit, handler);
		} catch (SVNException e) {
			System.out.println("Error in doLog() ");
			e.printStackTrace();
		}
		return result;
	}

	// 获得某一个指定路径下的文件或者文件夹的信息
	public SVNLogEntry getonepathSvninfo(String path) throws SVNException {
		SVNURL epositoryBaseUrl = SVNURL.parseURIEncoded(urlString);
		 long startRevision =0;//开始
		 long endRevision = -1;//为-1时 表示最后一个版本
		Collection<SVNLogEntry> logEntries = repos.log(new String[] {path}, null, startRevision, endRevision, true,
				true);
		SVNLogEntry[] svnLogEntries = logEntries.toArray(new SVNLogEntry[0]);
		return svnLogEntries[svnLogEntries.length-1];
	}

	public static void main(String[] args) throws Exception {
		SvnKitUtil demo = new SvnKitUtil();
		Date now = new Date();
		Date twoDayAgo = new Date(now.getTime() - 15 * 24 * 3600000);
		try {
			// 获得某一时间内版本号 修改的文件路径
			// demo.staticticsCodeAddByTime(now, twoDayAgo);
			// 获得某一个版本的修改文件路径
			// demo.getChangeFileList(37);
			//获得指定文件的信息
			System.out.println("版本号"+demo.getonepathSvninfo("pom.xml").getRevision());

		} catch (SVNException e) {
			e.printStackTrace();
		}
	}

}


 

 类似资料: