如何在 JGit 中强制合并(使用冲突标记)?

How to force merge (with conflict markers) in JGit?

我们的应用程序在使用 JGit 获取/合并时需要使用不同的合并策略。例如,对于 JSON 文件中的任何冲突文件,我们想要恢复任何本地更改并获得 THEIRS。如果它们不是 JSON 文件,我们希望保留本地更改并合并,甚至在存在冲突时保留冲突标记。但是,无论我如何设置合并策略,我都会得到一个 CheckoutConflictException(停止合并),即使正常的命令行 git 会完成合并。我做错了什么?

public String update() throws Exception {
    // We fetch manually and do a dry-run merge to catch any conflicts, so we can reset them

    fetch();
    Ref fetchHead = this.repo().getRepository().findRef("FETCH_HEAD");

    try {
        MergeResult mergeResult = this.repo().merge()
            .setStrategy(MergeStrategy.RECURSIVE)
            .setCommit(false)
            .setFastForward(MergeCommand.FastForwardMode.FF)
            .include(fetchHead)
            .call();
    } catch (CheckoutConflictException cce) {
        List<String> conflicts = cce.getConflictingPaths();

        if (conflicts != null && conflicts.size() > 0) {
            for (String file : conflicts) {
                if (file.endsWith(".json")) {
                    revert(Paths.get(file));
                }
            }
        }
    }

    /// ... and now that we've reverted any conflicted JSON files, we can try the merge for real

    MergeResult mergeResult = this.repo().merge()
        .setStrategy(MergeStrategy.RECURSIVE)
        .setCommit(true)
        .setFastForward(MergeCommand.FastForwardMode.FF)
        .include(fetchHead)
        .call();

    return mergeResult.getMergeStatus().toString();
}

我能够使用 stashCreate() 和 stashApply() 获得我想要的行为。具体来说,stashCreate() 用于存储还原任何 JSON 文件后留下的任何更改,然后使用 PullCommand(而非 MergeCommand)提取任何新更改,然后使用 stashApply() 应用任何存储的本地更改。我测试它没有冲突,JSON 文件中有冲突,在本地和远程之间更改了文件的不同部分的其他文件中有冲突,以及更改了相同行的其他文件。它适用于上述所有情况。