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