Question

Is there a way to fetch the note content of every note in a git repository using jgit. I tried couple of approaches but none of it seem to work.

The below approach, doest list the note content nor the commit ID.

    FileRepository repo;
    try {
        repo = new FileRepository("C:\\example\\repos");
        RevWalk walk = new RevWalk(repo);
        NoteMap map = NoteMap.newEmptyMap();
        Ref ref = repo.getRef("refs/notes/commits");

        if (ref != null) {
            RevCommit notesCommit = walk.parseCommit(ref.getObjectId());
            map = NoteMap.read(walk.getObjectReader(), notesCommit);
            map.getNote(notesCommit.getId());
            Iterator<Note> it = map.iterator();

            while (it.hasNext()) {

                System.out.println(it.next());
            }

Please let me know?

Was it helpful?

Solution

jgit provides a ListNotes command which can provide either specific or all notes in the repository. From there you can then read the not-content.

You can see an example of how this is used at the jgit-cookbook in the Snippet ListNotes.java.

The actual data can then be fetched by a normal Blob-read because notes are stored as normal blobs like file contents, i.e.

ObjectLoader loader = repository.open(note.getData());
loader.copyTo(System.out);

OTHER TIPS

If you want to get the note contents as a string without printing it on a standard output(console), you can use following lines of code.

ObjectLoader loader = repository.open(note.getData());
byte[] bytes = loader.getBytes();
String s = new String(bytes);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top