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.OutputStream;
20 import java.io.OutputStreamWriter;
21 import java.io.UnsupportedEncodingException;
22 import java.nio.charset.Charset;
23 import java.text.MessageFormat;
24 import java.util.List;
25
26 import org.eclipse.jgit.internal.JGitText;
27 import org.eclipse.jgit.util.References;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class CommitBuilder {
41 private static final ObjectIdObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
42
43 private static final byte[] htree = Constants.encodeASCII("tree");
44
45 private static final byte[] hparent = Constants.encodeASCII("parent");
46
47 private static final byte[] hauthor = Constants.encodeASCII("author");
48
49 private static final byte[] hcommitter = Constants.encodeASCII("committer");
50
51 private static final byte[] hgpgsig = Constants.encodeASCII("gpgsig");
52
53 private static final byte[] hencoding = Constants.encodeASCII("encoding");
54
55 private ObjectId treeId;
56
57 private ObjectId[] parentIds;
58
59 private PersonIdent author;
60
61 private PersonIdent committer;
62
63 private GpgSignature gpgSignature;
64
65 private String message;
66
67 private Charset encoding;
68
69
70
71
72 public CommitBuilder() {
73 parentIds = EMPTY_OBJECTID_LIST;
74 encoding = UTF_8;
75 }
76
77
78
79
80
81
82 public ObjectId getTreeId() {
83 return treeId;
84 }
85
86
87
88
89
90
91
92 public void setTreeId(AnyObjectId id) {
93 treeId = id.copy();
94 }
95
96
97
98
99
100
101 public PersonIdent getAuthor() {
102 return author;
103 }
104
105
106
107
108
109
110
111 public void setAuthor(PersonIdent newAuthor) {
112 author = newAuthor;
113 }
114
115
116
117
118
119
120 public PersonIdent getCommitter() {
121 return committer;
122 }
123
124
125
126
127
128
129
130 public void setCommitter(PersonIdent newCommitter) {
131 committer = newCommitter;
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public void setGpgSignature(GpgSignature newSignature) {
152 gpgSignature = newSignature;
153 }
154
155
156
157
158
159
160
161
162 public GpgSignature getGpgSignature() {
163 return gpgSignature;
164 }
165
166
167
168
169
170
171 public ObjectId[] getParentIds() {
172 return parentIds;
173 }
174
175
176
177
178
179
180
181 public void setParentId(AnyObjectId newParent) {
182 parentIds = new ObjectId[] { newParent.copy() };
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196 public void setParentIds(AnyObjectId./../../org/eclipse/jgit/lib/AnyObjectId.html#AnyObjectId">AnyObjectId parent1, AnyObjectId parent2) {
197 parentIds = new ObjectId[] { parent1.copy(), parent2.copy() };
198 }
199
200
201
202
203
204
205
206 public void setParentIds(ObjectId... newParents) {
207 parentIds = new ObjectId[newParents.length];
208 for (int i = 0; i < newParents.length; i++)
209 parentIds[i] = newParents[i].copy();
210 }
211
212
213
214
215
216
217
218 public void setParentIds(List<? extends AnyObjectId> newParents) {
219 parentIds = new ObjectId[newParents.size()];
220 for (int i = 0; i < newParents.size(); i++)
221 parentIds[i] = newParents.get(i).copy();
222 }
223
224
225
226
227
228
229
230 public void addParentId(AnyObjectId additionalParent) {
231 if (parentIds.length == 0) {
232 setParentId(additionalParent);
233 } else {
234 ObjectId[] newParents = new ObjectId[parentIds.length + 1];
235 System.arraycopy(parentIds, 0, newParents, 0, parentIds.length);
236 newParents[parentIds.length] = additionalParent.copy();
237 parentIds = newParents;
238 }
239 }
240
241
242
243
244
245
246 public String getMessage() {
247 return message;
248 }
249
250
251
252
253
254
255
256 public void setMessage(String newMessage) {
257 message = newMessage;
258 }
259
260
261
262
263
264
265
266
267
268 @Deprecated
269 public void setEncoding(String encodingName) {
270 encoding = Charset.forName(encodingName);
271 }
272
273
274
275
276
277
278
279 public void setEncoding(Charset enc) {
280 encoding = enc;
281 }
282
283
284
285
286
287
288 public Charset getEncoding() {
289 return encoding;
290 }
291
292
293
294
295
296
297
298
299
300
301 public byte[] build() throws UnsupportedEncodingException {
302 ByteArrayOutputStream os = new ByteArrayOutputStream();
303 OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
304 try {
305 os.write(htree);
306 os.write(' ');
307 getTreeId().copyTo(os);
308 os.write('\n');
309
310 for (ObjectId p : getParentIds()) {
311 os.write(hparent);
312 os.write(' ');
313 p.copyTo(os);
314 os.write('\n');
315 }
316
317 os.write(hauthor);
318 os.write(' ');
319 w.write(getAuthor().toExternalString());
320 w.flush();
321 os.write('\n');
322
323 os.write(hcommitter);
324 os.write(' ');
325 w.write(getCommitter().toExternalString());
326 w.flush();
327 os.write('\n');
328
329 if (getGpgSignature() != null) {
330 os.write(hgpgsig);
331 os.write(' ');
332 writeGpgSignatureString(getGpgSignature().toExternalString(), os);
333 os.write('\n');
334 }
335
336 if (!References.isSameObject(getEncoding(), UTF_8)) {
337 os.write(hencoding);
338 os.write(' ');
339 os.write(Constants.encodeASCII(getEncoding().name()));
340 os.write('\n');
341 }
342
343 os.write('\n');
344
345 if (getMessage() != null) {
346 w.write(getMessage());
347 w.flush();
348 }
349 } catch (IOException err) {
350
351
352
353 throw new RuntimeException(err);
354 }
355 return os.toByteArray();
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 static void writeGpgSignatureString(String in, OutputStream out)
379 throws IOException, IllegalArgumentException {
380 int length = in.length();
381 for (int i = 0; i < length; ++i) {
382 char ch = in.charAt(i);
383 switch (ch) {
384 case '\r':
385 if (i + 1 < length && in.charAt(i + 1) == '\n') {
386 ++i;
387 }
388 if (i + 1 < length) {
389 out.write('\n');
390 out.write(' ');
391 }
392 break;
393 case '\n':
394 if (i + 1 < length) {
395 out.write('\n');
396 out.write(' ');
397 }
398 break;
399 default:
400
401 if (ch > 127)
402 throw new IllegalArgumentException(MessageFormat
403 .format(JGitText.get().notASCIIString, in));
404 out.write(ch);
405 break;
406 }
407 }
408 }
409
410
411
412
413
414
415
416
417
418
419 public byte[] toByteArray() throws UnsupportedEncodingException {
420 return build();
421 }
422
423
424 @SuppressWarnings("nls")
425 @Override
426 public String toString() {
427 StringBuilder r = new StringBuilder();
428 r.append("Commit");
429 r.append("={\n");
430
431 r.append("tree ");
432 r.append(treeId != null ? treeId.name() : "NOT_SET");
433 r.append("\n");
434
435 for (ObjectId p : parentIds) {
436 r.append("parent ");
437 r.append(p.name());
438 r.append("\n");
439 }
440
441 r.append("author ");
442 r.append(author != null ? author.toString() : "NOT_SET");
443 r.append("\n");
444
445 r.append("committer ");
446 r.append(committer != null ? committer.toString() : "NOT_SET");
447 r.append("\n");
448
449 r.append("gpgSignature ");
450 r.append(gpgSignature != null ? gpgSignature.toString() : "NOT_SET");
451 r.append("\n");
452
453 if (encoding != null && !References.isSameObject(encoding, UTF_8)) {
454 r.append("encoding ");
455 r.append(encoding.name());
456 r.append("\n");
457 }
458
459 r.append("\n");
460 r.append(message != null ? message : "");
461 r.append("}");
462 return r.toString();
463 }
464 }