RemoveNoteCommand.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 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.ObjectInserter;
  16. import org.eclipse.jgit.lib.Ref;
  17. import org.eclipse.jgit.lib.Repository;
  18. import org.eclipse.jgit.notes.Note;
  19. import org.eclipse.jgit.notes.NoteMap;
  20. import org.eclipse.jgit.revwalk.RevCommit;
  21. import org.eclipse.jgit.revwalk.RevObject;
  22. import org.eclipse.jgit.revwalk.RevWalk;

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

  30.     private RevObject id;

  31.     private String notesRef = Constants.R_NOTES_COMMITS;

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

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     public Note call() throws GitAPIException {
  46.         checkCallable();
  47.         try (RevWalk walk = new RevWalk(repo);
  48.                 ObjectInserter inserter = repo.newObjectInserter()) {
  49.             NoteMap map = NoteMap.newEmptyMap();
  50.             RevCommit notesCommit = null;
  51.             Ref ref = repo.exactRef(notesRef);
  52.             // if we have a notes ref, use it
  53.             if (ref != null) {
  54.                 notesCommit = walk.parseCommit(ref.getObjectId());
  55.                 map = NoteMap.read(walk.getObjectReader(), notesCommit);
  56.             }
  57.             map.set(id, null, inserter);
  58.             AddNoteCommand.commitNoteMap(repo, notesRef, walk, map, notesCommit,
  59.                     inserter,
  60.                     "Notes removed by 'git notes remove'"); //$NON-NLS-1$
  61.             return map.getNote(id);
  62.         } catch (IOException e) {
  63.             throw new JGitInternalException(e.getMessage(), e);
  64.         }
  65.     }

  66.     /**
  67.      * Sets the object id of object you want to remove a note
  68.      *
  69.      * @param id
  70.      *            the {@link org.eclipse.jgit.revwalk.RevObject} to remove a
  71.      *            note from.
  72.      * @return {@code this}
  73.      */
  74.     public RemoveNoteCommand setObjectId(RevObject id) {
  75.         checkCallable();
  76.         this.id = id;
  77.         return this;
  78.     }

  79.     /**
  80.      * Set the name of the <code>Ref</code> to remove a note from.
  81.      *
  82.      * @param notesRef
  83.      *            the {@code Ref} to read notes from. Note, the default value of
  84.      *            {@link org.eclipse.jgit.lib.Constants#R_NOTES_COMMITS} will be
  85.      *            used if nothing is set
  86.      * @return {@code this}
  87.      * @see Constants#R_NOTES_COMMITS
  88.      */
  89.     public RemoveNoteCommand setNotesRef(String notesRef) {
  90.         checkCallable();
  91.         this.notesRef = notesRef;
  92.         return this;
  93.     }

  94. }