SVNKit 操做svn 实例

 

首先实现svn 权限验证:

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

        // 创建库连接
        SVNRepository repository;

        try {
            repository = SVNRepositoryFactory.create(SVNURL
                    .parseURIEncoded(svnRoot));
        } catch (SVNException e) {
            logger.error(e.getMessage(), e);
            return null;
        }

        // 身份验证
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());

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

        DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
        SVNClientManager clientManager = SVNClientManager.newInstance(options,
                authManager);
        return clientManager;
    }

1. 新增SVN 目录

    /**
     * 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, String url, String commitMessage) {
        try {
            SVNURL svnurl = SVNURL.parseURIEncoded(url);
            SVNCommitClient svnCommitClient = clientManager.getCommitClient();
            return svnCommitClient.doMkDir( new SVNURL[] { svnurl }, commitMessage, null, true);
        } catch (SVNException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

2.删除SVN目录

    /**
     * 删除模型
     */
    public static void deleteModel(String url,String message,String defaultSvnRoot,String username,String password) throws Exception{
        // 权限验证
    	SVNClientManager ourClientManager = authSvn(defaultSvnRoot, username,password);
        SVNCommitClient commitClient=ourClientManager.getCommitClient();
        commitClient.setIgnoreExternals(false);
        
    	SVNURL  repositoryOptUrl=SVNURL.parseURIEncoded(url);
        SVNURL deleteUrls[]=new SVNURL[1];
        deleteUrls[0]=repositoryOptUrl;
        commitClient.doDelete(deleteUrls, message);
    }

 3. 修改svn 目录

  /**
     *  修改svn 目录 ,  将 oldUrl 修改为 newUrl
     * @param buffUrl  oldUrl
     * @param storeUrl newUrl
     * @param message
     * @param defaultSvnRoot  SvnRoot
     * @param username
     * @param password
     * @return
     */
    public static SVNCommitInfo modifyModel(String buffUrl, String storeUrl, String message, String defaultSvnRoot, String username, String password) {
        // 权限验证
        SVNClientManager ourClientManager = authSvn(defaultSvnRoot, username, password);
        SVNCopyClient copyClient = ourClientManager.getCopyClient();
        copyClient.setIgnoreExternals(false);

        try {
            SVNURL oldUrl = SVNURL.parseURIEncoded(defaultSvnRoot + buffUrl);
            SVNURL newUrl = SVNURL.parseURIEncoded(defaultSvnRoot+storeUrl);
            SVNCopySource[] modifySources = new SVNCopySource[1];

            modifySources[0] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, oldUrl);

            return copyClient.doCopy(modifySources, newUrl, true, true, true, message, null);

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

4. 复制svn文件(将A目录下的文件复制到B 目录下)

     如果 B目录不存在,即可以直接将A目录复制到B, 系统会自动创建B目录。

     如果 B目录存在,不能直接将A进行复制,不然会将整个A目录复制到B目录下,即为 /B/A,   此时应该遍历A下面的一级目录(目录+文件),将A目录下的数据进行复制到B.

   /**
     * 复制 A目录(子文件)到 B 目录
     */
    public static SVNCommitInfo copyModel(String buffUrl,String storeUrl,String message,String revision,String defaultSvnRoot,String username,String password) {
        SVNClientManager ourClientManager = authSvn(defaultSvnRoot, username, password);
        SVNCopyClient copyClient = ourClientManager.getCopyClient();
        copyClient.setIgnoreExternals(false);
        try {
            // 根目录
            SVNURL root = SVNURL.parseURIEncoded(defaultSvnRoot);
            // 被复制目录
            SVNURL repositoryOptUrl = SVNURL.parseURIEncoded(defaultSvnRoot + buffUrl);
            // 目的目录
            SVNURL destUrl = SVNURL.parseURIEncoded(defaultSvnRoot + storeUrl);

            SVNRepository repository = ourClientManager.createRepository(root, true);

            // 检测 目的 url目录是否存在;  -1 代表当前最新版本
            SVNNodeKind svnNodeKind = repository.checkPath(storeUrl, -1);
            SVNCopySource[] copySources;
            // 如果目标目录B存在, 获取复制路径下A子目录到目标地址; 否则直接复制A 到B
            if (svnNodeKind == SVNNodeKind.DIR) {
                String basePath = defaultSvnRoot + buffUrl;
                repository.setLocation(repositoryOptUrl, true);
                List urlPaths = listEntries(repository, "",Long.valueOf(revision));
                copySources = new SVNCopySource[urlPaths.size()];
                for (int i = 0; i < urlPaths.size(); i++) {
                    copySources[i] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, SVNURL.parseURIEncoded(basePath +urlPaths.get(i)));
                }
            } else {
                copySources = new SVNCopySource[1];
                if ("-1".equals(revision)) {
                    copySources[0] = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, repositoryOptUrl);
                } else {
                    copySources[0] = new SVNCopySource(SVNRevision.create(Long.valueOf(revision)), SVNRevision.create(Long.valueOf(revision)), repositoryOptUrl);
                }
            }
            return copyClient.doCopy(copySources, destUrl, false, true, false, message, null, false, null);

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

    }


  /**
     * 此函数递归的获取版本库中某一目录下的条目。
     * @param repository
     * @param path
     * @throws SVNException
     */
    public static List listEntries(SVNRepository repository, String path, long revision)
            throws SVNException {
        List<String> urlPaths =new ArrayList();
        //获取版本库的path目录下的所有条目。参数-1表示是最新版本。
        Collection entries = repository.getDir(path, revision, null,(Collection) null);
        Iterator iterator = entries.iterator();
        while (iterator.hasNext()) {
            SVNDirEntry entry = (SVNDirEntry) iterator.next();
            logger.info("/" + (path.equals("") ? "" : path + "/") + entry.getName());
            urlPaths.add("/"+entry.getName());
//            // 如果时目录递归查询获取目录下面的文件信息
//            if (entry.getKind() == SVNNodeKind.DIR) {
//                listEntries(repository, (path.equals("")) ? entry.getName(): path + "/" + entry.getName());
//            }
        }
        return urlPaths;
    }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值