Search code examples
nodegit

How to get commit and tree using Revparse with nodegit?


I'm trying to use nodegit to get the tree of a commit based on a reference or oid using Revparse, I thought the following code would work, however I'm getting getTree undefined errors:

return git.Repository.open(path_to_repo)
    .then((repo) => git.Revparse.single(repo, "other"))
    .then((commit) => commit.getTree());

How do I cast the object returned by Revparse to a commit?


Solution

  • So RevParse.single returns an Object which really is just a low level libgit2 object. That will have to have its type checked to make sure it's an Object.TYPE.COMMIT. If it is then you can grab the OID and use that to get the actual Commit.

    Since NodeGit is really just bindings to libgit2 there's isn't (currently) any way to really cast an object from 1 thing to another. You have to do the lookups yourself.

    Now if you're just trying to get the commit that a given reference points to you could modify your code to this:

    return git.Repository.open(path_to_repo)
        .then((repo) => repo.getReferenceCommit("other"))
        .then((commit) => commit.getTree());