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