View Javadoc
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  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  
51  import org.eclipse.jgit.api.Git;
52  import org.eclipse.jgit.junit.JGitTestUtil;
53  import org.eclipse.jgit.junit.RepositoryTestCase;
54  import org.eclipse.jgit.lib.Repository;
55  import org.eclipse.jgit.util.FS;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  public class RepoCommandSymlinkTest extends RepositoryTestCase {
60  	@Before
61  	public void beforeMethod() {
62  		// If this assumption fails the tests are skipped. When running on a
63  		// filesystem not supporting symlinks I don't want this tests
64  		org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
65  	}
66  
67  	private Repository defaultDb;
68  
69  	private String rootUri;
70  	private String defaultUri;
71  
72  	@Override
73  	public void setUp() throws Exception {
74  		super.setUp();
75  
76  		defaultDb = createWorkRepository();
77  		try (Git git = new Git(defaultDb)) {
78  			JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "hello world");
79  			git.add().addFilepattern("hello.txt").call();
80  			git.commit().setMessage("Initial commit").call();
81  			addRepoToClose(defaultDb);
82  		}
83  
84  		defaultUri = defaultDb.getDirectory().toURI().toString();
85  		int root = defaultUri.lastIndexOf("/",
86  				defaultUri.lastIndexOf("/.git") - 1)
87  				+ 1;
88  		rootUri = defaultUri.substring(0, root)
89  				+ "manifest";
90  		defaultUri = defaultUri.substring(root);
91  	}
92  
93  	@Test
94  	public void testLinkFileBare() throws Exception {
95  		try (
96  				Repository remoteDb = createBareRepository();
97  				Repository tempDb = createWorkRepository()) {
98  			StringBuilder xmlContent = new StringBuilder();
99  			xmlContent
100 					.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
101 					.append("<manifest>")
102 					.append("<remote name=\"remote1\" fetch=\".\" />")
103 					.append("<default revision=\"master\" remote=\"remote1\" />")
104 					.append("<project path=\"foo\" name=\"").append(defaultUri)
105 					.append("\" revision=\"master\" >")
106 					.append("<linkfile src=\"hello.txt\" dest=\"LinkedHello\" />")
107 					.append("<linkfile src=\"hello.txt\" dest=\"foo/LinkedHello\" />")
108 					.append("<linkfile src=\"hello.txt\" dest=\"subdir/LinkedHello\" />")
109 					.append("</project>")
110 					.append("<project path=\"bar/baz\" name=\"")
111 					.append(defaultUri).append("\" revision=\"master\" >")
112 					.append("<linkfile src=\"hello.txt\" dest=\"bar/foo/LinkedHello\" />")
113 					.append("</project>").append("</manifest>");
114 			JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
115 					xmlContent.toString());
116 			RepoCommand command = new RepoCommand(remoteDb);
117 			command.setPath(
118 					tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
119 					.setURI(rootUri).call();
120 			// Clone it
121 			File directory = createTempDirectory("testCopyFileBare");
122 			Repository localDb = Git.cloneRepository().setDirectory(directory)
123 					.setURI(remoteDb.getDirectory().toURI().toString()).call()
124 					.getRepository();
125 
126 			// The LinkedHello symlink should exist.
127 			File linkedhello = new File(localDb.getWorkTree(), "LinkedHello");
128 			assertTrue("The LinkedHello file should exist",
129 					localDb.getFS().exists(linkedhello));
130 			assertTrue("The LinkedHello file should be a symlink",
131 					localDb.getFS().isSymLink(linkedhello));
132 			assertEquals("foo/hello.txt",
133 					localDb.getFS().readSymLink(linkedhello));
134 
135 			// The foo/LinkedHello file should be skipped.
136 			File linkedfoohello = new File(localDb.getWorkTree(), "foo/LinkedHello");
137 			assertFalse("The foo/LinkedHello file should be skipped",
138 					localDb.getFS().exists(linkedfoohello));
139 
140 			// The subdir/LinkedHello file should use a relative ../
141 			File linkedsubdirhello = new File(localDb.getWorkTree(),
142 					"subdir/LinkedHello");
143 			assertTrue("The subdir/LinkedHello file should exist",
144 					localDb.getFS().exists(linkedsubdirhello));
145 			assertTrue("The subdir/LinkedHello file should be a symlink",
146 					localDb.getFS().isSymLink(linkedsubdirhello));
147 			assertEquals("../foo/hello.txt",
148 					localDb.getFS().readSymLink(linkedsubdirhello));
149 
150 			// The bar/foo/LinkedHello file should use a single relative ../
151 			File linkedbarfoohello = new File(localDb.getWorkTree(),
152 					"bar/foo/LinkedHello");
153 			assertTrue("The bar/foo/LinkedHello file should exist",
154 					localDb.getFS().exists(linkedbarfoohello));
155 			assertTrue("The bar/foo/LinkedHello file should be a symlink",
156 					localDb.getFS().isSymLink(linkedbarfoohello));
157 			assertEquals("../baz/hello.txt",
158 					localDb.getFS().readSymLink(linkedbarfoohello));
159 
160 			localDb.close();
161 		}
162 	}
163 }