How do I do the equivalent of “git diff --name-status” with jgit?

前端 未结 1 1761
闹比i
闹比i 2021-01-05 05:52

I want to get a list of changed/added/deleted files between revision XXXXXX and HEAD. This is what I have so far:

String oldHash = \"a97e5553e37a25bd1a3c99ea         


        
相关标签:
1条回答
  • 2021-01-05 06:32

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

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

    and for the revision id:

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

    Once you have the tree ids you can create the tree iterators and get the diffs:

    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();
    
    0 讨论(0)
提交回复
热议问题