1
2
3
4
5
6
7
8
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.ObjectInserter;
18 import org.eclipse.jgit.lib.Ref;
19 import org.eclipse.jgit.lib.Repository;
20 import org.eclipse.jgit.notes.Note;
21 import org.eclipse.jgit.notes.NoteMap;
22 import org.eclipse.jgit.revwalk.RevCommit;
23 import org.eclipse.jgit.revwalk.RevObject;
24 import org.eclipse.jgit.revwalk.RevWalk;
25
26
27
28
29
30
31
32 public class RemoveNoteCommand extends GitCommand<Note> {
33
34 private RevObject id;
35
36 private String notesRef = Constants.R_NOTES_COMMITS;
37
38
39
40
41
42
43
44
45
46 protected RemoveNoteCommand(Repository repo) {
47 super(repo);
48 }
49
50
51 @Override
52 public Note call() throws GitAPIException {
53 checkCallable();
54 try (RevWalk walk = new RevWalk(repo);
55 ObjectInserter inserter = repo.newObjectInserter()) {
56 NoteMap map = NoteMap.newEmptyMap();
57 RevCommit notesCommit = null;
58 Ref ref = repo.exactRef(notesRef);
59
60 if (ref != null) {
61 notesCommit = walk.parseCommit(ref.getObjectId());
62 map = NoteMap.read(walk.getObjectReader(), notesCommit);
63 }
64 map.set(id, null, inserter);
65 AddNoteCommand.commitNoteMap(repo, notesRef, walk, map, notesCommit,
66 inserter,
67 "Notes removed by 'git notes remove'");
68 return map.getNote(id);
69 } catch (IOException e) {
70 throw new JGitInternalException(e.getMessage(), e);
71 }
72 }
73
74
75
76
77
78
79
80
81
82 public RemoveNoteCommand setObjectId(RevObject id) {
83 checkCallable();
84 this.id = id;
85 return this;
86 }
87
88
89
90
91
92
93
94
95
96
97
98 public RemoveNoteCommand setNotesRef(String notesRef) {
99 checkCallable();
100 this.notesRef = notesRef;
101 return this;
102 }
103
104 }