Jones
Jones

Reputation: 1214

Using JGit, "IncorrectObjectTypeException: Object .... is not a tree

I'm trying to implement the code here two diff two git commits.

try {                                                                                      
    ObjectId head = repo.resolve(tipCommit); //String with commit hash                                              
    ObjectId base = repo.resolve(baseCommit); //String with commit hash                                              
    log.info("Resolved HEAD as " + head.toString() + ", and base as " + base.toString());  
    try {                                                                                  
        ObjectReader reader = repo.newObjectReader();                                      
        log.info(repo.getAllRefs());                                                       
        log.info(base);                                                                    
        CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();                       
        oldTreeIter.reset(reader, base);                                                   
        CanonicalTreeParser newTreeIter = new CanonicalTreeParser();                       
        newTreeIter.reset(reader, head);                                                   
        try {                                                                              
            Git git = new Git(repo);                                                       
            List<DiffEntry> diffs = git.diff()                                             
                    .setNewTree(newTreeIter)                                               
                    .setOldTree(oldTreeIter)                                               
                    .call();                                                               
            //List<File> changedFiles = filesFromDiffs(diffs, git);                        
            git.close();                                                                   
            log.info("Changed Files: \n" + changedFiles);                                  
            return changedFiles;                                                           
        } catch (Exception e) {                                                            
            log.error("Error while trying to diff commits: ", e);                          
            //close git?                                                                   
        }                                                                                  
        //Do we need to close the reader here?                                             
    } catch (Exception e) {                                                                
        log.error("Error setting up repository parsers: ", e);                             
    }                                                                                        

This results in the following output:

Resolved HEAD as AnyObjectId[6c6b900d0a2d2783a2bdcde62cde0e04ac72aea0], and base as AnyObjectId[d97d892bba40f3676628625af0ae175deaffe9f8]
[SymbolicRef[HEAD -> refs/heads/mainline=6c6b900d0a2d2783a2bdcde62cde0e04ac72aea0], Ref[refs/heads/mainline=6c6b900d0a2d2783a2bdcde62cde0e04ac72aea0], Ref[refs/remotes/matt-2016-08-19T20-43-26/mainline=6c6b900d0a2d2783a2bdcde62cde0e04ac72aea0], SymbolicRef[refs/remotes/origin/HEAD -> refs/remotes/origin/mainline=d97d892bba40f3676628625af0ae175deaffe9f8], Ref[refs/remotes/origin/mainline=d97d892bba40f3676628625af0ae175deaffe9f8]]
AnyObjectId[d97d892bba40f3676628625af0ae175deaffe9f8]
Error setting up repository parsers: 
org.eclipse.jgit.errors.IncorrectObjectTypeException: Object d97d892bba40f3676628625af0ae175deaffe9f8 is not a tree.  

As far as I can tell, both the commits I'm trying to compare are in the repo. So I don't know why the base commit isn't recognized.

Upvotes: 2

Views: 1309

Answers (2)

Pratik Shende
Pratik Shende

Reputation: 46

baseCommit should be like:

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

After you pull the latest changes, tipCommit should look like this:

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

Hope, this will solve your problem.

Upvotes: 0

R&#252;diger Herrmann
R&#252;diger Herrmann

Reputation: 20985

You are trying to feed commit id's (base, head) to the tree parsers. The latter, however, expect tree id's to be passed to reset().

If you carefully look at the code you mention, you will note that tree id's are resolved.

Upvotes: 2

Related Questions