Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

NameConflictTreeWalkTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2008, 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.treewalk;
  44. import java.io.ByteArrayInputStream;
  45. import org.eclipse.jgit.dircache.DirCache;
  46. import org.eclipse.jgit.dircache.DirCacheBuilder;
  47. import org.eclipse.jgit.dircache.DirCacheEntry;
  48. import org.eclipse.jgit.dircache.DirCacheIterator;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.lib.FileMode;
  51. import org.eclipse.jgit.lib.ObjectWriter;
  52. import org.eclipse.jgit.lib.RepositoryTestCase;
  53. public class NameConflictTreeWalkTest extends RepositoryTestCase {
  54. private static final FileMode TREE = FileMode.TREE;
  55. private static final FileMode SYMLINK = FileMode.SYMLINK;
  56. private static final FileMode MISSING = FileMode.MISSING;
  57. private static final FileMode REGULAR_FILE = FileMode.REGULAR_FILE;
  58. private static final FileMode EXECUTABLE_FILE = FileMode.EXECUTABLE_FILE;
  59. public void testNoDF_NoGap() throws Exception {
  60. final DirCache tree0 = db.readDirCache();
  61. final DirCache tree1 = db.readDirCache();
  62. {
  63. final DirCacheBuilder b0 = tree0.builder();
  64. final DirCacheBuilder b1 = tree1.builder();
  65. b0.add(makeEntry("a", REGULAR_FILE));
  66. b0.add(makeEntry("a.b", EXECUTABLE_FILE));
  67. b1.add(makeEntry("a/b", REGULAR_FILE));
  68. b0.add(makeEntry("a0b", SYMLINK));
  69. b0.finish();
  70. b1.finish();
  71. assertEquals(3, tree0.getEntryCount());
  72. assertEquals(1, tree1.getEntryCount());
  73. }
  74. final TreeWalk tw = new TreeWalk(db);
  75. tw.reset();
  76. tw.addTree(new DirCacheIterator(tree0));
  77. tw.addTree(new DirCacheIterator(tree1));
  78. assertModes("a", REGULAR_FILE, MISSING, tw);
  79. assertModes("a.b", EXECUTABLE_FILE, MISSING, tw);
  80. assertModes("a", MISSING, TREE, tw);
  81. tw.enterSubtree();
  82. assertModes("a/b", MISSING, REGULAR_FILE, tw);
  83. assertModes("a0b", SYMLINK, MISSING, tw);
  84. }
  85. public void testDF_NoGap() throws Exception {
  86. final DirCache tree0 = db.readDirCache();
  87. final DirCache tree1 = db.readDirCache();
  88. {
  89. final DirCacheBuilder b0 = tree0.builder();
  90. final DirCacheBuilder b1 = tree1.builder();
  91. b0.add(makeEntry("a", REGULAR_FILE));
  92. b0.add(makeEntry("a.b", EXECUTABLE_FILE));
  93. b1.add(makeEntry("a/b", REGULAR_FILE));
  94. b0.add(makeEntry("a0b", SYMLINK));
  95. b0.finish();
  96. b1.finish();
  97. assertEquals(3, tree0.getEntryCount());
  98. assertEquals(1, tree1.getEntryCount());
  99. }
  100. final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
  101. tw.reset();
  102. tw.addTree(new DirCacheIterator(tree0));
  103. tw.addTree(new DirCacheIterator(tree1));
  104. assertModes("a", REGULAR_FILE, TREE, tw);
  105. assertTrue(tw.isSubtree());
  106. tw.enterSubtree();
  107. assertModes("a/b", MISSING, REGULAR_FILE, tw);
  108. assertModes("a.b", EXECUTABLE_FILE, MISSING, tw);
  109. assertModes("a0b", SYMLINK, MISSING, tw);
  110. }
  111. public void testDF_GapByOne() throws Exception {
  112. final DirCache tree0 = db.readDirCache();
  113. final DirCache tree1 = db.readDirCache();
  114. {
  115. final DirCacheBuilder b0 = tree0.builder();
  116. final DirCacheBuilder b1 = tree1.builder();
  117. b0.add(makeEntry("a", REGULAR_FILE));
  118. b0.add(makeEntry("a.b", EXECUTABLE_FILE));
  119. b1.add(makeEntry("a.b", EXECUTABLE_FILE));
  120. b1.add(makeEntry("a/b", REGULAR_FILE));
  121. b0.add(makeEntry("a0b", SYMLINK));
  122. b0.finish();
  123. b1.finish();
  124. assertEquals(3, tree0.getEntryCount());
  125. assertEquals(2, tree1.getEntryCount());
  126. }
  127. final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
  128. tw.reset();
  129. tw.addTree(new DirCacheIterator(tree0));
  130. tw.addTree(new DirCacheIterator(tree1));
  131. assertModes("a", REGULAR_FILE, TREE, tw);
  132. assertTrue(tw.isSubtree());
  133. tw.enterSubtree();
  134. assertModes("a/b", MISSING, REGULAR_FILE, tw);
  135. assertModes("a.b", EXECUTABLE_FILE, EXECUTABLE_FILE, tw);
  136. assertModes("a0b", SYMLINK, MISSING, tw);
  137. }
  138. public void testDF_SkipsSeenSubtree() throws Exception {
  139. final DirCache tree0 = db.readDirCache();
  140. final DirCache tree1 = db.readDirCache();
  141. {
  142. final DirCacheBuilder b0 = tree0.builder();
  143. final DirCacheBuilder b1 = tree1.builder();
  144. b0.add(makeEntry("a", REGULAR_FILE));
  145. b1.add(makeEntry("a.b", EXECUTABLE_FILE));
  146. b1.add(makeEntry("a/b", REGULAR_FILE));
  147. b0.add(makeEntry("a0b", SYMLINK));
  148. b1.add(makeEntry("a0b", SYMLINK));
  149. b0.finish();
  150. b1.finish();
  151. assertEquals(2, tree0.getEntryCount());
  152. assertEquals(3, tree1.getEntryCount());
  153. }
  154. final NameConflictTreeWalk tw = new NameConflictTreeWalk(db);
  155. tw.reset();
  156. tw.addTree(new DirCacheIterator(tree0));
  157. tw.addTree(new DirCacheIterator(tree1));
  158. assertModes("a", REGULAR_FILE, TREE, tw);
  159. assertTrue(tw.isSubtree());
  160. tw.enterSubtree();
  161. assertModes("a/b", MISSING, REGULAR_FILE, tw);
  162. assertModes("a.b", MISSING, EXECUTABLE_FILE, tw);
  163. assertModes("a0b", SYMLINK, SYMLINK, tw);
  164. }
  165. private DirCacheEntry makeEntry(final String path, final FileMode mode)
  166. throws Exception {
  167. final byte[] pathBytes = Constants.encode(path);
  168. final DirCacheEntry ent = new DirCacheEntry(path);
  169. ent.setFileMode(mode);
  170. ent.setObjectId(new ObjectWriter(db).computeBlobSha1(pathBytes.length,
  171. new ByteArrayInputStream(pathBytes)));
  172. return ent;
  173. }
  174. private static void assertModes(final String path, final FileMode mode0,
  175. final FileMode mode1, final TreeWalk tw) throws Exception {
  176. assertTrue("has " + path, tw.next());
  177. assertEquals(path, tw.getPathString());
  178. assertEquals(mode0, tw.getFileMode(0));
  179. assertEquals(mode1, tw.getFileMode(1));
  180. }
  181. }