javagitjgitgit-notes

How to read Git notes using JGit given a commit-sha


I am trying to read Git Notes information from a custom ref refs/notes/abcd of a particular commit in a repository using JGit

Here is what I tried:

Repository repository = repositoryManager.openRepository(repoName);
Git git = new Git(repository);
ObjectId oid = repository.resolve("5740b142a7b5f66768a2d904b267dccaef1a095f");
Note note = git.notesShow().setNotesRef("refs/notes/abcd").setObjectId(oid).call();
ObjectLoader loader = repository.open(note.getData());
byte[] data = loader.getBytes();
System.out.println(new String(data, "utf-8"));

I get the following compilation error:

error: incompatible types: org.eclipse.jgit.lib.ObjectId cannot be converted to org.eclipse.jgit.revwalk.RevObject

How do I pass a RevObject variable to Git setObjectId() given a commit-sha string?


Solution

  • With a RevWalk, the object id can be parsed and the resulting RevCommit can be passed to the ShowNoteCommand.

    For example:

    RevCommit commit;
    try( RevWalk revWalk = new RevWalk( repository ) ) {
      commit = revWalk.parseCommit( oid );
    }
    
    git.notesShow().setObjectId( commit )...