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 10.0KB

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