1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.gitrepo;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.File;
17
18 import org.eclipse.jgit.api.Git;
19 import org.eclipse.jgit.junit.JGitTestUtil;
20 import org.eclipse.jgit.junit.RepositoryTestCase;
21 import org.eclipse.jgit.lib.Repository;
22 import org.eclipse.jgit.util.FS;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 public class RepoCommandSymlinkTest extends RepositoryTestCase {
27 @Before
28 public void beforeMethod() {
29
30
31 org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
32 }
33
34 private Repository defaultDb;
35
36 private String rootUri;
37 private String defaultUri;
38
39 @Override
40 public void setUp() throws Exception {
41 super.setUp();
42
43 defaultDb = createWorkRepository();
44 try (Git git = new Git(defaultDb)) {
45 JGitTestUtil.writeTrashFile(defaultDb, "hello.txt", "hello world");
46 git.add().addFilepattern("hello.txt").call();
47 git.commit().setMessage("Initial commit").call();
48 addRepoToClose(defaultDb);
49 }
50
51 defaultUri = defaultDb.getDirectory().toURI().toString();
52 int root = defaultUri.lastIndexOf("/",
53 defaultUri.lastIndexOf("/.git") - 1)
54 + 1;
55 rootUri = defaultUri.substring(0, root)
56 + "manifest";
57 defaultUri = defaultUri.substring(root);
58 }
59
60 @Test
61 public void testLinkFileBare() throws Exception {
62 try (
63 Repository remoteDb = createBareRepository();
64 Repository tempDb = createWorkRepository()) {
65 StringBuilder xmlContent = new StringBuilder();
66 xmlContent
67 .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
68 .append("<manifest>")
69 .append("<remote name=\"remote1\" fetch=\".\" />")
70 .append("<default revision=\"master\" remote=\"remote1\" />")
71 .append("<project path=\"foo\" name=\"").append(defaultUri)
72 .append("\" revision=\"master\" >")
73 .append("<linkfile src=\"hello.txt\" dest=\"LinkedHello\" />")
74 .append("<linkfile src=\"hello.txt\" dest=\"foo/LinkedHello\" />")
75 .append("<linkfile src=\"hello.txt\" dest=\"subdir/LinkedHello\" />")
76 .append("</project>")
77 .append("<project path=\"bar/baz\" name=\"")
78 .append(defaultUri).append("\" revision=\"master\" >")
79 .append("<linkfile src=\"hello.txt\" dest=\"bar/foo/LinkedHello\" />")
80 .append("</project>").append("</manifest>");
81 JGitTestUtil.writeTrashFile(tempDb, "manifest.xml",
82 xmlContent.toString());
83 RepoCommand command = new RepoCommand(remoteDb);
84 command.setPath(
85 tempDb.getWorkTree().getAbsolutePath() + "/manifest.xml")
86 .setURI(rootUri).call();
87
88 File directory = createTempDirectory("testCopyFileBare");
89 try (Repository localDb = Git.cloneRepository()
90 .setDirectory(directory)
91 .setURI(remoteDb.getDirectory().toURI().toString()).call()
92 .getRepository()) {
93
94
95 File linkedhello = new File(localDb.getWorkTree(),
96 "LinkedHello");
97 assertTrue("The LinkedHello file should exist",
98 localDb.getFS().exists(linkedhello));
99 assertTrue("The LinkedHello file should be a symlink",
100 localDb.getFS().isSymLink(linkedhello));
101 assertEquals("foo/hello.txt",
102 localDb.getFS().readSymLink(linkedhello));
103
104
105 File linkedfoohello = new File(localDb.getWorkTree(),
106 "foo/LinkedHello");
107 assertFalse("The foo/LinkedHello file should be skipped",
108 localDb.getFS().exists(linkedfoohello));
109
110
111 File linkedsubdirhello = new File(localDb.getWorkTree(),
112 "subdir/LinkedHello");
113 assertTrue("The subdir/LinkedHello file should exist",
114 localDb.getFS().exists(linkedsubdirhello));
115 assertTrue("The subdir/LinkedHello file should be a symlink",
116 localDb.getFS().isSymLink(linkedsubdirhello));
117 assertEquals("../foo/hello.txt",
118 localDb.getFS().readSymLink(linkedsubdirhello));
119
120
121 File linkedbarfoohello = new File(localDb.getWorkTree(),
122 "bar/foo/LinkedHello");
123 assertTrue("The bar/foo/LinkedHello file should exist",
124 localDb.getFS().exists(linkedbarfoohello));
125 assertTrue("The bar/foo/LinkedHello file should be a symlink",
126 localDb.getFS().isSymLink(linkedbarfoohello));
127 assertEquals("../baz/hello.txt",
128 localDb.getFS().readSymLink(linkedbarfoohello));
129 }
130 }
131 }
132 }