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