3

I'm using JGit to extract diffs between two commits but I often face a problem that JGit throws MissingObjectException and says missing unknown commit ID like this:

org.eclipse.jgit.errors.MissingObjectException: Missing unknown 9eae334e9492f55a841e6eb7ab302ff11d03ab21
    at org.eclipse.jgit.internal.storage.file.WindowCursor.open(WindowCursor.java:168)
    at org.eclipse.jgit.lib.ObjectReader.open(ObjectReader.java:236)
    at org.eclipse.jgit.revwalk.RevWalk.parseAny(RevWalk.java:890)
    at org.eclipse.jgit.revwalk.RevWalk.parseCommit(RevWalk.java:800)
    at collect.CollectTestcase.autoExtraction(CollectTestcase.java:99)*

It often happens when running the code

RevWalk walk = new RevWalk(repo);
walk.parseCommit(commitId)

Does someone knows what's wrong with it?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Jianlei Chi
  • 31
  • 1
  • 2

1 Answers1

2

JGit throws a MissingObjectException if there is no object with the given ID in the repository.

There are different types of objects in git, common are commits, blobs, and trees.

RevWalk offers API to search for specific types, like parseCommit, as well as for any type of object with parseAny. The information, that you were searching for a commit, gets lost along the way which leads to the confusing 'unknown' in the error message. It should actually read 'Missing commit abc...'.

But despite the irritating message, it means that there is no such commit. Either you are passing the ID of an object of a different type, e.g. a tree object, or there is no such object at all. You can use parseAny to see if there is an object with the given ID. If an object could be found, use getType of the returned RevObject to find out which type it is.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • 1
    Thanks for your reply, I have checked the scenario of the Exception. It happens in tomcat85, the original repo cannot be found in GitHub. I pulled from a mirror in https://github.com/lelou6666/tomcat85 However, when I try to parse the commit of `9601a937ff3b7ef5d04b2a0e950d0e44e1bb4cbd`, it happens. Even though I can visit this commit, it says "This commit doesn't belong to any branch on this repository, and may belong to a fork outside of the repository" I also tried `git checkout` and it says 'reference is not a tree' Maybe it's caused by the mirror repo? How can I parse such a commit? – Jianlei Chi Jun 24 '21 at 08:35