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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileInputStream;
  47. import java.io.InputStreamReader;
  48. import java.util.ArrayList;
  49. import java.util.Iterator;
  50. import java.util.LinkedHashMap;
  51. import java.util.Map;
  52. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.eclipse.jgit.treewalk.TreeWalk;
  57. import org.eclipse.jgit.util.JGitTestUtil;
  58. public class DirCacheCGitCompatabilityTest extends LocalDiskRepositoryTestCase {
  59. private final File index = pathOf("gitgit.index");
  60. public void testReadIndex_LsFiles() throws Exception {
  61. final Map<String, CGitIndexRecord> ls = readLsFiles();
  62. final DirCache dc = new DirCache(index);
  63. assertEquals(0, dc.getEntryCount());
  64. dc.read();
  65. assertEquals(ls.size(), dc.getEntryCount());
  66. {
  67. final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
  68. for (int i = 0; rItr.hasNext(); i++)
  69. assertEqual(rItr.next(), dc.getEntry(i));
  70. }
  71. }
  72. public void testTreeWalk_LsFiles() throws Exception {
  73. final Repository db = createBareRepository();
  74. final Map<String, CGitIndexRecord> ls = readLsFiles();
  75. final DirCache dc = new DirCache(index);
  76. assertEquals(0, dc.getEntryCount());
  77. dc.read();
  78. assertEquals(ls.size(), dc.getEntryCount());
  79. {
  80. final Iterator<CGitIndexRecord> rItr = ls.values().iterator();
  81. final TreeWalk tw = new TreeWalk(db);
  82. tw.reset();
  83. tw.setRecursive(true);
  84. tw.addTree(new DirCacheIterator(dc));
  85. while (rItr.hasNext()) {
  86. final DirCacheIterator dcItr;
  87. assertTrue(tw.next());
  88. dcItr = tw.getTree(0, DirCacheIterator.class);
  89. assertNotNull(dcItr);
  90. assertEqual(rItr.next(), dcItr.getDirCacheEntry());
  91. }
  92. }
  93. }
  94. private static void assertEqual(final CGitIndexRecord c,
  95. final DirCacheEntry j) {
  96. assertNotNull(c);
  97. assertNotNull(j);
  98. assertEquals(c.path, j.getPathString());
  99. assertEquals(c.id, j.getObjectId());
  100. assertEquals(c.mode, j.getRawMode());
  101. assertEquals(c.stage, j.getStage());
  102. }
  103. public void testReadIndex_DirCacheTree() throws Exception {
  104. final Map<String, CGitIndexRecord> cList = readLsFiles();
  105. final Map<String, CGitLsTreeRecord> cTree = readLsTree();
  106. final DirCache dc = new DirCache(index);
  107. assertEquals(0, dc.getEntryCount());
  108. dc.read();
  109. assertEquals(cList.size(), dc.getEntryCount());
  110. final DirCacheTree jTree = dc.getCacheTree(false);
  111. assertNotNull(jTree);
  112. assertEquals("", jTree.getNameString());
  113. assertEquals("", jTree.getPathString());
  114. assertTrue(jTree.isValid());
  115. assertEquals(ObjectId
  116. .fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"), jTree
  117. .getObjectId());
  118. assertEquals(cList.size(), jTree.getEntrySpan());
  119. final ArrayList<CGitLsTreeRecord> subtrees = new ArrayList<CGitLsTreeRecord>();
  120. for (final CGitLsTreeRecord r : cTree.values()) {
  121. if (FileMode.TREE.equals(r.mode))
  122. subtrees.add(r);
  123. }
  124. assertEquals(subtrees.size(), jTree.getChildCount());
  125. for (int i = 0; i < jTree.getChildCount(); i++) {
  126. final DirCacheTree sj = jTree.getChild(i);
  127. final CGitLsTreeRecord sc = subtrees.get(i);
  128. assertEquals(sc.path, sj.getNameString());
  129. assertEquals(sc.path + "/", sj.getPathString());
  130. assertTrue(sj.isValid());
  131. assertEquals(sc.id, sj.getObjectId());
  132. }
  133. }
  134. private File pathOf(final String name) {
  135. return JGitTestUtil.getTestResourceFile(name);
  136. }
  137. private Map<String, CGitIndexRecord> readLsFiles() throws Exception {
  138. final LinkedHashMap<String, CGitIndexRecord> r = new LinkedHashMap<String, CGitIndexRecord>();
  139. final BufferedReader br = new BufferedReader(new InputStreamReader(
  140. new FileInputStream(pathOf("gitgit.lsfiles")), "UTF-8"));
  141. try {
  142. String line;
  143. while ((line = br.readLine()) != null) {
  144. final CGitIndexRecord cr = new CGitIndexRecord(line);
  145. r.put(cr.path, cr);
  146. }
  147. } finally {
  148. br.close();
  149. }
  150. return r;
  151. }
  152. private Map<String, CGitLsTreeRecord> readLsTree() throws Exception {
  153. final LinkedHashMap<String, CGitLsTreeRecord> r = new LinkedHashMap<String, CGitLsTreeRecord>();
  154. final BufferedReader br = new BufferedReader(new InputStreamReader(
  155. new FileInputStream(pathOf("gitgit.lstree")), "UTF-8"));
  156. try {
  157. String line;
  158. while ((line = br.readLine()) != null) {
  159. final CGitLsTreeRecord cr = new CGitLsTreeRecord(line);
  160. r.put(cr.path, cr);
  161. }
  162. } finally {
  163. br.close();
  164. }
  165. return r;
  166. }
  167. private static class CGitIndexRecord {
  168. final int mode;
  169. final ObjectId id;
  170. final int stage;
  171. final String path;
  172. CGitIndexRecord(final String line) {
  173. final int tab = line.indexOf('\t');
  174. final int sp1 = line.indexOf(' ');
  175. final int sp2 = line.indexOf(' ', sp1 + 1);
  176. mode = Integer.parseInt(line.substring(0, sp1), 8);
  177. id = ObjectId.fromString(line.substring(sp1 + 1, sp2));
  178. stage = Integer.parseInt(line.substring(sp2 + 1, tab));
  179. path = line.substring(tab + 1);
  180. }
  181. }
  182. private static class CGitLsTreeRecord {
  183. final int mode;
  184. final ObjectId id;
  185. final String path;
  186. CGitLsTreeRecord(final String line) {
  187. final int tab = line.indexOf('\t');
  188. final int sp1 = line.indexOf(' ');
  189. final int sp2 = line.indexOf(' ', sp1 + 1);
  190. mode = Integer.parseInt(line.substring(0, sp1), 8);
  191. id = ObjectId.fromString(line.substring(sp2 + 1, tab));
  192. path = line.substring(tab + 1);
  193. }
  194. }
  195. }