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