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.OutputStreamWriter;
53 import java.io.UnsupportedEncodingException;
54 import java.nio.charset.Charset;
55 import java.util.List;
56
57
58
59
60
61
62
63
64
65
66
67
68 public class CommitBuilder {
69 private static final ObjectId[] EMPTY_OBJECTID_LIST = new ObjectId[0];
70
71 private static final byte[] htree = Constants.encodeASCII("tree");
72
73 private static final byte[] hparent = Constants.encodeASCII("parent");
74
75 private static final byte[] hauthor = Constants.encodeASCII("author");
76
77 private static final byte[] hcommitter = Constants.encodeASCII("committer");
78
79 private static final byte[] hencoding = Constants.encodeASCII("encoding");
80
81 private ObjectId treeId;
82
83 private ObjectId[] parentIds;
84
85 private PersonIdent author;
86
87 private PersonIdent committer;
88
89 private String message;
90
91 private Charset encoding;
92
93
94
95
96 public CommitBuilder() {
97 parentIds = EMPTY_OBJECTID_LIST;
98 encoding = UTF_8;
99 }
100
101
102
103
104
105
106 public ObjectId getTreeId() {
107 return treeId;
108 }
109
110
111
112
113
114
115
116 public void setTreeId(AnyObjectId id) {
117 treeId = id.copy();
118 }
119
120
121
122
123
124
125 public PersonIdent getAuthor() {
126 return author;
127 }
128
129
130
131
132
133
134
135 public void setAuthor(PersonIdent newAuthor) {
136 author = newAuthor;
137 }
138
139
140
141
142
143
144 public PersonIdent getCommitter() {
145 return committer;
146 }
147
148
149
150
151
152
153
154 public void setCommitter(PersonIdent newCommitter) {
155 committer = newCommitter;
156 }
157
158
159
160
161
162
163 public ObjectId[] getParentIds() {
164 return parentIds;
165 }
166
167
168
169
170
171
172
173 public void setParentId(AnyObjectId newParent) {
174 parentIds = new ObjectId[] { newParent.copy() };
175 }
176
177
178
179
180
181
182
183
184
185
186
187
188 public void setParentIds(AnyObjectId parent1, AnyObjectId parent2) {
189 parentIds = new ObjectId[] { parent1.copy(), parent2.copy() };
190 }
191
192
193
194
195
196
197
198 public void setParentIds(ObjectId... newParents) {
199 parentIds = new ObjectId[newParents.length];
200 for (int i = 0; i < newParents.length; i++)
201 parentIds[i] = newParents[i].copy();
202 }
203
204
205
206
207
208
209
210 public void setParentIds(List<? extends AnyObjectId> newParents) {
211 parentIds = new ObjectId[newParents.size()];
212 for (int i = 0; i < newParents.size(); i++)
213 parentIds[i] = newParents.get(i).copy();
214 }
215
216
217
218
219
220
221
222 public void addParentId(AnyObjectId additionalParent) {
223 if (parentIds.length == 0) {
224 setParentId(additionalParent);
225 } else {
226 ObjectId[] newParents = new ObjectId[parentIds.length + 1];
227 System.arraycopy(parentIds, 0, newParents, 0, parentIds.length);
228 newParents[parentIds.length] = additionalParent.copy();
229 parentIds = newParents;
230 }
231 }
232
233
234
235
236
237
238 public String getMessage() {
239 return message;
240 }
241
242
243
244
245
246
247
248 public void setMessage(String newMessage) {
249 message = newMessage;
250 }
251
252
253
254
255
256
257
258
259 public void setEncoding(String encodingName) {
260 encoding = Charset.forName(encodingName);
261 }
262
263
264
265
266
267
268
269 public void setEncoding(Charset enc) {
270 encoding = enc;
271 }
272
273
274
275
276
277
278 public Charset getEncoding() {
279 return encoding;
280 }
281
282
283
284
285
286
287
288
289
290
291 public byte[] build() throws UnsupportedEncodingException {
292 ByteArrayOutputStream os = new ByteArrayOutputStream();
293 OutputStreamWriter w = new OutputStreamWriter(os, getEncoding());
294 try {
295 os.write(htree);
296 os.write(' ');
297 getTreeId().copyTo(os);
298 os.write('\n');
299
300 for (ObjectId p : getParentIds()) {
301 os.write(hparent);
302 os.write(' ');
303 p.copyTo(os);
304 os.write('\n');
305 }
306
307 os.write(hauthor);
308 os.write(' ');
309 w.write(getAuthor().toExternalString());
310 w.flush();
311 os.write('\n');
312
313 os.write(hcommitter);
314 os.write(' ');
315 w.write(getCommitter().toExternalString());
316 w.flush();
317 os.write('\n');
318
319 if (getEncoding() != UTF_8) {
320 os.write(hencoding);
321 os.write(' ');
322 os.write(Constants.encodeASCII(getEncoding().name()));
323 os.write('\n');
324 }
325
326 os.write('\n');
327
328 if (getMessage() != null) {
329 w.write(getMessage());
330 w.flush();
331 }
332 } catch (IOException err) {
333
334
335
336 throw new RuntimeException(err);
337 }
338 return os.toByteArray();
339 }
340
341
342
343
344
345
346
347
348
349
350 public byte[] toByteArray() throws UnsupportedEncodingException {
351 return build();
352 }
353
354
355 @SuppressWarnings("nls")
356 @Override
357 public String toString() {
358 StringBuilder r = new StringBuilder();
359 r.append("Commit");
360 r.append("={\n");
361
362 r.append("tree ");
363 r.append(treeId != null ? treeId.name() : "NOT_SET");
364 r.append("\n");
365
366 for (ObjectId p : parentIds) {
367 r.append("parent ");
368 r.append(p.name());
369 r.append("\n");
370 }
371
372 r.append("author ");
373 r.append(author != null ? author.toString() : "NOT_SET");
374 r.append("\n");
375
376 r.append("committer ");
377 r.append(committer != null ? committer.toString() : "NOT_SET");
378 r.append("\n");
379
380 if (encoding != null && encoding != UTF_8) {
381 r.append("encoding ");
382 r.append(encoding.name());
383 r.append("\n");
384 }
385
386 r.append("\n");
387 r.append(message != null ? message : "");
388 r.append("}");
389 return r.toString();
390 }
391 }