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.

IndexDiffWithSymlinkTest.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2015 Thomas Wolf <thomas.wolf@paranor.ch>
  3. *
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Distribution License v1.0 which
  6. * accompanies this distribution, is reproduced below, and is
  7. * available at http://www.eclipse.org/org/documents/edl-v10.php
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials provided
  21. * with the distribution.
  22. *
  23. * - Neither the name of the Eclipse Foundation, Inc. nor the
  24. * names of its contributors may be used to endorse or promote
  25. * products derived from this software without specific prior
  26. * written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  29. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  30. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  31. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  35. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  40. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.indexdiff;
  43. import static java.nio.charset.StandardCharsets.UTF_8;
  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. import java.io.BufferedOutputStream;
  51. import java.io.BufferedReader;
  52. import java.io.File;
  53. import java.io.FileOutputStream;
  54. import java.io.IOException;
  55. import java.io.InputStream;
  56. import java.io.InputStreamReader;
  57. import java.io.OutputStream;
  58. import java.io.OutputStreamWriter;
  59. import java.io.Writer;
  60. import java.lang.reflect.InvocationTargetException;
  61. import java.lang.reflect.Method;
  62. import java.nio.file.Files;
  63. import java.nio.file.Path;
  64. import java.nio.file.Paths;
  65. import java.util.Collections;
  66. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  67. import org.eclipse.jgit.lib.Constants;
  68. import org.eclipse.jgit.lib.IndexDiff;
  69. import org.eclipse.jgit.lib.Repository;
  70. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  71. import org.eclipse.jgit.treewalk.FileTreeIterator;
  72. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  73. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  74. import org.eclipse.jgit.util.FS;
  75. import org.eclipse.jgit.util.SystemReader;
  76. import org.junit.Before;
  77. import org.junit.Test;
  78. /**
  79. * MacOS-only test for dealing with symlinks in IndexDiff. Recreates using cgit
  80. * a test repository prepared with git 2.2.1 on MacOS 10.7.5 containing some
  81. * symlinks. Examines a symlink pointing to a file named "äéü.txt" (should be
  82. * encoded as UTF-8 NFC), changes it through Java, examines it again to verify
  83. * it's been changed to UTF-8 NFD, and finally calculates an IndexDiff.
  84. */
  85. public class IndexDiffWithSymlinkTest extends LocalDiskRepositoryTestCase {
  86. private static final String FILEREPO = "filerepo";
  87. private static final String TESTFOLDER = "testfolder";
  88. private static final String TESTTARGET = "äéü.txt";
  89. private static final String TESTLINK = "aeu.txt";
  90. private static final byte[] NFC = // "äéü.txt" in NFC
  91. { -61, -92, -61, -87, -61, -68, 46, 116, 120, 116 };
  92. private static final byte[] NFD = // "äéü.txt" in NFD
  93. { 97, -52, -120, 101, -52, -127, 117, -52, -120, 46, 116, 120, 116 };
  94. private File testRepoDir;
  95. @Override
  96. @Before
  97. public void setUp() throws Exception {
  98. assumeTrue(SystemReader.getInstance().isMacOS()
  99. && FS.DETECTED.supportsSymlinks());
  100. super.setUp();
  101. File testDir = createTempDirectory(this.getClass().getSimpleName());
  102. try (InputStream in = this.getClass().getClassLoader()
  103. .getResourceAsStream(
  104. this.getClass().getPackage().getName().replace('.', '/') + '/'
  105. + FILEREPO + ".txt")) {
  106. assertNotNull("Test repo file not found", in);
  107. testRepoDir = restoreGitRepo(in, testDir, FILEREPO);
  108. }
  109. }
  110. private File restoreGitRepo(InputStream in, File testDir, String name)
  111. throws Exception {
  112. File exportedTestRepo = new File(testDir, name + ".txt");
  113. copy(in, exportedTestRepo);
  114. // Use CGit to restore
  115. File restoreScript = new File(testDir, name + ".sh");
  116. try (OutputStream out = new BufferedOutputStream(
  117. new FileOutputStream(restoreScript));
  118. Writer writer = new OutputStreamWriter(out, UTF_8)) {
  119. writer.write("echo `which git` 1>&2\n");
  120. writer.write("echo `git --version` 1>&2\n");
  121. writer.write("git init " + name + " && \\\n");
  122. writer.write("cd ./" + name + " && \\\n");
  123. writer.write("git fast-import < ../" + name + ".txt && \\\n");
  124. writer.write("git checkout -f\n");
  125. }
  126. String[] cmd = { "/bin/sh", "./" + name + ".sh" };
  127. int exitCode;
  128. String stdErr;
  129. ProcessBuilder builder = new ProcessBuilder(cmd);
  130. builder.environment().put("HOME",
  131. FS.DETECTED.userHome().getAbsolutePath());
  132. builder.directory(testDir);
  133. Process process = builder.start();
  134. try (InputStream stdOutStream = process.getInputStream();
  135. InputStream stdErrStream = process.getErrorStream();
  136. OutputStream stdInStream = process.getOutputStream()) {
  137. readStream(stdOutStream);
  138. stdErr = readStream(stdErrStream);
  139. process.waitFor();
  140. exitCode = process.exitValue();
  141. }
  142. if (exitCode != 0) {
  143. fail("cgit repo restore returned " + exitCode + '\n' + stdErr);
  144. }
  145. return new File(new File(testDir, name), Constants.DOT_GIT);
  146. }
  147. private void copy(InputStream from, File to) throws IOException {
  148. try (OutputStream out = new FileOutputStream(to)) {
  149. byte[] buffer = new byte[4096];
  150. int n;
  151. while ((n = from.read(buffer)) > 0) {
  152. out.write(buffer, 0, n);
  153. }
  154. }
  155. }
  156. private String readStream(InputStream stream) throws IOException {
  157. try (BufferedReader in = new BufferedReader(
  158. new InputStreamReader(stream, UTF_8))) {
  159. StringBuilder out = new StringBuilder();
  160. String line;
  161. while ((line = in.readLine()) != null) {
  162. out.append(line).append('\n');
  163. }
  164. return out.toString();
  165. }
  166. }
  167. @Test
  168. public void testSymlinkWithEncodingDifference() throws Exception {
  169. try (Repository testRepo = FileRepositoryBuilder.create(testRepoDir)) {
  170. File workingTree = testRepo.getWorkTree();
  171. File symLink = new File(new File(workingTree, TESTFOLDER),
  172. TESTLINK);
  173. // Read the symlink as it was created by cgit
  174. Path linkTarget = Files.readSymbolicLink(symLink.toPath());
  175. assertEquals("Unexpected link target", TESTTARGET,
  176. linkTarget.toString());
  177. byte[] raw = rawPath(linkTarget);
  178. if (raw != null) {
  179. assertArrayEquals("Expected an NFC link target", NFC, raw);
  180. }
  181. // Now re-create that symlink through Java
  182. assertTrue("Could not delete symlink", symLink.delete());
  183. Files.createSymbolicLink(symLink.toPath(), Paths.get(TESTTARGET));
  184. // Read it again
  185. linkTarget = Files.readSymbolicLink(symLink.toPath());
  186. assertEquals("Unexpected link target", TESTTARGET,
  187. linkTarget.toString());
  188. raw = rawPath(linkTarget);
  189. if (raw != null) {
  190. assertArrayEquals("Expected an NFD link target", NFD, raw);
  191. }
  192. // Do the indexdiff
  193. WorkingTreeIterator iterator = new FileTreeIterator(testRepo);
  194. IndexDiff diff = new IndexDiff(testRepo, Constants.HEAD, iterator);
  195. diff.setFilter(PathFilterGroup.createFromStrings(
  196. Collections.singleton(TESTFOLDER + '/' + TESTLINK)));
  197. diff.diff();
  198. // We're testing that this does NOT throw "EOFException: Short read
  199. // of block." The diff will not report any modified files -- the
  200. // link modification is not visible to JGit, which always works with
  201. // the Java internal NFC encoding. CGit does report the link as an
  202. // unstaged modification here, though.
  203. }
  204. }
  205. private byte[] rawPath(Path p) {
  206. try {
  207. Method method = p.getClass().getDeclaredMethod("asByteArray");
  208. if (method != null) {
  209. method.setAccessible(true);
  210. return (byte[]) method.invoke(p);
  211. }
  212. } catch (NoSuchMethodException | IllegalAccessException
  213. | IllegalArgumentException | InvocationTargetException e) {
  214. // Ignore and fall through.
  215. }
  216. return null;
  217. }
  218. }