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.

MergeFormatter.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.merge;
  44. import java.io.IOException;
  45. import java.io.OutputStream;
  46. import java.nio.charset.Charset;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import org.eclipse.jgit.diff.RawText;
  50. /**
  51. * A class to convert merge results into a Git conformant textual presentation
  52. */
  53. public class MergeFormatter {
  54. /**
  55. * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
  56. * objects in a Git conformant way. This method also assumes that the
  57. * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
  58. * oriented files which use LF as delimiter. This method will also use LF to
  59. * separate chunks and conflict metadata, therefore it fits only to texts
  60. * that are LF-separated lines.
  61. *
  62. * @param out
  63. * the output stream where to write the textual presentation
  64. * @param res
  65. * the merge result which should be presented
  66. * @param seqName
  67. * When a conflict is reported each conflicting range will get a
  68. * name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  69. * " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
  70. * names for the sequences are given in this list
  71. * @param charsetName
  72. * the name of the character set used when writing conflict
  73. * metadata
  74. * @throws java.io.IOException
  75. * @deprecated Use
  76. * {@link #formatMerge(OutputStream, MergeResult, List, Charset)}
  77. * instead.
  78. */
  79. @Deprecated
  80. public void formatMerge(OutputStream out, MergeResult<RawText> res,
  81. List<String> seqName, String charsetName) throws IOException {
  82. formatMerge(out, res, seqName, Charset.forName(charsetName));
  83. }
  84. /**
  85. * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
  86. * objects in a Git conformant way. This method also assumes that the
  87. * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
  88. * oriented files which use LF as delimiter. This method will also use LF to
  89. * separate chunks and conflict metadata, therefore it fits only to texts
  90. * that are LF-separated lines.
  91. *
  92. * @param out
  93. * the output stream where to write the textual presentation
  94. * @param res
  95. * the merge result which should be presented
  96. * @param seqName
  97. * When a conflict is reported each conflicting range will get a
  98. * name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  99. * " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
  100. * names for the sequences are given in this list
  101. * @param charset
  102. * the character set used when writing conflict metadata
  103. * @throws java.io.IOException
  104. * @since 5.2
  105. */
  106. public void formatMerge(OutputStream out, MergeResult<RawText> res,
  107. List<String> seqName, Charset charset) throws IOException {
  108. new MergeFormatterPass(out, res, seqName, charset).formatMerge();
  109. }
  110. /**
  111. * Formats the results of a merge of exactly two
  112. * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
  113. * This convenience method accepts the names for the three sequences (base
  114. * and the two merged sequences) as explicit parameters and doesn't require
  115. * the caller to specify a List
  116. *
  117. * @param out
  118. * the {@link java.io.OutputStream} where to write the textual
  119. * presentation
  120. * @param res
  121. * the merge result which should be presented
  122. * @param baseName
  123. * the name ranges from the base should get
  124. * @param oursName
  125. * the name ranges from ours should get
  126. * @param theirsName
  127. * the name ranges from theirs should get
  128. * @param charsetName
  129. * the name of the character set used when writing conflict
  130. * metadata
  131. * @throws java.io.IOException
  132. * @deprecated use
  133. * {@link #formatMerge(OutputStream, MergeResult, String, String, String, Charset)}
  134. * instead.
  135. */
  136. @Deprecated
  137. public void formatMerge(OutputStream out, MergeResult res, String baseName,
  138. String oursName, String theirsName, String charsetName) throws IOException {
  139. formatMerge(out, res, baseName, oursName, theirsName,
  140. Charset.forName(charsetName));
  141. }
  142. /**
  143. * Formats the results of a merge of exactly two
  144. * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
  145. * This convenience method accepts the names for the three sequences (base
  146. * and the two merged sequences) as explicit parameters and doesn't require
  147. * the caller to specify a List
  148. *
  149. * @param out
  150. * the {@link java.io.OutputStream} where to write the textual
  151. * presentation
  152. * @param res
  153. * the merge result which should be presented
  154. * @param baseName
  155. * the name ranges from the base should get
  156. * @param oursName
  157. * the name ranges from ours should get
  158. * @param theirsName
  159. * the name ranges from theirs should get
  160. * @param charset
  161. * the character set used when writing conflict metadata
  162. * @throws java.io.IOException
  163. * @since 5.2
  164. */
  165. @SuppressWarnings("unchecked")
  166. public void formatMerge(OutputStream out, MergeResult res, String baseName,
  167. String oursName, String theirsName, Charset charset)
  168. throws IOException {
  169. List<String> names = new ArrayList<>(3);
  170. names.add(baseName);
  171. names.add(oursName);
  172. names.add(theirsName);
  173. formatMerge(out, res, names, charset);
  174. }
  175. }