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