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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.merge;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.nio.charset.Charset;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import org.eclipse.jgit.diff.RawText;
  17. /**
  18. * A class to convert merge results into a Git conformant textual presentation
  19. */
  20. public class MergeFormatter {
  21. /**
  22. * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
  23. * objects in a Git conformant way. This method also assumes that the
  24. * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
  25. * oriented files which use LF as delimiter. This method will also use LF to
  26. * separate chunks and conflict metadata, therefore it fits only to texts
  27. * that are LF-separated lines.
  28. *
  29. * @param out
  30. * the output stream where to write the textual presentation
  31. * @param res
  32. * the merge result which should be presented
  33. * @param seqName
  34. * When a conflict is reported each conflicting range will get a
  35. * name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  36. * " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
  37. * names for the sequences are given in this list
  38. * @param charsetName
  39. * the name of the character set used when writing conflict
  40. * metadata
  41. * @throws java.io.IOException
  42. * @deprecated Use
  43. * {@link #formatMerge(OutputStream, MergeResult, List, Charset)}
  44. * instead.
  45. */
  46. @Deprecated
  47. public void formatMerge(OutputStream out, MergeResult<RawText> res,
  48. List<String> seqName, String charsetName) throws IOException {
  49. formatMerge(out, res, seqName, Charset.forName(charsetName));
  50. }
  51. /**
  52. * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
  53. * objects in a Git conformant way. This method also assumes that the
  54. * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
  55. * oriented files which use LF as delimiter. This method will also use LF to
  56. * separate chunks and conflict metadata, therefore it fits only to texts
  57. * that are LF-separated lines.
  58. *
  59. * @param out
  60. * the output stream where to write the textual presentation
  61. * @param res
  62. * the merge result which should be presented
  63. * @param seqName
  64. * When a conflict is reported each conflicting range will get a
  65. * name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  66. * " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
  67. * names for the sequences are given in this list
  68. * @param charset
  69. * the character set used when writing conflict metadata
  70. * @throws java.io.IOException
  71. * @since 5.2
  72. */
  73. public void formatMerge(OutputStream out, MergeResult<RawText> res,
  74. List<String> seqName, Charset charset) throws IOException {
  75. new MergeFormatterPass(out, res, seqName, charset).formatMerge();
  76. }
  77. /**
  78. * Formats the results of a merge of exactly two
  79. * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
  80. * This convenience method accepts the names for the three sequences (base
  81. * and the two merged sequences) as explicit parameters and doesn't require
  82. * the caller to specify a List
  83. *
  84. * @param out
  85. * the {@link java.io.OutputStream} where to write the textual
  86. * presentation
  87. * @param res
  88. * the merge result which should be presented
  89. * @param baseName
  90. * the name ranges from the base should get
  91. * @param oursName
  92. * the name ranges from ours should get
  93. * @param theirsName
  94. * the name ranges from theirs should get
  95. * @param charsetName
  96. * the name of the character set used when writing conflict
  97. * metadata
  98. * @throws java.io.IOException
  99. * @deprecated use
  100. * {@link #formatMerge(OutputStream, MergeResult, String, String, String, Charset)}
  101. * instead.
  102. */
  103. @Deprecated
  104. public void formatMerge(OutputStream out, MergeResult res, String baseName,
  105. String oursName, String theirsName, String charsetName) throws IOException {
  106. formatMerge(out, res, baseName, oursName, theirsName,
  107. Charset.forName(charsetName));
  108. }
  109. /**
  110. * Formats the results of a merge of exactly two
  111. * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
  112. * This convenience method accepts the names for the three sequences (base
  113. * and the two merged sequences) as explicit parameters and doesn't require
  114. * the caller to specify a List
  115. *
  116. * @param out
  117. * the {@link java.io.OutputStream} where to write the textual
  118. * presentation
  119. * @param res
  120. * the merge result which should be presented
  121. * @param baseName
  122. * the name ranges from the base should get
  123. * @param oursName
  124. * the name ranges from ours should get
  125. * @param theirsName
  126. * the name ranges from theirs should get
  127. * @param charset
  128. * the character set used when writing conflict metadata
  129. * @throws java.io.IOException
  130. * @since 5.2
  131. */
  132. @SuppressWarnings("unchecked")
  133. public void formatMerge(OutputStream out, MergeResult res, String baseName,
  134. String oursName, String theirsName, Charset charset)
  135. throws IOException {
  136. List<String> names = new ArrayList<>(3);
  137. names.add(baseName);
  138. names.add(oursName);
  139. names.add(theirsName);
  140. formatMerge(out, res, names, charset);
  141. }
  142. }