1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.api;
44
45 import java.io.IOException;
46
47 import org.eclipse.jgit.api.errors.GitAPIException;
48 import org.eclipse.jgit.api.errors.JGitInternalException;
49 import org.eclipse.jgit.lib.CommitBuilder;
50 import org.eclipse.jgit.lib.Constants;
51 import org.eclipse.jgit.lib.ObjectId;
52 import org.eclipse.jgit.lib.ObjectInserter;
53 import org.eclipse.jgit.lib.PersonIdent;
54 import org.eclipse.jgit.lib.Ref;
55 import org.eclipse.jgit.lib.RefUpdate;
56 import org.eclipse.jgit.lib.Repository;
57 import org.eclipse.jgit.notes.Note;
58 import org.eclipse.jgit.notes.NoteMap;
59 import org.eclipse.jgit.revwalk.RevCommit;
60 import org.eclipse.jgit.revwalk.RevObject;
61 import org.eclipse.jgit.revwalk.RevWalk;
62
63
64
65
66
67
68
69 public class AddNoteCommand extends GitCommand<Note> {
70
71 private RevObject id;
72
73 private String message;
74
75 private String notesRef = Constants.R_NOTES_COMMITS;
76
77
78
79
80 protected AddNoteCommand(Repository repo) {
81 super(repo);
82 }
83
84 @Override
85 public Note call() throws GitAPIException {
86 checkCallable();
87 NoteMap map = NoteMap.newEmptyMap();
88 RevCommit notesCommit = null;
89 try (RevWalk walk = new RevWalk(repo);
90 ObjectInserter inserter = repo.newObjectInserter()) {
91 Ref ref = repo.findRef(notesRef);
92
93 if (ref != null) {
94 notesCommit = walk.parseCommit(ref.getObjectId());
95 map = NoteMap.read(walk.getObjectReader(), notesCommit);
96 }
97 map.set(id, message, inserter);
98 commitNoteMap(walk, map, notesCommit, inserter,
99 "Notes added by 'git notes add'");
100 return map.getNote(id);
101 } catch (IOException e) {
102 throw new JGitInternalException(e.getMessage(), e);
103 }
104 }
105
106
107
108
109
110
111
112
113 public AddNoteCommand setObjectId(RevObject id) {
114 checkCallable();
115 this.id = id;
116 return this;
117 }
118
119
120
121
122
123
124 public AddNoteCommand setMessage(String message) {
125 checkCallable();
126 this.message = message;
127 return this;
128 }
129
130 private void commitNoteMap(RevWalk walk, NoteMap map,
131 RevCommit notesCommit,
132 ObjectInserter inserter,
133 String msg)
134 throws IOException {
135
136 CommitBuilder builder = new CommitBuilder();
137 builder.setTreeId(map.writeTree(inserter));
138 builder.setAuthor(new PersonIdent(repo));
139 builder.setCommitter(builder.getAuthor());
140 builder.setMessage(msg);
141 if (notesCommit != null)
142 builder.setParentIds(notesCommit);
143 ObjectId commit = inserter.insert(builder);
144 inserter.flush();
145 RefUpdate refUpdate = repo.updateRef(notesRef);
146 if (notesCommit != null)
147 refUpdate.setExpectedOldObjectId(notesCommit);
148 else
149 refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
150 refUpdate.setNewObjectId(commit);
151 refUpdate.update(walk);
152 }
153
154
155
156
157
158
159
160
161
162
163 public AddNoteCommand setNotesRef(String notesRef) {
164 checkCallable();
165 this.notesRef = notesRef;
166 return this;
167 }
168
169 }