如果在JGit中使用pull-rebase时发生任何冲突,如何接受我的

时间:2015-01-01 17:35:55

标签: 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;
        }
    }
}

但是,如果在合并时发生任何冲突,则无效。如果确实发生了,我想接受我的

1 个答案:

答案 0 :(得分:0)

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

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);
}