现在我正在使用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();
}
}
这个问题是它列出了提交中的所有文件,无论它们是否已被更改。假设我更改了一个css文件,提交了repo然后运行了这个代码,整个仓库中的每个其他文件的更改次数都会增加一次。
编辑:我相信这段代码可以做到:
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));
}
}
}
}
}
答案 0 :(得分:3)
我认为你要找的是所有提交的各自差异。以下帖子应该有助于列出所有提交:How to write git log --stat command in JGit
要获得两次提交之间的差异,请查看此处:How to show changes between commits with JGit
为了更深入地讨论JGit的diff API,我前段时间写了一篇文章:http://www.codeaffine.com/2016/06/16/jgit-diff/