You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EolStreamTypeUtil.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. package org.eclipse.jgit.util.io;
  43. import java.io.InputStream;
  44. import java.io.OutputStream;
  45. import org.eclipse.jgit.attributes.Attributes;
  46. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  47. import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
  48. import org.eclipse.jgit.treewalk.WorkingTreeOptions;
  49. /**
  50. * Utility used to create input and output stream wrappers for
  51. * {@link org.eclipse.jgit.lib.CoreConfig.EolStreamType}
  52. *
  53. * @since 4.3
  54. */
  55. public final class EolStreamTypeUtil {
  56. private static final boolean FORCE_EOL_LF_ON_CHECKOUT = false;
  57. private EolStreamTypeUtil() {
  58. }
  59. /**
  60. * Convenience method used to detect if CRLF conversion has been configured
  61. * using the
  62. * <ul>
  63. * <li>global repo options</li>
  64. * <li>global attributes</li>
  65. * <li>info attributes</li>
  66. * <li>working tree .gitattributes</li>
  67. *
  68. * @param op
  69. * is the
  70. * {@link org.eclipse.jgit.treewalk.TreeWalk.OperationType} of
  71. * the current traversal
  72. * @param options
  73. * are the {@link org.eclipse.jgit.lib.Config} options with key
  74. * {@link org.eclipse.jgit.treewalk.WorkingTreeOptions#KEY}
  75. * @param attrs
  76. * are the {@link org.eclipse.jgit.attributes.Attributes} of the
  77. * file for which the
  78. * {@link org.eclipse.jgit.lib.CoreConfig.EolStreamType} is to be
  79. * detected
  80. * @return the stream conversion
  81. * {@link org.eclipse.jgit.lib.CoreConfig.EolStreamType} to be
  82. * performed for the selected
  83. * {@link org.eclipse.jgit.treewalk.TreeWalk.OperationType}
  84. */
  85. public static EolStreamType detectStreamType(OperationType op,
  86. WorkingTreeOptions options, Attributes attrs) {
  87. switch (op) {
  88. case CHECKIN_OP:
  89. return checkInStreamType(options, attrs);
  90. case CHECKOUT_OP:
  91. return checkOutStreamType(options, attrs);
  92. default:
  93. throw new IllegalArgumentException("unknown OperationType " + op); //$NON-NLS-1$
  94. }
  95. }
  96. /**
  97. * Wrap the input stream depending on
  98. * {@link org.eclipse.jgit.lib.CoreConfig.EolStreamType}
  99. *
  100. * @param in
  101. * original stream
  102. * @param conversion
  103. * to be performed
  104. * @return the converted stream depending on
  105. * {@link org.eclipse.jgit.lib.CoreConfig.EolStreamType}
  106. */
  107. public static InputStream wrapInputStream(InputStream in,
  108. EolStreamType conversion) {
  109. switch (conversion) {
  110. case TEXT_CRLF:
  111. return new AutoCRLFInputStream(in, false);
  112. case TEXT_LF:
  113. return new AutoLFInputStream(in, false);
  114. case AUTO_CRLF:
  115. return new AutoCRLFInputStream(in, true);
  116. case AUTO_LF:
  117. return new AutoLFInputStream(in, true);
  118. default:
  119. return in;
  120. }
  121. }
  122. /**
  123. * Wrap the output stream depending on
  124. * {@link org.eclipse.jgit.lib.CoreConfig.EolStreamType}
  125. *
  126. * @param out
  127. * original stream
  128. * @param conversion
  129. * to be performed
  130. * @return the converted stream depending on
  131. * {@link org.eclipse.jgit.lib.CoreConfig.EolStreamType}
  132. */
  133. public static OutputStream wrapOutputStream(OutputStream out,
  134. EolStreamType conversion) {
  135. switch (conversion) {
  136. case TEXT_CRLF:
  137. return new AutoCRLFOutputStream(out, false);
  138. case AUTO_CRLF:
  139. return new AutoCRLFOutputStream(out, true);
  140. case TEXT_LF:
  141. return new AutoLFOutputStream(out, false);
  142. case AUTO_LF:
  143. return new AutoLFOutputStream(out, true);
  144. default:
  145. return out;
  146. }
  147. }
  148. private static EolStreamType checkInStreamType(WorkingTreeOptions options,
  149. Attributes attrs) {
  150. if (attrs.isUnset("text")) {//$NON-NLS-1$
  151. // "binary" or "-text" (which is included in the binary expansion)
  152. return EolStreamType.DIRECT;
  153. }
  154. // old git system
  155. if (attrs.isSet("crlf")) {//$NON-NLS-1$
  156. return EolStreamType.TEXT_LF;
  157. } else if (attrs.isUnset("crlf")) {//$NON-NLS-1$
  158. return EolStreamType.DIRECT;
  159. } else if ("input".equals(attrs.getValue("crlf"))) {//$NON-NLS-1$ //$NON-NLS-2$
  160. return EolStreamType.TEXT_LF;
  161. }
  162. // new git system
  163. String eol = attrs.getValue("eol"); //$NON-NLS-1$
  164. if (eol != null)
  165. // check-in is always normalized to LF
  166. return EolStreamType.TEXT_LF;
  167. if (attrs.isSet("text")) { //$NON-NLS-1$
  168. return EolStreamType.TEXT_LF;
  169. }
  170. if ("auto".equals(attrs.getValue("text"))) { //$NON-NLS-1$ //$NON-NLS-2$
  171. return EolStreamType.AUTO_LF;
  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. return EolStreamType.DIRECT;
  181. }
  182. private static EolStreamType checkOutStreamType(WorkingTreeOptions options,
  183. Attributes attrs) {
  184. if (attrs.isUnset("text")) {//$NON-NLS-1$
  185. // "binary" or "-text" (which is included in the binary expansion)
  186. return EolStreamType.DIRECT;
  187. }
  188. // old git system
  189. if (attrs.isSet("crlf")) {//$NON-NLS-1$
  190. return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
  191. : EolStreamType.DIRECT;
  192. } else if (attrs.isUnset("crlf")) {//$NON-NLS-1$
  193. return EolStreamType.DIRECT;
  194. } else if ("input".equals(attrs.getValue("crlf"))) {//$NON-NLS-1$ //$NON-NLS-2$
  195. return EolStreamType.DIRECT;
  196. }
  197. // new git system
  198. String eol = attrs.getValue("eol"); //$NON-NLS-1$
  199. if (eol != null && "crlf".equals(eol)) //$NON-NLS-1$
  200. return EolStreamType.TEXT_CRLF;
  201. if (eol != null && "lf".equals(eol)) //$NON-NLS-1$
  202. return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
  203. : EolStreamType.DIRECT;
  204. if (attrs.isSet("text")) { //$NON-NLS-1$
  205. switch (options.getAutoCRLF()) {
  206. case TRUE:
  207. return EolStreamType.TEXT_CRLF;
  208. default:
  209. // no decision
  210. }
  211. switch (options.getEOL()) {
  212. case CRLF:
  213. return EolStreamType.TEXT_CRLF;
  214. case LF:
  215. return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
  216. : EolStreamType.DIRECT;
  217. case NATIVE:
  218. default:
  219. return EolStreamType.DIRECT;
  220. }
  221. }
  222. if ("auto".equals(attrs.getValue("text"))) { //$NON-NLS-1$ //$NON-NLS-2$
  223. switch (options.getAutoCRLF()) {
  224. case TRUE:
  225. return EolStreamType.AUTO_CRLF;
  226. default:
  227. // no decision
  228. }
  229. switch (options.getEOL()) {
  230. case CRLF:
  231. return EolStreamType.AUTO_CRLF;
  232. case LF:
  233. return FORCE_EOL_LF_ON_CHECKOUT ? EolStreamType.TEXT_LF
  234. : EolStreamType.DIRECT;
  235. case NATIVE:
  236. default:
  237. return EolStreamType.DIRECT;
  238. }
  239. }
  240. switch (options.getAutoCRLF()) {
  241. case TRUE:
  242. return EolStreamType.AUTO_CRLF;
  243. default:
  244. // no decision
  245. }
  246. return EolStreamType.DIRECT;
  247. }
  248. }