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

SymlinksTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. try (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. /**
  93. * Steps: 1.Add symlink 'a' 2.Commit 3.Create branch '1' 4.Replace symlink
  94. * 'a' by file 'a' 5.Commit 6.Checkout branch '1'
  95. *
  96. * The working tree should contain 'a' with FileMode.SYMLINK after the
  97. * checkout.
  98. *
  99. * @throws Exception
  100. */
  101. @Test
  102. public void fileModeTestSymlinkThenFile() throws Exception {
  103. try (Git git = new Git(db)) {
  104. writeTrashFile("b", "Hello world b");
  105. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b");
  106. git.add().addFilepattern(".").call();
  107. git.commit().setMessage("add file b & symlink a").call();
  108. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  109. git.rm().addFilepattern("a").call();
  110. writeTrashFile("a", "Hello world a");
  111. git.add().addFilepattern("a").call();
  112. git.commit().setMessage("add file a").call();
  113. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  114. db.getWorkTree(), "a"), db.getFS());
  115. assertEquals(FileMode.REGULAR_FILE, entry.getMode());
  116. git.checkout().setName(branch_1.getName()).call();
  117. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  118. db.getFS());
  119. assertEquals(FileMode.SYMLINK, entry.getMode());
  120. }
  121. }
  122. /**
  123. * Steps: 1.Add folder 'a' 2.Commit 3.Create branch '1' 4.Replace folder 'a'
  124. * by symlink 'a' 5.Commit 6.Checkout branch '1'
  125. *
  126. * The working tree should contain 'a' with FileMode.TREE after the
  127. * checkout.
  128. *
  129. * @throws Exception
  130. */
  131. @Test
  132. public void fileModeTestFolderThenSymlink() throws Exception {
  133. try (Git git = new Git(db)) {
  134. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  135. writeTrashFile("a/b", "Hello world b");
  136. writeTrashFile("c", "Hello world c");
  137. git.add().addFilepattern(".").call();
  138. git.commit().setMessage("add folder a").call();
  139. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  140. git.rm().addFilepattern("a").call();
  141. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c");
  142. git.add().addFilepattern("a").call();
  143. git.commit().setMessage("add symlink a").call();
  144. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  145. db.getWorkTree(), "a"), db.getFS());
  146. assertEquals(FileMode.SYMLINK, entry.getMode());
  147. git.checkout().setName(branch_1.getName()).call();
  148. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  149. db.getFS());
  150. assertEquals(FileMode.TREE, entry.getMode());
  151. }
  152. }
  153. /**
  154. * Steps: 1.Add symlink 'a' 2.Commit 3.Create branch '1' 4.Replace symlink
  155. * 'a' by folder 'a' 5.Commit 6.Checkout branch '1'
  156. *
  157. * The working tree should contain 'a' with FileMode.SYMLINK after the
  158. * checkout.
  159. *
  160. * @throws Exception
  161. */
  162. @Test
  163. public void fileModeTestSymlinkThenFolder() throws Exception {
  164. try (Git git = new Git(db)) {
  165. writeTrashFile("c", "Hello world c");
  166. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "c");
  167. git.add().addFilepattern(".").call();
  168. git.commit().setMessage("add symlink a").call();
  169. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  170. git.rm().addFilepattern("a").call();
  171. FileUtils.mkdirs(new File(db.getWorkTree(), "a"));
  172. writeTrashFile("a/b", "Hello world b");
  173. git.add().addFilepattern("a").call();
  174. git.commit().setMessage("add folder a").call();
  175. FileEntry entry = new FileTreeIterator.FileEntry(new File(
  176. db.getWorkTree(), "a"), db.getFS());
  177. assertEquals(FileMode.TREE, entry.getMode());
  178. git.checkout().setName(branch_1.getName()).call();
  179. entry = new FileTreeIterator.FileEntry(new File(db.getWorkTree(), "a"),
  180. db.getFS());
  181. assertEquals(FileMode.SYMLINK, entry.getMode());
  182. }
  183. }
  184. /**
  185. * Steps: 1.Add file 'b' 2.Commit 3.Create branch '1' 4.Add symlink 'a'
  186. * 5.Commit 6.Checkout branch '1'
  187. *
  188. * The working tree should not contain 'a' -> FileMode.MISSING after the
  189. * checkout.
  190. *
  191. * @throws Exception
  192. */
  193. @Test
  194. public void fileModeTestMissingThenSymlink() throws Exception {
  195. try (Git git = new Git(db);
  196. TreeWalk tw = new TreeWalk(db);) {
  197. writeTrashFile("b", "Hello world b");
  198. git.add().addFilepattern(".").call();
  199. RevCommit commit1 = git.commit().setMessage("add file b").call();
  200. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  201. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b");
  202. git.add().addFilepattern("a").call();
  203. RevCommit commit2 = git.commit().setMessage("add symlink a").call();
  204. git.checkout().setName(branch_1.getName()).call();
  205. tw.addTree(commit1.getTree());
  206. tw.addTree(commit2.getTree());
  207. List<DiffEntry> scan = DiffEntry.scan(tw);
  208. assertEquals(1, scan.size());
  209. assertEquals(FileMode.SYMLINK, scan.get(0).getNewMode());
  210. assertEquals(FileMode.MISSING, scan.get(0).getOldMode());
  211. }
  212. }
  213. /**
  214. * Steps: 1.Add symlink 'a' 2.Commit 3.Create branch '1' 4.Delete symlink
  215. * 'a' 5.Commit 6.Checkout branch '1'
  216. *
  217. * The working tree should contain 'a' with FileMode.SYMLINK after the
  218. * checkout.
  219. *
  220. * @throws Exception
  221. */
  222. @Test
  223. public void fileModeTestSymlinkThenMissing() throws Exception {
  224. try (Git git = new Git(db);
  225. TreeWalk tw = new TreeWalk(db);) {
  226. writeTrashFile("b", "Hello world b");
  227. FileUtils.createSymLink(new File(db.getWorkTree(), "a"), "b");
  228. git.add().addFilepattern(".").call();
  229. RevCommit commit1 = git.commit().setMessage("add file b & symlink a")
  230. .call();
  231. Ref branch_1 = git.branchCreate().setName("branch_1").call();
  232. git.rm().addFilepattern("a").call();
  233. RevCommit commit2 = git.commit().setMessage("delete symlink a").call();
  234. git.checkout().setName(branch_1.getName()).call();
  235. tw.addTree(commit1.getTree());
  236. tw.addTree(commit2.getTree());
  237. List<DiffEntry> scan = DiffEntry.scan(tw);
  238. assertEquals(1, scan.size());
  239. assertEquals(FileMode.MISSING, scan.get(0).getNewMode());
  240. assertEquals(FileMode.SYMLINK, scan.get(0).getOldMode());
  241. }
  242. }
  243. @Test
  244. public void createSymlinkAfterTarget() throws Exception {
  245. try (Git git = new Git(db)) {
  246. writeTrashFile("a", "start");
  247. git.add().addFilepattern("a").call();
  248. RevCommit base = git.commit().setMessage("init").call();
  249. writeTrashFile("target", "someData");
  250. FileUtils.createSymLink(new File(db.getWorkTree(), "link"), "target");
  251. git.add().addFilepattern("target").addFilepattern("link").call();
  252. git.commit().setMessage("add target").call();
  253. assertEquals(4, db.getWorkTree().list().length); // self-check
  254. git.checkout().setName(base.name()).call();
  255. assertEquals(2, db.getWorkTree().list().length); // self-check
  256. git.checkout().setName("master").call();
  257. assertEquals(4, db.getWorkTree().list().length);
  258. String data = read(new File(db.getWorkTree(), "target"));
  259. assertEquals(8, new File(db.getWorkTree(), "target").length());
  260. assertEquals("someData", data);
  261. data = read(new File(db.getWorkTree(), "link"));
  262. assertEquals("target",
  263. FileUtils.readSymLink(new File(db.getWorkTree(), "link")));
  264. assertEquals("someData", data);
  265. }
  266. }
  267. @Test
  268. public void createFileSymlinkBeforeTarget() throws Exception {
  269. try (Git git = new Git(db)) {
  270. writeTrashFile("a", "start");
  271. git.add().addFilepattern("a").call();
  272. RevCommit base = git.commit().setMessage("init").call();
  273. writeTrashFile("target", "someData");
  274. FileUtils.createSymLink(new File(db.getWorkTree(), "tlink"), "target");
  275. git.add().addFilepattern("target").addFilepattern("tlink").call();
  276. git.commit().setMessage("add target").call();
  277. assertEquals(4, db.getWorkTree().list().length); // self-check
  278. git.checkout().setName(base.name()).call();
  279. assertEquals(2, db.getWorkTree().list().length); // self-check
  280. git.checkout().setName("master").call();
  281. assertEquals(4, db.getWorkTree().list().length);
  282. String data = read(new File(db.getWorkTree(), "target"));
  283. assertEquals(8, new File(db.getWorkTree(), "target").length());
  284. assertEquals("someData", data);
  285. data = read(new File(db.getWorkTree(), "tlink"));
  286. assertEquals("target",
  287. FileUtils.readSymLink(new File(db.getWorkTree(), "tlink")));
  288. assertEquals("someData", data);
  289. }
  290. }
  291. @Test
  292. public void createDirSymlinkBeforeTarget() throws Exception {
  293. try (Git git = new Git(db)) {
  294. writeTrashFile("a", "start");
  295. git.add().addFilepattern("a").call();
  296. RevCommit base = git.commit().setMessage("init").call();
  297. FileUtils.createSymLink(new File(db.getWorkTree(), "link"), "target");
  298. FileUtils.mkdir(new File(db.getWorkTree(), "target"));
  299. writeTrashFile("target/file", "someData");
  300. git.add().addFilepattern("target").addFilepattern("link").call();
  301. git.commit().setMessage("add target").call();
  302. assertEquals(4, db.getWorkTree().list().length); // self-check
  303. git.checkout().setName(base.name()).call();
  304. assertEquals(2, db.getWorkTree().list().length); // self-check
  305. git.checkout().setName("master").call();
  306. assertEquals(4, db.getWorkTree().list().length);
  307. String data = read(new File(db.getWorkTree(), "target/file"));
  308. assertEquals(8, new File(db.getWorkTree(), "target/file").length());
  309. assertEquals("someData", data);
  310. data = read(new File(db.getWorkTree(), "link/file"));
  311. assertEquals("target",
  312. FileUtils.readSymLink(new File(db.getWorkTree(), "link")));
  313. assertEquals("someData", data);
  314. }
  315. }
  316. }