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