AddNoteCommand.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.CommitBuilder;
  15. import org.eclipse.jgit.lib.Constants;
  16. import org.eclipse.jgit.lib.ObjectId;
  17. import org.eclipse.jgit.lib.ObjectInserter;
  18. import org.eclipse.jgit.lib.PersonIdent;
  19. import org.eclipse.jgit.lib.Ref;
  20. import org.eclipse.jgit.lib.RefUpdate;
  21. import org.eclipse.jgit.lib.Repository;
  22. import org.eclipse.jgit.notes.Note;
  23. import org.eclipse.jgit.notes.NoteMap;
  24. import org.eclipse.jgit.revwalk.RevCommit;
  25. import org.eclipse.jgit.revwalk.RevObject;
  26. import org.eclipse.jgit.revwalk.RevWalk;

  27. /**
  28.  * Add object notes.
  29.  *
  30.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-notes.html"
  31.  *      >Git documentation about Notes</a>
  32.  */
  33. public class AddNoteCommand extends GitCommand<Note> {

  34.     private RevObject id;

  35.     private String message;

  36.     private String notesRef = Constants.R_NOTES_COMMITS;

  37.     /**
  38.      * Constructor for AddNoteCommand
  39.      *
  40.      * @param repo
  41.      *            the {@link org.eclipse.jgit.lib.Repository}
  42.      */
  43.     protected AddNoteCommand(Repository repo) {
  44.         super(repo);
  45.     }

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

  68.     /**
  69.      * Sets the object id of object you want a note on. If the object already
  70.      * has a note, the existing note will be replaced.
  71.      *
  72.      * @param id
  73.      *            a {@link org.eclipse.jgit.revwalk.RevObject}
  74.      * @return {@code this}
  75.      */
  76.     public AddNoteCommand setObjectId(RevObject id) {
  77.         checkCallable();
  78.         this.id = id;
  79.         return this;
  80.     }

  81.     /**
  82.      * Set the notes message
  83.      *
  84.      * @param message
  85.      *            the notes message used when adding a note
  86.      * @return {@code this}
  87.      */
  88.     public AddNoteCommand setMessage(String message) {
  89.         checkCallable();
  90.         this.message = message;
  91.         return this;
  92.     }

  93.     static void commitNoteMap(Repository r, String ref, RevWalk walk,
  94.             NoteMap map,
  95.             RevCommit notesCommit,
  96.             ObjectInserter inserter,
  97.             String msg)
  98.             throws IOException {
  99.         // commit the note
  100.         CommitBuilder builder = new CommitBuilder();
  101.         builder.setTreeId(map.writeTree(inserter));
  102.         builder.setAuthor(new PersonIdent(r));
  103.         builder.setCommitter(builder.getAuthor());
  104.         builder.setMessage(msg);
  105.         if (notesCommit != null)
  106.             builder.setParentIds(notesCommit);
  107.         ObjectId commit = inserter.insert(builder);
  108.         inserter.flush();
  109.         RefUpdate refUpdate = r.updateRef(ref);
  110.         if (notesCommit != null)
  111.             refUpdate.setExpectedOldObjectId(notesCommit);
  112.         else
  113.             refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
  114.         refUpdate.setNewObjectId(commit);
  115.         refUpdate.update(walk);
  116.     }

  117.     /**
  118.      * Set name of a {@code Ref} to read notes from
  119.      *
  120.      * @param notesRef
  121.      *            the ref to read notes from. Note, the default value of
  122.      *            {@link org.eclipse.jgit.lib.Constants#R_NOTES_COMMITS} will be
  123.      *            used if nothing is set
  124.      * @return {@code this}
  125.      * @see Constants#R_NOTES_COMMITS
  126.      */
  127.     public AddNoteCommand setNotesRef(String notesRef) {
  128.         checkCallable();
  129.         this.notesRef = notesRef;
  130.         return this;
  131.     }

  132. }