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 RemoveNoteCommand extends GitCommand<Note> {
70
71 private RevObject id;
72
73 private String notesRef = Constants.R_NOTES_COMMITS;
74
75
76
77
78 protected RemoveNoteCommand(Repository repo) {
79 super(repo);
80 }
81
82 @Override
83 public Note call() throws GitAPIException {
84 checkCallable();
85 try (RevWalk walk = new RevWalk(repo);
86 ObjectInserter inserter = repo.newObjectInserter()) {
87 NoteMap map = NoteMap.newEmptyMap();
88 RevCommit notesCommit = null;
89 Ref ref = repo.exactRef(notesRef);
90
91 if (ref != null) {
92 notesCommit = walk.parseCommit(ref.getObjectId());
93 map = NoteMap.read(walk.getObjectReader(), notesCommit);
94 }
95 map.set(id, null, inserter);
96 commitNoteMap(walk, map, notesCommit, inserter,
97 "Notes removed by 'git notes remove'");
98 return map.getNote(id);
99 } catch (IOException e) {
100 throw new JGitInternalException(e.getMessage(), e);
101 }
102 }
103
104
105
106
107
108
109
110 public RemoveNoteCommand setObjectId(RevObject id) {
111 checkCallable();
112 this.id = id;
113 return this;
114 }
115
116 private void commitNoteMap(RevWalk walk, NoteMap map,
117 RevCommit notesCommit,
118 ObjectInserter inserter,
119 String msg)
120 throws IOException {
121
122 CommitBuilder builder = new CommitBuilder();
123 builder.setTreeId(map.writeTree(inserter));
124 builder.setAuthor(new PersonIdent(repo));
125 builder.setCommitter(builder.getAuthor());
126 builder.setMessage(msg);
127 if (notesCommit != null)
128 builder.setParentIds(notesCommit);
129 ObjectId commit = inserter.insert(builder);
130 inserter.flush();
131 RefUpdate refUpdate = repo.updateRef(notesRef);
132 if (notesCommit != null)
133 refUpdate.setExpectedOldObjectId(notesCommit);
134 else
135 refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
136 refUpdate.setNewObjectId(commit);
137 refUpdate.update(walk);
138 }
139
140
141
142
143
144
145
146
147
148
149 public RemoveNoteCommand setNotesRef(String notesRef) {
150 checkCallable();
151 this.notesRef = notesRef;
152 return this;
153 }
154
155 }