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