目录
? ? ? ? 小伙伴们大家好,今天搭配ChatGPT搞了一个可以统计每天代码提交量的脚本,一起来看看吧
? ? ? ? 开发工具:IDEA,版本管理工具:Git,项目依赖管理工具:Maven,Java环境:1.8
? ? ? ? 1.1 使用Maven作为项目的构建工具,配置要引入的依赖等待maven自动下载
? ? ? ? 注意:可能出现Maven提示找不到这个依赖库
org.eclipse.jgit:org.eclipse.jgit:jar:5.11.0.202105131744-r was not found in https://repo.maven.apache.org/maven2 during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced
? ? ? ? 解决方法:到官网下载jar包,手动导入(注意与自己的jdk版本兼容的),我这里选择的5.版本的,下载以后,就跟上篇文章讲的自定义jar包的引入方式相同了,新建一个与src同级的包,将jar包复制到这里,右键jar包,点击“add libiary"即可,手动引入也就完成了
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.11.0.202105131744-r</version>
</dependency>
? ? ? ? 这里除了本地使用,也可以封装为jar包,供别人使用,封装jar包方法跟这个如出一辙
【Java封装Jar包】将自己的代码封装为一个jar包??以便在别的项目可以直接引用使用-CSDN博客
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.*;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.patch.FileHeader;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.List;
public class TestCountByGit {
public static void main(String[] args) {
// 1. 获取今天的日期
LocalDate today = LocalDate.now();
// 2. 打开项目文件夹
String projectFolder = "D:\\Projects\\GithubTest\\springboot_hb";
Path folderPath = Paths.get(projectFolder);
// 3. 获取昨天的日期
LocalDate yesterday = today.minusDays(1);
// 4. 统计行数
int totalLines = countLinesSinceYesterday(folderPath, yesterday);
// 5. 输出统计结果
String result = today.toString() + " 提交的代码行数为:" + totalLines;
System.out.println(result);
}
private static int countLinesSinceYesterday(Path folderPath, LocalDate yesterday) {
int totalLines = 0;
try (Git git = Git.open(folderPath.toFile())) {
// 使用JGit获取Git提交记录
LogCommand logCommand = git.log();
Iterable<RevCommit> commits = logCommand.call();
for (RevCommit commit : commits) {
// 获取提交的时间戳
PersonIdent author = commit.getAuthorIdent();
long commitTime = author.getWhen().getTime();
// 将时间戳转换为LocalDate对象
LocalDate commitDate = LocalDate.ofEpochDay(commitTime / (24 * 60 * 60 * 1000));
// 判断提交日期是否在昨天之后
if (commitDate.isAfter(yesterday)) {
// 获取提交的代码更改
try (RevWalk revWalk = new RevWalk(git.getRepository())) {
RevCommit parentCommit = revWalk.parseCommit(commit.getParent(0));
int linesChanged = getLinesChanged(git, parentCommit.getId(), commit.getId());
totalLines += linesChanged;
}
}
}
} catch (IOException | GitAPIException e) {
e.printStackTrace();
}
return totalLines;
}
private static int getLinesChanged(Git git, ObjectId oldCommit, ObjectId newCommit) throws IOException {
// 使用JGit获取两个提交之间的代码更改,并统计新增行数
DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE);
diffFormatter.setRepository(git.getRepository());
diffFormatter.setDiffComparator(RawTextComparator.DEFAULT);
diffFormatter.setDetectRenames(true);
List<DiffEntry> diffs = diffFormatter.scan(oldCommit, newCommit);
int linesChanged = 0;
for (DiffEntry diff : diffs) {
FileHeader fileHeader = diffFormatter.toFileHeader(diff);
EditList editList = fileHeader.toEditList();
for (Edit edit : editList) {
linesChanged += edit.getEndB() - edit.getBeginB();
}
}
return linesChanged;
}
}
这个分支里面总共有四个提交 ,其中一个是今天提交的,运行结果如下,统计到的代码行数为257行,可能这些行数不一定全都有内容,但是简单的统计功能还是实现了的
? ? ? ? main方法分析:这个文件路径指的是项目包的路径
????????countLinesSinceYesterday方法分析
????????getLinesChanged方法分析
? ? ? ? ?当前脚本只是统计本地的分支,通过JGit库打开本地的Git仓库统计更改,不涉及对于远程的分支或者别人的提交
? ? ? ? 想要统计远程分支的提交,可以改动方法,大概就是先获取远程仓库的引用,然后拉取到本地,再进行操作????????
? ? ? ? 后面有时间的话,改一下方法结构,封装成一个jar包给大家方便使用