如果在 JGit 中拉变基时发生任何冲突,如何接受我的

How to accept mine if any conflicts occur while pull-rebase in JGit

我有一段代码可以使用 rebase:

 private void pullWithRebase(Git git) throws GitAPIException {
    git.checkout().setName("master").call();
    List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
    String remoteMasterBranchName = "refs/remotes/origin/master";
    for (Ref ref : branches) {
        if (remoteMasterBranchName.equals(ref.getName())) {
            PullResult result = git.pull().setRemoteBranchName("master").setRebase(true).call();
            return;
        }
    }
}

但是如果合并时发生任何冲突,它就不起作用。如果确实发生了,我想接受我的

我最后只是合并了两个分支,直接通过修改文件解决了任何冲突。

private static final String CONFLICT_HEAD_MARKER = "<<<<<<<";
private static final String CONFLICT_BORDER_MARKER = "=======";
private static final String CONFLICT_UPSTREAM_MARKER = ">>>>>>>";

private boolean acceptHead;

public void resolve(File file) throws IOException {
    File temp = new File(file.getParent(), "temp" + System.currentTimeMillis());
    try(BufferedReader reader = new BufferedReader(new FileReader(file));
        BufferedWriter writer = new BufferedWriter(new FileWriter(temp))) {
        String currentLine;
        boolean removePartition = false;
        while((currentLine = reader.readLine()) != null) {
            if (currentLine.contains(CONFLICT_HEAD_MARKER)) {
                removePartition = !acceptHead;
                continue;
            } else if (currentLine.contains(CONFLICT_BORDER_MARKER)) {
                removePartition = acceptHead;
                continue;
            } else if (currentLine.contains(CONFLICT_UPSTREAM_MARKER)) {
                removePartition = false;
                continue;
            }
            if (!removePartition) {
                writer.write(currentLine + System.getProperty("line.separator"));
            }
        }
    }
    FileUtils.forceDelete(file);
    FileUtils.moveFile(temp, file);
}