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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2012-2013, Robin Rosenberg <robin.rosenberg@dewire.com>
  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 static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import org.eclipse.jgit.api.Git;
  50. import org.eclipse.jgit.api.ResetCommand.ResetType;
  51. import org.eclipse.jgit.dircache.DirCache;
  52. import org.eclipse.jgit.dircache.DirCacheEditor;
  53. import org.eclipse.jgit.dircache.DirCacheEntry;
  54. import org.eclipse.jgit.dircache.DirCacheIterator;
  55. import org.eclipse.jgit.junit.RepositoryTestCase;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.FileMode;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.ObjectInserter;
  60. import org.eclipse.jgit.util.FS;
  61. import org.eclipse.jgit.util.FileUtils;
  62. import org.junit.Test;
  63. public class FileTreeIteratorJava7Test extends RepositoryTestCase {
  64. @Test
  65. public void testFileModeSymLinkIsNotATree() throws IOException {
  66. org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  67. FS fs = db.getFS();
  68. // mål = target in swedish, just to get som unicode in here
  69. writeTrashFile("mål/data", "targetdata");
  70. fs.createSymLink(new File(trash, "länk"), "mål");
  71. FileTreeIterator fti = new FileTreeIterator(db);
  72. assertFalse(fti.eof());
  73. assertEquals("länk", fti.getEntryPathString());
  74. assertEquals(FileMode.SYMLINK, fti.getEntryFileMode());
  75. fti.next(1);
  76. assertFalse(fti.eof());
  77. assertEquals("mål", fti.getEntryPathString());
  78. assertEquals(FileMode.TREE, fti.getEntryFileMode());
  79. fti.next(1);
  80. assertTrue(fti.eof());
  81. }
  82. @Test
  83. public void testSymlinkNotModifiedThoughNormalized() throws Exception {
  84. DirCache dc = db.lockDirCache();
  85. DirCacheEditor dce = dc.editor();
  86. final String UNNORMALIZED = "target/";
  87. final byte[] UNNORMALIZED_BYTES = Constants.encode(UNNORMALIZED);
  88. try (ObjectInserter oi = db.newObjectInserter()) {
  89. final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
  90. UNNORMALIZED_BYTES, 0, UNNORMALIZED_BYTES.length);
  91. dce.add(new DirCacheEditor.PathEdit("link") {
  92. @Override
  93. public void apply(DirCacheEntry ent) {
  94. ent.setFileMode(FileMode.SYMLINK);
  95. ent.setObjectId(linkid);
  96. ent.setLength(UNNORMALIZED_BYTES.length);
  97. }
  98. });
  99. assertTrue(dce.commit());
  100. }
  101. try (Git git = new Git(db)) {
  102. git.commit().setMessage("Adding link").call();
  103. git.reset().setMode(ResetType.HARD).call();
  104. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  105. FileTreeIterator fti = new FileTreeIterator(db);
  106. // self-check
  107. assertEquals("link", fti.getEntryPathString());
  108. assertEquals("link", dci.getEntryPathString());
  109. // test
  110. assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
  111. db.newObjectReader()));
  112. }
  113. }
  114. /**
  115. * Like #testSymlinkNotModifiedThoughNormalized but there is no
  116. * normalization being done.
  117. *
  118. * @throws Exception
  119. */
  120. @Test
  121. public void testSymlinkModifiedNotNormalized() throws Exception {
  122. DirCache dc = db.lockDirCache();
  123. DirCacheEditor dce = dc.editor();
  124. final String NORMALIZED = "target";
  125. final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
  126. try (ObjectInserter oi = db.newObjectInserter()) {
  127. final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
  128. NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
  129. dce.add(new DirCacheEditor.PathEdit("link") {
  130. @Override
  131. public void apply(DirCacheEntry ent) {
  132. ent.setFileMode(FileMode.SYMLINK);
  133. ent.setObjectId(linkid);
  134. ent.setLength(NORMALIZED_BYTES.length);
  135. }
  136. });
  137. assertTrue(dce.commit());
  138. }
  139. try (Git git = new Git(db)) {
  140. git.commit().setMessage("Adding link").call();
  141. git.reset().setMode(ResetType.HARD).call();
  142. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  143. FileTreeIterator fti = new FileTreeIterator(db);
  144. // self-check
  145. assertEquals("link", fti.getEntryPathString());
  146. assertEquals("link", dci.getEntryPathString());
  147. // test
  148. assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
  149. db.newObjectReader()));
  150. }
  151. }
  152. /**
  153. * Like #testSymlinkNotModifiedThoughNormalized but here the link is
  154. * modified.
  155. *
  156. * @throws Exception
  157. */
  158. @Test
  159. public void testSymlinkActuallyModified() throws Exception {
  160. org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  161. final String NORMALIZED = "target";
  162. final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
  163. try (ObjectInserter oi = db.newObjectInserter()) {
  164. final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
  165. NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
  166. DirCache dc = db.lockDirCache();
  167. DirCacheEditor dce = dc.editor();
  168. dce.add(new DirCacheEditor.PathEdit("link") {
  169. @Override
  170. public void apply(DirCacheEntry ent) {
  171. ent.setFileMode(FileMode.SYMLINK);
  172. ent.setObjectId(linkid);
  173. ent.setLength(NORMALIZED_BYTES.length);
  174. }
  175. });
  176. assertTrue(dce.commit());
  177. }
  178. try (Git git = new Git(db)) {
  179. git.commit().setMessage("Adding link").call();
  180. git.reset().setMode(ResetType.HARD).call();
  181. FileUtils.delete(new File(trash, "link"), FileUtils.NONE);
  182. FS.DETECTED.createSymLink(new File(trash, "link"), "newtarget");
  183. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  184. FileTreeIterator fti = new FileTreeIterator(db);
  185. // self-check
  186. assertEquals("link", fti.getEntryPathString());
  187. assertEquals("link", dci.getEntryPathString());
  188. // test
  189. assertTrue(fti.isModified(dci.getDirCacheEntry(), true,
  190. db.newObjectReader()));
  191. }
  192. }
  193. }