Right now I am using git log:
try (Git git = new Git(repo)) {
Iterable<RevCommit> commits = git.log().all().call();
for (RevCommit commit : commits) {
System.out.println("LogCommit: " + commit);
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.reset(commit.getTree().getId());
while( treeWalk.next() ) {
String filename = treeWalk.getPathString();
Integer currentCount = 0;
if(fileChanges.containsKey(filename)) {
currentCount = fileChanges.get(filename);
}
fileChanges.put(filename, new Integer(currentCount + 1));
}
treeWalk.close();
}
}
The problem with this is it lists all of the files in the commit, whether or not they have been changed. Suppose I change a css file, commit the repo and then run this code, every other file in the entire repo will have the number of times changed incremented by one.
EDIT: I believe this snippet of code does it:
try (Git git = new Git(repo)) {
DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE);
df.setRepository( git.getRepository() );
Iterable<RevCommit> commits = git.log().all().call();
for (RevCommit commit : commits) {
if(commit.getParents().length != 0) {
System.out.println("LogCommit: " + commit);
List<DiffEntry> entries = df.scan(commit.getId(), commit.getParent(0).getId());
for( DiffEntry entry : entries ) {
String filename = entry.getPath(DiffEntry.Side.NEW);
if(!filename.equals("/dev/null")) {
Integer currentCount = 0;
if(fileChanges.containsKey(filename)) {
currentCount = fileChanges.get(filename);
}else {
System.out.println(" DiffEntry: " +entry.getPath(DiffEntry.Side.NEW));
}
fileChanges.put(filename, new Integer(currentCount + 1));
}
}
}
}
}