续接上文
本文介绍svnkit获取提交日志信息和提交的文件,提交文件包括文件夹下面的文件
直接上代码
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntryPath;
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.io.SVNRepository;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* @author liuyanzhao
* @date 2021/12/20 10:02
*/
@Slf4j
public class SVNUtilDemo
{
public static ISVNOptions isvnOptions;
// 初始化
static
{
//1.根据访问协议初始化工厂
DAVRepositoryFactory.setup();
//2.使用默认选项
isvnOptions = SVNWCUtil.createDefaultOptions(true);
}
@Data
public static class SVNCommitLogDTO
{
/**
* 版本号
*/
private Long revision;
/**
* 提交人信息
*/
private String author;
/**
* 提交时间
*/
private Date commitDate;
/**
* 提交信息
*/
private String message;
/**
* 仓库前缀
*/
private String repoPrefix;
/**
* 文件列表
*/
private List<SVNFileEntryDTO> fileList;
}
/**
* SVN文件信息
*/
@Data
public static class SVNFileEntryDTO
{
/**
* 文件名
*/
private String name;
/**
* 文件大小
*/
private Long size;
/**
* 文件类别,文件file,文件夹dir
*/
private String kind;
/**
* 版本号
*/
private Long revision;
/**
* 操作文件类型:A添加,D删除,U更新
*/
private String submitType;
/**
* 文件路径 :如 /测试项目/112/中文.ppt
*/
private String path;
}
/**
* 获得指定版本提交日志信息
*
* @param username 用户名
* @param password 密码
* @param repoUrl 仓库地址
* @param revision 版本号
* @return
*/
public static SVNCommitLogDTO getCommitLog(String username, String password, String repoUrl, Long revision)
{
//3.初始化权限
ISVNAuthenticationManager isvnAuthenticationManager = SVNWCUtil.createDefaultAuthenticationManager(username, password.toCharArray());
//4.创建SVNClientManager的实例
SVNClientManager svnClientManager = SVNClientManager.newInstance(isvnOptions, isvnAuthenticationManager);
SVNLogClient svnLogClient = svnClientManager.getLogClient();
SVNCommitLogDTO svnCommitLogDTO = new SVNCommitLogDTO();
try
{
svnLogClient.doLog(SVNURL.parseURIEncoded(repoUrl),
new String[] {}, SVNRevision.create(revision), SVNRevision.create(revision),
SVNRevision.create(0), false, true, false,
1, null,
svnLogEntry -> {
svnCommitLogDTO.setAuthor(svnLogEntry.getAuthor());
svnCommitLogDTO.setMessage(svnLogEntry.getMessage());
svnCommitLogDTO.setRevision(svnLogEntry.getRevision());
Collection<SVNLogEntryPath> svnLogEntryPathCollection = svnLogEntry.getChangedPaths().values();
List<SVNFileEntryDTO> fileEntryDTOList = new ArrayList<>();
Set<String> filePathSet = new HashSet<>();
for (SVNLogEntryPath svnLogEntryPath : svnLogEntryPathCollection)
{
SVNFileEntryDTO file = new SVNFileEntryDTO();
file.setPath(svnLogEntryPath.getPath());
file.setSubmitType(String.valueOf(svnLogEntryPath.getType()));
file.setSize(0L);
file.setKind(svnLogEntryPath.getKind().toString());
file.setRevision(revision);
file.setName(SVNUrlUtil.getLastDirNameByFilePath(file.getPath()));
if (!filePathSet.contains(svnLogEntryPath.getPath()))
{
filePathSet.add(file.getPath());
fileEntryDTOList.add(file);
}
if ("A".equals(file.getSubmitType()) && "dir".equals(svnLogEntryPath.getKind().toString()))
{
// 创建一个新的连接,因为不可重入 参考 http://vcs.atspace.co.uk/2012/09/21/are-svnkit-methods-reenterable/
SVNClientManager svnClientManager2 = SVNClientManager.newInstance(isvnOptions, isvnAuthenticationManager);
SVNRepository repository = svnClientManager2.createRepository(SVNURL.parseURIEncoded(repoUrl), true);
List<SVNFileEntryDTO> svnFileEntryDTOList = new ArrayList<>();
listEntries(repository, svnLogEntryPath.getPath(), svnFileEntryDTOList);
for (SVNFileEntryDTO svnFileEntryDTO : svnFileEntryDTOList)
{
svnFileEntryDTO.setSubmitType(file.getSubmitType());
svnFileEntryDTO.setPath(svnFileEntryDTO.getPath());
if (!filePathSet.contains(svnFileEntryDTO.getPath()))
{
filePathSet.add(svnFileEntryDTO.getPath());
fileEntryDTOList.add(svnFileEntryDTO);
}
}
}
}
// 去重
svnCommitLogDTO.setFileList(fileEntryDTOList);
svnCommitLogDTO.setCommitDate(svnLogEntry.getDate());
});
} catch (Exception e)
{
e.printStackTrace();
}
return svnCommitLogDTO;
}
public static void listEntries(SVNRepository repository, String path, List<SVNFileEntryDTO> svnFileEntryDTOList)
throws SVNException
{
// 获取版本库的path目录下的所有条目。参数-1表示是最新版本。
Collection entries = null;
try
{
entries = repository.getDir(path, -1, null, (Collection) null);
} catch (Exception e)
{
log.error("repository.getDir error, path:{}", path, e);
return;
}
Iterator iterator = entries.iterator();
while (iterator.hasNext())
{
SVNDirEntry entry = (SVNDirEntry) iterator.next();
// 创建文件对象
SVNFileEntryDTO svnFileEntryDTO = new SVNFileEntryDTO();
svnFileEntryDTO.setName(entry.getName());
svnFileEntryDTO.setRevision(entry.getRevision());
svnFileEntryDTO.setPath("/" + ("".equals(path) ? "" : path + "/") + entry.getName());
svnFileEntryDTO.setPath(svnFileEntryDTO.getPath().replace("//", "/"));
svnFileEntryDTO.setKind(entry.getKind().toString());
svnFileEntryDTO.setSize(entry.getSize());
svnFileEntryDTOList.add(svnFileEntryDTO);
if (entry.getKind() == SVNNodeKind.DIR)
{
listEntries(repository, (path.equals("")) ? entry.getName() : path + "/" + entry.getName(), svnFileEntryDTOList);
}
}
}
/**
* 功能说明:
* 判断文件是否存在
*/
public static void main(String[] args) throws Exception
{
String username = "admin"; // svn账号
String password = "123456"; // svn密码
String repoUrl = "http://127.0.0.1/svn/项目"; // 仓库地址
long revision = 100L; //提交版本号
SVNCommitLogDTO commitLog = getCommitLog(username, password, repoUrl, revision);
System.out.println(commitLog);
}
}
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