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

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