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