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