View Javadoc
1   /*
2    * Copyright (C) 2015, Ivan Motsch <ivan.motsch@bsiag.com>
3    *
4    * This program and the accompanying materials are made available
5    * under the terms of the Eclipse Distribution License v1.0 which
6    * accompanies this distribution, is reproduced below, and is
7    * available at http://www.eclipse.org/org/documents/edl-v10.php
8    *
9    * All rights reserved.
10   *
11   * Redistribution and use in source and binary forms, with or
12   * without modification, are permitted provided that the following
13   * conditions are met:
14   *
15   * - Redistributions of source code must retain the above copyright
16   *   notice, this list of conditions and the following disclaimer.
17   *
18   * - Redistributions in binary form must reproduce the above
19   *   copyright notice, this list of conditions and the following
20   *   disclaimer in the documentation and/or other materials provided
21   *   with the distribution.
22   *
23   * - Neither the name of the Eclipse Foundation, Inc. nor the
24   *   names of its contributors may be used to endorse or promote
25   *   products derived from this software without specific prior
26   *   written permission.
27   *
28   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
30   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
33   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Utility used to create input and output stream wrappers for
56   * {@link EolStreamType}
57   *
58   * @since 4.3
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  	 * Convenience method used to detect if CRLF conversion has been configured
68  	 * using the
69  	 * <ul>
70  	 * <li>global repo options</li>
71  	 * <li>global attributes</li>
72  	 * <li>info attributes</li>
73  	 * <li>working tree .gitattributes</li>
74  	 *
75  	 * @param op
76  	 *            is the {@link OperationType} of the current traversal
77  	 * @param options
78  	 *            are the {@link Config} options with key
79  	 *            {@link WorkingTreeOptions#KEY}
80  	 * @param attrs
81  	 *            are the {@link Attributes} of the file for which the
82  	 *            {@link EolStreamType} is to be detected
83  	 *
84  	 * @return the stream conversion {@link EolStreamType} to be performed for
85  	 *         the selected {@link OperationType}
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); //$NON-NLS-1$
96  		}
97  	}
98  
99  	/**
100 	 * @param in
101 	 *            original stream
102 	 * @param conversion
103 	 *            to be performed
104 	 * @return the converted stream depending on {@link EolStreamType}
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 	 * @param out
124 	 *            original stream
125 	 * @param conversion
126 	 *            to be performed
127 	 * @return the converted stream depending on {@link EolStreamType}
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 		// old git system
148 		if (attrs.isSet("crlf")) {//$NON-NLS-1$
149 			return EolStreamType.TEXT_LF;
150 		} else if (attrs.isUnset("crlf")) {//$NON-NLS-1$
151 			return EolStreamType.DIRECT;
152 		} else if ("input".equals(attrs.getValue("crlf"))) {//$NON-NLS-1$ //$NON-NLS-2$
153 			return EolStreamType.TEXT_LF;
154 		}
155 
156 		// new git system
157 		if (attrs.isUnset("text")) {//$NON-NLS-1$
158 			return EolStreamType.DIRECT;
159 		}
160 		String eol = attrs.getValue("eol"); //$NON-NLS-1$
161 		if (eol != null)
162 			// check-in is always normalized to LF
163 			return EolStreamType.TEXT_LF;
164 
165 		if (attrs.isSet("text")) { //$NON-NLS-1$
166 			return EolStreamType.TEXT_LF;
167 		}
168 
169 		if ("auto".equals(attrs.getValue("text"))) { //$NON-NLS-1$ //$NON-NLS-2$
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 		// old git system
187 		if (attrs.isSet("crlf")) {//$NON-NLS-1$
188 			return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
189 					: EolStreamType.DIRECT;
190 		} else if (attrs.isUnset("crlf")) {//$NON-NLS-1$
191 			return EolStreamType.DIRECT;
192 		} else if ("input".equals(attrs.getValue("crlf"))) {//$NON-NLS-1$ //$NON-NLS-2$
193 			return EolStreamType.DIRECT;
194 		}
195 
196 		// new git system
197 		if (attrs.isUnset("text")) {//$NON-NLS-1$
198 			return EolStreamType.DIRECT;
199 		}
200 		String eol = attrs.getValue("eol"); //$NON-NLS-1$
201 		if (eol != null && "crlf".equals(eol)) //$NON-NLS-1$
202 			return EolStreamType.TEXT_CRLF;
203 		if (eol != null && "lf".equals(eol)) //$NON-NLS-1$
204 			return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
205 					: EolStreamType.DIRECT;
206 
207 		if (attrs.isSet("text")) { //$NON-NLS-1$
208 			switch (options.getAutoCRLF()) {
209 			case TRUE:
210 				return EolStreamType.TEXT_CRLF;
211 			default:
212 				// no decision
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"))) { //$NON-NLS-1$ //$NON-NLS-2$
227 			switch (options.getAutoCRLF()) {
228 			case TRUE:
229 				return EolStreamType.AUTO_CRLF;
230 			default:
231 				// no decision
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 			// no decision
250 		}
251 
252 		return EolStreamType.DIRECT;
253 	}
254 
255 }