svnkit检出,修改,删除,提交操作

svnkit检出,修改,删除,提交操作


maven引用

		<!-- svn客户端-->
		<dependency>
			<groupId>org.tmatesoft.svnkit</groupId>
			<artifactId>svnkit</artifactId>
			<version>1.8.14</version>
		</dependency>

好吧直接上客户端代码,做了一些简单的svn操作,检出,更新,删除,添加,提交,有写的不好见谅,也请指正,多谢


import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.wc.SVNInfo;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import javax.validation.constraints.NotNull;
import java.io.File;

/**
 * @Author by qingcheng
 * @Date 2019/11/4 10:07
 * @Description 自定义svn客户端,简化svnApi操作
 */
public class SVNClient {

    // ========================================================================================================
    // 客户端要素
    /**
     * svn身份验证器实例
     */
    private ISVNAuthenticationManager auth;

    /**
     * 版本库url实例
     */
    private SVNURL url;

    /**
     * svn客户端管理器实例
     */
    private SVNClientManager manager;

    /**
     * svn版本管理客户端
     */
    private SVNWCClient rvClient;

    /**
     * svn目录操作客户端
     */
    private SVNUpdateClient upClient;

    /**
     * svn提交客户端
     */
    private SVNCommitClient commitClient;

    // ========================================================================================================
    // 构造
    /**
     * 客户端构造器
     *
     * @param username svn账号
     * @param password svn密码
     * @param url      svn地址
     * @throws SVNException svn客户端异常
     */
    SVNClient(@NotNull String username, String password, @NotNull String url) throws SVNException {
        // 初始化认证器
        this.auth = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());
        // 初始化url
        this.url = SVNURL.parseURIEncoded(url);
        // 初始化版本库
        /*this.repository = SVNRepositoryFactory.create(this.url);
        this.repository.setAuthenticationManager(this.auth);*/
        // 初始化客户端管理器
        this.manager = SVNClientManager.newInstance(SVNWCUtil.createDefaultOptions(true), this.auth);
        // 为节约性能,只在需要时调用需要初始化的操作客户端
    }

    // ========================================================================================================
    // 客户端功能方法

    /**
     * TODO 获取svn最新版本
     *
     * @return svn最新版本号
     * @throws SVNException
     */
    public Long lastRevision() throws SVNException {
        intiRevisionClient();
        SVNInfo info = rvClient.doInfo(url, SVNRevision.HEAD, SVNRevision.HEAD);
        SVNRevision lastRevision = info.getCommittedRevision();
        return lastRevision.getNumber();
    }

    /**
     * TODO 检出或更新最新版本
     *
     * @param target 检出目录
     * @return 检出目标版本
     * @throws SVNException
     */
    public Long checkoutOrUpdate(String target) throws SVNException {
        return checkoutOrUpdate(target, SVNRevision.HEAD);
    }

    /**
     * TODO 检出或更新指定版本
     *
     * @param target 检出目录
     * @param revision 版本号
     * @return 检出目标版本
     * @throws SVNException
     */
    public Long checkoutOrUpdate(String target, Long revision) throws SVNException {
        return checkoutOrUpdate(target, SVNRevision.create(revision));
    }

    /**
     * TODO 检出或更新指定版本
     *
     * @param target 检出目录
     * @param revision svn版本
     * @return 检出目标版本
     * @throws SVNException
     */
    public Long checkoutOrUpdate(String target, SVNRevision revision) throws SVNException {
        intiUpdateClient();
        Long revisionNum;
        // 检查svn版本管理目录是否存在,存在则执行更新操作,不存在执行检出操作
        File targetSvn = new File(target + "/.svn");
        if (targetSvn.exists()) {
            revisionNum = upClient.doUpdate(new File(target), revision, SVNDepth.INFINITY, true, true);
        } else {
            revisionNum = upClient.doCheckout(url, new File(target), SVNRevision.HEAD, revision, SVNDepth.INFINITY, true);
        }
        return revisionNum;
    }

    /**
     * TODO 检出svn最新版本
     *
     * @param target 检出目录
     * @return 检出目标版本
     * @throws SVNException
     */
    public Long checkout(String target) throws SVNException {
        intiUpdateClient();
        Long revisionNum = upClient.doCheckout(url, new File(target), SVNRevision.HEAD, SVNRevision.HEAD, SVNDepth.INFINITY, true);
        return revisionNum;
    }

    /**
     * TODO 更新到svn最新版本
     *
     * @param target 目标目录
     * @return 检出目标版本
     * @throws SVNException
     */
    public Long update(String target) throws SVNException {
        intiUpdateClient();
        Long revisionNum = upClient.doUpdate(new File(target), SVNRevision.HEAD, SVNDepth.INFINITY, true, true);
        return revisionNum;
    }

    /**
     * TODO 添加版本控制文件
     *
     * @param files 移除版本控制文件
     * @return 检出目标版本
     * @throws SVNException
     */
    public void add(File[] files) throws SVNException {
        intiRevisionClient();
        rvClient.doAdd(files, true, false, false, SVNDepth.INFINITY, true, false, false);
    }

    /**
     * TODO 移除版本控制文件
     *
     * @param files 移除版本控制文件
     * @return 检出目标版本
     * @throws SVNException
     */
    public void delete(File[] files) throws SVNException {
        intiRevisionClient();
        // 移除文件版本控制并删除文件
        for (File f : files) {
            rvClient.doDelete(f, true, true, false);
        }
    }

    /**
     * TODO 提交版本库
     *
     * @param files   提交文件
     * @param message 提交信息
     * @return 检出目标版本
     * @throws SVNException
     */
    public Long commit(File[] files, String message) throws SVNException {
        intiCommitClient();
        SVNCommitInfo info = commitClient.doCommit(files, true, message, null, null, true, true, SVNDepth.INFINITY);
        Long revisionNum = -1l;
        if (info != null) {
            revisionNum = info.getNewRevision();
            if (info.getErrorMessage() != null) {
                throw new SVNException(info.getErrorMessage());
            }
        }
        return revisionNum;
    }


    /**
     * TODO 提交版本库
     *
     * @return 检出目标版本
     * @throws SVNException
     */
    public void clear() {
        manager.dispose();
    }

    // ========================================================================================================
    // svn操作客户端初始化方法
    // 初始化svn版本管理客户端
    private void intiRevisionClient() {
        if (this.rvClient == null) {
            this.rvClient = manager.getWCClient();
        }
    }

    // 初始化目录操作客户端
    private void intiUpdateClient() {
        if (this.upClient == null) {
            this.upClient = manager.getUpdateClient();
        }
    }

    // 初始化提交客户端
    private void intiCommitClient() {
        if (this.commitClient == null) {
            this.commitClient = manager.getCommitClient();
        }
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值