View Javadoc
1   /*
2    * Copyright (C) 2006-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.lib;
14  
15  import static java.nio.charset.StandardCharsets.UTF_8;
16  
17  import java.io.ByteArrayOutputStream;
18  import java.io.IOException;
19  import java.io.OutputStreamWriter;
20  
21  import org.eclipse.jgit.revwalk.RevObject;
22  
23  /**
24   * Mutable builder to construct an annotated tag recording a project state.
25   *
26   * Applications should use this object when they need to manually construct a
27   * tag and want precise control over its fields.
28   *
29   * To read a tag object, construct a {@link org.eclipse.jgit.revwalk.RevWalk}
30   * and obtain a {@link org.eclipse.jgit.revwalk.RevTag} instance by calling
31   * {@link org.eclipse.jgit.revwalk.RevWalk#parseTag(AnyObjectId)}.
32   */
33  public class TagBuilder {
34  	private ObjectId object;
35  
36  	private int type = Constants.OBJ_BAD;
37  
38  	private String tag;
39  
40  	private PersonIdent tagger;
41  
42  	private String message;
43  
44  	/**
45  	 * Get the type of object this tag refers to.
46  	 *
47  	 * @return the type of object this tag refers to.
48  	 */
49  	public int getObjectType() {
50  		return type;
51  	}
52  
53  	/**
54  	 * Get the object this tag refers to.
55  	 *
56  	 * @return the object this tag refers to.
57  	 */
58  	public ObjectId getObjectId() {
59  		return object;
60  	}
61  
62  	/**
63  	 * Set the object this tag refers to, and its type.
64  	 *
65  	 * @param obj
66  	 *            the object.
67  	 * @param objType
68  	 *            the type of {@code obj}. Must be a valid type code.
69  	 */
70  	public void setObjectId(AnyObjectId obj, int objType) {
71  		object = obj.copy();
72  		type = objType;
73  	}
74  
75  	/**
76  	 * Set the object this tag refers to, and infer its type.
77  	 *
78  	 * @param obj
79  	 *            the object the tag will refer to.
80  	 */
81  	public void setObjectId(RevObject obj) {
82  		setObjectId(obj, obj.getType());
83  	}
84  
85  	/**
86  	 * Get short name of the tag (no {@code refs/tags/} prefix).
87  	 *
88  	 * @return short name of the tag (no {@code refs/tags/} prefix).
89  	 */
90  	public String getTag() {
91  		return tag;
92  	}
93  
94  	/**
95  	 * Set the name of this tag.
96  	 *
97  	 * @param shortName
98  	 *            new short name of the tag. This short name should not start
99  	 *            with {@code refs/} as typically a tag is stored under the
100 	 *            reference derived from {@code "refs/tags/" + getTag()}.
101 	 */
102 	public void setTag(String shortName) {
103 		this.tag = shortName;
104 	}
105 
106 	/**
107 	 * Get creator of this tag.
108 	 *
109 	 * @return creator of this tag. May be null.
110 	 */
111 	public PersonIdent getTagger() {
112 		return tagger;
113 	}
114 
115 	/**
116 	 * Set the creator of this tag.
117 	 *
118 	 * @param taggerIdent
119 	 *            the creator. May be null.
120 	 */
121 	public void setTagger(PersonIdent taggerIdent) {
122 		tagger = taggerIdent;
123 	}
124 
125 	/**
126 	 * Get the complete commit message.
127 	 *
128 	 * @return the complete commit message.
129 	 */
130 	public String getMessage() {
131 		return message;
132 	}
133 
134 	/**
135 	 * Set the tag's message.
136 	 *
137 	 * @param newMessage
138 	 *            the tag's message.
139 	 */
140 	public void setMessage(String newMessage) {
141 		message = newMessage;
142 	}
143 
144 	/**
145 	 * Format this builder's state as an annotated tag object.
146 	 *
147 	 * @return this object in the canonical annotated tag format, suitable for
148 	 *         storage in a repository.
149 	 */
150 	public byte[] build() {
151 		ByteArrayOutputStream os = new ByteArrayOutputStream();
152 		try (OutputStreamWriter w = new OutputStreamWriter(os,
153 				UTF_8)) {
154 			w.write("object "); //$NON-NLS-1$
155 			getObjectId().copyTo(w);
156 			w.write('\n');
157 
158 			w.write("type "); //$NON-NLS-1$
159 			w.write(Constants.typeString(getObjectType()));
160 			w.write("\n"); //$NON-NLS-1$
161 
162 			w.write("tag "); //$NON-NLS-1$
163 			w.write(getTag());
164 			w.write("\n"); //$NON-NLS-1$
165 
166 			if (getTagger() != null) {
167 				w.write("tagger "); //$NON-NLS-1$
168 				w.write(getTagger().toExternalString());
169 				w.write('\n');
170 			}
171 
172 			w.write('\n');
173 			if (getMessage() != null)
174 				w.write(getMessage());
175 		} catch (IOException err) {
176 			// This should never occur, the only way to get it above is
177 			// for the ByteArrayOutputStream to throw, but it doesn't.
178 			//
179 			throw new RuntimeException(err);
180 		}
181 		return os.toByteArray();
182 	}
183 
184 	/**
185 	 * Format this builder's state as an annotated tag object.
186 	 *
187 	 * @return this object in the canonical annotated tag format, suitable for
188 	 *         storage in a repository.
189 	 */
190 	public byte[] toByteArray() {
191 		return build();
192 	}
193 
194 	/** {@inheritDoc} */
195 	@SuppressWarnings("nls")
196 	@Override
197 	public String toString() {
198 		StringBuilder r = new StringBuilder();
199 		r.append("Tag");
200 		r.append("={\n");
201 
202 		r.append("object ");
203 		r.append(object != null ? object.name() : "NOT_SET");
204 		r.append("\n");
205 
206 		r.append("type ");
207 		r.append(object != null ? Constants.typeString(type) : "NOT_SET");
208 		r.append("\n");
209 
210 		r.append("tag ");
211 		r.append(tag != null ? tag : "NOT_SET");
212 		r.append("\n");
213 
214 		if (tagger != null) {
215 			r.append("tagger ");
216 			r.append(tagger);
217 			r.append("\n");
218 		}
219 
220 		r.append("\n");
221 		r.append(message != null ? message : "");
222 		r.append("}");
223 		return r.toString();
224 	}
225 }