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.dircache;
45
46 import static org.eclipse.jgit.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertArrayEquals;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertTrue;
51 import static org.junit.Assert.fail;
52
53 import java.io.BufferedReader;
54 import java.io.ByteArrayOutputStream;
55 import java.io.File;
56 import java.io.FileInputStream;
57 import java.io.InputStreamReader;
58 import java.util.ArrayList;
59 import java.util.Iterator;
60 import java.util.LinkedHashMap;
61 import java.util.Map;
62
63 import org.eclipse.jgit.errors.CorruptObjectException;
64 import org.eclipse.jgit.junit.JGitTestUtil;
65 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
66 import org.eclipse.jgit.lib.FileMode;
67 import org.eclipse.jgit.lib.ObjectId;
68 import org.eclipse.jgit.lib.Repository;
69 import org.eclipse.jgit.treewalk.TreeWalk;
70 import org.eclipse.jgit.util.FS;
71 import org.eclipse.jgit.util.IO;
72 import org.junit.Test;
73
74 public class DirCacheCGitCompatabilityTest extends LocalDiskRepositoryTestCase {
75 private final File index = pathOf("gitgit.index");
76
77 @Test
78 public void testReadIndex_LsFiles() throws Exception {
79 final Map<String, CGitIndexRecord> ls = readLsFiles();
80 final DirCache dc = new DirCache(index, FS.DETECTED);
81 assertEquals(0, dc.getEntryCount());
82 dc.read();
83 assertEquals(ls.size(), dc.getEntryCount());
84 {
85 final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
86 for (int i = 0; rItr.hasNext(); i++)
87 assertEqual(rItr.next(), dc.getEntry(i));
88 }
89 }
90
91 @Test
92 public void testTreeWalk_LsFiles() throws Exception {
93 final Repository db = createBareRepository();
94 final Map<String, CGitIndexRecord> ls = readLsFiles();
95 final DirCache dc = new DirCache(index, db.getFS());
96 assertEquals(0, dc.getEntryCount());
97 dc.read();
98 assertEquals(ls.size(), dc.getEntryCount());
99 {
100 final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
101 try (final TreeWalk tw = new TreeWalk(db)) {
102 tw.setRecursive(true);
103 tw.addTree(new DirCacheIterator(dc));
104 while (rItr.hasNext()) {
105 final DirCacheIterator dcItr;
106
107 assertTrue(tw.next());
108 dcItr = tw.getTree(0, DirCacheIterator.class);
109 assertNotNull(dcItr);
110
111 assertEqual(rItr.next(), dcItr.getDirCacheEntry());
112 }
113 }
114 }
115 }
116
117 @Test
118 public void testUnsupportedOptionalExtension() throws Exception {
119 final DirCache dc = new DirCache(pathOf("gitgit.index.ZZZZ"),
120 FS.DETECTED);
121 dc.read();
122 assertEquals(1, dc.getEntryCount());
123 assertEquals("A", dc.getEntry(0).getPathString());
124 }
125
126 @Test
127 public void testUnsupportedRequiredExtension() throws Exception {
128 final DirCache dc = new DirCache(pathOf("gitgit.index.aaaa"),
129 FS.DETECTED);
130 try {
131 dc.read();
132 fail("Cache loaded an unsupported extension");
133 } catch (CorruptObjectException err) {
134 assertEquals("DIRC extension 'aaaa'"
135 + " not supported by this version.", err.getMessage());
136 }
137 }
138
139 @Test
140 public void testCorruptChecksumAtFooter() throws Exception {
141 final DirCache dc = new DirCache(pathOf("gitgit.index.badchecksum"),
142 FS.DETECTED);
143 try {
144 dc.read();
145 fail("Cache loaded despite corrupt checksum");
146 } catch (CorruptObjectException err) {
147 assertEquals("DIRC checksum mismatch", err.getMessage());
148 }
149 }
150
151 private static void assertEqual(final CGitIndexRecord c,
152 final DirCacheEntry j) {
153 assertNotNull(c);
154 assertNotNull(j);
155
156 assertEquals(c.path, j.getPathString());
157 assertEquals(c.id, j.getObjectId());
158 assertEquals(c.mode, j.getRawMode());
159 assertEquals(c.stage, j.getStage());
160 }
161
162 @Test
163 public void testReadIndex_DirCacheTree() throws Exception {
164 final Map<String, CGitIndexRecord> cList = readLsFiles();
165 final Map<String, CGitLsTreeRecord> cTree = readLsTree();
166 final DirCache dc = new DirCache(index, FS.DETECTED);
167 assertEquals(0, dc.getEntryCount());
168 dc.read();
169 assertEquals(cList.size(), dc.getEntryCount());
170
171 final DirCacheTree jTree = dc.getCacheTree(false);
172 assertNotNull(jTree);
173 assertEquals("", jTree.getNameString());
174 assertEquals("", jTree.getPathString());
175 assertTrue(jTree.isValid());
176 assertEquals(ObjectId
177 .fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"), jTree
178 .getObjectId());
179 assertEquals(cList.size(), jTree.getEntrySpan());
180
181 final ArrayList<CGitLsTreeRecord> subtrees = new ArrayList<>();
182 for (final CGitLsTreeRecord r : cTree.values()) {
183 if (FileMode.TREE.equals(r.mode))
184 subtrees.add(r);
185 }
186 assertEquals(subtrees.size(), jTree.getChildCount());
187
188 for (int i = 0; i < jTree.getChildCount(); i++) {
189 final DirCacheTree sj = jTree.getChild(i);
190 final CGitLsTreeRecord sc = subtrees.get(i);
191 assertEquals(sc.path, sj.getNameString());
192 assertEquals(sc.path + "/", sj.getPathString());
193 assertTrue(sj.isValid());
194 assertEquals(sc.id, sj.getObjectId());
195 }
196 }
197
198 @Test
199 public void testReadWriteV3() throws Exception {
200 final File file = pathOf("gitgit.index.v3");
201 final DirCache dc = new DirCache(file, FS.DETECTED);
202 dc.read();
203
204 assertEquals(10, dc.getEntryCount());
205 assertV3TreeEntry(0, "dir1/file1.txt", false, false, dc);
206 assertV3TreeEntry(1, "dir2/file2.txt", true, false, dc);
207 assertV3TreeEntry(2, "dir3/file3.txt", false, false, dc);
208 assertV3TreeEntry(3, "dir3/file3a.txt", true, false, dc);
209 assertV3TreeEntry(4, "dir4/file4.txt", true, false, dc);
210 assertV3TreeEntry(5, "dir4/file4a.txt", false, false, dc);
211 assertV3TreeEntry(6, "file.txt", true, false, dc);
212 assertV3TreeEntry(7, "newdir1/newfile1.txt", false, true, dc);
213 assertV3TreeEntry(8, "newdir1/newfile2.txt", false, true, dc);
214 assertV3TreeEntry(9, "newfile.txt", false, true, dc);
215
216 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
217 dc.writeTo(null, bos);
218 final byte[] indexBytes = bos.toByteArray();
219 final byte[] expectedBytes = IO.readFully(file);
220 assertArrayEquals(expectedBytes, indexBytes);
221 }
222
223 private static void assertV3TreeEntry(int indexPosition, String path,
224 boolean skipWorkTree, boolean intentToAdd, DirCache dc) {
225 final DirCacheEntry entry = dc.getEntry(indexPosition);
226 assertEquals(path, entry.getPathString());
227 assertEquals(skipWorkTree, entry.isSkipWorkTree());
228 assertEquals(intentToAdd, entry.isIntentToAdd());
229 }
230
231 private static File pathOf(final String name) {
232 return JGitTestUtil.getTestResourceFile(name);
233 }
234
235 private static Map<String, CGitIndexRecord> readLsFiles() throws Exception {
236 final LinkedHashMap<String, CGitIndexRecord> r = new LinkedHashMap<>();
237 final BufferedReader br = new BufferedReader(new InputStreamReader(
238 new FileInputStream(pathOf("gitgit.lsfiles")), "UTF-8"));
239 try {
240 String line;
241 while ((line = br.readLine()) != null) {
242 final CGitIndexRecord cr = new CGitIndexRecord(line);
243 r.put(cr.path, cr);
244 }
245 } finally {
246 br.close();
247 }
248 return r;
249 }
250
251 private static Map<String, CGitLsTreeRecord> readLsTree() throws Exception {
252 final LinkedHashMap<String, CGitLsTreeRecord> r = new LinkedHashMap<>();
253 final BufferedReader br = new BufferedReader(new InputStreamReader(
254 new FileInputStream(pathOf("gitgit.lstree")), "UTF-8"));
255 try {
256 String line;
257 while ((line = br.readLine()) != null) {
258 final CGitLsTreeRecord cr = new CGitLsTreeRecord(line);
259 r.put(cr.path, cr);
260 }
261 } finally {
262 br.close();
263 }
264 return r;
265 }
266
267 private static class CGitIndexRecord {
268 final int mode;
269
270 final ObjectId id;
271
272 final int stage;
273
274 final String path;
275
276 CGitIndexRecord(final String line) {
277 final int tab = line.indexOf('\t');
278 final int sp1 = line.indexOf(' ');
279 final int sp2 = line.indexOf(' ', sp1 + 1);
280 mode = Integer.parseInt(line.substring(0, sp1), 8);
281 id = ObjectId.fromString(line.substring(sp1 + 1, sp2));
282 stage = Integer.parseInt(line.substring(sp2 + 1, tab));
283 path = line.substring(tab + 1);
284 }
285 }
286
287 private static class CGitLsTreeRecord {
288 final int mode;
289
290 final ObjectId id;
291
292 final String path;
293
294 CGitLsTreeRecord(final String line) {
295 final int tab = line.indexOf('\t');
296 final int sp1 = line.indexOf(' ');
297 final int sp2 = line.indexOf(' ', sp1 + 1);
298 mode = Integer.parseInt(line.substring(0, sp1), 8);
299 id = ObjectId.fromString(line.substring(sp2 + 1, tab));
300 path = line.substring(tab + 1);
301 }
302 }
303 }