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