JAVA代码实现svn的代码量统计

本文是用JAVA 来操作svn 获取svn项目的代码提交量,主要用于获取置顶时间的提交代码量。

思路:先通过连接svn然后获取svn的log日志,然后获取每次提交的版本号,然后通过版本号来获取每次提交的代码量。

参考博客:https://blog.csdn.net/weixin_41793807/article/details/82699305

https://wiki.svnkit.com/Printing_Out_Repository_History

通过这两篇博文修改了一下 符合自己的需求,如有疑问可以留言

代码直接复制可用,但有个缺点 不是适合大项目 ,大项目工程的话还需要优化一下,比如使用多线程 线程池 来节省时间。

package com.example.demo.test;

import org.tmatesoft.svn.core.SVNDepth;
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.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.ByteArrayOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author 作者:lcz
 * @create 创建时间: 2019-03-05 14:45
 **/
public class Svn {


    public static void main(String[] args) throws Exception{
        Svn svn = new Svn("用户名", "密码", "svn地址");
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(System.currentTimeMillis());
        int count = svn.getAddLinesByDateRangeAndAuthorName(dateFormat.parse("2019-01-04"), dateFormat.parse("2019-04-01"), "你要查的人");
        System.out.println(count);
        System.out.println(System.currentTimeMillis());
    }



    String username;
    String password;
    String url;
    DefaultSVNOptions options ;
    ISVNAuthenticationManager authManager;
    SVNRepository svnRepository;
    public Svn(String username, String password, String url) {
        this.username = username;
        this.password = password;
        this.url = url;
        init();
    }

    void init() {
        options= SVNWCUtil.createDefaultOptions(true);
        options.setDiffCommand("-x -w");
        authManager= SVNWCUtil.createDefaultAuthenticationManager( username, password.toCharArray());;
        try {
            svnRepository= SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
        } catch (SVNException e) {
            e.printStackTrace();
        }
        svnRepository.setAuthenticationManager(authManager);
    }


    public int getAddLinesByDateRangeAndAuthorName(Date startDate, Date endDate,String authorName) throws Exception {
        int count=0;
        List SVNLogEntryList = getLogFromSvnRepository(startDate, endDate, authorName);
        for (SVNLogEntry svnLogEntry : SVNLogEntryList) {
            long revision = svnLogEntry.getRevision();
            count += getAddLinesByVersion(revision);
        }
        return count;
    }



    public  List getLogFromSvnRepository(Date startDate, Date endDate,String authorName) throws Exception {
        long startRevision = svnRepository.getDatedRevision(startDate);
        long endRevision = svnRepository.getDatedRevision(endDate);
        @SuppressWarnings("unchecked")
        Collection logEntries = svnRepository.log(new String[]{""}, null,
                startRevision, endRevision, true, true);
        SVNLogEntry[] svnLogEntries = logEntries.toArray(new SVNLogEntry[0]);
        List list = new ArrayList<>();
        for (SVNLogEntry svnLogEntry : svnLogEntries) {
            String author = svnLogEntry.getAuthor();
            System.out.println(author);
            if (author!=null&&author.equals(authorName)) {
                list.add(svnLogEntry);
            }
        }
        return list;
    }


    public  int getAddLinesByVersion(long version) {
        long l2 = System.currentTimeMillis();
        String logContent=null;
        try {
            SVNDiffClient diffClient = new SVNDiffClient(authManager, options);
            diffClient.setGitDiffFormat(true);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            diffClient.doDiff(SVNURL.parseURIEncoded(url),
                    SVNRevision.create(version-1),
                    SVNURL.parseURIEncoded(url),
                    SVNRevision.create(version),
                    SVNDepth.UNKNOWN, true, byteArrayOutputStream);
            byte[] bytes = byteArrayOutputStream.toByteArray();
            logContent= new String(bytes);
            byteArrayOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        long l3 = System.currentTimeMillis();
        System.out.println("通过版本号获取代码增加行数时间:"+(l3 - l2));
        return getTotalAddLinesFromLogContent(logContent);
    }

    public int getTotalAddLinesFromLogContent(String logContent) {
        int totalAddLine=0;
        for (String s : logContent.split("\n")) {
            for (char c : s.toCharArray()) {
                if(c=='+'){
                    totalAddLine++;
                }else if (c == '-') {
                    totalAddLine--;
                }else {
                    break;
                }
            }
        }
        return totalAddLine;
    }
}

看完如果有地方需要改正或有更好的建议请留言 谢谢

你可能感兴趣的:(java,svn,统计svn代码量)