How to execute the equivalent of "git diff -name-status" using jgit?

I want to get a list of modified / added / deleted files between versions XXXXXX and HEAD. This is what I still have:

String oldHash = "a97e5553e37a25bd1a3c99eab303145baed08dbd"; Git git = Git.open(new File("/tmp/jgit")); Repository repository = git.getRepository(); ObjectId old = repository.resolve(oldHash); ObjectId head = repository.resolve("HEAD"); // how do i get the trees from the obj. id? List<<DiffEntry> diffs = git.diff().setNewTree(null).setOldTree(null).call(); for(DiffEntry diff : diffs) { // do stuff } 

Is this correct, and if so, how do I get the trees needed for git.diff ()?

+7
source share
1 answer

You can get the tree id for HEAD and hash by calling:

 ObjectId head = repository.resolve("HEAD^{tree}"); 

and for version id:

 ObjectId old = repository.resolve(oldHash + "^{tree}"); 

Once you have tree identifiers, you can create tree iterators and get diff:

 ObjectReader reader = repository.newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.reset(reader, oldId); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.reset(reader, headId); List<DiffEntry> diffs= git.diff() .setNewTree(newTreeIter) .setOldTree(oldTreeIter) .call(); 
+9
source

All Articles