1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 package org.eclipse.jgit.indexdiff;
43
44 import static org.junit.Assert.assertArrayEquals;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48 import static org.junit.Assert.fail;
49 import static org.junit.Assume.assumeTrue;
50
51 import java.io.BufferedOutputStream;
52 import java.io.BufferedReader;
53 import java.io.File;
54 import java.io.FileOutputStream;
55 import java.io.IOException;
56 import java.io.InputStream;
57 import java.io.InputStreamReader;
58 import java.io.OutputStream;
59 import java.io.OutputStreamWriter;
60 import java.io.Writer;
61 import java.lang.reflect.InvocationTargetException;
62 import java.lang.reflect.Method;
63 import java.nio.charset.StandardCharsets;
64 import java.nio.file.Files;
65 import java.nio.file.Path;
66 import java.nio.file.Paths;
67 import java.util.Collections;
68
69 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
70 import org.eclipse.jgit.lib.Constants;
71 import org.eclipse.jgit.lib.IndexDiff;
72 import org.eclipse.jgit.lib.Repository;
73 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
74 import org.eclipse.jgit.treewalk.FileTreeIterator;
75 import org.eclipse.jgit.treewalk.WorkingTreeIterator;
76 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
77 import org.eclipse.jgit.util.FS;
78 import org.eclipse.jgit.util.SystemReader;
79 import org.junit.Before;
80 import org.junit.Test;
81
82
83
84
85
86
87
88
89 public class IndexDiffWithSymlinkTest extends LocalDiskRepositoryTestCase {
90
91 private static final String FILEREPO = "filerepo";
92
93 private static final String TESTFOLDER = "testfolder";
94
95 private static final String TESTTARGET = "äéü.txt";
96
97 private static final String TESTLINK = "aeu.txt";
98
99 private static final byte[] NFC =
100 { -61, -92, -61, -87, -61, -68, 46, 116, 120, 116 };
101
102 private static final byte[] NFD =
103 { 97, -52, -120, 101, -52, -127, 117, -52, -120, 46, 116, 120, 116 };
104
105 private File testRepoDir;
106
107 @Override
108 @Before
109 public void setUp() throws Exception {
110 assumeTrue(SystemReader.getInstance().isMacOS()
111 && FS.DETECTED.supportsSymlinks());
112 super.setUp();
113 File testDir = createTempDirectory(this.getClass().getSimpleName());
114 InputStream in = this.getClass().getClassLoader().getResourceAsStream(
115 this.getClass().getPackage().getName().replace('.', '/') + '/'
116 + FILEREPO + ".txt");
117 assertNotNull("Test repo file not found", in);
118 try {
119 testRepoDir = restoreGitRepo(in, testDir, FILEREPO);
120 } finally {
121 in.close();
122 }
123 }
124
125 private File restoreGitRepo(InputStream in, File testDir, String name)
126 throws Exception {
127 File exportedTestRepo = new File(testDir, name + ".txt");
128 copy(in, exportedTestRepo);
129
130 File restoreScript = new File(testDir, name + ".sh");
131 try (OutputStream out = new BufferedOutputStream(
132 new FileOutputStream(restoreScript));
133 Writer writer = new OutputStreamWriter(out,
134 StandardCharsets.UTF_8)) {
135 writer.write("echo `which git` 1>&2\n");
136 writer.write("echo `git --version` 1>&2\n");
137 writer.write("git init " + name + " && \\\n");
138 writer.write("cd ./" + name + " && \\\n");
139 writer.write("git fast-import < ../" + name + ".txt && \\\n");
140 writer.write("git checkout -f\n");
141 }
142 String[] cmd = { "/bin/sh", "./" + name + ".sh" };
143 int exitCode;
144 String stdErr;
145 Process process = Runtime.getRuntime().exec(cmd, null, testDir);
146 try (InputStream stdOutStream = process.getInputStream();
147 InputStream stdErrStream = process.getErrorStream();
148 OutputStream stdInStream = process.getOutputStream()) {
149 readStream(stdOutStream);
150 stdErr = readStream(stdErrStream);
151 process.waitFor();
152 exitCode = process.exitValue();
153 }
154 if (exitCode != 0) {
155 fail("cgit repo restore returned " + exitCode + '\n' + stdErr);
156 }
157 return new File(new File(testDir, name), Constants.DOT_GIT);
158 }
159
160 private void copy(InputStream from, File to) throws IOException {
161 try (OutputStream out = new FileOutputStream(to)) {
162 byte[] buffer = new byte[4096];
163 int n;
164 while ((n = from.read(buffer)) > 0) {
165 out.write(buffer, 0, n);
166 }
167 }
168 }
169
170 private String readStream(InputStream stream) throws IOException {
171 try (BufferedReader in = new BufferedReader(
172 new InputStreamReader(stream))) {
173 StringBuilder out = new StringBuilder();
174 String line;
175 while ((line = in.readLine()) != null) {
176 out.append(line).append('\n');
177 }
178 return out.toString();
179 }
180 }
181
182 @Test
183 public void testSymlinkWithEncodingDifference() throws Exception {
184 try (Repository testRepo = FileRepositoryBuilder.create(testRepoDir)) {
185 File workingTree = testRepo.getWorkTree();
186 File symLink = new File(new File(workingTree, TESTFOLDER),
187 TESTLINK);
188
189 Path linkTarget = Files.readSymbolicLink(symLink.toPath());
190 assertEquals("Unexpected link target", TESTTARGET,
191 linkTarget.toString());
192 byte[] raw = rawPath(linkTarget);
193 if (raw != null) {
194 assertArrayEquals("Expected an NFC link target", NFC, raw);
195 }
196
197 assertTrue("Could not delete symlink", symLink.delete());
198 Files.createSymbolicLink(symLink.toPath(), Paths.get(TESTTARGET));
199
200 linkTarget = Files.readSymbolicLink(symLink.toPath());
201 assertEquals("Unexpected link target", TESTTARGET,
202 linkTarget.toString());
203 raw = rawPath(linkTarget);
204 if (raw != null) {
205 assertArrayEquals("Expected an NFD link target", NFD, raw);
206 }
207
208 WorkingTreeIterator iterator = new FileTreeIterator(testRepo);
209 IndexDiff diff = new IndexDiff(testRepo, Constants.HEAD, iterator);
210 diff.setFilter(PathFilterGroup.createFromStrings(
211 Collections.singleton(TESTFOLDER + '/' + TESTLINK)));
212 diff.diff();
213
214
215
216
217
218 }
219 }
220
221 private byte[] rawPath(Path p) {
222 try {
223 Method method = p.getClass().getDeclaredMethod("asByteArray");
224 if (method != null) {
225 method.setAccessible(true);
226 return (byte[]) method.invoke(p);
227 }
228 } catch (NoSuchMethodException | IllegalAccessException
229 | IllegalArgumentException | InvocationTargetException e) {
230
231 }
232 return null;
233 }
234 }