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 package org.eclipse.jgit.dircache;
46
47 import static org.eclipse.jgit.lib.FileMode.TREE;
48 import static org.eclipse.jgit.lib.TreeFormatter.entrySize;
49
50 import java.io.IOException;
51 import java.io.OutputStream;
52 import java.nio.ByteBuffer;
53 import java.util.Arrays;
54 import java.util.Comparator;
55
56 import org.eclipse.jgit.errors.UnmergedPathException;
57 import org.eclipse.jgit.lib.Constants;
58 import org.eclipse.jgit.lib.ObjectId;
59 import org.eclipse.jgit.lib.ObjectInserter;
60 import org.eclipse.jgit.lib.TreeFormatter;
61 import org.eclipse.jgit.util.MutableInteger;
62 import org.eclipse.jgit.util.RawParseUtils;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public class DirCacheTree {
78 private static final byte[] NO_NAME = {};
79
80 private static final DirCacheTree[] NO_CHILDREN = {};
81
82 private static final Comparator<DirCacheTree> TREE_CMP = new Comparator<DirCacheTree>() {
83 public int compare(final DirCacheTree o1, final DirCacheTree o2) {
84 final byte[] a = o1.encodedName;
85 final byte[] b = o2.encodedName;
86 final int aLen = a.length;
87 final int bLen = b.length;
88 int cPos;
89 for (cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
90 final int cmp = (a[cPos] & 0xff) - (b[cPos] & 0xff);
91 if (cmp != 0)
92 return cmp;
93 }
94 if (aLen == bLen)
95 return 0;
96 if (aLen < bLen)
97 return '/' - (b[cPos] & 0xff);
98 return (a[cPos] & 0xff) - '/';
99 }
100 };
101
102
103 private DirCacheTree parent;
104
105
106 private byte[] encodedName;
107
108
109 private int entrySpan;
110
111
112 private ObjectId id;
113
114
115 private DirCacheTree[] children;
116
117
118 private int childCnt;
119
120 DirCacheTree() {
121 encodedName = NO_NAME;
122 children = NO_CHILDREN;
123 childCnt = 0;
124 entrySpan = -1;
125 }
126
127 private DirCacheTree(final DirCacheTree myParent, final byte[] path,
128 final int pathOff, final int pathLen) {
129 parent = myParent;
130 encodedName = new byte[pathLen];
131 System.arraycopy(path, pathOff, encodedName, 0, pathLen);
132 children = NO_CHILDREN;
133 childCnt = 0;
134 entrySpan = -1;
135 }
136
137 DirCacheTree(final byte[] in, final MutableInteger off,
138 final DirCacheTree myParent) {
139 parent = myParent;
140
141 int ptr = RawParseUtils.next(in, off.value, '\0');
142 final int nameLen = ptr - off.value - 1;
143 if (nameLen > 0) {
144 encodedName = new byte[nameLen];
145 System.arraycopy(in, off.value, encodedName, 0, nameLen);
146 } else
147 encodedName = NO_NAME;
148
149 entrySpan = RawParseUtils.parseBase10(in, ptr, off);
150 final int subcnt = RawParseUtils.parseBase10(in, off.value, off);
151 off.value = RawParseUtils.next(in, off.value, '\n');
152
153 if (entrySpan >= 0) {
154
155
156
157 id = ObjectId.fromRaw(in, off.value);
158 off.value += Constants.OBJECT_ID_LENGTH;
159 }
160
161 if (subcnt > 0) {
162 boolean alreadySorted = true;
163 children = new DirCacheTree[subcnt];
164 for (int i = 0; i < subcnt; i++) {
165 children[i] = new DirCacheTree(in, off, this);
166
167
168
169
170
171
172 if (alreadySorted && i > 0
173 && TREE_CMP.compare(children[i - 1], children[i]) > 0)
174 alreadySorted = false;
175 }
176 if (!alreadySorted)
177 Arrays.sort(children, 0, subcnt, TREE_CMP);
178 } else {
179
180
181 children = NO_CHILDREN;
182 }
183 childCnt = subcnt;
184 }
185
186 void write(final byte[] tmp, final OutputStream os) throws IOException {
187 int ptr = tmp.length;
188 tmp[--ptr] = '\n';
189 ptr = RawParseUtils.formatBase10(tmp, ptr, childCnt);
190 tmp[--ptr] = ' ';
191 ptr = RawParseUtils.formatBase10(tmp, ptr, isValid() ? entrySpan : -1);
192 tmp[--ptr] = 0;
193
194 os.write(encodedName);
195 os.write(tmp, ptr, tmp.length - ptr);
196 if (isValid()) {
197 id.copyRawTo(tmp, 0);
198 os.write(tmp, 0, Constants.OBJECT_ID_LENGTH);
199 }
200 for (int i = 0; i < childCnt; i++)
201 children[i].write(tmp, os);
202 }
203
204
205
206
207
208
209
210
211
212
213
214
215 public boolean isValid() {
216 return id != null;
217 }
218
219
220
221
222
223
224
225
226
227
228 public int getEntrySpan() {
229 return entrySpan;
230 }
231
232
233
234
235
236
237 public int getChildCount() {
238 return childCnt;
239 }
240
241
242
243
244
245
246
247
248 public DirCacheTree getChild(final int i) {
249 return children[i];
250 }
251
252 ObjectId getObjectId() {
253 return id;
254 }
255
256
257
258
259
260
261
262
263
264
265
266 public String getNameString() {
267 final ByteBuffer bb = ByteBuffer.wrap(encodedName);
268 return Constants.CHARSET.decode(bb).toString();
269 }
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public String getPathString() {
284 final StringBuilder r = new StringBuilder();
285 appendName(r);
286 return r.toString();
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 ObjectId writeTree(final DirCacheEntry[] cache, int cIdx,
314 final int pathOffset, final ObjectInserter ow)
315 throws UnmergedPathException, IOException {
316 if (id == null) {
317 final int endIdx = cIdx + entrySpan;
318 final TreeFormatter fmt = new TreeFormatter(computeSize(cache,
319 cIdx, pathOffset, ow));
320 int childIdx = 0;
321 int entryIdx = cIdx;
322
323 while (entryIdx < endIdx) {
324 final DirCacheEntry e = cache[entryIdx];
325 final byte[] ep = e.path;
326 if (childIdx < childCnt) {
327 final DirCacheTree st = children[childIdx];
328 if (st.contains(ep, pathOffset, ep.length)) {
329 fmt.append(st.encodedName, TREE, st.id);
330 entryIdx += st.entrySpan;
331 childIdx++;
332 continue;
333 }
334 }
335
336 fmt.append(ep, pathOffset, ep.length - pathOffset, e
337 .getFileMode(), e.idBuffer(), e.idOffset());
338 entryIdx++;
339 }
340
341 id = ow.insert(fmt);
342 }
343 return id;
344 }
345
346 private int computeSize(final DirCacheEntry[] cache, int cIdx,
347 final int pathOffset, final ObjectInserter ow)
348 throws UnmergedPathException, IOException {
349 final int endIdx = cIdx + entrySpan;
350 int childIdx = 0;
351 int entryIdx = cIdx;
352 int size = 0;
353
354 while (entryIdx < endIdx) {
355 final DirCacheEntry e = cache[entryIdx];
356 if (e.getStage() != 0)
357 throw new UnmergedPathException(e);
358
359 final byte[] ep = e.path;
360 if (childIdx < childCnt) {
361 final DirCacheTree st = children[childIdx];
362 if (st.contains(ep, pathOffset, ep.length)) {
363 final int stOffset = pathOffset + st.nameLength() + 1;
364 st.writeTree(cache, entryIdx, stOffset, ow);
365
366 size += entrySize(TREE, st.nameLength());
367
368 entryIdx += st.entrySpan;
369 childIdx++;
370 continue;
371 }
372 }
373
374 size += entrySize(e.getFileMode(), ep.length - pathOffset);
375 entryIdx++;
376 }
377
378 return size;
379 }
380
381 private void appendName(final StringBuilder r) {
382 if (parent != null) {
383 parent.appendName(r);
384 r.append(getNameString());
385 r.append('/');
386 } else if (nameLength() > 0) {
387 r.append(getNameString());
388 r.append('/');
389 }
390 }
391
392 final int nameLength() {
393 return encodedName.length;
394 }
395
396 final boolean contains(final byte[] a, int aOff, final int aLen) {
397 final byte[] e = encodedName;
398 final int eLen = e.length;
399 for (int eOff = 0; eOff < eLen && aOff < aLen; eOff++, aOff++)
400 if (e[eOff] != a[aOff])
401 return false;
402 if (aOff >= aLen)
403 return false;
404 return a[aOff] == '/';
405 }
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426 void validate(final DirCacheEntry[] cache, final int cCnt, int cIdx,
427 final int pathOff) {
428 if (entrySpan >= 0 && cIdx + entrySpan <= cCnt) {
429
430
431
432 return;
433 }
434
435 entrySpan = 0;
436 if (cCnt == 0) {
437
438
439 return;
440 }
441
442 final byte[] firstPath = cache[cIdx].path;
443 int stIdx = 0;
444 while (cIdx < cCnt) {
445 final byte[] currPath = cache[cIdx].path;
446 if (pathOff > 0 && !peq(firstPath, currPath, pathOff)) {
447
448
449
450 break;
451 }
452
453 DirCacheTree st = stIdx < childCnt ? children[stIdx] : null;
454 final int cc = namecmp(currPath, pathOff, st);
455 if (cc > 0) {
456
457
458 removeChild(stIdx);
459 continue;
460 }
461
462 if (cc < 0) {
463 final int p = slash(currPath, pathOff);
464 if (p < 0) {
465
466
467
468 cIdx++;
469 entrySpan++;
470 continue;
471 }
472
473
474
475 st = new DirCacheTree(this, currPath, pathOff, p - pathOff);
476 insertChild(stIdx, st);
477 }
478
479
480
481 st.validate(cache, cCnt, cIdx, pathOff + st.nameLength() + 1);
482 cIdx += st.entrySpan;
483 entrySpan += st.entrySpan;
484 stIdx++;
485 }
486
487
488
489
490 while (stIdx < childCnt)
491 removeChild(childCnt - 1);
492 }
493
494 private void insertChild(final int stIdx, final DirCacheTree st) {
495 final DirCacheTree[] c = children;
496 if (childCnt + 1 <= c.length) {
497 if (stIdx < childCnt)
498 System.arraycopy(c, stIdx, c, stIdx + 1, childCnt - stIdx);
499 c[stIdx] = st;
500 childCnt++;
501 return;
502 }
503
504 final int n = c.length;
505 final DirCacheTree[] a = new DirCacheTree[n + 1];
506 if (stIdx > 0)
507 System.arraycopy(c, 0, a, 0, stIdx);
508 a[stIdx] = st;
509 if (stIdx < n)
510 System.arraycopy(c, stIdx, a, stIdx + 1, n - stIdx);
511 children = a;
512 childCnt++;
513 }
514
515 private void removeChild(final int stIdx) {
516 final int n = --childCnt;
517 if (stIdx < n)
518 System.arraycopy(children, stIdx + 1, children, stIdx, n - stIdx);
519 children[n] = null;
520 }
521
522 static boolean peq(final byte[] a, final byte[] b, int aLen) {
523 if (b.length < aLen)
524 return false;
525 for (aLen--; aLen >= 0; aLen--)
526 if (a[aLen] != b[aLen])
527 return false;
528 return true;
529 }
530
531 private static int namecmp(final byte[] a, int aPos, final DirCacheTree ct) {
532 if (ct == null)
533 return -1;
534 final byte[] b = ct.encodedName;
535 final int aLen = a.length;
536 final int bLen = b.length;
537 int bPos = 0;
538 for (; aPos < aLen && bPos < bLen; aPos++, bPos++) {
539 final int cmp = (a[aPos] & 0xff) - (b[bPos] & 0xff);
540 if (cmp != 0)
541 return cmp;
542 }
543 if (bPos == bLen)
544 return a[aPos] == '/' ? 0 : -1;
545 return aLen - bLen;
546 }
547
548 private static int slash(final byte[] a, int aPos) {
549 final int aLen = a.length;
550 for (; aPos < aLen; aPos++)
551 if (a[aPos] == '/')
552 return aPos;
553 return -1;
554 }
555
556 @Override
557 public String toString() {
558 return getNameString();
559 }
560 }