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  
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   * Remove object notes.
28   *
29   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-notes.html"
30   *      >Git documentation about Notes</a>
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  	 * <p>
40  	 * Constructor for RemoveNoteCommand.
41  	 * </p>
42  	 *
43  	 * @param repo
44  	 *            the {@link org.eclipse.jgit.lib.Repository}
45  	 */
46  	protected RemoveNoteCommand(Repository repo) {
47  		super(repo);
48  	}
49  
50  	/** {@inheritDoc} */
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  			// if we have a notes ref, use it
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'"); //$NON-NLS-1$
68  			return map.getNote(id);
69  		} catch (IOException e) {
70  			throw new JGitInternalException(e.getMessage(), e);
71  		}
72  	}
73  
74  	/**
75  	 * Sets the object id of object you want to remove a note
76  	 *
77  	 * @param id
78  	 *            the {@link org.eclipse.jgit.revwalk.RevObject} to remove a
79  	 *            note from.
80  	 * @return {@code this}
81  	 */
82  	public RemoveNoteCommand setObjectId(RevObject id) {
83  		checkCallable();
84  		this.id = id;
85  		return this;
86  	}
87  
88  	/**
89  	 * Set the name of the <code>Ref</code> to remove a note from.
90  	 *
91  	 * @param notesRef
92  	 *            the {@code Ref} to read notes from. Note, the default value of
93  	 *            {@link org.eclipse.jgit.lib.Constants#R_NOTES_COMMITS} will be
94  	 *            used if nothing is set
95  	 * @return {@code this}
96  	 * @see Constants#R_NOTES_COMMITS
97  	 */
98  	public RemoveNoteCommand setNotesRef(String notesRef) {
99  		checkCallable();
100 		this.notesRef = notesRef;
101 		return this;
102 	}
103 
104 }