如何在JGit中编写git log --stat命令 [英] How to write git log --stat command in JGit

查看:1468
本文介绍了如何在JGit中编写git log --stat命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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 Diffs and Edits that tell how many files and lines per file were changed.

你可能想看一下这篇文章来开始:如何显示JGit提交之间的变化

You may want to look at this post to get started: How to show changes between commits with JGit

这里有一篇文章深入介绍了JGit的差异API: http://www.codeaffine.com/2016/06/16/jgit-diff/

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命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