能搜索到这篇博客的,应该应该也是跟我碰到了一样的问题,如何获取svn的指定时间的段的日志信息,我获取这个信息是为了将其转化为系统的release notes(版本发行日志)。
在此特别鸣谢简书的博主@刘同学lwq,好文章不能被埋没了,下附链接
https://www.jianshu.com/p/deeb4cafbab2
那么如何用java获取指定时间段的SVN日志呢?
思路一:直接调用SVNKit的根据时间段获取日志信息的API,这样就可以做到用多少取多少。
思路二:但是我去查了SVNKit 的所有API,发现并没可以直接根据某个时间段获取svn信息的API,于是我就想到从svn中取出从第0个版本到当前版本的所有的svn日志信息,放入内存中,然后将输入的时间与日志信息中每次代码更新的时间进行比较,取出某个时间段的日志信息。这个方法有个很大的缺点,过于依赖内存,如果日志数据量过大,会消耗很多的的内存。
正当我打算认命之际,无意中发现了这个博主的博客,我承认这个大佬比我厉害,对API 的认识够强。顺便吐槽一句,网上关于java获取指定时间段的SVN日志方面的博文不好找。
下附代码:
首先引入依赖
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.8.14</version>
</dependency>
获取svn的日志信息
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
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.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;
public class ModerOption {
//svn地址
private static String url = "http://xxxx:xxx/svn/project/anka/trunk/Anka";
private static SVNRepository repository = null;
public Collection filterCommitHistory(Date begin, Date end) throws Exception {
DAVRepositoryFactory.setup();
try {
repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
}
catch (SVNException e) {
e.printStackTrace();
}
// 身份验证
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager("svn用户名","svn密码");
repository.setAuthenticationManager(authManager);
long startRevision = 0;
long endRevision = -1;//表示最后一个版本
final Collection<SVNLogEntry > history = new ArrayList<>();
repository.log(new String[]{""},
startRevision,
endRevision,
true,
true,
new ISVNLogEntryHandler() {
@Override
public void handleLogEntry(SVNLogEntry svnlogentry)
throws SVNException {
//依据提交时间进行过滤
if (svnlogentry.getDate().after(begin)
&& svnlogentry.getDate().before(end)) {
fillResult(svnlogentry);
}
}
public void fillResult(SVNLogEntry svnlogentry) {
history.add(svnlogentry);
}
});
return history;
}
public static void main(String[] args) throws Exception {
ModerOption test = new ModerOption();
// 过滤条件
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
final Date begin = format.parse("2019-04-13");
final Date end = format.parse("2019-05-14");
Collection logEntries = test.filterCommitHistory(begin, end);
for(Object temp : logEntries ){
SVNLogEntry logEntry = (SVNLogEntry)temp;
//这里只打印了日志中的时间
System.out.println(logEntry.getDate());
}
}
}