使用SvnKit获得提交记录

由于工作需要通过JAVA获得SVN的提交记录,所以看了下SVNKit,

下面是代码,保存备份。

 

SvnKit项目地址:http://svnkit.com/index.html

 

package com.svnkit;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;
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 SVNUtilTest {
    private static String url = "项目SVN路径";
    private static SVNRepository repository = null;

    @BeforeClass
    public static void setupLibrary() {
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        }
        catch (SVNException e) {
            logger.error(e.getErrorMessage(), e);
        }
        // 身份验证
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager("SVN用户名","SVN密码");
        repository.setAuthenticationManager(authManager);
    }

    @Test
    public void filterCommitHistoryTest() throws Exception {
        // 过滤条件
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        final Date begin = format.parse("2014-02-13");
        final Date end = format.parse("2014-02-14");
        final String author = "";
        long startRevision = 0;
        long endRevision = -1;//表示最后一个版本
        final List<String> history = new ArrayList<String>();
		//String[] 为过滤的文件路径前缀,为空表示不进行过滤
        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)) {
                                   // 依据提交人过滤
                                   if (!"".equals(author)) {
                                       if (author.equals(svnlogentry.getAuthor())) {
                                           fillResult(svnlogentry);
                                       }
                                   } else {
                                       fillResult(svnlogentry);
                                   }
                               }
                           }

                           public void fillResult(SVNLogEntry svnlogentry) {
                          //getChangedPaths为提交的历史记录MAP key为文件名,value为文件详情
                               history.addAll(svnlogentry.getChangedPaths().keySet());
                           }
                       });
        for (String path : history) {
            System.out.println(path);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值