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 package org.eclipse.jgit.internal.storage.pack;
45
46 import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
47 import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
48
49 import java.io.IOException;
50 import java.util.List;
51 import java.util.Set;
52
53 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
54 import org.eclipse.jgit.errors.MissingObjectException;
55 import org.eclipse.jgit.lib.AnyObjectId;
56 import org.eclipse.jgit.lib.FileMode;
57 import org.eclipse.jgit.lib.MutableObjectId;
58 import org.eclipse.jgit.lib.ObjectId;
59 import org.eclipse.jgit.lib.ObjectIdOwnerMap;
60 import org.eclipse.jgit.lib.ObjectLoader;
61 import org.eclipse.jgit.lib.ObjectReader;
62 import org.eclipse.jgit.lib.ProgressMonitor;
63 import org.eclipse.jgit.revwalk.RevTree;
64 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
65
66 class BaseSearch {
67 private static final int M_BLOB = FileMode.REGULAR_FILE.getBits();
68
69 private static final int M_TREE = FileMode.TREE.getBits();
70
71 private final ProgressMonitor progress;
72
73 private final ObjectReader reader;
74
75 private final ObjectId[] baseTrees;
76
77 private final ObjectIdOwnerMap<ObjectToPack> objectsMap;
78
79 private final List<ObjectToPack> edgeObjects;
80
81 private final IntSet alreadyProcessed;
82
83 private final ObjectIdOwnerMap<TreeWithData> treeCache;
84
85 private final CanonicalTreeParser parser;
86
87 private final MutableObjectId idBuf;
88
89 BaseSearch(ProgressMonitor countingMonitor, Set<RevTree> bases,
90 ObjectIdOwnerMap<ObjectToPack> objects,
91 List<ObjectToPack> edges, ObjectReader or) {
92 progress = countingMonitor;
93 reader = or;
94 baseTrees = bases.toArray(new ObjectId[0]);
95 objectsMap = objects;
96 edgeObjects = edges;
97
98 alreadyProcessed = new IntSet();
99 treeCache = new ObjectIdOwnerMap<>();
100 parser = new CanonicalTreeParser();
101 idBuf = new MutableObjectId();
102 }
103
104 void addBase(int objectType, byte[] pathBuf, int pathLen, int pathHash)
105 throws IOException {
106 final int tailMode = modeForType(objectType);
107 if (tailMode == 0)
108 return;
109
110 if (!alreadyProcessed.add(pathHash))
111 return;
112
113 if (pathLen == 0) {
114 for (ObjectId root : baseTrees)
115 add(root, OBJ_TREE, pathHash);
116 return;
117 }
118
119 final int firstSlash = nextSlash(pathBuf, 0, pathLen);
120
121 CHECK_BASE: for (ObjectId root : baseTrees) {
122 int ptr = 0;
123 int end = firstSlash;
124 int mode = end != pathLen ? M_TREE : tailMode;
125
126 parser.reset(readTree(root));
127 while (!parser.eof()) {
128 int cmp = parser.pathCompare(pathBuf, ptr, end, mode);
129
130 if (cmp < 0) {
131 parser.next();
132 continue;
133 }
134
135 if (cmp > 0)
136 continue CHECK_BASE;
137
138 if (end == pathLen) {
139 if (parser.getEntryFileMode().getObjectType() == objectType) {
140 idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
141 add(idBuf, objectType, pathHash);
142 }
143 continue CHECK_BASE;
144 }
145
146 if (!FileMode.TREE.equals(parser.getEntryRawMode()))
147 continue CHECK_BASE;
148
149 ptr = end + 1;
150 end = nextSlash(pathBuf, ptr, pathLen);
151 mode = end != pathLen ? M_TREE : tailMode;
152
153 idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
154 parser.reset(readTree(idBuf));
155 }
156 }
157 }
158
159 private static int modeForType(int typeCode) {
160 switch (typeCode) {
161 case OBJ_TREE:
162 return M_TREE;
163
164 case OBJ_BLOB:
165 return M_BLOB;
166
167 default:
168 return 0;
169 }
170 }
171
172 private static int nextSlash(byte[] pathBuf, int ptr, int end) {
173 while (ptr < end && pathBuf[ptr] != '/')
174 ptr++;
175 return ptr;
176 }
177
178 private void add(AnyObjectId id, int objectType, int pathHash) {
179 ObjectToPack obj = new ObjectToPack(id, objectType);
180 obj.setEdge();
181 obj.setPathHash(pathHash);
182
183 if (objectsMap.addIfAbsent(obj) == obj) {
184 edgeObjects.add(obj);
185 progress.update(1);
186 }
187 }
188
189 private byte[] readTree(AnyObjectId id) throws MissingObjectException,
190 IncorrectObjectTypeException, IOException {
191 TreeWithData tree = treeCache.get(id);
192 if (tree != null)
193 return tree.buf;
194
195 ObjectLoader ldr = reader.open(id, OBJ_TREE);
196 byte[] buf = ldr.getCachedBytes(Integer.MAX_VALUE);
197 treeCache.add(new TreeWithData(id, buf));
198 return buf;
199 }
200
201 private static class TreeWithData extends ObjectIdOwnerMap.Entry {
202 final byte[] buf;
203
204 TreeWithData(AnyObjectId id, byte[] buf) {
205 super(id);
206 this.buf = buf;
207 }
208 }
209 }