Browse Source

MergeFormatter{Pass}: Use Charset instead of String

Deprecate constructors and methods taking a character set name as
a String, in favor of new variants taking a Charset.

Change-Id: I616c601daf232fa17610dba1087fd902030d46ea
Signed-off-by: David Pursehouse <david.pursehouse@gmail.com>
stable-5.2
David Pursehouse 6 years ago
parent
commit
b65a310abc
  1. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeAlgorithmTest.java
  2. 2
      org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergerTest.java
  3. 74
      org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeFormatter.java
  4. 46
      org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeFormatterPass.java
  5. 4
      org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java

2
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergeAlgorithmTest.java

@ -302,7 +302,7 @@ public class MergeAlgorithmTest {
MergeResult r = new MergeAlgorithm().merge(RawTextComparator.DEFAULT, MergeResult r = new MergeAlgorithm().merge(RawTextComparator.DEFAULT,
T(commonBase), T(ours), T(theirs)); T(commonBase), T(ours), T(theirs));
ByteArrayOutputStream bo=new ByteArrayOutputStream(50); ByteArrayOutputStream bo=new ByteArrayOutputStream(50);
fmt.formatMerge(bo, r, "B", "O", "T", Constants.CHARACTER_ENCODING); fmt.formatMerge(bo, r, "B", "O", "T", UTF_8);
return new String(bo.toByteArray(), UTF_8); return new String(bo.toByteArray(), UTF_8);
} }

2
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/MergerTest.java

