選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SymlinksTest.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) 2013, Axel Richard <axel.richard@obeo.fr>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v1.0 which accompanies this
  7. * distribution, is reproduced below, and is available at
  8. * 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 without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice, this
  16. * list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
  23. * contributors may be used to endorse or promote products derived from this
  24. * software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. package org.eclipse.jgit.symlinks;
  39. import static org.junit.Assert.assertEquals;
  40. import java.io.File;
  41. import java.util.List;
  42. import org.eclipse.jgit.api.Git;
  43. import org.eclipse.jgit.diff.DiffEntry;
  44. import org.eclipse.jgit.junit.RepositoryTestCase;
  45. import org.eclipse.jgit.lib.FileMode;
  46. import org.eclipse.jgit.lib.Ref;
  47. import org.eclipse.jgit.revwalk.RevCommit;
  48. import org.eclipse.jgit.treewalk.FileTreeIterator;
  49. import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
  50. import org.eclipse.jgit.treewalk.TreeWalk;
  51. import org.eclipse.jgit.util.FS;
  52. import org.eclipse.jgit.util.FileUtils;
  53. import org.junit.Before;
  54. import org.junit.Test;
  55. public class SymlinksTest extends RepositoryTestCase {
  56. @Before
  57. public void beforeMethod() {
  58. // If this assumption fails the tests are skipped. When running on a
  59. // filesystem not supporting symlinks I don't want this tests
  60. org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  61. }
  62. /**
  63. * Steps: 1.Add file 'a' 2.Commit 3.Create branch '1' 4.Replace file 'a' by
  64. * symlink 'a' 5.Commit 6.Checkout branch '1'
  65. *
  66. * The working tree should contain 'a' with FileMode.REGULAR_FILE after the
  67. * checkout.
  68. *
  69. * @throws Exception
  70. */
  71. @Test
  72. public void fileModeTestFileThenSymlink() throws Exception {
  73. Git git = new Git(db);
  74. writeTrashFile("a", "Hello world a");
  75. writeTrashFile("b", "Hello world b");
  76. git.add().addFilepattern(".").call();
  77. git.commit().setMessage("add files a & b").call();
  78. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  79. git.rm().addFilepattern("a").call();
  80. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b");
  81. git.add().addFilepattern("a").call();
  82. git.commit().setMessage("add symlink a").call();
  83. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  84. db.getWorkTree(), "a"), db.getFS());
  85. assertEquals(FileMode.SYMLINK, entry.getMode());
  86. git.checkout().setName(branch_1.getName()).call();
  87. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  88. db.getFS());
  89. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  90. }
  91. /**
  92. * Steps: 1.Add symlink 'a' 2.Commit 3.Create branch '1' 4.Replace symlink
  93. * 'a' by file 'a' 5.Commit 6.Checkout branch '1'
  94. *
  95. * The working tree should contain 'a' with FileMode.SYMLINK after the
  96. * checkout.
  97. *
  98. * @throws Exception
  99. */
  100. @Test
  101. public void fileModeTestSymlinkThenFile() throws Exception {
  102. Git git = new Git(db);
  103. writeTrashFile("b", "Hello world b");
  104. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b");
  105. git.add().addFilepattern(".").call();
  106. git.commit().setMessage("add file b & symlink a").call();
  107. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  108. git.rm().addFilepattern("a").call();
  109. writeTrashFile("a", "Hello world a");
  110. git.add().addFilepattern("a").call();
  111. git.commit().setMessage("add file a").call();
  112. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  113. db.getWorkTree(), "a"), db.getFS());
  114. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  115. git.checkout().setName(branch_1.getName()).call();
  116. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  117. db.getFS());
  118. assertEquals(FileMode.SYMLINK, entry.getMode());
  119. }
  120. /**
  121. * Steps: 1.Add folder 'a' 2.Commit 3.Create branch '1' 4.Replace folder 'a'
  122. * by symlink 'a' 5.Commit 6.Checkout branch '1'
  123. *
  124. * The working tree should contain 'a' with FileMode.TREE after the
  125. * checkout.
  126. *
  127. * @throws Exception
  128. */
  129. @Test
  130. public void fileModeTestFolderThenSymlink() throws Exception {
  131. Git git = new Git(db);
  132. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  133. writeTrashFile("a/b", "Hello world b");
  134. writeTrashFile("c", "Hello world c");
  135. git.add().addFilepattern(".").call();
  136. git.commit().setMessage("add folder a").call();
  137. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  138. git.rm().addFilepattern("a").call();
  139. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c");
  140. git.add().addFilepattern("a").call();
  141. git.commit().setMessage("add symlink a").call();
  142. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  143. db.getWorkTree(), "a"), db.getFS());
  144. assertEquals(FileMode.SYMLINK, entry.getMode());
  145. git.checkout().setName(branch_1.getName()).call();
  146. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  147. db.getFS());
  148. assertEquals(FileMode.TREE, entry.getMode());
  149. }
  150. /**
  151. * Steps: 1.Add symlink 'a' 2.Commit 3.Create branch '1' 4.Replace symlink
  152. * 'a' by folder 'a' 5.Commit 6.Checkout branch '1'
  153. *
  154. * The working tree should contain 'a' with FileMode.SYMLINK after the
  155. * checkout.
  156. *
  157. * @throws Exception
  158. */
  159. @Test
  160. public void fileModeTestSymlinkThenFolder() throws Exception {
  161. Git git = new Git(db);
  162. writeTrashFile("c", "Hello world c");
  163. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c");
  164. git.add().addFilepattern(".").call();
  165. git.commit().setMessage("add symlink a").call();
  166. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  167. git.rm().addFilepattern("a").call();
  168. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  169. writeTrashFile("a/b", "Hello world b");
  170. git.add().addFilepattern("a").call();
  171. git.commit().setMessage("add folder a").call();
  172. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  173. db.getWorkTree(), "a"), db.getFS());
  174. assertEquals(FileMode.TREE, entry.getMode());
  175. git.checkout().setName(branch_1.getName()).call();
  176. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  177. db.getFS());
  178. assertEquals(FileMode.SYMLINK, entry.getMode());
  179. }
  180. /**
  181. * Steps: 1.Add file 'b' 2.Commit 3.Create branch '1' 4.Add symlink 'a'
  182. * 5.Commit 6.Checkout branch '1'
  183. *
  184. * The working tree should not contain 'a' -> FileMode.MISSING after the
  185. * checkout.
  186. *
  187. * @throws Exception
  188. */
  189. @Test
  190. public void fileModeTestMissingThenSymlink() throws Exception {
  191. Git git = new Git(db);
  192. writeTrashFile("b", "Hello world b");
  193. git.add().addFilepattern(".").call();
  194. RevCommit commit1 = git.commit().setMessage("add file b").call();
  195. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  196. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b");
  197. git.add().addFilepattern("a").call();
  198. RevCommit commit2 = git.commit().setMessage("add symlink a").call();
  199. git.checkout().setName(branch_1.getName()).call();
  200. TreeWalk tw = new TreeWalk(db);
  201. tw.addTree(commit1.getTree());
  202. tw.addTree(commit2.getTree());
  203. List<DiffEntry> scan = DiffEntry.scan(tw);
  204. assertEquals(1, scan.size());
  205. assertEquals(FileMode.SYMLINK, scan.get(0).getNewMode());
  206. assertEquals(FileMode.MISSING, scan.get(0).getOldMode());
  207. }
  208. /**
  209. * Steps: 1.Add symlink 'a' 2.Commit 3.Create branch '1' 4.Delete symlink
  210. * 'a' 5.Commit 6.Checkout branch '1'
  211. *
  212. * The working tree should contain 'a' with FileMode.SYMLINK after the
  213. * checkout.
  214. *
  215. * @throws Exception
  216. */
  217. @Test
  218. public void fileModeTestSymlinkThenMissing() throws Exception {
  219. Git git = new Git(db);
  220. writeTrashFile("b", "Hello world b");
  221. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b");
  222. git.add().addFilepattern(".").call();
  223. RevCommit commit1 = git.commit().setMessage("add file b & symlink a")
  224. .call();
  225. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  226. git.rm().addFilepattern("a").call();
  227. RevCommit commit2 = git.commit().setMessage("delete symlink a").call();
  228. git.checkout().setName(branch_1.getName()).call();
  229. TreeWalk tw = new TreeWalk(db);
  230. tw.addTree(commit1.getTree());
  231. tw.addTree(commit2.getTree());
  232. List<DiffEntry> scan = DiffEntry.scan(tw);
  233. assertEquals(1, scan.size());
  234. assertEquals(FileMode.MISSING, scan.get(0).getNewMode());
  235. assertEquals(FileMode.SYMLINK, scan.get(0).getOldMode());
  236. }
  237. }