SVN Java 客户端 svnkit 查询两个日期或版本之间的文件差异,类似 svn diff 命令

/*
 * Copyleft                      
 */
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.internal.wc.SVNFileUtil;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;

/**
 * 
 * @author wongtp
 * @date 2023-09-12
 */
public final class SvnUtils {
    
    private SvnUtils() { }
    
    /**
     * @param url  需要查询变更日志的根路径
     * @param username  SVN 用户名,注意需要对 url 有访问权限
     * @param password  SVN 密码
     * @param oldRevisionDate  变更日志开始时间
     * @param newRevisionDate  变更日志结束时间
     * @return  返回变更日志列表,逐行输出
     * @throws SVNException
     */
    public static List<String> getChangeLog(String url, String username, String password, Date oldRevisionDate, Date newRevisionDate) throws SVNException, IOException {
        DefaultSVNOptions svnOption = new DefaultSVNOptions();
        // 关掉认证信息缓存到磁盘
        svnOption.setAuthStorageEnabled(false);
        // 关闭自动查找配置
        svnOption.setUseAutoProperties(false);
        // 貌似没啥用,关掉关掉
        svnOption.setKeepLocks(false);
        
        SVNClientManager clientManager = SVNClientManager.newInstance(svnOption, username, password);
        try (ByteArrayOutputStream result = new ByteArrayOutputStream()) {
            SVNURL svnUrl = SVNURL.parseURIEncoded(url);
            SVNRevision oldVersion = SVNRevision.create(oldRevisionDate);
            SVNRevision newVersion = SVNRevision.create(newRevisionDate);
            
            clientManager.getDiffClient().doDiff(svnUrl, newVersion, oldVersion, newVersion, SVNDepth.INFINITY, false, result);
            
            Charset charset = SVNFileUtil.isWindows ? Charset.forName("gbk") : StandardCharsets.UTF_8;
            return Arrays.asList(new String(result.toByteArray(), charset).split(System.lineSeparator()));
        } finally {
            clientManager.dispose();
        }
    }
}

你可能感兴趣的:(svn,java,python,svnkit,diff)