Here's how to find commit changes based on all parent commits
var tree = new TreeWalk(repository) tree.addTree(commit.getTree) commit.getParents foreach { parent => tree.addTree(parent.getTree) } tree.setFilter(TreeFilter.ANY_DIFF)
(scala code)
Note that TreeFilter.ANY_DIFF works for a single tree runner and returns all the elements available in the root commit.
Then you have to iterate over the tree to see if your file is in the given delta (this is pretty simple).
while (tree.next) if (tree.getDepth == cleanPath.size) { // we are at the right level, do what you want } else { if (tree.isSubtree && name == cleanPath(tree.getDepth)) { tree.enterSubtree } } }
(cleanPath is clean in the repo path, divided by '/')
Now wrap this code in a RevWalk.next loop and you will get commits and files modified by commit.
You can use a different filter than ANY_DIFF because ANY_DIFF is true if one tree is different. This is a little contrary to intuition in the case of merging, where the blob has not changed compared to all the parent trees. So, here is the ALL_DIFF core, which will display only those elements that are different from all the parent trees:
override def include(walker: TreeWalk): Boolean = { val n = walker.getTreeCount(); if (n == 1) { return true; } val m = walker.getRawMode(0) var i = 1 while (i < n) { if (walker.getRawMode(i) == m && walker.idEqual(i, 0)) { return false } i += 1 } true }
(scala code derived from AnyDiffFilter)
treffer
source share