Search in sources :

Example 6 with MergeOperation

use of org.eclipse.egit.core.op.MergeOperation in project egit by eclipse.

the class MergeWithPreferredStrategyTest method testMergeUsesPreferredStrategy.

@Test
public void testMergeUsesPreferredStrategy() throws Exception {
    MergeOperation operation = new MergeOperation(testRepository.getRepository(), SIDE);
    operation.execute(new NullProgressMonitor());
    // With the MergeStrategy OURS, new files from branch 'side' should be
    // ignored and new files from branch 'master' should be present
    assertEquals(4, countCommitsInHead());
    assertTrue(testRepository.getIFile(project.getProject(), file2).exists());
    assertFalse(testRepository.getIFile(project.getProject(), file3).exists());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) MergeOperation(org.eclipse.egit.core.op.MergeOperation) Test(org.junit.Test)

Example 7 with MergeOperation

use of org.eclipse.egit.core.op.MergeOperation in project egit by eclipse.

the class GitFlowOperation method mergeTo.

/**
 * @param monitor
 * @param branchName
 * @param targetBranchName
 * @param squash
 * @param fastForwardSingleCommit Has no effect if {@code squash} is true.
 * @return result of merging back to targetBranchName
 * @throws CoreException
 * @since 4.1
 */
@NonNull
protected MergeResult mergeTo(IProgressMonitor monitor, String branchName, String targetBranchName, boolean squash, boolean fastForwardSingleCommit) throws CoreException {
    try {
        if (!repository.hasBranch(targetBranchName)) {
            throw new RuntimeException(String.format(CoreText.GitFlowOperation_branchNotFound, targetBranchName));
        }
        SubMonitor progress = SubMonitor.convert(monitor, 2);
        boolean dontCloseProjects = false;
        BranchOperation branchOperation = new BranchOperation(repository.getRepository(), targetBranchName, dontCloseProjects);
        branchOperation.execute(progress.newChild(1));
        Status status = branchOperation.getResult().getStatus();
        if (!CheckoutResult.Status.OK.equals(status)) {
            throw new CoreException(error(format(CoreText.GitFlowOperation_unableToCheckout, branchName, status.toString())));
        }
        MergeOperation mergeOperation = new MergeOperation(repository.getRepository(), branchName);
        mergeOperation.setSquash(squash);
        if (squash) {
            mergeOperation.setCommit(true);
        }
        if (!squash && (!fastForwardSingleCommit || hasMultipleCommits(branchName))) {
            mergeOperation.setFastForwardMode(NO_FF);
        }
        mergeOperation.execute(progress.newChild(1));
        MergeResult result = mergeOperation.getResult();
        if (result == null) {
            throw new CoreException(error(format(CoreText.GitFlowOperation_unableToMerge, branchName, targetBranchName)));
        }
        return result;
    } catch (GitAPIException | IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Status(org.eclipse.jgit.api.CheckoutResult.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) MergeOperation(org.eclipse.egit.core.op.MergeOperation) CoreException(org.eclipse.core.runtime.CoreException) CreateLocalBranchOperation(org.eclipse.egit.core.op.CreateLocalBranchOperation) DeleteBranchOperation(org.eclipse.egit.core.op.DeleteBranchOperation) BranchOperation(org.eclipse.egit.core.op.BranchOperation) SubMonitor(org.eclipse.core.runtime.SubMonitor) MergeResult(org.eclipse.jgit.api.MergeResult) IOException(java.io.IOException) NonNull(org.eclipse.jgit.annotations.NonNull)

Example 8 with MergeOperation

use of org.eclipse.egit.core.op.MergeOperation in project egit by eclipse.

the class MergeCommand method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    RepositoryTreeNode node = getSelectedNodes(event).get(0);
    final Repository repository = node.getRepository();
    if (!MergeActionHandler.checkMergeIsPossible(repository, getShell(event)) || LaunchFinder.shouldCancelBecauseOfRunningLaunches(repository, null)) {
        return null;
    }
    BasicConfigurationDialog.show(repository);
    String targetRef;
    if (node instanceof RefNode) {
        String refName = ((RefNode) node).getObject().getName();
        try {
            if (refName.equals(repository.getFullBranch()))
                targetRef = null;
            else
                targetRef = refName;
        } catch (IOException e) {
            targetRef = null;
        }
    } else if (node instanceof TagNode)
        targetRef = ((TagNode) node).getObject().getName();
    else
        targetRef = null;
    final String refName;
    final MergeOperation op;
    if (targetRef != null) {
        refName = targetRef;
        op = new MergeOperation(repository, refName);
    } else {
        MergeTargetSelectionDialog mergeTargetSelectionDialog = new MergeTargetSelectionDialog(getShell(event), repository);
        if (mergeTargetSelectionDialog.open() != IDialogConstants.OK_ID)
            return null;
        refName = mergeTargetSelectionDialog.getRefName();
        op = new MergeOperation(repository, refName);
        op.setSquash(mergeTargetSelectionDialog.isMergeSquash());
        op.setFastForwardMode(mergeTargetSelectionDialog.getFastForwardMode());
        op.setCommit(mergeTargetSelectionDialog.isCommit());
    }
    String jobname = NLS.bind(UIText.MergeAction_JobNameMerge, refName);
    Job job = new WorkspaceJob(jobname) {

        @Override
        public IStatus runInWorkspace(IProgressMonitor monitor) {
            try {
                op.execute(monitor);
            } catch (final CoreException e) {
                return e.getStatus();
            }
            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.setRule(op.getSchedulingRule());
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent jobEvent) {
            IStatus result = jobEvent.getJob().getResult();
            if (result.getSeverity() == IStatus.CANCEL)
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        // don't use getShell(event) here since
                        // the active shell has changed since the
                        // execution has been triggered.
                        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                        MessageDialog.openInformation(shell, UIText.MergeAction_MergeCanceledTitle, UIText.MergeAction_MergeCanceledMessage);
                    }
                });
            else if (!result.isOK())
                Activator.handleError(result.getMessage(), result.getException(), true);
            else
                Display.getDefault().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                        MergeResultDialog.getDialog(shell, repository, op.getResult()).open();
                    }
                });
        }
    });
    job.schedule();
    return null;
}
Also used : MergeOperation(org.eclipse.egit.core.op.MergeOperation) IStatus(org.eclipse.core.runtime.IStatus) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) IOException(java.io.IOException) MergeTargetSelectionDialog(org.eclipse.egit.ui.internal.dialogs.MergeTargetSelectionDialog) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) RefNode(org.eclipse.egit.ui.internal.repository.tree.RefNode) Repository(org.eclipse.jgit.lib.Repository) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Shell(org.eclipse.swt.widgets.Shell) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) CoreException(org.eclipse.core.runtime.CoreException) Job(org.eclipse.core.runtime.jobs.Job) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) TagNode(org.eclipse.egit.ui.internal.repository.tree.TagNode)

