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.

CrLfNativeTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch>
  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.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import java.io.File;
  47. import java.util.HashMap;
  48. import java.util.Map;
  49. import org.eclipse.jgit.api.ResetCommand.ResetType;
  50. import org.eclipse.jgit.junit.MockSystemReader;
  51. import org.eclipse.jgit.junit.RepositoryTestCase;
  52. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  53. import org.eclipse.jgit.storage.file.FileBasedConfig;
  54. import org.eclipse.jgit.treewalk.FileTreeIterator;
  55. import org.eclipse.jgit.treewalk.TreeWalk;
  56. import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
  57. import org.eclipse.jgit.util.SystemReader;
  58. import org.junit.Test;
  59. public class CrLfNativeTest extends RepositoryTestCase {
  60. @Test
  61. public void checkoutWithCrLfNativeUnix() throws Exception {
  62. verifyNativeCheckout(new MockSystemReader() {
  63. {
  64. setUnix();
  65. }
  66. });
  67. }
  68. @Test
  69. public void checkoutWithCrLfNativeWindows() throws Exception {
  70. verifyNativeCheckout(new MockSystemReader() {
  71. {
  72. setWindows();
  73. }
  74. });
  75. }
  76. private void verifyNativeCheckout(SystemReader systemReader)
  77. throws Exception {
  78. SystemReader.setInstance(systemReader);
  79. Git git = Git.wrap(db);
  80. FileBasedConfig config = db.getConfig();
  81. config.setString("core", null, "autocrlf", "false");
  82. config.setString("core", null, "eol", "native");
  83. config.save();
  84. // core.eol is active only if text is set, or if text=auto
  85. writeTrashFile(".gitattributes", "*.txt text\n");
  86. File file = writeTrashFile("file.txt", "line 1\nline 2\n");
  87. git.add().addFilepattern("file.txt").addFilepattern(".gitattributes")
  88. .call();
  89. git.commit().setMessage("Initial").call();
  90. // Check-in with core.eol=native normalization
  91. assertEquals(
  92. "[.gitattributes, mode:100644, content:*.txt text\n]"
  93. + "[file.txt, mode:100644, content:line 1\nline 2\n]",
  94. indexState(CONTENT));
  95. writeTrashFile("file.txt", "something else");
  96. git.add().addFilepattern("file.txt").call();
  97. git.commit().setMessage("New commit").call();
  98. git.reset().setMode(ResetType.HARD).setRef("HEAD~").call();
  99. // Check-out should convert to the native line separator
  100. checkFile(file, systemReader.isWindows() ? "line 1\r\nline 2\r\n"
  101. : "line 1\nline 2\n");
  102. Status status = git.status().call();
  103. assertTrue("git status should be clean", status.isClean());
  104. }
  105. /**
  106. * Verifies the handling of the crlf attribute: crlf == text, -crlf ==
  107. * -text, crlf=input == eol=lf
  108. *
  109. * @throws Exception
  110. */
  111. @Test
  112. public void testCrLfAttribute() throws Exception {
  113. FileBasedConfig config = db.getConfig();
  114. config.setString("core", null, "autocrlf", "false");
  115. config.setString("core", null, "eol", "crlf");
  116. config.save();
  117. writeTrashFile(".gitattributes",
  118. "*.txt text\n*.crlf crlf\n*.bin -text\n*.nocrlf -crlf\n*.input crlf=input\n*.eol eol=lf");
  119. writeTrashFile("foo.txt", "");
  120. writeTrashFile("foo.crlf", "");
  121. writeTrashFile("foo.bin", "");
  122. writeTrashFile("foo.nocrlf", "");
  123. writeTrashFile("foo.input", "");
  124. writeTrashFile("foo.eol", "");
  125. Map<String, EolStreamType> inTypes = new HashMap<>();
  126. Map<String, EolStreamType> outTypes = new HashMap<>();
  127. try (TreeWalk walk = new TreeWalk(db)) {
  128. walk.addTree(new FileTreeIterator(db));
  129. while (walk.next()) {
  130. String path = walk.getPathString();
  131. if (".gitattributes".equals(path)) {
  132. continue;
  133. }
  134. EolStreamType in = walk
  135. .getEolStreamType(OperationType.CHECKIN_OP);
  136. EolStreamType out = walk
  137. .getEolStreamType(OperationType.CHECKOUT_OP);
  138. inTypes.put(path, in);
  139. outTypes.put(path, out);
  140. }
  141. }
  142. assertEquals("", checkTypes("check-in", inTypes));
  143. assertEquals("", checkTypes("check-out", outTypes));
  144. }
  145. private String checkTypes(String prefix, Map<String, EolStreamType> types) {
  146. StringBuilder result = new StringBuilder();
  147. EolStreamType a = types.get("foo.crlf");
  148. EolStreamType b = types.get("foo.txt");
  149. report(result, prefix, "crlf != text", a, b);
  150. a = types.get("foo.nocrlf");
  151. b = types.get("foo.bin");
  152. report(result, prefix, "-crlf != -text", a, b);
  153. a = types.get("foo.input");
  154. b = types.get("foo.eol");
  155. report(result, prefix, "crlf=input != eol=lf", a, b);
  156. return result.toString();
  157. }
  158. private void report(StringBuilder result, String prefix, String label,
  159. EolStreamType a,
  160. EolStreamType b) {
  161. if (a == null || b == null || !a.equals(b)) {
  162. result.append(prefix).append(' ').append(label).append(": ")
  163. .append(toString(a)).append(" != ").append(toString(b))
  164. .append('\n');
  165. }
  166. }
  167. private String toString(EolStreamType type) {
  168. return type == null ? "null" : type.name();
  169. }
  170. }