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<SVNLogEntry> SVNLogEntryList = getLogFromSvnRepository(startDate, endDate, authorName);
        for (SVNLogEntry svnLogEntry : SVNLogEntryList) {
            long revision = svnLogEntry.getRevision();
            count += getAddLinesByVersion(revision);
        }
        return count;
    }



    public  List<SVNLogEntry> getLogFromSvnRepository(Date startDate, Date endDate,String authorName) throws Exception {
        long startRevision = svnRepository.getDatedRevision(startDate);
        long endRevision = svnRepository.getDatedRevision(endDate);
        @SuppressWarnings("unchecked")
        Collection<SVNLogEntry> logEntries = svnRepository.log(new String[]{""}, null,
                startRevision, endRevision, true, true);
        SVNLogEntry[] svnLogEntries = logEntries.toArray(new SVNLogEntry[0]);
        List<SVNLogEntry> 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;
    }
}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用PHP的SVN库来连接到SVN服务器并获取代码库的统计信息。以下是一个示例代码,可用于获取某个版本号的代码统计信息: ```php <?php // SVN服务器设置 $svn_server = "svn://svn.example.com/myproject"; $svn_username = "username"; $svn_password = "password"; // 获取SVN库 $svn = svn_connect($svn_server); svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_USERNAME, $svn_username); svn_auth_set_parameter(SVN_AUTH_PARAM_DEFAULT_PASSWORD, $svn_password); // 指定版本号 $revision = 1234; // 统计代码 $command = "svn diff -r " . ($revision - 1) . ":" . $revision . " " . $svn_server . " --summarize"; $output = shell_exec($command); // 解析输出结果 $lines_added = 0; $lines_deleted = 0; $files_changed = 0; $lines = explode("\n", $output); foreach ($lines as $line) { if (preg_match('/^[ADUMR]\s+(.*)$/', $line, $matches)) { $files_changed++; $file = $matches[1]; if (preg_match('/\.php$/', $file)) { $command = "svn diff -x -w -r " . ($revision - 1) . ":" . $revision . " " . $svn_server . "/" . $file . " | grep -E '^[+|\-]' | wc -l"; $output = trim(shell_exec($command)); $lines_added += (int) $output; $command = "svn diff -x -w -r " . ($revision - 1) . ":" . $revision . " " . $svn_server . "/" . $file . " | grep -E '^\-' | wc -l"; $output = trim(shell_exec($command)); $lines_deleted += (int) $output; } } } // 输出统计信息 echo "Files changed: " . $files_changed . "\n"; echo "Lines added: " . $lines_added . "\n"; echo "Lines deleted: " . $lines_deleted . "\n"; ?> ``` 在以上代码中,首先需要设置SVN服务器的地址、用户名和密码。然后,使用`svn_connect()`函数连接到SVN服务器,并使用`svn_auth_set_parameter()`函数设置用户名和密码。接下来,使用`svn diff`命令获取指定版本号的代码库变更信息,并使用`shell_exec()`函数执行该命令。然后,解析输出结果,统计代码并输出统计信息。 在解析输出结果时,使用了正则表达式来匹配文件路径和变更类型,并使用`svn diff`命令获取指定文件的代码变更信息。然后,使用`grep`命令和正则表达式来匹配添加和删除的行,并使用`wc -l`命令统计行数。最后,累加添加和删除的行数,并输出统计信息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值