Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when resolving a non-existent commit #298

Closed
Vampire opened this issue Jul 31, 2019 · 1 comment
Closed

NullPointerException when resolving a non-existent commit #298

Vampire opened this issue Jul 31, 2019 · 1 comment
Labels

Comments

@Vampire
Copy link
Contributor

Vampire commented Jul 31, 2019

I tried to get a non-existent commit.
In my case it was HEAD on an orphaned branch where naturally no head reference is present, the same should happen on a newly initialized repository though.
In that situation I called Grgit.head() which resulted in the following exception:

Caused by: java.lang.NullPointerException
        at org.eclipse.jgit.lib.Repository.resolve(Repository.java:531)
        at org.eclipse.jgit.lib.Repository.resolve(Repository.java:492)
        at org.eclipse.jgit.lib.Repository$resolve.call(Unknown Source)
        at org.eclipse.jgit.lib.Repository$resolve.call(Unknown Source)
        at org.ajoberstar.grgit.util.JGitUtil.resolveObject(JGitUtil.groovy:43)
        at org.ajoberstar.grgit.util.JGitUtil.resolveCommit(JGitUtil.groovy:86)
        at org.ajoberstar.grgit.util.JGitUtil$resolveCommit$0.callStatic(Unknown Source)
        at org.ajoberstar.grgit.util.JGitUtil.resolveCommit(JGitUtil.groovy:87)
        at org.ajoberstar.grgit.util.JGitUtil$resolveCommit$0.call(Unknown Source)
        at org.ajoberstar.grgit.service.ResolveService.toCommit(ResolveService.groovy:78)
        at org.ajoberstar.grgit.service.ResolveService$toCommit.call(Unknown Source)
        at org.ajoberstar.grgit.Grgit.head(Grgit.groovy:138)

The reason is in org.ajoberstar.grgit.util.JGitUtil#resolveCommit(org.ajoberstar.grgit.Repository, java.lang.String):

  static Commit resolveCommit(Repository repo, String revstr) {
    ObjectId id = resolveObject(repo, revstr)
    return resolveCommit(repo, id)
  }

resolveObject for a string that cannot be resolved to an object id returns null according to situation and also its JavaDoc.
resolveCommit(repo, id) then recursively calls itself (not the object id overload like it would be in Java) which then results in the call resolveObject(repo, null) which then ultimately throws the NullPointerException.

I guess the correct fix would be

  static Commit resolveCommit(Repository repo, String revstr) {
    ObjectId id = resolveObject(repo, revstr)
    return id ? resolveCommit(repo, id) : null
  }
@ajoberstar ajoberstar added the bug label Aug 4, 2019
@ajoberstar ajoberstar added this to the 3.2.0 milestone Aug 4, 2019
@ajoberstar
Copy link
Owner

Thanks for reporting, will fix in next release.

@ajoberstar ajoberstar modified the milestones: 3.2.0, 4.0.0 Nov 15, 2019
@ajoberstar ajoberstar removed this from the 4.0.0 milestone Apr 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants