Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write git log --stat command in JGit

Tags:

java

jgit

I have following git command:

git log --stat=1000 --all > gitstat.log

Is it possible to achieve this in JGit?

If yes, what is the equivalent way to write this in JGit?

like image 572
Maytham Avatar asked Nov 24 '16 23:11

Maytham


People also ask

What is git log -- stat?

The --stat flag causes git log to display. the files that were modified in each commit. the number of lines added or removed. a summary line with the total number of files and lines changed.

What is git log command?

Git log is a utility tool to review and read a history of everything that happens to a repository. Multiple options can be used with a git log to make history more specific. Generally, the git log is a record of commits.

How do I check git log in terminal?

The command for that would be git log –oneline -n where n represents the number up to which commit you to want to see the logs.

What is git log -- Oneline?

git log --oneline --graph. The git log graph allows you to visualize branches. To include extra data about all branches, commits and authors in the graph you can add the decorate and all switches: git log –all –decorate –oneline –graph.


1 Answers

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().

Once a RevWalk instance is set up, use its iterator or its next() method to traverse the history.

Putting that together would look like this:

try (RevWalk revWalk = new RevWalk(repository)) {
  for (Ref ref : repository.getAllRefs().values()) {
    revWalk.markStart(revWalk.parseCommit(ref.getObjectId()));
  }
  for (RevCommit commit : revWalk) {
    // print commit metadata and diff
  }
}

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.

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.

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

And here for an article that covers JGit's diff APIs in depth: http://www.codeaffine.com/2016/06/16/jgit-diff/

like image 50
Rüdiger Herrmann Avatar answered Sep 17 '22 15:09

Rüdiger Herrmann