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.dircache.DirCache.cmp;
48 import static org.eclipse.jgit.dircache.DirCacheTree.peq;
49 import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
50
51 import java.io.IOException;
52 import java.text.MessageFormat;
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.Comparator;
56 import java.util.List;
57
58 import org.eclipse.jgit.internal.JGitText;
59 import org.eclipse.jgit.lib.Constants;
60 import org.eclipse.jgit.util.Paths;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class DirCacheEditor extends BaseDirCacheEditor {
76 private static final Comparator<PathEdit> EDIT_CMP = new Comparator<PathEdit>() {
77 public int compare(final PathEdit o1, final PathEdit o2) {
78 final byte[] a = o1.path;
79 final byte[] b = o2.path;
80 return cmp(a, a.length, b, b.length);
81 }
82 };
83
84 private final List<PathEdit> edits;
85 private int editIdx;
86
87
88
89
90
91
92
93
94
95
96 protected DirCacheEditor(final DirCache dc, final int ecnt) {
97 super(dc, ecnt);
98 edits = new ArrayList<PathEdit>();
99 }
100
101
102
103
104
105
106
107
108
109
110
111 public void add(final PathEdit edit) {
112 edits.add(edit);
113 }
114
115 @Override
116 public boolean commit() throws IOException {
117 if (edits.isEmpty()) {
118
119
120 cache.unlock();
121 return true;
122 }
123 return super.commit();
124 }
125
126 public void finish() {
127 if (!edits.isEmpty()) {
128 applyEdits();
129 replace();
130 }
131 }
132
133 private void applyEdits() {
134 Collections.sort(edits, EDIT_CMP);
135 editIdx = 0;
136
137 final int maxIdx = cache.getEntryCount();
138 int lastIdx = 0;
139 while (editIdx < edits.size()) {
140 PathEdit e = edits.get(editIdx++);
141 int eIdx = cache.findEntry(lastIdx, e.path, e.path.length);
142 final boolean missing = eIdx < 0;
143 if (eIdx < 0)
144 eIdx = -(eIdx + 1);
145 final int cnt = Math.min(eIdx, maxIdx) - lastIdx;
146 if (cnt > 0)
147 fastKeep(lastIdx, cnt);
148
149 if (e instanceof DeletePath) {
150 lastIdx = missing ? eIdx : cache.nextEntry(eIdx);
151 continue;
152 }
153 if (e instanceof DeleteTree) {
154 lastIdx = cache.nextEntry(e.path, e.path.length, eIdx);
155 continue;
156 }
157
158 if (missing) {
159 DirCacheEntry ent = new DirCacheEntry(e.path);
160 e.apply(ent);
161 if (ent.getRawMode() == 0) {
162 throw new IllegalArgumentException(MessageFormat.format(
163 JGitText.get().fileModeNotSetForPath,
164 ent.getPathString()));
165 }
166 lastIdx = e.replace
167 ? deleteOverlappingSubtree(ent, eIdx)
168 : eIdx;
169 fastAdd(ent);
170 } else {
171
172 lastIdx = cache.nextEntry(eIdx);
173 for (int i = eIdx; i < lastIdx; i++) {
174 final DirCacheEntry ent = cache.getEntry(i);
175 e.apply(ent);
176 fastAdd(ent);
177 }
178 }
179 }
180
181 final int cnt = maxIdx - lastIdx;
182 if (cnt > 0)
183 fastKeep(lastIdx, cnt);
184 }
185
186 private int deleteOverlappingSubtree(DirCacheEntry ent, int eIdx) {
187 byte[] entPath = ent.path;
188 int entLen = entPath.length;
189
190
191
192
193
194 for (int p = pdir(entPath, entLen); p > 0; p = pdir(entPath, p)) {
195 int i = findEntry(entPath, p);
196 if (i >= 0) {
197
198
199
200 int n = --entryCnt - i;
201 System.arraycopy(entries, i + 1, entries, i, n);
202 break;
203 }
204
205
206
207 i = -(i + 1);
208 if (i < entryCnt && inDir(entries[i], entPath, p)) {
209 break;
210 }
211 }
212
213 int maxEnt = cache.getEntryCount();
214 if (eIdx >= maxEnt) {
215 return maxEnt;
216 }
217
218 DirCacheEntry next = cache.getEntry(eIdx);
219 if (Paths.compare(next.path, 0, next.path.length, 0,
220 entPath, 0, entLen, TYPE_TREE) < 0) {
221
222
223
224 insertEdit(new DeleteTree(entPath));
225 return eIdx;
226 }
227
228
229 while (eIdx < maxEnt && inDir(cache.getEntry(eIdx), entPath, entLen)) {
230 eIdx++;
231 }
232 return eIdx;
233 }
234
235 private int findEntry(byte[] p, int pLen) {
236 int low = 0;
237 int high = entryCnt;
238 while (low < high) {
239 int mid = (low + high) >>> 1;
240 int cmp = cmp(p, pLen, entries[mid]);
241 if (cmp < 0) {
242 high = mid;
243 } else if (cmp == 0) {
244 while (mid > 0 && cmp(p, pLen, entries[mid - 1]) == 0) {
245 mid--;
246 }
247 return mid;
248 } else {
249 low = mid + 1;
250 }
251 }
252 return -(low + 1);
253 }
254
255 private void insertEdit(DeleteTree d) {
256 for (int i = editIdx; i < edits.size(); i++) {
257 int cmp = EDIT_CMP.compare(d, edits.get(i));
258 if (cmp < 0) {
259 edits.add(i, d);
260 return;
261 } else if (cmp == 0) {
262 return;
263 }
264 }
265 edits.add(d);
266 }
267
268 private static boolean inDir(DirCacheEntry e, byte[] path, int pLen) {
269 return e.path.length > pLen && e.path[pLen] == '/'
270 && peq(path, e.path, pLen);
271 }
272
273 private static int pdir(byte[] path, int e) {
274 for (e--; e > 0; e--) {
275 if (path[e] == '/') {
276 return e;
277 }
278 }
279 return 0;
280 }
281
282
283
284
285
286
287
288
289
290
291 public abstract static class PathEdit {
292 final byte[] path;
293 boolean replace = true;
294
295
296
297
298
299
300
301 public PathEdit(final String entryPath) {
302 path = Constants.encode(entryPath);
303 }
304
305 PathEdit(byte[] path) {
306 this.path = path;
307 }
308
309
310
311
312
313
314
315
316 public PathEdit(final DirCacheEntry ent) {
317 path = ent.path;
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331 public PathEdit setReplace(boolean ok) {
332 replace = ok;
333 return this;
334 }
335
336
337
338
339
340
341
342
343
344
345
346 public abstract void apply(DirCacheEntry ent);
347
348 @Override
349 public String toString() {
350 String p = DirCacheEntry.toString(path);
351 return getClass().getSimpleName() + '[' + p + ']';
352 }
353 }
354
355
356
357
358
359
360
361
362
363
364 public static final class DeletePath extends PathEdit {
365
366
367
368
369
370
371 public DeletePath(final String entryPath) {
372 super(entryPath);
373 }
374
375
376
377
378
379
380
381
382 public DeletePath(final DirCacheEntry ent) {
383 super(ent);
384 }
385
386 public void apply(final DirCacheEntry ent) {
387 throw new UnsupportedOperationException(JGitText.get().noApplyInDelete);
388 }
389 }
390
391
392
393
394
395
396
397
398
399
400
401
402
403 public static final class DeleteTree extends PathEdit {
404
405
406
407
408
409
410
411
412
413 public DeleteTree(String entryPath) {
414 super(entryPath.isEmpty()
415 || entryPath.charAt(entryPath.length() - 1) == '/'
416 ? entryPath
417 : entryPath + '/');
418 }
419
420 DeleteTree(byte[] path) {
421 super(appendSlash(path));
422 }
423
424 private static byte[] appendSlash(byte[] path) {
425 int n = path.length;
426 if (n > 0 && path[n - 1] != '/') {
427 byte[] r = new byte[n + 1];
428 System.arraycopy(path, 0, r, 0, n);
429 r[n] = '/';
430 return r;
431 }
432 return path;
433 }
434
435 public void apply(final DirCacheEntry ent) {
436 throw new UnsupportedOperationException(JGitText.get().noApplyInDelete);
437 }
438 }
439 }