使用svnkit api,纯java操作svn,实现svn提交,更新等操作(修正版)

本文介绍了如何使用svnkit的high level API进行SVN操作,包括检查工作拷贝、更新及提交。针对不同情况,如工作拷贝存在则执行更新,不存在则根据SVN上是否存在相应项目进行checkout。在执行commit操作时,确保本地路径为工作拷贝,检查文件版本控制,并执行提交操作。
摘要由CSDN通过智能技术生成

此篇是在上一篇基础上修改了bug。


import java.io.File;

import org.apache.log4j.Logger;
import org.tmatesoft.svn.core.SVNCommitInfo;
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.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatus;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

/**
 * SVNKit Utility
 * @author lena yang
 *
 */
public class SVNUtil {
	
	private static Logger logger = Logger.getLogger(SVNUtil.class);
	
	/**
	 * 通过不同的协议初始化版本库
	 */
	public static void setupLibrary() {
		DAVRepositoryFactory.setup();
		SVNRepositoryFactoryImpl.setup();
		FSRepositoryFactory.setup();
	}

	/**
	 * 验证登录svn
	 */
	public static SVNClientManager authSvn(String svnRoot, String username,
			String password) {
		// 初始化版本库
		setupLibrary();

		// 创建库连接
		SVNRepository repository = null;
		try {
			repository = SVNRepositoryFactory.create(SVNURL
					.parseURIEncoded(svnRoot));
		} catch (SVNException e) {
			logger.error(e.getErrorMessage(), e);
			return null;
		}

		// 身份验证
		ISVNAuthenticationManager authManager = SVNWCUtil

		.createDefaultAuthenticationManager(username, password);

		// 创建身份验证管理器
		repository.setAuthenticationManager(authManager);

		DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
		SVNClientManager clientManager = SVNClientManager.newInstance(options,
				authManager);
		return clientManager;
	}
	
	/**
	 * Make directory in svn repository
	 * @param clientManager
	 * @param url 
	 * 			eg: http://svn.ambow.com/wlpt/bsp/trunk 
	 * @param commitMessage
	 * @return
	 * @throws SVNException
	 */
	public static SVNCommitInfo makeDirectory(SVNClientManager clientManager,
			SVNURL url, String commitMessage) {
		try {
			return clientManager.getCommitClient().doMkDir(
					new SVNURL[] { url }, commitMessage);
		} catch (SVNException e) {
			logger.error(e.getErrorMessage(), e);
		}
		return null;
	}
	
	/**
	 * Imports an unversioned directory into a repository location denoted by a
	 * 	destination URL
	 * @param clientManager
	 * @param localPath
	 * 			a local unversioned directory or singal file that will be imported into a 
	 * 			repository;
	 * @param dstURL
	 * 			a repository location where the local unversioned directory/file will be 
     * 			imported into
	 * @param commitMessage
	 * @param isRecursive 递归
	 * @return
	 */
	public static SVNCommitInfo importDirectory(SVNClientManager clientManager,
			File localPath, SVNURL dstURL, String commitMessage,
			boolean isRecursive) {
		try {
			return clientManager.getCommitClient().doImport(localPath, dstURL,
					commitMessage, null, true, true,
					SVNDepth.fromRecurse(isRecursive));
		} catch (SVNException e) {
			logger.er
  • 10
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值