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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2018, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.File;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import org.eclipse.jgit.api.ResetCommand.ResetType;
  17. import org.eclipse.jgit.junit.MockSystemReader;
  18. import org.eclipse.jgit.junit.RepositoryTestCase;
  19. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  20. import org.eclipse.jgit.storage.file.FileBasedConfig;
  21. import org.eclipse.jgit.treewalk.FileTreeIterator;
  22. import org.eclipse.jgit.treewalk.TreeWalk;
  23. import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
  24. import org.eclipse.jgit.util.SystemReader;
  25. import org.junit.Test;
  26. public class CrLfNativeTest extends RepositoryTestCase {
  27. @Test
  28. public void checkoutWithCrLfNativeUnix() throws Exception {
  29. verifyNativeCheckout(new MockSystemReader() {
  30. {
  31. setUnix();
  32. }
  33. });
  34. }
  35. @Test
  36. public void checkoutWithCrLfNativeWindows() throws Exception {
  37. verifyNativeCheckout(new MockSystemReader() {
  38. {
  39. setWindows();
  40. }
  41. });
  42. }
  43. private void verifyNativeCheckout(SystemReader systemReader)
  44. throws Exception {
  45. SystemReader.setInstance(systemReader);
  46. Git git = Git.wrap(db);
  47. FileBasedConfig config = db.getConfig();
  48. config.setString("core", null, "autocrlf", "false");
  49. config.setString("core", null, "eol", "native");
  50. config.save();
  51. // core.eol is active only if text is set, or if text=auto
  52. writeTrashFile(".gitattributes", "*.txt text\n");
  53. File file = writeTrashFile("file.txt", "line 1\nline 2\n");
  54. git.add().addFilepattern("file.txt").addFilepattern(".gitattributes")
  55. .call();
  56. git.commit().setMessage("Initial").call();
  57. // Check-in with core.eol=native normalization
  58. assertEquals(
  59. "[.gitattributes, mode:100644, content:*.txt text\n]"
  60. + "[file.txt, mode:100644, content:line 1\nline 2\n]",
  61. indexState(CONTENT));
  62. writeTrashFile("file.txt", "something else");
  63. git.add().addFilepattern("file.txt").call();
  64. git.commit().setMessage("New commit").call();
  65. git.reset().setMode(ResetType.HARD).setRef("HEAD~").call();
  66. // Check-out should convert to the native line separator
  67. checkFile(file, systemReader.isWindows() ? "line 1\r\nline 2\r\n"
  68. : "line 1\nline 2\n");
  69. Status status = git.status().call();
  70. assertTrue("git status should be clean", status.isClean());
  71. }
  72. /**
  73. * Verifies the handling of the crlf attribute: crlf == text, -crlf ==
  74. * -text, crlf=input == eol=lf
  75. *
  76. * @throws Exception
  77. */
  78. @Test
  79. public void testCrLfAttribute() throws Exception {
  80. FileBasedConfig config = db.getConfig();
  81. config.setString("core", null, "autocrlf", "false");
  82. config.setString("core", null, "eol", "crlf");
  83. config.save();
  84. writeTrashFile(".gitattributes",
  85. "*.txt text\n*.crlf crlf\n*.bin -text\n*.nocrlf -crlf\n*.input crlf=input\n*.eol eol=lf");
  86. writeTrashFile("foo.txt", "");
  87. writeTrashFile("foo.crlf", "");
  88. writeTrashFile("foo.bin", "");
  89. writeTrashFile("foo.nocrlf", "");
  90. writeTrashFile("foo.input", "");
  91. writeTrashFile("foo.eol", "");
  92. Map<String, EolStreamType> inTypes = new HashMap<>();
  93. Map<String, EolStreamType> outTypes = new HashMap<>();
  94. try (TreeWalk walk = new TreeWalk(db)) {
  95. walk.addTree(new FileTreeIterator(db));
  96. while (walk.next()) {
  97. String path = walk.getPathString();
  98. if (".gitattributes".equals(path)) {
  99. continue;
  100. }
  101. EolStreamType in = walk
  102. .getEolStreamType(OperationType.CHECKIN_OP);
  103. EolStreamType out = walk
  104. .getEolStreamType(OperationType.CHECKOUT_OP);
  105. inTypes.put(path, in);
  106. outTypes.put(path, out);
  107. }
  108. }
  109. assertEquals("", checkTypes("check-in", inTypes));
  110. assertEquals("", checkTypes("check-out", outTypes));
  111. }
  112. private String checkTypes(String prefix, Map<String, EolStreamType> types) {
  113. StringBuilder result = new StringBuilder();
  114. EolStreamType a = types.get("foo.crlf");
  115. EolStreamType b = types.get("foo.txt");
  116. report(result, prefix, "crlf != text", a, b);
  117. a = types.get("foo.nocrlf");
  118. b = types.get("foo.bin");
  119. report(result, prefix, "-crlf != -text", a, b);
  120. a = types.get("foo.input");
  121. b = types.get("foo.eol");
  122. report(result, prefix, "crlf=input != eol=lf", a, b);
  123. return result.toString();
  124. }
  125. private void report(StringBuilder result, String prefix, String label,
  126. EolStreamType a,
  127. EolStreamType b) {
  128. if (a == null || b == null || !a.equals(b)) {
  129. result.append(prefix).append(' ').append(label).append(": ")
  130. .append(toString(a)).append(" != ").append(toString(b))
  131. .append('\n');
  132. }
  133. }
  134. private String toString(EolStreamType type) {
  135. return type == null ? "null" : type.name();
  136. }
  137. }