You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShowNoteCommand.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 org.eclipse.jgit.api.errors.GitAPIException;
  13. import org.eclipse.jgit.api.errors.JGitInternalException;
  14. import org.eclipse.jgit.lib.Constants;
  15. import org.eclipse.jgit.lib.Ref;
  16. import org.eclipse.jgit.lib.Repository;
  17. import org.eclipse.jgit.notes.Note;
  18. import org.eclipse.jgit.notes.NoteMap;
  19. import org.eclipse.jgit.revwalk.RevCommit;
  20. import org.eclipse.jgit.revwalk.RevObject;
  21. import org.eclipse.jgit.revwalk.RevWalk;
  22. /**
  23. * Show an object note.
  24. *
  25. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-notes.html"
  26. * >Git documentation about Notes</a>
  27. */
  28. public class ShowNoteCommand extends GitCommand<Note> {
  29. private RevObject id;
  30. private String notesRef = Constants.R_NOTES_COMMITS;
  31. /**
  32. * Constructor for ShowNoteCommand.
  33. *
  34. * @param repo
  35. * the {@link org.eclipse.jgit.lib.Repository}
  36. */
  37. protected ShowNoteCommand(Repository repo) {
  38. super(repo);
  39. }
  40. /** {@inheritDoc} */
  41. @Override
  42. public Note call() throws GitAPIException {
  43. checkCallable();
  44. NoteMap map = NoteMap.newEmptyMap();
  45. RevCommit notesCommit = null;
  46. try (RevWalk walk = new RevWalk(repo)) {
  47. Ref ref = repo.exactRef(notesRef);
  48. // if we have a notes ref, use it
  49. if (ref != null) {
  50. notesCommit = walk.parseCommit(ref.getObjectId());
  51. map = NoteMap.read(walk.getObjectReader(), notesCommit);
  52. }
  53. return map.getNote(id);
  54. } catch (IOException e) {
  55. throw new JGitInternalException(e.getMessage(), e);
  56. }
  57. }
  58. /**
  59. * Sets the object id of object you want a note on
  60. *
  61. * @param id
  62. * the {@link org.eclipse.jgit.revwalk.RevObject} to show notes
  63. * for.
  64. * @return {@code this}
  65. */
  66. public ShowNoteCommand setObjectId(RevObject id) {
  67. checkCallable();
  68. this.id = id;
  69. return this;
  70. }
  71. /**
  72. * Set the {@code Ref} to read notes from.
  73. *
  74. * @param notesRef
  75. * the ref to read notes from. Note, the default value of
  76. * {@link org.eclipse.jgit.lib.Constants#R_NOTES_COMMITS} will be
  77. * used if nothing is set
  78. * @return {@code this}
  79. * @see Constants#R_NOTES_COMMITS
  80. */
  81. public ShowNoteCommand setNotesRef(String notesRef) {
  82. checkCallable();
  83. this.notesRef = notesRef;
  84. return this;
  85. }
  86. }