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