如何在JGit中使用diff3冲突样式?

时间:2019-03-28 14:32:53

标签: java jgit diff3

在git中,在合并冲突的情况下使用diff3作为冲突样式既简单又有用,其中冲突块由我们的/基础/其版本组成。 但是,当我使用jGit进行合并时,它仅报告我们和他们的情况,我发现没有将diff3配置为冲突样式的API。

我试图在Google中进行搜索,但大多数结果与Git有关。而且,我还深入研究了jGit的源代码,但似乎必须修改MergeFormatterPass(https://github.com/eclipse/jgit/blob/master/org.eclipse.jgit/src/org/eclipse/jgit/merge/MergeFormatterPass.java)才能在冲突块中保留基本内容。

尽管jGit在Github上是开源的,但它不允许出现问题。

private String mergeTextually(String leftContent, String baseContent, String rightContent) {
    String textualMergeResult = null;
    try {
      // TODO merge with diff3 conflict style
      RawTextComparator textComparator = RawTextComparator.WS_IGNORE_ALL;
      @SuppressWarnings("rawtypes")
      MergeResult mergeResult =
          new MergeAlgorithm()
              .merge(
                  textComparator,
                  new RawText(Constants.encode(baseContent)),
                  new RawText(Constants.encode(leftContent)),
                  new RawText(Constants.encode(rightContent)));
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      (new MergeFormatter())
          .formatMerge(
              output,
              mergeResult,
              Side.BASE.asString(),
              Side.OURS.asString(),
              Side.THEIRS.asString(),
              StandardCharsets.UTF_8);
      textualMergeResult = new String(output.toByteArray(), StandardCharsets.UTF_8);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return textualMergeResult;
  }

diff3中预期的冲突块:

<<<<<<<
    /**
     * The path that was passed in the constructor.
     */
    public Path getRoot() {
        return root;
=======
    private Path getRoot() {
        return root;
|||||||
    public JavaParser getJavaParser() {
        return javaParser;
    }

    public SourceRoot setJavaParser(JavaParser javaParser) {
        this.javaParser = javaParser;
        return this;
>>>>>>>

jGit生成的实际冲突块:

<<<<<<<
    /**
     * The path that was passed in the constructor.
     */
    public Path getRoot() {
        return root;
=======
    public JavaParser getJavaParser() {
        return javaParser;
    }

    public SourceRoot setJavaParser(JavaParser javaParser) {
        this.javaParser = javaParser;
        return this;
>>>>>>>

0 个答案:

没有答案