1
2
3
4
5
6
7
8
9
10
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 import java.io.UnsupportedEncodingException;
21 import java.nio.charset.Charset;
22
23 import org.eclipse.jgit.api.errors.JGitInternalException;
24 import org.eclipse.jgit.internal.JGitText;
25 import org.eclipse.jgit.revwalk.RevObject;
26 import org.eclipse.jgit.util.References;
27
28
29
30
31
32
33
34
35
36
37
38 public class TagBuilder extends ObjectBuilder {
39
40 private static final byte[] hobject = Constants.encodeASCII("object");
41
42 private static final byte[] htype = Constants.encodeASCII("type");
43
44 private static final byte[] htag = Constants.encodeASCII("tag");
45
46 private static final byte[] htagger = Constants.encodeASCII("tagger");
47
48 private ObjectId object;
49
50 private int type = Constants.OBJ_BAD;
51
52 private String tag;
53
54
55
56
57
58
59 public int getObjectType() {
60 return type;
61 }
62
63
64
65
66
67
68 public ObjectId getObjectId() {
69 return object;
70 }
71
72
73
74
75
76
77
78
79
80 public void setObjectId(AnyObjectId obj, int objType) {
81 object = obj.copy();
82 type = objType;
83 }
84
85
86
87
88
89
90
91 public void setObjectId(RevObject obj) {
92 setObjectId(obj, obj.getType());
93 }
94
95
96
97
98
99
100 public String getTag() {
101 return tag;
102 }
103
104
105
106
107
108
109
110
111
112 public void setTag(String shortName) {
113 this.tag = shortName;
114 }
115
116
117
118
119
120
121 public PersonIdent getTagger() {
122 return getAuthor();
123 }
124
125
126
127
128
129
130
131 public void setTagger(PersonIdent taggerIdent) {
132 setAuthor(taggerIdent);
133 }
134
135
136
137
138
139
140
141 @Override
142 public byte[] build() throws UnsupportedEncodingException {
143 ByteArrayOutputStream os = new ByteArrayOutputStream();
144 try (OutputStreamWriter w = new OutputStreamWriter(os,
145 getEncoding())) {
146
147 os.write(hobject);
148 os.write(' ');
149 getObjectId().copyTo(os);
150 os.write('\n');
151
152 os.write(htype);
153 os.write(' ');
154 os.write(Constants
155 .encodeASCII(Constants.typeString(getObjectType())));
156 os.write('\n');
157
158 os.write(htag);
159 os.write(' ');
160 w.write(getTag());
161 w.flush();
162 os.write('\n');
163
164 if (getTagger() != null) {
165 os.write(htagger);
166 os.write(' ');
167 w.write(getTagger().toExternalString());
168 w.flush();
169 os.write('\n');
170 }
171
172 writeEncoding(getEncoding(), os);
173
174 os.write('\n');
175 String msg = getMessage();
176 if (msg != null) {
177 w.write(msg);
178 w.flush();
179 }
180
181 GpgSignature signature = getGpgSignature();
182 if (signature != null) {
183 if (msg != null && !msg.isEmpty() && !msg.endsWith("\n")) {
184
185
186
187
188
189
190 throw new JGitInternalException(
191 JGitText.get().signedTagMessageNoLf);
192 }
193 String externalForm = signature.toExternalString();
194 w.write(externalForm);
195 w.flush();
196 if (!externalForm.endsWith("\n")) {
197 os.write('\n');
198 }
199 }
200 } catch (IOException err) {
201
202
203
204 throw new RuntimeException(err);
205 }
206 return os.toByteArray();
207 }
208
209
210
211
212
213
214
215
216
217 @Deprecated
218 public byte[] toByteArray() {
219 try {
220 return build();
221 } catch (UnsupportedEncodingException e) {
222 return null;
223 }
224 }
225
226
227 @SuppressWarnings("nls")
228 @Override
229 public String toString() {
230 StringBuilder r = new StringBuilder();
231 r.append("Tag");
232 r.append("={\n");
233
234 r.append("object ");
235 r.append(object != null ? object.name() : "NOT_SET");
236 r.append("\n");
237
238 r.append("type ");
239 r.append(object != null ? Constants.typeString(type) : "NOT_SET");
240 r.append("\n");
241
242 r.append("tag ");
243 r.append(tag != null ? tag : "NOT_SET");
244 r.append("\n");
245
246 if (getTagger() != null) {
247 r.append("tagger ");
248 r.append(getTagger());
249 r.append("\n");
250 }
251
252 Charset encoding = getEncoding();
253 if (!References.isSameObject(encoding, UTF_8)) {
254 r.append("encoding ");
255 r.append(encoding.name());
256 r.append("\n");
257 }
258
259 r.append("\n");
260 r.append(getMessage() != null ? getMessage() : "");
261 GpgSignature signature = getGpgSignature();
262 r.append(signature != null ? signature.toExternalString() : "");
263 r.append("}");
264 return r.toString();
265 }
266 }