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  import java.util.ArrayList;
14  import java.util.Iterator;
15  import java.util.List;
16  
17  import org.eclipse.jgit.api.errors.GitAPIException;
18  import org.eclipse.jgit.api.errors.JGitInternalException;
19  import org.eclipse.jgit.lib.Constants;
20  import org.eclipse.jgit.lib.Ref;
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.RevWalk;
26  
27  /**
28   * List 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 ListNotesCommand extends GitCommand<List<Note>> {
34  
35  	private String notesRef = Constants.R_NOTES_COMMITS;
36  
37  	/**
38  	 * Constructor for ListNotesCommand.
39  	 *
40  	 * @param repo
41  	 *            the {@link org.eclipse.jgit.lib.Repository}
42  	 */
43  	protected ListNotesCommand(Repository repo) {
44  		super(repo);
45  	}
46  
47  	/** {@inheritDoc} */
48  	@Override
49  	public List<Note> call() throws GitAPIException {
50  		checkCallable();
51  		List<Note> notes = new ArrayList<>();
52  		NoteMap map = NoteMap.newEmptyMap();
53  		try (RevWalkvWalk.html#RevWalk">RevWalk walk = new RevWalk(repo)) {
54  			Ref ref = repo.findRef(notesRef);
55  			// if we have a notes ref, use it
56  			if (ref != null) {
57  				RevCommit notesCommit = walk.parseCommit(ref.getObjectId());
58  				map = NoteMap.read(walk.getObjectReader(), notesCommit);
59  			}
60  
61  			Iterator<Note> i = map.iterator();
62  			while (i.hasNext())
63  				notes.add(i.next());
64  		} catch (IOException e) {
65  			throw new JGitInternalException(e.getMessage(), e);
66  		}
67  
68  		return notes;
69  	}
70  
71  	/**
72  	 * Set the {@code Ref} to read notes from
73  	 *
74  	 * @param notesRef
75  	 *            the name of the {@code Ref} to read notes from. Note, the
76  	 *            default value of
77  	 *            {@link org.eclipse.jgit.lib.Constants#R_NOTES_COMMITS} will be
78  	 *            used if nothing is set
79  	 * @return {@code this}
80  	 * @see Constants#R_NOTES_COMMITS
81  	 */
82  	public ListNotesCommand setNotesRef(String notesRef) {
83  		checkCallable();
84  		this.notesRef = notesRef;
85  		return this;
86  	}
87  
88  }