Example usage for org.eclipse.jgit.api MergeCommand setStrategy

List of usage examples for org.eclipse.jgit.api MergeCommand setStrategy

Introduction

In this page you can find the example usage for org.eclipse.jgit.api MergeCommand setStrategy.

Prototype

public MergeCommand setStrategy(MergeStrategy mergeStrategy) 

Source Link

Document

Set merge strategy

Usage

From source file:br.com.riselabs.cotonet.builder.NetworkBuilder.java

License:Open Source License

/**
 * Returns the conflicting files of the given scenario.
 * //from   w  ww .j av  a2  s .  c o  m
 * @param scenario
 * @return
 * @throws CheckoutConflictException
 * @throws GitAPIException
 */
private List<File> getConflictingFiles(MergeScenario scenario)
        throws CheckoutConflictException, GitAPIException {
    Git git = Git.wrap(getProject().getRepository());
    // this is for the cases of restarting after exception in a conflict
    // scenario analysis
    try {
        git.reset().setRef(scenario.getLeft().getName()).setMode(ResetType.HARD).call();
    } catch (JGitInternalException e) {
        Logger.log(log, "[" + project.getName() + "] JGit Reset Command ended with exception."
                + " Trying external reset command.");
        ExternalGitCommand egit = new ExternalGitCommand();
        try {
            egit.setType(CommandType.RESET).setDirectory(project.getRepository().getDirectory().getParentFile())
                    .call();
        } catch (BlameException e1) {
            Logger.logStackTrace(log, e1);
            return null;
        }
    }

    CheckoutCommand ckoutCmd = git.checkout();
    ckoutCmd.setName(scenario.getLeft().getName());
    ckoutCmd.setStartPoint(scenario.getLeft());
    ckoutCmd.call();

    MergeCommand mergeCmd = git.merge();
    mergeCmd.setCommit(false);
    mergeCmd.setStrategy(MergeStrategy.RECURSIVE);
    mergeCmd.include(scenario.getRight());

    Set<String> conflictingPaths;
    try {
        // dealing with MissingObjectException
        MergeResult mResult = mergeCmd.call();
        // dealing with Ghosts conflicts
        conflictingPaths = mResult.getConflicts().keySet();
    } catch (NullPointerException | JGitInternalException e) {
        StringBuilder sb = new StringBuilder();
        sb.append("[" + project.getName() + ":" + project.getUrl() + "] " + "Skipping merge scenario due to '"
                + e.getMessage() + "'\n");
        sb.append("--> Exception: " + e.getClass());
        sb.append("--> Skipped scenario:\n");
        sb.append("::Base:" + scenario.getBase().getName() + "\n");
        sb.append("::Left:" + scenario.getLeft().getName() + "\n");
        sb.append("::Right:" + scenario.getRight().getName() + "\n");
        Logger.log(log, sb.toString());
        return null;
    }
    List<File> result = new ArrayList<File>();
    for (String path : conflictingPaths) {
        result.add(new File(getProject().getRepository().getDirectory().getParent(), path));
    }
    return result;
}

From source file:org.ajoberstar.gradle.git.tasks.GitMerge.java

License:Apache License

/**
 * Merges some changes with the current branch.
 */// w w  w  .  jav a 2 s  . com
@TaskAction
public void merge() {
    MergeCommand cmd = getGit().merge();
    cmd.include(findRef());
    cmd.setStrategy(getMergeStrategy());
    try {
        cmd.call();
    } catch (CheckoutConflictException e) {
        throw new GradleException("The working tree changes conflict with the specified commit.", e);
    } catch (GitAPIException e) {
        throw new GradleException("Problem with merge.", e);
    }
    //TODO add progress monitor to log progress to Gradle status bar
}

From source file:org.eclipse.egit.core.op.MergeOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    if (mergeResult != null)
        throw new CoreException(
                new Status(IStatus.ERROR, Activator.getPluginId(), CoreText.OperationAlreadyExecuted));
    IProgressMonitor monitor;//from   ww  w. jav  a 2s . c o  m
    if (m == null)
        monitor = new NullProgressMonitor();
    else
        monitor = m;
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        public void run(IProgressMonitor mymonitor) throws CoreException {
            IProject[] validProjects = ProjectUtil.getValidProjects(repository);
            mymonitor.beginTask(NLS.bind(CoreText.MergeOperation_ProgressMerge, refName), 3);
            Git git = new Git(repository);
            mymonitor.worked(1);
            MergeCommand merge;
            try {
                merge = git.merge().include(repository.getRef(refName));
            } catch (IOException e) {
                throw new TeamException(CoreText.MergeOperation_InternalError, e);
            }
            if (mergeStrategy != null) {
                merge.setStrategy(mergeStrategy);
            }
            try {
                mergeResult = merge.call();
                mymonitor.worked(1);
                if (MergeResult.MergeStatus.FAILED.equals(mergeResult.getMergeStatus()))
                    throw new TeamException(mergeResult.toString());
                else if (MergeResult.MergeStatus.NOT_SUPPORTED.equals(mergeResult.getMergeStatus()))
                    throw new TeamException(
                            new Status(IStatus.INFO, Activator.getPluginId(), mergeResult.toString()));
            } catch (NoHeadException e) {
                throw new TeamException(CoreText.MergeOperation_MergeFailedNoHead, e);
            } catch (ConcurrentRefUpdateException e) {
                throw new TeamException(CoreText.MergeOperation_MergeFailedRefUpdate, e);
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                ProjectUtil.refreshValidProjects(validProjects, new SubProgressMonitor(mymonitor, 1));
                mymonitor.done();
            }
        }
    };
    // lock workspace to protect working tree changes
    ResourcesPlugin.getWorkspace().run(action, monitor);
}