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.util.io;
44
45 import java.io.InputStream;
46 import java.io.OutputStream;
47
48 import org.eclipse.jgit.attributes.Attributes;
49 import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
50 import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
51 import org.eclipse.jgit.treewalk.WorkingTreeOptions;
52
53
54
55
56
57
58
59 public final class EolStreamTypeUtil {
60 private static final boolean FORCE_EOL_LF_ON_CHECKOUT = false;
61
62 private EolStreamTypeUtil() {
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public static EolStreamType detectStreamType(OperationType op,
93 WorkingTreeOptions options, Attributes attrs) {
94 switch (op) {
95 case CHECKIN_OP:
96 return checkInStreamType(options, attrs);
97 case CHECKOUT_OP:
98 return checkOutStreamType(options, attrs);
99 default:
100 throw new IllegalArgumentException("unknown OperationType " + op);
101 }
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115 public static InputStream wrapInputStream(InputStream in,
116 EolStreamType conversion) {
117 switch (conversion) {
118 case TEXT_CRLF:
119 return new AutoCRLFInputStream(in, false);
120 case TEXT_LF:
121 return new AutoLFInputStream(in, false);
122 case AUTO_CRLF:
123 return new AutoCRLFInputStream(in, true);
124 case AUTO_LF:
125 return new AutoLFInputStream(in, true);
126 default:
127 return in;
128 }
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142 public static OutputStream wrapOutputStream(OutputStream out,
143 EolStreamType conversion) {
144 switch (conversion) {
145 case TEXT_CRLF:
146 return new AutoCRLFOutputStream(out, false);
147 case AUTO_CRLF:
148 return new AutoCRLFOutputStream(out, true);
149 case TEXT_LF:
150 return new AutoLFOutputStream(out, false);
151 case AUTO_LF:
152 return new AutoLFOutputStream(out, true);
153 default:
154 return out;
155 }
156 }
157
158 private static EolStreamType checkInStreamType(WorkingTreeOptions options,
159 Attributes attrs) {
160 if (attrs.isUnset("text")) {
161
162 return EolStreamType.DIRECT;
163 }
164
165
166 if (attrs.isSet("crlf")) {
167 return EolStreamType.TEXT_LF;
168 } else if (attrs.isUnset("crlf")) {
169 return EolStreamType.DIRECT;
170 } else if ("input".equals(attrs.getValue("crlf"))) {
171 return EolStreamType.TEXT_LF;
172 }
173
174
175 String eol = attrs.getValue("eol");
176 if (eol != null)
177
178 return EolStreamType.TEXT_LF;
179
180 if (attrs.isSet("text")) {
181 return EolStreamType.TEXT_LF;
182 }
183
184 if ("auto".equals(attrs.getValue("text"))) {
185 return EolStreamType.AUTO_LF;
186 }
187
188 switch (options.getAutoCRLF()) {
189 case TRUE:
190 case INPUT:
191 return EolStreamType.AUTO_LF;
192 case FALSE:
193 return EolStreamType.DIRECT;
194 }
195
196 return EolStreamType.DIRECT;
197 }
198
199 private static EolStreamType checkOutStreamType(WorkingTreeOptions options,
200 Attributes attrs) {
201 if (attrs.isUnset("text")) {
202
203 return EolStreamType.DIRECT;
204 }
205
206
207 if (attrs.isSet("crlf")) {
208 return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
209 : EolStreamType.DIRECT;
210 } else if (attrs.isUnset("crlf")) {
211 return EolStreamType.DIRECT;
212 } else if ("input".equals(attrs.getValue("crlf"))) {
213 return EolStreamType.DIRECT;
214 }
215
216
217 String eol = attrs.getValue("eol");
218 if (eol != null && "crlf".equals(eol))
219 return EolStreamType.TEXT_CRLF;
220 if (eol != null && "lf".equals(eol))
221 return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
222 : EolStreamType.DIRECT;
223
224 if (attrs.isSet("text")) {
225 switch (options.getAutoCRLF()) {
226 case TRUE:
227 return EolStreamType.TEXT_CRLF;
228 default:
229
230 }
231 switch (options.getEOL()) {
232 case CRLF:
233 return EolStreamType.TEXT_CRLF;
234 case LF:
235 return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
236 : EolStreamType.DIRECT;
237 case NATIVE:
238 default:
239 return EolStreamType.DIRECT;
240 }
241 }
242
243 if ("auto".equals(attrs.getValue("text"))) {
244 switch (options.getAutoCRLF()) {
245 case TRUE:
246 return EolStreamType.AUTO_CRLF;
247 default:
248
249 }
250 switch (options.getEOL()) {
251 case CRLF:
252 return EolStreamType.AUTO_CRLF;
253 case LF:
254 return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
255 : EolStreamType.DIRECT;
256 case NATIVE:
257 default:
258 return EolStreamType.DIRECT;
259 }
260 }
261
262 switch (options.getAutoCRLF()) {
263 case TRUE:
264 return EolStreamType.AUTO_CRLF;
265 default:
266
267 }
268
269 return EolStreamType.DIRECT;
270 }
271
272 }