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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2016, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.gitrepo;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import org.eclipse.jgit.api.Git;
  16. import org.eclipse.jgit.junit.JGitTestUtil;
  17. import org.eclipse.jgit.junit.RepositoryTestCase;
  18. import org.eclipse.jgit.lib.Repository;
  19. import org.eclipse.jgit.util.FS;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. public class RepoCommandSymlinkTest extends RepositoryTestCase {
  23. @Before
  24. public void beforeMethod() {
  25. // If this assumption fails the tests are skipped. When running on a
  26. // filesystem not supporting symlinks I don't want this tests
  27. org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  28. }
  29. private Repository defaultDb;
  30. private String rootUri;
  31. private String defaultUri;
  32. @Override
  33. public void setUp() throws Exception {
  34. super.setUp();
  35. defaultDb = createWorkRepository();
  36. try (Git git = new Git(defaultDb)) {
  37. JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "hello world");
  38. git.add().addFilepattern("hello.txt").call();
  39. git.commit().setMessage("Initial commit").call();
  40. addRepoToClose(defaultDb);
  41. }
  42. defaultUri = defaultDb.getDirectory().toURI().toString();
  43. int root = defaultUri.lastIndexOf("/",
  44. defaultUri.lastIndexOf("/.git") - 1)
  45. + 1;
  46. rootUri = defaultUri.substring(0, root)
  47. + "manifest";
  48. defaultUri = defaultUri.substring(root);
  49. }
  50. @Test
  51. public void testLinkFileBare() throws Exception {
  52. try (
  53. Repository remoteDb = createBareRepository();
  54. Repository tempDb = createWorkRepository()) {
  55. StringBuilder xmlContent = new StringBuilder();
  56. xmlContent
  57. .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
  58. .append("<manifest>")
  59. .append("<remote name=\"remote1\" fetch=\".\" />")
  60. .append("<default revision=\"master\" remote=\"remote1\" />")
  61. .append("<project path=\"foo\" name=\"").append(defaultUri)
  62. .append("\" revision=\"master\" >")
  63. .append("<linkfile src=\"hello.txt\" dest=\"LinkedHello\" />")
  64. .append("<linkfile src=\"hello.txt\" dest=\"foo/LinkedHello\" />")
  65. .append("<linkfile src=\"hello.txt\" dest=\"subdir/LinkedHello\" />")
  66. .append("</project>")
  67. .append("<project path=\"bar/baz\" name=\"")
  68. .append(defaultUri).append("\" revision=\"master\" >")
  69. .append("<linkfile src=\"hello.txt\" dest=\"bar/foo/LinkedHello\" />")
  70. .append("</project>").append("</manifest>");
  71. JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
  72. xmlContent.toString());
  73. RepoCommand command = new RepoCommand(remoteDb);
  74. command.setPath(
  75. tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
  76. .setURI(rootUri).call();
  77. // Clone it
  78. File directory = createTempDirectory("testCopyFileBare");
  79. try (Repository localDb = Git.cloneRepository()
  80. .setDirectory(directory)
  81. .setURI(remoteDb.getDirectory().toURI().toString()).call()
  82. .getRepository()) {
  83. // The LinkedHello symlink should exist.
  84. File linkedhello = new File(localDb.getWorkTree(),
  85. "LinkedHello");
  86. assertTrue("The LinkedHello file should exist",
  87. localDb.getFS().exists(linkedhello));
  88. assertTrue("The LinkedHello file should be a symlink",
  89. localDb.getFS().isSymLink(linkedhello));
  90. assertEquals("foo/hello.txt",
  91. localDb.getFS().readSymLink(linkedhello));
  92. // The foo/LinkedHello file should be skipped.
  93. File linkedfoohello = new File(localDb.getWorkTree(),
  94. "foo/LinkedHello");
  95. assertFalse("The foo/LinkedHello file should be skipped",
  96. localDb.getFS().exists(linkedfoohello));
  97. // The subdir/LinkedHello file should use a relative ../
  98. File linkedsubdirhello = new File(localDb.getWorkTree(),
  99. "subdir/LinkedHello");
  100. assertTrue("The subdir/LinkedHello file should exist",
  101. localDb.getFS().exists(linkedsubdirhello));
  102. assertTrue("The subdir/LinkedHello file should be a symlink",
  103. localDb.getFS().isSymLink(linkedsubdirhello));
  104. assertEquals("../foo/hello.txt",
  105. localDb.getFS().readSymLink(linkedsubdirhello));
  106. // The bar/foo/LinkedHello file should use a single relative ../
  107. File linkedbarfoohello = new File(localDb.getWorkTree(),
  108. "bar/foo/LinkedHello");
  109. assertTrue("The bar/foo/LinkedHello file should exist",
  110. localDb.getFS().exists(linkedbarfoohello));
  111. assertTrue("The bar/foo/LinkedHello file should be a symlink",
  112. localDb.getFS().isSymLink(linkedbarfoohello));
  113. assertEquals("../baz/hello.txt",
  114. localDb.getFS().readSymLink(linkedbarfoohello));
  115. }
  116. }
  117. }
  118. }