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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* Copyright (C) 2010, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.lib;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
import static org.eclipse.jgit.lib.Constants.encode;
import static org.eclipse.jgit.lib.FileMode.GITLINK;
import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
import static org.eclipse.jgit.lib.FileMode.TREE;
import java.io.IOException;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.util.TemporaryBuffer;
/**
* Mutable formatter to construct a single tree object.
*
* This formatter does not process subtrees. Callers must handle creating each
* subtree on their own.
*
* To maintain good performance for bulk operations, this formatter does not
* validate its input. Callers are responsible for ensuring the resulting tree
* object is correctly well formed by writing entries in the correct order.
*/
public class TreeFormatter {
/**
* Compute the size of a tree entry record.
*
* This method can be used to estimate the correct size of a tree prior to
* allocating a formatter. Getting the size correct at allocation time
* ensures the internal buffer is sized correctly, reducing copying.
*
* @param mode
* the mode the entry will have.
* @param nameLen
* the length of the name, in bytes.
* @return the length of the record.
*/
public static int entrySize(FileMode mode, int nameLen) {
return mode.copyToLength() + nameLen + OBJECT_ID_LENGTH + 2;
}
private byte[] buf;
private int ptr;
private TemporaryBuffer.Heap overflowBuffer;
/**
* Create an empty formatter with a default buffer size.
*/
public TreeFormatter() {
this(8192);
}
/**
* Create an empty formatter with the specified buffer size.
*
* @param size
* estimated size of the tree, in bytes. Callers can use
* {@link #entrySize(FileMode, int)} to estimate the size of each
* entry in advance of allocating the formatter.
*/
public TreeFormatter(int size) {
buf = new byte[size];
}
/**
* Add a link to a submodule commit, mode is {@link org.eclipse.jgit.lib.FileMode#GITLINK}.
*
* @param name
* name of the entry.
* @param commit
* the ObjectId to store in this entry.
*/
public void append(String name, RevCommit commit) {
append(name, GITLINK, commit);
}
/**
* Add a subtree, mode is {@link org.eclipse.jgit.lib.FileMode#TREE}.
*
* @param name
* name of the entry.
* @param tree
* the ObjectId to store in this entry.
*/
public void append(String name, RevTree tree) {
append(name, TREE, tree);
}
/**
* Add a regular file, mode is {@link org.eclipse.jgit.lib.FileMode#REGULAR_FILE}.
*
* @param name
* name of the entry.
* @param blob
* the ObjectId to store in this entry.
*/
public void append(String name, RevBlob blob) {
append(name, REGULAR_FILE, blob);
}
/**
* Append any entry to the tree.
*
* @param name
* name of the entry.
* @param mode
* mode describing the treatment of {@code id}.
* @param id
* the ObjectId to store in this entry.
*/
public void append(String name, FileMode mode, AnyObjectId id) {
append(encode(name), mode, id);
}
/**
* Append any entry to the tree.
*
* @param name
* name of the entry. The name should be UTF-8 encoded, but file
* name encoding is not a well defined concept in Git.
* @param mode
* mode describing the treatment of {@code id}.
* @param id
* the ObjectId to store in this entry.
*/
public void append(byte[] name, FileMode mode, AnyObjectId id) {
append(name, 0, name.length, mode, id);
}
/**
* Append any entry to the tree.
*
* @param nameBuf
* buffer holding the name of the entry. The name should be UTF-8
* encoded, but file name encoding is not a well defined concept
* in Git.
* @param namePos
* first position within {@code nameBuf} of the name data.
* @param nameLen
* number of bytes from {@code nameBuf} to use as the name.
* @param mode
* mode describing the treatment of {@code id}.
* @param id
* the ObjectId to store in this entry.
*/
public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
AnyObjectId id) {
append(nameBuf, namePos, nameLen, mode, id, false);
}
/**
* Append any entry to the tree.
*
* @param nameBuf
* buffer holding the name of the entry. The name should be UTF-8
* encoded, but file name encoding is not a well defined concept
* in Git.
* @param namePos
* first position within {@code nameBuf} of the name data.
* @param nameLen
* number of bytes from {@code nameBuf} to use as the name.
* @param mode
* mode describing the treatment of {@code id}.
* @param id
* the ObjectId to store in this entry.
* @param allowEmptyName
* allow an empty filename (creating a corrupt tree)
* @since 4.6
*/
public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
AnyObjectId id, boolean allowEmptyName) {
if (nameLen == 0 && !allowEmptyName) {
throw new IllegalArgumentException(
JGitText.get().invalidTreeZeroLengthName);
}
if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
id.copyRawTo(buf, ptr);
ptr += OBJECT_ID_LENGTH;
} else {
try {
fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
id.copyRawTo(overflowBuffer);
} catch (IOException badBuffer) {
// This should never occur.
throw new RuntimeException(badBuffer);
}
}
}
/**
* Append any entry to the tree.
*
* @param nameBuf
* buffer holding the name of the entry. The name should be UTF-8
* encoded, but file name encoding is not a well defined concept
* in Git.
* @param namePos
* first position within {@code nameBuf} of the name data.
* @param nameLen
* number of bytes from {@code nameBuf} to use as the name.
* @param mode
* mode describing the treatment of {@code id}.
* @param idBuf
* buffer holding the raw ObjectId of the entry.
* @param idPos
* first position within {@code idBuf} to copy the id from.
*/
public void append(byte[] nameBuf, int namePos, int nameLen, FileMode mode,
byte[] idBuf, int idPos) {
if (fmtBuf(nameBuf, namePos, nameLen, mode)) {
System.arraycopy(idBuf, idPos, buf, ptr, OBJECT_ID_LENGTH);
ptr += OBJECT_ID_LENGTH;
} else {
try {
fmtOverflowBuffer(nameBuf, namePos, nameLen, mode);
overflowBuffer.write(idBuf, idPos, OBJECT_ID_LENGTH);
} catch (IOException badBuffer) {
// This should never occur.
throw new RuntimeException(badBuffer);
}
}
}
private boolean fmtBuf(byte[] nameBuf, int namePos, int nameLen,
FileMode mode) {
if (buf == null || buf.length < ptr + entrySize(mode, nameLen))
return false;
mode.copyTo(buf, ptr);
ptr += mode.copyToLength();
buf[ptr++] = ' ';
System.arraycopy(nameBuf, namePos, buf, ptr, nameLen);
ptr += nameLen;
buf[ptr++] = 0;
return true;
}
private void fmtOverflowBuffer(byte[] nameBuf, int namePos, int nameLen,
FileMode mode) throws IOException {
if (buf != null) {
overflowBuffer = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
overflowBuffer.write(buf, 0, ptr);
buf = null;
}
mode.copyTo(overflowBuffer);
overflowBuffer.write((byte) ' ');
overflowBuffer.write(nameBuf, namePos, nameLen);
overflowBuffer.write((byte) 0);
}
/**
* Insert this tree and obtain its ObjectId.
*
* @param ins
* the inserter to store the tree.
* @return computed ObjectId of the tree
* @throws java.io.IOException
* the tree could not be stored.
*/
public ObjectId insertTo(ObjectInserter ins) throws IOException {
if (buf != null)
return ins.insert(OBJ_TREE, buf, 0, ptr);
final long len = overflowBuffer.length();
return ins.insert(OBJ_TREE, len, overflowBuffer.openInputStream());
}
/**
* Compute the ObjectId for this tree
*
* @param ins a {@link org.eclipse.jgit.lib.ObjectInserter} object.
* @return ObjectId for this tree
*/
public ObjectId computeId(ObjectInserter ins) {
if (buf != null)
return ins.idFor(OBJ_TREE, buf, 0, ptr);
final long len = overflowBuffer.length();
try {
return ins.idFor(OBJ_TREE, len, overflowBuffer.openInputStream());
} catch (IOException e) {
// this should never happen
throw new RuntimeException(e);
}
}
/**
* Copy this formatter's buffer into a byte array.
*
* This method is not efficient, as it needs to create a copy of the
* internal buffer in order to supply an array of the correct size to the
* caller. If the buffer is just to pass to an ObjectInserter, consider
* using {@link org.eclipse.jgit.lib.ObjectInserter#insert(TreeFormatter)}
* instead.
*
* @return a copy of this formatter's buffer.
*/
public byte[] toByteArray() {
if (buf != null) {
byte[] r = new byte[ptr];
System.arraycopy(buf, 0, r, 0, ptr);
return r;
}
try {
return overflowBuffer.toByteArray();
} catch (IOException err) {
// This should never happen, its read failure on a byte array.
throw new RuntimeException(err);
}
}
@SuppressWarnings("nls")
@Override
public String toString() {
byte[] raw = toByteArray();
CanonicalTreeParser p = new CanonicalTreeParser();
p.reset(raw);
StringBuilder r = new StringBuilder();
r.append("Tree={");
if (!p.eof()) {
r.append('\n');
try {
new ObjectChecker().checkTree(raw);
} catch (CorruptObjectException error) {
r.append("*** ERROR: ").append(error.getMessage()).append("\n");
r.append('\n');
}
}
while (!p.eof()) {
final FileMode mode = p.getEntryFileMode();
r.append(mode);
r.append(' ');
r.append(Constants.typeString(mode.getObjectType()));
r.append(' ');
r.append(p.getEntryObjectId().name());
r.append(' ');
r.append(p.getEntryPathString());
r.append('\n');
p.next();
}
r.append("}");
return r.toString();
}
}
|