1
2
3
4
5
6
7
8
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
29
30
31
32
33 public class ListNotesCommand extends GitCommand<List<Note>> {
34
35 private String notesRef = Constants.R_NOTES_COMMITS;
36
37
38
39
40
41
42
43 protected ListNotesCommand(Repository repo) {
44 super(repo);
45 }
46
47
48 @Override
49 public List<Note> call() throws GitAPIException {
50 checkCallable();
51 List<Note> notes = new ArrayList<>();
52 NoteMap map = NoteMap.newEmptyMap();
53 try (RevWalk walk = new RevWalk(repo)) {
54 Ref ref = repo.findRef(notesRef);
55
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
73
74
75
76
77
78
79
80
81
82 public ListNotesCommand setNotesRef(String notesRef) {
83 checkCallable();
84 this.notesRef = notesRef;
85 return this;
86 }
87
88 }