When we do git log --shortstat
we get the number of lines inserted, deleted, and changed. Something like:
1 file changed, 9 insertions(+), 3 deletions(-)
Please help me with getting the number of lines inserted, deleted, and changed.
I am doing a repository clone to get git project on local machine. Here is the same code:
RepoClone repoClone = new RepoClone();
repoClone.repoCloner();
repository = builder.setGitDir(repoClone.repoDir).setMustExist(true).build();
I am even able to get a TreeWalk
:
TreeWalk treeWalk = getCommitsTreeWalk();
I am able to retrieve file name, count of number of commits per file, LOC, and the number of developers who worked on each xml/ java file.
while (treeWalk.next()) {
if (treeWalk.getPathString().endsWith(".xml") || treeWalk.getPathString().endsWith(".java")) {
jsonDataset = new JSONObject();
countDevelopers = new HashSet<String>();
count = 0;
logs = new Git(repository).log().addPath(treeWalk.getPathString()).call();
for (RevCommit rev: logs) {
countDevelopers.add(rev.getAuthorIdent().getEmailAddress());
count++;
}
jsonDataset.put("FileName", treeWalk.getPathString());
jsonDataset.put("CountDevelopers", countDevelopers.size());
jsonDataset.put("CountCommits", count);
jsonDataset.put("LOC", countLines(treeWalk.getPathString()));
commitDetails.put(jsonDataset);
}
}
Now, I want to retrieve the number of lines inserted and deleted for each file.
The following code snippet compares two commits and prints the changes. diffFormatter.scan()
returns a list of DiffEntry
s which each describes an added, deleted or modified file. Each of the diff entries in turn has a list of HunkHeader
s which desribe the changes within that file.
// Create two commits to be compared
File file = new File( git.getRepository().getWorkTree(), "file.txt" );
writeFile( file, "line1\n" );
RevCommit oldCommit = commitChanges();
writeFile( file, "line1\nline2\n" );
RevCommit newCommit = commitChanges();
// Obtain tree iterators to traverse the tree of the old/new commit
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset( reader, oldCommit.getTree() );
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset( reader, newCommit.getTree() );
// Use a DiffFormatter to compare new and old tree and return a list of changes
DiffFormatter diffFormatter = new DiffFormatter( DisabledOutputStream.INSTANCE );
diffFormatter.setRepository( git.getRepository() );
diffFormatter.setContext( 0 );
List<DiffEntry> entries = diffFormatter.scan( newTreeIter, oldTreeIter );
// Print the contents of the DiffEntries
for( DiffEntry entry : entries ) {
System.out.println( entry );
FileHeader fileHeader = diffFormatter.toFileHeader( entry );
List<? extends HunkHeader> hunks = fileHeader.getHunks();
for( HunkHeader hunk : hunks ) {
System.out.println( hunk );
}
}
I think with the information provided by DiffEntry
and HunkHeader
you should be able to get the desired --shortstat
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With