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.

RepoCommandSymlinkTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2016, 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.gitrepo;
  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 org.eclipse.jgit.api.Git;
  49. import org.eclipse.jgit.junit.JGitTestUtil;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.Repository;
  52. import org.eclipse.jgit.util.FS;
  53. import org.junit.Before;
  54. import org.junit.Test;
  55. public class RepoCommandSymlinkTest 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. private Repository defaultDb;
  63. private String rootUri;
  64. private String defaultUri;
  65. @Override
  66. public void setUp() throws Exception {
  67. super.setUp();
  68. defaultDb = createWorkRepository();
  69. try (Git git = new Git(defaultDb)) {
  70. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "hello world");
  71. git.add().addFilepattern("hello.txt").call();
  72. git.commit().setMessage("Initial commit").call();
  73. addRepoToClose(defaultDb);
  74. }
  75. defaultUri = defaultDb.getDirectory().toURI().toString();
  76. int root = defaultUri.lastIndexOf("/",
  77. defaultUri.lastIndexOf("/.git") - 1)
  78. + 1;
  79. rootUri = defaultUri.substring(0, root)
  80. + "manifest";
  81. defaultUri = defaultUri.substring(root);
  82. }
  83. @Test
  84. public void testLinkFileBare() throws Exception {
  85. try (
  86. Repository remoteDb = createBareRepository();
  87. Repository tempDb = createWorkRepository()) {
  88. StringBuilder xmlContent = new StringBuilder();
  89. xmlContent
  90. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  91. .append("<manifest>")
  92. .append("<remote name=\"remote1\" fetch=\".\" />")
  93. .append("<default revision=\"master\" remote=\"remote1\" />")
  94. .append("<project path=\"foo\" name=\"").append(defaultUri)
  95. .append("\" revision=\"master\" >")
  96. .append("<linkfile src=\"hello.txt\" dest=\"LinkedHello\" />")
  97. .append("<linkfile src=\"hello.txt\" dest=\"foo/LinkedHello\" />")
  98. .append("<linkfile src=\"hello.txt\" dest=\"subdir/LinkedHello\" />")
  99. .append("</project>")
  100. .append("<project path=\"bar/baz\" name=\"")
  101. .append(defaultUri).append("\" revision=\"master\" >")
  102. .append("<linkfile src=\"hello.txt\" dest=\"bar/foo/LinkedHello\" />")
  103. .append("</project>").append("</manifest>");
  104. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  105. xmlContent.toString());
  106. RepoCommand command = new RepoCommand(remoteDb);
  107. command.setPath(
  108. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  109. .setURI(rootUri).call();
  110. // Clone it
  111. File directory = createTempDirectory("testCopyFileBare");
  112. Repository localDb = Git.cloneRepository().setDirectory(directory)
  113. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  114. .getRepository();
  115. // The LinkedHello symlink should exist.
  116. File linkedhello = new File(localDb.getWorkTree(), "LinkedHello");
  117. assertTrue("The LinkedHello file should exist",
  118. localDb.getFS().exists(linkedhello));
  119. assertTrue("The LinkedHello file should be a symlink",
  120. localDb.getFS().isSymLink(linkedhello));
  121. assertEquals("foo/hello.txt",
  122. localDb.getFS().readSymLink(linkedhello));
  123. // The foo/LinkedHello file should be skipped.
  124. File linkedfoohello = new File(localDb.getWorkTree(), "foo/LinkedHello");
  125. assertFalse("The foo/LinkedHello file should be skipped",
  126. localDb.getFS().exists(linkedfoohello));
  127. // The subdir/LinkedHello file should use a relative ../
  128. File linkedsubdirhello = new File(localDb.getWorkTree(),
  129. "subdir/LinkedHello");
  130. assertTrue("The subdir/LinkedHello file should exist",
  131. localDb.getFS().exists(linkedsubdirhello));
  132. assertTrue("The subdir/LinkedHello file should be a symlink",
  133. localDb.getFS().isSymLink(linkedsubdirhello));
  134. assertEquals("../foo/hello.txt",
  135. localDb.getFS().readSymLink(linkedsubdirhello));
  136. // The bar/foo/LinkedHello file should use a single relative ../
  137. File linkedbarfoohello = new File(localDb.getWorkTree(),
  138. "bar/foo/LinkedHello");
  139. assertTrue("The bar/foo/LinkedHello file should exist",
  140. localDb.getFS().exists(linkedbarfoohello));
  141. assertTrue("The bar/foo/LinkedHello file should be a symlink",
  142. localDb.getFS().isSymLink(linkedbarfoohello));
  143. assertEquals("../baz/hello.txt",
  144. localDb.getFS().readSymLink(linkedbarfoohello));
  145. localDb.close();
  146. }
  147. }
  148. }