SVNKit学习——使用低级别的API(ISVNEditor接口)直接操作Repository的目录和文件(五)
本文是参考官方文档的实现,官方wiki:https://wiki.svnkit.com/Committing_To_A_Repository
本文核心使用的是ISVNEditor这个接口直接对Repository进行各种AM操作~
以下两张示例图分别代表我们操作前、操作后仓库的结构:


具体实现:

package com.demo; import com.google.gson.Gson;
import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException; /**
* 提交到仓库
* 这块看官方demno的意思如果不用权限认证,会使用a session user name作为提交的author,但是我试了会报错401,author required~
* 本例是基于初始仓库图A转换为目标仓库图B的过程,我们需要执行的操作有:
* 1.删除nodeB/itemB1
* 2.编辑nodeC/itemC1
* 3.新增nodeC/itemC2,并设置itemC2的文件属性
* 4.新增nodeB子节点nodeD
*/
public class CommitToRepository {
public static void main(String[] args) throws Exception{
//1.根据访问协议初始化工厂
DAVRepositoryFactory.setup();;
//2.初始化仓库,由于我们所有的操作都是基于nodeB节点以下的,所以我们将nodeB作为本次操作的root节点
String url = "https://wlyfree-PC:8443/svn/svnkitRepository2/trunk/nodeB";
SVNRepository svnRepository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
//3.初始化权限
String username = "wly";
String password = "wly";
char[] pwd = password.toCharArray();
ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username,pwd);
svnRepository.setAuthenticationManager(isvnAuthenticationManager);
//====================================DEMO START=========================================
ISVNEditor editor = null;
long revisionNo = -1; //指定版本号为最新版本
//4.1.删除nodeB/itemB1
try{
//获取编辑器
editor = svnRepository.getCommitEditor("delete file",null,true,null);
String itemB1Path = "itemB1";//要删除的文件路径
SVNCommitInfo svnCommitInfo = deleteFile(editor,revisionNo);//执行删除并返回执行结果
System.out.println("执行删除操作的返回结果:" + svnCommitInfo);
}catch (SVNException e){
//发生异常需要终止操作
editor.abortEdit();
e.printStackTrace();;
}
//4.2.编辑nodeC/itemC1
try{
//获取编辑器
editor = svnRepository.getCommitEditor("modify file",null,true,null);
SVNCommitInfo svnCommitInfo = modifyFile(editor,revisionNo);
System.out.println("执行编辑操作的返回结果:" + svnCommitInfo);
}catch(SVNException e){
//发生异常需要终止操作
editor.abortEdit();
e.printStackTrace();;
}
//4.3.新增nodeC/itemC2,并设置itemC2的文件属性
try{
editor = svnRepository.getCommitEditor("add file",null,true,null);
SVNCommitInfo svnCommitInfo = addFile(editor,revisionNo);
System.out.println("执行新增文件操作的返回结果:" + svnCommitInfo);
//校验nodeC/itemC2的属性是否成功设置进去
SVNProperties s = new SVNProperties();
svnRepository.getFile("nodeC/itemC2",-1,s,null);
Gson gson = new Gson();
System.err.println(gson.toJson(s));
}catch (SVNException e){
editor.abortEdit();
e.printStackTrace();
} //4.4.新增nodeB子节点nodeD
try{
editor = svnRepository.getCommitEditor("add dir",null,true,null);
SVNCommitInfo svnCommitInfo = addDir(editor,revisionNo);
System.out.println("执行新增目录操作的返回结果:" + svnCommitInfo);
}catch (SVNException e){
editor.abortEdit();
e.printStackTrace();
}
} /**
* 删除文件
* @param editor 编辑器
* @param revisionNo 修订版版本号
* @return SVNCommitInfo 提交结果信息
* @throws SVNException
*/
private static SVNCommitInfo deleteFile(ISVNEditor editor,long revisionNo) throws SVNException{
// 进入Root节点,即nodeB
editor.openRoot(revisionNo);
//4.3.删除文件
editor.deleteEntry("itemB1",revisionNo);
//操作完成要关闭编辑器,并返回操作结果
return editor.closeEdit();
} /**
* 编辑文件
* @param editor 编辑器
* @param revisionNo 修订版版本号
* @return SVNCommitInfo 提交结果信息
* @throws SVNException
*/
private static SVNCommitInfo modifyFile(ISVNEditor editor,long revisionNo) throws SVNException{
// 进入Root节点,即nodeB
editor.openRoot(revisionNo);
//.进入nodeC节点
editor.openDir("nodeC",revisionNo);
// 编辑nodeC/itemC1的内容
String itemC1Path = "nodeC/itemC1";//路径都是相对于root的
editor.openFile(itemC1Path,revisionNo);
//确保客户端这个文件的内容和服务端的是一样的,如果不一致的话是不允许提交的。底层实现使用MD5
String baseChecksum = null;
editor.applyTextDelta(itemC1Path,baseChecksum);
//提交文件变更的数据,windows默认是100kb大小
byte[] oldData = new byte[]{};
byte[] newData = null;
try {
newData = "我来测试一下编辑2".getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ByteArrayInputStream baseData = new ByteArrayInputStream(oldData);
ByteArrayInputStream workingData = new ByteArrayInputStream(newData);
SVNDeltaGenerator svnDeltaGenerator = new SVNDeltaGenerator();//100KB-windows generator
String checksum = svnDeltaGenerator.sendDelta(itemC1Path,baseData,0,workingData,editor,true);
// 关闭文件
editor.closeFile(itemC1Path,checksum);
// 关闭目录nodeC
editor.closeDir();
// 关闭根目录nodeB
editor.closeDir();
// 关闭编辑器,并返回执行结果
return editor.closeEdit();
} /**
* 新增文件
* @param editor
* @param revisionNo
* @return
* @throws SVNException
*/
private static SVNCommitInfo addFile(ISVNEditor editor,long revisionNo) throws SVNException{
// 进入Root节点,即nodeB
editor.openRoot(revisionNo);
//.进入nodeC节点
editor.openDir("nodeC",revisionNo);
// 新增itemC2文件
editor.addFile("nodeC/itemC2",null,revisionNo);
//确保客户端这个文件的内容和服务端的是一样的,如果不一致的话是不允许提交的。底层实现使用MD5
String itemC2Path = "nodeC/itemC2";
String baseChecksum = null;
editor.applyTextDelta(itemC2Path,baseChecksum);
//提交文件变更的数据,windows默认是100kb大小
byte[] oldData = new byte[]{};//旧数据
byte[] newData = null;//新数据
try {
newData = "我来测试一下 - addFile".getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ByteArrayInputStream baseData = new ByteArrayInputStream(oldData);
ByteArrayInputStream workingData = new ByteArrayInputStream(newData);
SVNDeltaGenerator svnDeltaGenerator = new SVNDeltaGenerator();//100KB-windows generator
String checksum = svnDeltaGenerator.sendDelta(itemC2Path,baseData,0,workingData,editor,true);
//设置文件的属性,key是字符串,值被包装成SVNProperyValue了
editor.changeFileProperty("nodeC/itemC2","properName1",SVNPropertyValue.create("properValue1"));
editor.changeFileProperty("nodeC/itemC2","properName2",SVNPropertyValue.create("properValue2"));
System.out.println("checksum:" + checksum );
//关闭文件
editor.closeFile("nodeC/itemC2",checksum);
//关闭目录nodeC
editor.closeDir();
//关闭root
editor.closeDir();
return editor.closeEdit();
} /**
* 新增目录
* @param editor 编辑器
* @param revisionNo 修订版本号
* @return SVNCommitInfo 提交结果信息
* @throws SVNException
*/
private static SVNCommitInfo addDir(ISVNEditor editor,long revisionNo) throws SVNException{
// 进入Root节点,即nodeB
editor.openRoot(revisionNo);
//新增目录
editor.addDir("nodeD",null,revisionNo);
editor.closeDir();//nodeD
editor.closeDir();//nodeB
return editor.closeEdit();
}
}

运行效果:

执行删除操作的返回结果:r51 by 'wly' at Wed Dec 07 13:48:15 CST 2016
执行编辑操作的返回结果:r52 by 'wly' at Wed Dec 07 13:48:15 CST 2016
checksum:a107fb58070bfbaf11513c8750f87466
执行新增文件操作的返回结果:r53 by 'wly' at Wed Dec 07 13:48:15 CST 2016
{"myProperties":{"svn:entry:uuid":{"myValue":"e5dd1e38-0390-574a-b68d-e269ce50c382"},"svn:entry:revision":{"myValue":"53"},"properName1":{"myData":[112,114,111,112,101,114,86,97,108,117,101,49]},"svn:entry:committed-date":{"myValue":"2016-12-07T05:48:15.464756Z"},"properName2":{"myData":[112,114,111,112,101,114,86,97,108,117,101,50]},"svn:wc:ra_dav:version-url":{"myValue":"/svn/svnkitRepository2/!svn/ver/53/trunk/nodeB/nodeC/itemC2"},"svn:entry:checksum":{"myValue":"a107fb58070bfbaf11513c8750f87466"},"svn:entry:committed-rev":{"myValue":"53"},"svn:entry:last-author":{"myValue":"wly"}}}
执行新增目录操作的返回结果:r54 by 'wly' at Wed Dec 07 13:48:15 CST 2016

总结:
其实走读一遍代码就知道,无论进行什么操作都是有一定规律性的。
无论是操作目录还是文件,大的框架可以大体总结为以下几步:

//1.根据访问协议初始化工厂
//2.初始化仓库,由于我们所有的操作都是基于nodeB节点以下的,所以我们将nodeB作为本次操作的root节点
//3.初始化权限
//4.获取编辑器对象
//5.进入目录/文件
//6.执行操作
//7.关闭目录/文件
//8.关闭编辑器

实际工作中,感觉这种方式不是特别灵活,不一定适用于普通的应用场景,相对来讲,High-Level API更倾向于用户和SVN的交互。
SVNKit学习——使用低级别的API(ISVNEditor接口)直接操作Repository的目录和文件(五)的更多相关文章
- Vue学习笔记-Django REST framework3后端接口API学习
一 使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7 (Windows x86- ...
- [ Java学习基础 ] Java的抽象类与接口
一.抽象类 1. 抽象类 Java语言提供了两种类:一种是具体类:另一种是抽象子类. 2. 抽象类概念: 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的 ...
- node-webkit学习(3)Native UI API概览
node-webkit学习(3)Native UI API概览 文/玄魂 目录 node-webkit学习(3)Native UI API概览 前言 3.1 Native UI api概览 Exte ...
- ecCodes 学习 利用ecCodes Python API对GRIB文件进行读写
参考 https://www.ecmwf.int/assets/elearning/eccodes/eccodes2/story_html5.htmlhttps://confluence.ecmwf. ...
- 使用TensorFlow低级别的API进行编程
Tensorflow的低级API要使用张量(Tensor).图(Graph).会话(Session)等来进行编程.虽然从一定程度上来看使用低级的API非常的繁重,但是它能够帮助我们更好的理解Tenso ...
- SVNKit学习——wiki+简介(二)
这篇文章是参考SVNKit官网在wiki的文档,做了个人的理解~ 首先抛出一个疑问,Subversion是做什么的,SVNKit又是用来干什么的? 相信一般工作过的同学都用过或了解过svn,不了解的同 ...
- 【开源】.Net Api开放接口文档网站
开源地址:http://git.oschina.net/chejiangyi/ApiView 开源QQ群: .net 开源基础服务 238543768 ApiView .net api的接口文档查看 ...
- 免费手机号码归属地API查询接口和PHP使用实例分享
免费手机号码归属地API查询接口和PHP使用实例分享 最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点之间偷来了API的接口地址. 分享出来,大家可以用到就拿去 ...
- Mybatis学习总结(二)—使用接口实现数据的增删改查
在这一篇中,让我们使用接口来实现一个用户数据的增删改查. 完成后的项目结构如下图所示: 在这里,person代表了一个用户的实体类.在该类中,描述了相关的信息,包括id.name.age.id_num ...
随机推荐
- 获取请求Requst中访问请求的客户端IP
获取请求Request中访问请求的客户端IP /*获取请求客户端的IP地址*/ public static String getIpAddress(HttpServletRequest request ...
- RocketMQ 安装
RocketMQ 安装 1.进入目录 cd /usr 2.下载 wget http://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.3.0/rocke ...
- nginx自动部署脚本
需要下载脚本中需要的jar包nginx.pcre和zlib,自己也上传了一个自己部署的包 https://download.csdn.net/download/qq_17842663/10822976 ...
- PHP多维数据排序(不区分大小字母)
1. PHP中最普通的数组排序方法 sort(); 看个例子: <?php $test = array(); $test[] = 'ABCD'; $test[] = 'aaaa'; $test[ ...
- a[i]==i[a]==*(i+a)==*(a+i)
在C语言中,如果我们要访问一个数组的某个下标对应的元素,通常的写法是a[i].但从汇编的角度看,写成i[a]一点问题都没有. 下面通过代码给出证明. o foo1.c int main(int arg ...
- Mybatis缓存(一)
1.什么是缓存 Mybatis提供缓存,用于减轻数据压力,提高数据库性能. 2.Mybatis缓存分类 Mybatis的缓存分为一级缓存和二级缓存. Mybatis的一级缓存 1.一级缓存的范围 1 ...
- Linux中让普通用户拥有超级用户的权限
问题 假设用户名为:ali 如果用户名没有超级用户权限,当输入 sudo + 命令 时, 系统提示: ali is not in the sudoers file. This incident wi ...
- WPF Window对象的生命周期
WPF中所有窗口的基类型都是System.Windows.Window.Window通常用于SDI(SingleDocumentInterface).MDI(MultipleDocumentInter ...
- zookeeper【2】集群管理
Zookeeper 的核心是广播,这个机制保证了各个Server之间的同步.实现这个机制的协议叫做Zab协议. Zab协议有两种模式,它们分别是恢复模式(选主)和广播 模式(同步).当服务启动或者在领 ...
- MySQL的四种事务隔离级别【转】
本文实验的测试环境:Windows 10+cmd+MySQL5.6.36+InnoDB 一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做 ...