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