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