ListNotesCommand.java

  1. /*
  2.  * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.api;

  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Iterator;
  14. import java.util.List;

  15. import org.eclipse.jgit.api.errors.GitAPIException;
  16. import org.eclipse.jgit.api.errors.JGitInternalException;
  17. import org.eclipse.jgit.lib.Constants;
  18. import org.eclipse.jgit.lib.Ref;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.eclipse.jgit.notes.Note;
  21. import org.eclipse.jgit.notes.NoteMap;
  22. import org.eclipse.jgit.revwalk.RevCommit;
  23. import org.eclipse.jgit.revwalk.RevWalk;

  24. /**
  25.  * List object notes.
  26.  *
  27.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-notes.html"
  28.  *      >Git documentation about Notes</a>
  29.  */
  30. public class ListNotesCommand extends GitCommand<List<Note>> {

  31.     private String notesRef = Constants.R_NOTES_COMMITS;

  32.     /**
  33.      * Constructor for ListNotesCommand.
  34.      *
  35.      * @param repo
  36.      *            the {@link org.eclipse.jgit.lib.Repository}
  37.      */
  38.     protected ListNotesCommand(Repository repo) {
  39.         super(repo);
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public List<Note> call() throws GitAPIException {
  44.         checkCallable();
  45.         List<Note> notes = new ArrayList<>();
  46.         NoteMap map = NoteMap.newEmptyMap();
  47.         try (RevWalk walk = new RevWalk(repo)) {
  48.             Ref ref = repo.findRef(notesRef);
  49.             // if we have a notes ref, use it
  50.             if (ref != null) {
  51.                 RevCommit notesCommit = walk.parseCommit(ref.getObjectId());
  52.                 map = NoteMap.read(walk.getObjectReader(), notesCommit);
  53.             }

  54.             Iterator<Note> i = map.iterator();
  55.             while (i.hasNext())
  56.                 notes.add(i.next());
  57.         } catch (IOException e) {
  58.             throw new JGitInternalException(e.getMessage(), e);
  59.         }

  60.         return notes;
  61.     }

  62.     /**
  63.      * Set the {@code Ref} to read notes from
  64.      *
  65.      * @param notesRef
  66.      *            the name of the {@code Ref} to read notes from. Note, the
  67.      *            default value of
  68.      *            {@link org.eclipse.jgit.lib.Constants#R_NOTES_COMMITS} will be
  69.      *            used if nothing is set
  70.      * @return {@code this}
  71.      * @see Constants#R_NOTES_COMMITS
  72.      */
  73.     public ListNotesCommand setNotesRef(String notesRef) {
  74.         checkCallable();
  75.         this.notesRef = notesRef;
  76.         return this;
  77.     }

  78. }