You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirCacheCGitCompatabilityTest.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  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
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  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.
  23. *
  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
  27. * written permission.
  28. *
  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.
  42. */
  43. package org.eclipse.jgit.dircache;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.BufferedReader;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.File;
  51. import java.io.FileInputStream;
  52. import java.io.InputStreamReader;
  53. import java.util.ArrayList;
  54. import java.util.Arrays;
  55. import java.util.Iterator;
  56. import java.util.LinkedHashMap;
  57. import java.util.Map;
  58. import org.eclipse.jgit.errors.CorruptObjectException;
  59. import org.eclipse.jgit.junit.JGitTestUtil;
  60. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  61. import org.eclipse.jgit.lib.FileMode;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.treewalk.TreeWalk;
  65. import org.eclipse.jgit.util.FS;
  66. import org.eclipse.jgit.util.IO;
  67. import org.junit.Test;
  68. public class DirCacheCGitCompatabilityTest extends LocalDiskRepositoryTestCase {
  69. private final File index = pathOf("gitgit.index");
  70. @Test
  71. public void testReadIndex_LsFiles() throws Exception {
  72. final Map<String, CGitIndexRecord> ls = readLsFiles();
  73. final DirCache dc = new DirCache(index, FS.DETECTED);
  74. assertEquals(0, dc.getEntryCount());
  75. dc.read();
  76. assertEquals(ls.size(), dc.getEntryCount());
  77. {
  78. final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
  79. for (int i = 0; rItr.hasNext(); i++)
  80. assertEqual(rItr.next(), dc.getEntry(i));
  81. }
  82. }
  83. @Test
  84. public void testTreeWalk_LsFiles() throws Exception {
  85. final Repository db = createBareRepository();
  86. final Map<String, CGitIndexRecord> ls = readLsFiles();
  87. final DirCache dc = new DirCache(index, db.getFS());
  88. assertEquals(0, dc.getEntryCount());
  89. dc.read();
  90. assertEquals(ls.size(), dc.getEntryCount());
  91. {
  92. final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
  93. final TreeWalk tw = new TreeWalk(db);
  94. tw.setRecursive(true);
  95. tw.addTree(new DirCacheIterator(dc));
  96. while (rItr.hasNext()) {
  97. final DirCacheIterator dcItr;
  98. assertTrue(tw.next());
  99. dcItr = tw.getTree(0, DirCacheIterator.class);
  100. assertNotNull(dcItr);
  101. assertEqual(rItr.next(), dcItr.getDirCacheEntry());
  102. }
  103. }
  104. }
  105. @Test
  106. public void testUnsupportedOptionalExtension() throws Exception {
  107. final DirCache dc = new DirCache(pathOf("gitgit.index.ZZZZ"),
  108. FS.DETECTED);
  109. dc.read();
  110. assertEquals(1, dc.getEntryCount());
  111. assertEquals("A", dc.getEntry(0).getPathString());
  112. }
  113. @Test
  114. public void testUnsupportedRequiredExtension() throws Exception {
  115. final DirCache dc = new DirCache(pathOf("gitgit.index.aaaa"),
  116. FS.DETECTED);
  117. try {
  118. dc.read();
  119. fail("Cache loaded an unsupported extension");
  120. } catch (CorruptObjectException err) {
  121. assertEquals("DIRC extension 'aaaa'"
  122. + " not supported by this version.", err.getMessage());
  123. }
  124. }
  125. @Test
  126. public void testCorruptChecksumAtFooter() throws Exception {
  127. final DirCache dc = new DirCache(pathOf("gitgit.index.badchecksum"),
  128. FS.DETECTED);
  129. try {
  130. dc.read();
  131. fail("Cache loaded despite corrupt checksum");
  132. } catch (CorruptObjectException err) {
  133. assertEquals("DIRC checksum mismatch", err.getMessage());
  134. }
  135. }
  136. private static void assertEqual(final CGitIndexRecord c,
  137. final DirCacheEntry j) {
  138. assertNotNull(c);
  139. assertNotNull(j);
  140. assertEquals(c.path, j.getPathString());
  141. assertEquals(c.id, j.getObjectId());
  142. assertEquals(c.mode, j.getRawMode());
  143. assertEquals(c.stage, j.getStage());
  144. }
  145. @Test
  146. public void testReadIndex_DirCacheTree() throws Exception {
  147. final Map<String, CGitIndexRecord> cList = readLsFiles();
  148. final Map<String, CGitLsTreeRecord> cTree = readLsTree();
  149. final DirCache dc = new DirCache(index, FS.DETECTED);
  150. assertEquals(0, dc.getEntryCount());
  151. dc.read();
  152. assertEquals(cList.size(), dc.getEntryCount());
  153. final DirCacheTree jTree = dc.getCacheTree(false);
  154. assertNotNull(jTree);
  155. assertEquals("", jTree.getNameString());
  156. assertEquals("", jTree.getPathString());
  157. assertTrue(jTree.isValid());
  158. assertEquals(ObjectId
  159. .fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"), jTree
  160. .getObjectId());
  161. assertEquals(cList.size(), jTree.getEntrySpan());
  162. final ArrayList<CGitLsTreeRecord> subtrees = new ArrayList<CGitLsTreeRecord>();
  163. for (final CGitLsTreeRecord r : cTree.values()) {
  164. if (FileMode.TREE.equals(r.mode))
  165. subtrees.add(r);
  166. }
  167. 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());
  175. }
  176. }
  177. @Test
  178. public void testReadWriteV3() throws Exception {
  179. final File file = pathOf("gitgit.index.v3");
  180. final DirCache dc = new DirCache(file, FS.DETECTED);
  181. dc.read();
  182. assertEquals(10, dc.getEntryCount());
  183. assertV3TreeEntry(0, "dir1/file1.txt", false, false, dc);
  184. assertV3TreeEntry(1, "dir2/file2.txt", true, false, dc);
  185. assertV3TreeEntry(2, "dir3/file3.txt", false, false, dc);
  186. assertV3TreeEntry(3, "dir3/file3a.txt", true, false, dc);
  187. assertV3TreeEntry(4, "dir4/file4.txt", true, false, dc);
  188. assertV3TreeEntry(5, "dir4/file4a.txt", false, false, dc);
  189. assertV3TreeEntry(6, "file.txt", true, false, dc);
  190. assertV3TreeEntry(7, "newdir1/newfile1.txt", false, true, dc);
  191. assertV3TreeEntry(8, "newdir1/newfile2.txt", false, true, dc);
  192. assertV3TreeEntry(9, "newfile.txt", false, true, dc);
  193. final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  194. dc.writeTo(bos);
  195. final byte[] indexBytes = bos.toByteArray();
  196. final byte[] expectedBytes = IO.readFully(file);
  197. assertTrue(Arrays.equals(expectedBytes, indexBytes));
  198. }
  199. private static void assertV3TreeEntry(int indexPosition, String path,
  200. boolean skipWorkTree, boolean intentToAdd, DirCache dc) {
  201. final DirCacheEntry entry = dc.getEntry(indexPosition);
  202. assertEquals(path, entry.getPathString());
  203. assertEquals(skipWorkTree, entry.isSkipWorkTree());
  204. assertEquals(intentToAdd, entry.isIntentToAdd());
  205. }
  206. private File pathOf(final String name) {
  207. return JGitTestUtil.getTestResourceFile(name);
  208. }
  209. private Map<String, CGitIndexRecord> readLsFiles() throws Exception {
  210. final LinkedHashMap<String, CGitIndexRecord> r = new LinkedHashMap<String, CGitIndexRecord>();
  211. final BufferedReader br = new BufferedReader(new InputStreamReader(
  212. new FileInputStream(pathOf("gitgit.lsfiles")), "UTF-8"));
  213. try {
  214. String line;
  215. while ((line = br.readLine()) != null) {
  216. final CGitIndexRecord cr = new CGitIndexRecord(line);
  217. r.put(cr.path, cr);
  218. }
  219. } finally {
  220. br.close();
  221. }
  222. return r;
  223. }
  224. private Map<String, CGitLsTreeRecord> readLsTree() throws Exception {
  225. final LinkedHashMap<String, CGitLsTreeRecord> r = new LinkedHashMap<String, CGitLsTreeRecord>();
  226. final BufferedReader br = new BufferedReader(new InputStreamReader(
  227. new FileInputStream(pathOf("gitgit.lstree")), "UTF-8"));
  228. try {
  229. String line;
  230. while ((line = br.readLine()) != null) {
  231. final CGitLsTreeRecord cr = new CGitLsTreeRecord(line);
  232. r.put(cr.path, cr);
  233. }
  234. } finally {
  235. br.close();
  236. }
  237. return r;
  238. }
  239. private static class CGitIndexRecord {
  240. final int mode;
  241. final ObjectId id;
  242. final int stage;
  243. final String path;
  244. CGitIndexRecord(final String line) {
  245. final int tab = line.indexOf('\t');
  246. final int sp1 = line.indexOf(' ');
  247. final int sp2 = line.indexOf(' ', sp1 + 1);
  248. mode = Integer.parseInt(line.substring(0, sp1), 8);
  249. id = ObjectId.fromString(line.substring(sp1 + 1, sp2));
  250. stage = Integer.parseInt(line.substring(sp2 + 1, tab));
  251. path = line.substring(tab + 1);
  252. }
  253. }
  254. private static class CGitLsTreeRecord {
  255. final int mode;
  256. final ObjectId id;
  257. final String path;
  258. CGitLsTreeRecord(final String line) {
  259. final int tab = line.indexOf('\t');
  260. final int sp1 = line.indexOf(' ');
  261. final int sp2 = line.indexOf(' ', sp1 + 1);
  262. mode = Integer.parseInt(line.substring(0, sp1), 8);
  263. id = ObjectId.fromString(line.substring(sp2 + 1, tab));
  264. path = line.substring(tab + 1);
  265. }
  266. }
  267. }