Get commit records with SvnKit

Since the work needs to obtain the SVN submission record through JAVA, I looked at SVNKit,

Below is the code, save the backup.

 

SvnKit project address: 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 = "Project SVN path";
    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);
        }
        // Authentication
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager("SVN Username","SVN Password");
        repository.setAuthenticationManager(authManager);
    }

    @Test
    public void filterCommitHistoryTest() throws Exception {
        // filter condition
        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;//Indicates the last version
        final List<String> history = new ArrayList<String>();
		//String[] is the file path prefix to be filtered, if it is empty, it means no filtering
        repository.log(new String[]{""},
                       startRevision,
                       endRevision,
                       true,
                       true,
                       new ISVNLogEntryHandler() {
                           @Override
                           public void handleLogEntry(SVNLogEntry svnlogentry)
                                   throws SVNException {
				// filter by submission time
                               if (svnlogentry.getDate().after(begin)
                                   && svnlogentry.getDate().before(end)) {
                                   // filter by submitter
                                   if (!"".equals(author)) {
                                       if (author.equals(svnlogentry.getAuthor())) {
                                           fillResult(svnlogentry);
                                       }
                                   } else {
                                       fillResult(svnlogentry);
                                   }
                               }
                           }

                           public void fillResult(SVNLogEntry svnlogentry) {
                          //getChangedPaths is the submitted history MAP key is the file name, value is the file details
                               history.addAll(svnlogentry.getChangedPaths().keySet());
                           }
                       });
        for (String path : history) {
            System.out.println(path);
        }
    }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326838234&siteId=291194637