问题描述
我有以下git命令:
git log --stat=1000 --all > gitstat.log
是否可以在JGit中实现此目的?
Is it possible to achieve this in JGit?
如果是,在JGit中写这个的等价方法是什么?
If yes, what is the equivalent way to write this in JGit?
推荐答案
访问历史记录一个存储库,JGit提供 RevWalk
。其 markStart()
方法用于指定历史应该从哪些提交开始。
存储库中的所有引用都可以通过 Repository :: getAllRefs()
获得。
To access the history of a repository, JGit provides the RevWalk
. Its markStart()
method is used to specify at which commits the history should start.All refs in a repository can be obtained with Repository::getAllRefs()
.
正确设置RevWalk实例,使用其迭代器或其 next()
方法遍历历史记录。
Once a RevWalk instance is properly set up, use its iterator or its next()
method to traverse the history.
放置它们一起看起来像这样:
Putting that together would look like this:
Collection<Ref> allRefs = repository.getAllRefs().values();
RevWalk revWalk = new RevWalk( repository );
for( Ref ref : allRefs ) {
revWalk.markStart( revWalk.parseCommit( ref.getObjectId() ));
}
for( RevCommit commit : revWalk ) {
// print commit metadata and diff
}
revWalk.close();
请注意调用 parseCommit()的RevWalk实例
必须与调用 markStart()
的那个相同。否则, RevWalk
将产生有趣的结果。
Note that the RevWalk instance that calls parseCommit()
must be the same as the one that calls markStart()
. Otherwise, the RevWalk
will yield funny results.
一旦你有一个提交(通过这个,访问它的parent)您可以使用 DiffFormatter
获取 Diff
和编辑
s告诉每个文件有多少文件和行被更改。
Once you have a commit (and through this, access to its parent) you can use the DiffFormatter
to obtain a list of Diff
s and Edit
s that tell how many files and lines per file were changed.
你可能想看一下这篇文章来开始:
You may want to look at this post to get started: How to show changes between commits with JGit
这里有一篇文章深入介绍了JGit的差异API:
And here for an article that covers JGit's diff APIs in depth: http://www.codeaffine.com/2016/06/16/jgit-diff/
这篇关于如何在JGit中编写git log --stat命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!