Example 9 with MergeOperation

use of org.eclipse.egit.core.op.MergeOperation in project egit by eclipse.

the class MergeActionHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    Repository repository = getRepository(true, event);
    Shell shell = getShell(event);
    if (repository == null || !checkMergeIsPossible(repository, shell) || LaunchFinder.shouldCancelBecauseOfRunningLaunches(repository, null)) {
        return null;
    }
    BasicConfigurationDialog.show(repository);
    MergeTargetSelectionDialog mergeTargetSelectionDialog = new MergeTargetSelectionDialog(shell, repository);
    if (mergeTargetSelectionDialog.open() == IDialogConstants.OK_ID) {
        String refName = mergeTargetSelectionDialog.getRefName();
        MergeOperation op = new MergeOperation(repository, refName);
        op.setSquash(mergeTargetSelectionDialog.isMergeSquash());
        op.setFastForwardMode(mergeTargetSelectionDialog.getFastForwardMode());
        op.setCommit(mergeTargetSelectionDialog.isCommit());
        doMerge(repository, op, refName);
    }
    return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Shell(org.eclipse.swt.widgets.Shell) MergeOperation(org.eclipse.egit.core.op.MergeOperation) MergeTargetSelectionDialog(org.eclipse.egit.ui.internal.dialogs.MergeTargetSelectionDialog)

Example 10 with MergeOperation

use of org.eclipse.egit.core.op.MergeOperation in project jbosstools-openshift by jbosstools.

the class EGitUtils method merge.

/**
 * Merges current master with the given branch. The strategy used is Resolve
 * since egit does still not support the Recusive stragety.
 *
 * @param branch
 *            the branch to merge
 * @param repository
 *            the repository the branch is in
 * @param monitor
 *            the monitor to report the progress to
 * @return the result of the merge
 * @throws CoreException
 *
 * @see MergeStrategy#RESOLVE
 * @link
 *       http://www.eclipse.org/forums/index.php/mv/msg/261278/753913/#msg_753913
 * @link https://bugs.eclipse.org/bugs/show_bug.cgi?id=354099
 * @link https://bugs.eclipse.org/bugs/show_bug.cgi?id=359951
 */
private static MergeResult merge(String branch, Repository repository, IProgressMonitor monitor) throws CoreException {
    MergeOperation merge = new MergeOperation(repository, branch, MergeStrategy.RESOLVE.getName());
    merge.execute(monitor);
    return merge.getResult();
}
Also used : MergeOperation(org.eclipse.egit.core.op.MergeOperation)

Aggregations

MergeOperation (org.eclipse.egit.core.op.MergeOperation)14 Test (org.junit.Test)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IOException (java.io.IOException)3 Repository (org.eclipse.jgit.lib.Repository)3 Shell (org.eclipse.swt.widgets.Shell)3 File (java.io.File)2 CoreException (org.eclipse.core.runtime.CoreException)2 MergeTargetSelectionDialog (org.eclipse.egit.ui.internal.dialogs.MergeTargetSelectionDialog)2 MergeResult (org.eclipse.jgit.api.MergeResult)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ExecutionException (org.eclipse.core.commands.ExecutionException)1 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Path (org.eclipse.core.runtime.Path)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 Job (org.eclipse.core.runtime.jobs.Job)1