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.

DirCacheCheckoutTestWithSymlinks.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2013, Christian Halstrick <christian.halstrick@sap.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 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.lib;
  39. import static org.junit.Assert.assertTrue;
  40. import java.io.File;
  41. import org.eclipse.jgit.api.Git;
  42. import org.eclipse.jgit.internal.storage.file.FileRepository;
  43. import org.eclipse.jgit.junit.RepositoryTestCase;
  44. import org.eclipse.jgit.revwalk.RevCommit;
  45. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  46. import org.eclipse.jgit.util.FS;
  47. import org.eclipse.jgit.util.FileUtils;
  48. import org.junit.Before;
  49. import org.junit.Test;
  50. public class DirCacheCheckoutTestWithSymlinks extends RepositoryTestCase {
  51. @Before
  52. public void beforeMethod() {
  53. // If this assumption fails the tests are skipped. When running on a
  54. // filesystem not supporting symlinks I don't want this tests
  55. org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  56. }
  57. @Test
  58. public void testDontDeleteSymlinkOnTopOfRootDir() throws Exception {
  59. // create a parent folder containing a folder with a test repository
  60. File repos = createTempDirectory("repos");
  61. File testRepo = new File(repos, "repo");
  62. testRepo.mkdirs();
  63. Git git = Git.init().setDirectory(testRepo).call();
  64. db = (FileRepository) git.getRepository();
  65. // Create a situation where a checkout of master whould delete a file in
  66. // a subfolder of the root of the worktree. No other files/folders exist
  67. writeTrashFile("d/f", "f");
  68. git.add().addFilepattern(".").call();
  69. RevCommit initial = git.commit().setMessage("inital").call();
  70. git.rm().addFilepattern("d/f").call();
  71. git.commit().setMessage("modifyOnMaster").call();
  72. git.checkout().setCreateBranch(true).setName("side")
  73. .setStartPoint(initial).call();
  74. writeTrashFile("d/f", "f2");
  75. git.add().addFilepattern(".").call();
  76. git.commit().setMessage("modifyOnSide").call();
  77. git.getRepository().close();
  78. // Create a symlink pointing to the parent folder of the repo and open
  79. // the repo with the path containing the symlink
  80. File reposSymlink = createTempFile();
  81. FileUtils.createSymLink(reposSymlink, repos.getPath());
  82. Repository symlinkDB = FileRepositoryBuilder.create(new File(
  83. reposSymlink, "repo/.git"));
  84. Git symlinkRepo = Git.wrap(symlinkDB);
  85. symlinkRepo.checkout().setName("master").call();
  86. // check that the symlink still exists
  87. assertTrue("The symlink to the repo should exist after a checkout",
  88. reposSymlink.exists());
  89. }
  90. }