@ -972,7 +972,7 @@ public class MergerTest extends RepositoryTestCase {
merger.getMergeResults().get("file"); merger.getMergeResults().get("file");
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
fmt.formatMerge(out, merger.getMergeResults().get("file"), fmt.formatMerge(out, merger.getMergeResults().get("file"),
"BASE", "OURS", "THEIRS", UTF_8.name()); "BASE", "OURS", "THEIRS", UTF_8);
String expected = "<<<<<<< OURS\n" String expected = "<<<<<<< OURS\n"
+ "1master\n" + "1master\n"
+ "=======\n" + "=======\n"

74
org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeFormatter.java

@ -45,6 +45,7 @@ package org.eclipse.jgit.merge;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -72,15 +73,46 @@ public class MergeFormatter {
* " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The * " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
* names for the sequences are given in this list * names for the sequences are given in this list
* @param charsetName * @param charsetName
* the name of the characterSet used when writing conflict * the name of the character set used when writing conflict
* metadata * metadata
* @throws java.io.IOException * @throws java.io.IOException
* @deprecated Use
* {@link #formatMerge(OutputStream, MergeResult, List, Charset)}
* instead.
*/ */
@Deprecated
public void formatMerge(OutputStream out, MergeResult<RawText> res, public void formatMerge(OutputStream out, MergeResult<RawText> res,
List<String> seqName, String charsetName) throws IOException { List<String> seqName, String charsetName) throws IOException {
new MergeFormatterPass(out, res, seqName, charsetName).formatMerge(); new MergeFormatterPass(out, res, seqName, charsetName).formatMerge();
} }
/**
* Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
* objects in a Git conformant way. This method also assumes that the
* {@link org.eclipse.jgit.diff.RawText} objects being merged are line
* oriented files which use LF as delimiter. This method will also use LF to
* separate chunks and conflict metadata, therefore it fits only to texts
* that are LF-separated lines.
*
* @param out
* the output stream where to write the textual presentation
* @param res
* the merge result which should be presented
* @param seqName
* When a conflict is reported each conflicting range will get a
* name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
* " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
* names for the sequences are given in this list
* @param charset
* the character set used when writing conflict metadata
* @throws java.io.IOException
* @since 5.2
*/
public void formatMerge(OutputStream out, MergeResult<RawText> res,
List<String> seqName, Charset charset) throws IOException {
new MergeFormatterPass(out, res, seqName, charset).formatMerge();
}
/** /**
* Formats the results of a merge of exactly two * Formats the results of a merge of exactly two
* {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way. * {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
@ -100,17 +132,51 @@ public class MergeFormatter {
* @param theirsName * @param theirsName
* the name ranges from theirs should get * the name ranges from theirs should get
* @param charsetName * @param charsetName
* the name of the characterSet used when writing conflict * the name of the character set used when writing conflict
* metadata * metadata
* @throws java.io.IOException * @throws java.io.IOException
* @deprecated use
* {@link #formatMerge(OutputStream, MergeResult, String, String, String, Charset)}
* instead.
*/ */
@SuppressWarnings("unchecked") @Deprecated
public void formatMerge(OutputStream out, MergeResult res, String baseName, public void formatMerge(OutputStream out, MergeResult res, String baseName,
String oursName, String theirsName, String charsetName) throws IOException { String oursName, String theirsName, String charsetName) throws IOException {
formatMerge(out, res, baseName, oursName, theirsName,
Charset.forName(charsetName));
}
/**
* Formats the results of a merge of exactly two
* {@link org.eclipse.jgit.diff.RawText} objects in a Git conformant way.
* This convenience method accepts the names for the three sequences (base
* and the two merged sequences) as explicit parameters and doesn't require
* the caller to specify a List
*
* @param out
* the {@link java.io.OutputStream} where to write the textual
* presentation
* @param res
* the merge result which should be presented
* @param baseName
* the name ranges from the base should get
* @param oursName
* the name ranges from ours should get
* @param theirsName
* the name ranges from theirs should get
* @param charset
* the character set used when writing conflict metadata
* @throws java.io.IOException
* @since 5.2
*/
@SuppressWarnings("unchecked")
public void formatMerge(OutputStream out, MergeResult res, String baseName,
String oursName, String theirsName, Charset charset)
throws IOException {
List<String> names = new ArrayList<>(3); List<String> names = new ArrayList<>(3);
names.add(baseName); names.add(baseName);
names.add(oursName); names.add(oursName);
names.add(theirsName); names.add(theirsName);
formatMerge(out, res, names, charsetName); formatMerge(out, res, names, charset);
} }
} }

46
org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeFormatterPass.java

@ -46,6 +46,7 @@ package org.eclipse.jgit.merge;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.diff.RawText;
@ -59,19 +60,58 @@ class MergeFormatterPass {
private final List<String> seqName; private final List<String> seqName;
private final String charsetName; private final Charset charset;
private final boolean threeWayMerge; private final boolean threeWayMerge;
private String lastConflictingName; // is set to non-null whenever we are in private String lastConflictingName; // is set to non-null whenever we are in
// a conflict // a conflict
/**
* @deprecated use
* {@link #MergeFormatterPass(OutputStream, MergeResult, List, Charset)}
* instead.
* @param out
* the {@link java.io.OutputStream} where to write the textual
* presentation
* @param res
* the merge result which should be presented
* @param seqName
* When a conflict is reported each conflicting range will get a
* name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
* " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
* names for the sequences are given in this list
* @param charsetName
* the name of the character set used when writing conflict
* metadata
*/
@Deprecated
MergeFormatterPass(OutputStream out, MergeResult<RawText> res, List<String> seqName, MergeFormatterPass(OutputStream out, MergeResult<RawText> res, List<String> seqName,
String charsetName) { String charsetName) {
this(out, res, seqName, Charset.forName(charsetName));
}
/**
*
* @param out
* the {@link java.io.OutputStream} where to write the textual
* presentation
* @param res
* the merge result which should be presented
* @param seqName
* When a conflict is reported each conflicting range will get a
* name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
* " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
* names for the sequences are given in this list
* @param charset
* the character set used when writing conflict metadata
*/
MergeFormatterPass(OutputStream out, MergeResult<RawText> res,
List<String> seqName, Charset charset) {
this.out = new EolAwareOutputStream(out); this.out = new EolAwareOutputStream(out);
this.res = res; this.res = res;
this.seqName = seqName; this.seqName = seqName;
this.charsetName = charsetName; this.charset = charset;
this.threeWayMerge = (res.getSequences().size() == 3); this.threeWayMerge = (res.getSequences().size() == 3);
} }
@ -133,7 +173,7 @@ class MergeFormatterPass {
private void writeln(String s) throws IOException { private void writeln(String s) throws IOException {
out.beginln(); out.beginln();
out.write((s + "\n").getBytes(charsetName)); //$NON-NLS-1$ out.write((s + "\n").getBytes(charset)); //$NON-NLS-1$
} }
private void writeLine(RawText seq, int i) throws IOException { private void writeLine(RawText seq, int i) throws IOException {

4
org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java

@ -46,10 +46,10 @@
*/ */
package org.eclipse.jgit.merge; package org.eclipse.jgit.merge;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm.HISTOGRAM; import static org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm.HISTOGRAM;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFF_SECTION; import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DIFF_SECTION;
import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_ALGORITHM; import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_ALGORITHM;
import static org.eclipse.jgit.lib.Constants.CHARACTER_ENCODING;
import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
@ -1026,7 +1026,7 @@ public class ResolveMerger extends ThreeWayMerger {
db != null ? nonNullRepo().getDirectory() : null, inCoreLimit); db != null ? nonNullRepo().getDirectory() : null, inCoreLimit);
try { try {
new MergeFormatter().formatMerge(buf, result, new MergeFormatter().formatMerge(buf, result,
Arrays.asList(commitNames), CHARACTER_ENCODING); Arrays.asList(commitNames), UTF_8);
buf.close(); buf.close();
} catch (IOException e) { } catch (IOException e) {
buf.destroy(); buf.destroy();

Loading…
Cancel
Save