2 * Copyright (C) 2008-2010, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 package org
.eclipse
.jgit
.dircache
;
46 import java
.io
.BufferedReader
;
48 import java
.io
.FileInputStream
;
49 import java
.io
.InputStreamReader
;
50 import java
.util
.ArrayList
;
51 import java
.util
.Iterator
;
52 import java
.util
.LinkedHashMap
;
55 import org
.eclipse
.jgit
.errors
.CorruptObjectException
;
56 import org
.eclipse
.jgit
.junit
.LocalDiskRepositoryTestCase
;
57 import org
.eclipse
.jgit
.lib
.FileMode
;
58 import org
.eclipse
.jgit
.lib
.ObjectId
;
59 import org
.eclipse
.jgit
.lib
.Repository
;
60 import org
.eclipse
.jgit
.treewalk
.TreeWalk
;
61 import org
.eclipse
.jgit
.util
.JGitTestUtil
;
63 public class DirCacheCGitCompatabilityTest
extends LocalDiskRepositoryTestCase
{
64 private final File index
= pathOf("gitgit.index");
66 public void testReadIndex_LsFiles() throws Exception
{
67 final Map
<String
, CGitIndexRecord
> ls
= readLsFiles();
68 final DirCache dc
= new DirCache(index
);
69 assertEquals(0, dc
.getEntryCount());
71 assertEquals(ls
.size(), dc
.getEntryCount());
73 final Iterator
<CGitIndexRecord
> rItr
= ls
.values().iterator();
74 for (int i
= 0; rItr
.hasNext(); i
++)
75 assertEqual(rItr
.next(), dc
.getEntry(i
));
79 public void testTreeWalk_LsFiles() throws Exception
{
80 final Repository db
= createBareRepository();
81 final Map
<String
, CGitIndexRecord
> ls
= readLsFiles();
82 final DirCache dc
= new DirCache(index
);
83 assertEquals(0, dc
.getEntryCount());
85 assertEquals(ls
.size(), dc
.getEntryCount());
87 final Iterator
<CGitIndexRecord
> rItr
= ls
.values().iterator();
88 final TreeWalk tw
= new TreeWalk(db
);
90 tw
.setRecursive(true);
91 tw
.addTree(new DirCacheIterator(dc
));
92 while (rItr
.hasNext()) {
93 final DirCacheIterator dcItr
;
95 assertTrue(tw
.next());
96 dcItr
= tw
.getTree(0, DirCacheIterator
.class);
99 assertEqual(rItr
.next(), dcItr
.getDirCacheEntry());
104 public void testUnsupportedOptionalExtension() throws Exception
{
105 final DirCache dc
= new DirCache(pathOf("gitgit.index.ZZZZ"));
107 assertEquals(1, dc
.getEntryCount());
108 assertEquals("A", dc
.getEntry(0).getPathString());
111 public void testUnsupportedRequiredExtension() throws Exception
{
112 final DirCache dc
= new DirCache(pathOf("gitgit.index.aaaa"));
115 fail("Cache loaded an unsupported extension");
116 } catch (CorruptObjectException err
) {
117 assertEquals("DIRC extension 'aaaa'"
118 + " not supported by this version.", err
.getMessage());
122 public void testCorruptChecksumAtFooter() throws Exception
{
123 final DirCache dc
= new DirCache(pathOf("gitgit.index.badchecksum"));
126 fail("Cache loaded despite corrupt checksum");
127 } catch (CorruptObjectException err
) {
128 assertEquals("DIRC checksum mismatch", err
.getMessage());
132 private static void assertEqual(final CGitIndexRecord c
,
133 final DirCacheEntry j
) {
137 assertEquals(c
.path
, j
.getPathString());
138 assertEquals(c
.id
, j
.getObjectId());
139 assertEquals(c
.mode
, j
.getRawMode());
140 assertEquals(c
.stage
, j
.getStage());
143 public void testReadIndex_DirCacheTree() throws Exception
{
144 final Map
<String
, CGitIndexRecord
> cList
= readLsFiles();
145 final Map
<String
, CGitLsTreeRecord
> cTree
= readLsTree();
146 final DirCache dc
= new DirCache(index
);
147 assertEquals(0, dc
.getEntryCount());
149 assertEquals(cList
.size(), dc
.getEntryCount());
151 final DirCacheTree jTree
= dc
.getCacheTree(false);
152 assertNotNull(jTree
);
153 assertEquals("", jTree
.getNameString());
154 assertEquals("", jTree
.getPathString());
155 assertTrue(jTree
.isValid());
156 assertEquals(ObjectId
157 .fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"), jTree
159 assertEquals(cList
.size(), jTree
.getEntrySpan());
161 final ArrayList
<CGitLsTreeRecord
> subtrees
= new ArrayList
<CGitLsTreeRecord
>();
162 for (final CGitLsTreeRecord r
: cTree
.values()) {
163 if (FileMode
.TREE
.equals(r
.mode
))
166 assertEquals(subtrees
.size(), jTree
.getChildCount());
168 for (int i
= 0; i
< jTree
.getChildCount(); i
++) {
169 final DirCacheTree sj
= jTree
.getChild(i
);
170 final CGitLsTreeRecord sc
= subtrees
.get(i
);
171 assertEquals(sc
.path
, sj
.getNameString());
172 assertEquals(sc
.path
+ "/", sj
.getPathString());
173 assertTrue(sj
.isValid());
174 assertEquals(sc
.id
, sj
.getObjectId());
178 private File
pathOf(final String name
) {
179 return JGitTestUtil
.getTestResourceFile(name
);
182 private Map
<String
, CGitIndexRecord
> readLsFiles() throws Exception
{
183 final LinkedHashMap
<String
, CGitIndexRecord
> r
= new LinkedHashMap
<String
, CGitIndexRecord
>();
184 final BufferedReader br
= new BufferedReader(new InputStreamReader(
185 new FileInputStream(pathOf("gitgit.lsfiles")), "UTF-8"));
188 while ((line
= br
.readLine()) != null) {
189 final CGitIndexRecord cr
= new CGitIndexRecord(line
);
198 private Map
<String
, CGitLsTreeRecord
> readLsTree() throws Exception
{
199 final LinkedHashMap
<String
, CGitLsTreeRecord
> r
= new LinkedHashMap
<String
, CGitLsTreeRecord
>();
200 final BufferedReader br
= new BufferedReader(new InputStreamReader(
201 new FileInputStream(pathOf("gitgit.lstree")), "UTF-8"));
204 while ((line
= br
.readLine()) != null) {
205 final CGitLsTreeRecord cr
= new CGitLsTreeRecord(line
);
214 private static class CGitIndexRecord
{
223 CGitIndexRecord(final String line
) {
224 final int tab
= line
.indexOf('\t');
225 final int sp1
= line
.indexOf(' ');
226 final int sp2
= line
.indexOf(' ', sp1
+ 1);
227 mode
= Integer
.parseInt(line
.substring(0, sp1
), 8);
228 id
= ObjectId
.fromString(line
.substring(sp1
+ 1, sp2
));
229 stage
= Integer
.parseInt(line
.substring(sp2
+ 1, tab
));
230 path
= line
.substring(tab
+ 1);
234 private static class CGitLsTreeRecord
{
241 CGitLsTreeRecord(final String line
) {
242 final int tab
= line
.indexOf('\t');
243 final int sp1
= line
.indexOf(' ');
244 final int sp2
= line
.indexOf(' ', sp1
+ 1);
245 mode
= Integer
.parseInt(line
.substring(0, sp1
), 8);
246 id
= ObjectId
.fromString(line
.substring(sp2
+ 1, tab
));
247 path
= line
.substring(tab
+ 1);