Search in sources :

Example 6 with MergeStrategy

use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.

the class CherryPickOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor pm) throws CoreException {
            SubMonitor progress = SubMonitor.convert(pm, 2);
            progress.subTask(MessageFormat.format(CoreText.CherryPickOperation_cherryPicking, commit.name()));
            try (Git git = new Git(repo)) {
                CherryPickCommand command = git.cherryPick().include(commit.getId());
                MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                if (strategy != null) {
                    command.setStrategy(strategy);
                }
                result = command.call();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }
            progress.worked(1);
            ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repo), progress.newChild(1));
        }
    };
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) TeamException(org.eclipse.team.core.TeamException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) CherryPickCommand(org.eclipse.jgit.api.CherryPickCommand) SubMonitor(org.eclipse.core.runtime.SubMonitor)

Example 7 with MergeStrategy

use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.

the class SubmoduleUpdateOperation method execute.

@Override
public void execute(final IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor pm) throws CoreException {
            RepositoryUtil util = Activator.getDefault().getRepositoryUtil();
            SubMonitor progress = SubMonitor.convert(pm, 4);
            progress.setTaskName(MessageFormat.format(CoreText.SubmoduleUpdateOperation_updating, util.getRepositoryName(repository)));
            Git git = Git.wrap(repository);
            Collection<String> updated = null;
            try {
                SubmoduleInitCommand init = git.submoduleInit();
                for (String path : paths) init.addPath(path);
                init.call();
                progress.worked(1);
                SubmoduleUpdateCommand update = git.submoduleUpdate();
                for (String path : paths) update.addPath(path);
                update.setProgressMonitor(new EclipseGitProgressTransformer(progress.newChild(2)));
                MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                if (strategy != null) {
                    update.setStrategy(strategy);
                }
                update.setCallback(new CloneCommand.Callback() {

                    @Override
                    public void initializedSubmodules(Collection<String> submodules) {
                    // Nothing to do
                    }

                    @Override
                    public void cloningSubmodule(String path) {
                        progress.setTaskName(MessageFormat.format(CoreText.SubmoduleUpdateOperation_cloning, util.getRepositoryName(repository), path));
                    }

                    @Override
                    public void checkingOut(AnyObjectId commit, String path) {
                    // Nothing to do
                    }
                });
                updated = update.call();
                SubMonitor refreshMonitor = progress.newChild(1).setWorkRemaining(updated.size());
                for (String path : updated) {
                    Repository subRepo = SubmoduleWalk.getSubmoduleRepository(repository, path);
                    if (subRepo != null) {
                        ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(subRepo), refreshMonitor.newChild(1));
                    } else {
                        refreshMonitor.worked(1);
                    }
                }
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } catch (IOException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } finally {
                if (updated != null && !updated.isEmpty()) {
                    repository.notifyIndexChanged();
                }
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, monitor);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) SubmoduleUpdateCommand(org.eclipse.jgit.api.SubmoduleUpdateCommand) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) SubMonitor(org.eclipse.core.runtime.SubMonitor) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) IOException(java.io.IOException) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) TeamException(org.eclipse.team.core.TeamException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) SubmoduleInitCommand(org.eclipse.jgit.api.SubmoduleInitCommand)

Example 8 with MergeStrategy

use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.

the class RebaseOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    if (result != null)
        throw new CoreException(new Status(IStatus.ERROR, Activator.getPluginId(), CoreText.OperationAlreadyExecuted));
    final IProject[] validProjects = ProjectUtil.getValidOpenProjects(repository);
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor actMonitor) throws CoreException {
            SubMonitor progress = SubMonitor.convert(actMonitor, 2);
            try (Git git = new Git(repository)) {
                RebaseCommand cmd = git.rebase().setProgressMonitor(new EclipseGitProgressTransformer(progress.newChild(1)));
                MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                if (strategy != null) {
                    cmd.setStrategy(strategy);
                }
                if (handler != null) {
                    cmd.runInteractively(handler, true);
                }
                if (operation == Operation.BEGIN) {
                    cmd.setPreserveMerges(preserveMerges);
                    result = cmd.setUpstream(ref.getName()).call();
                } else {
                    result = cmd.setOperation(operation).call();
                }
            } catch (JGitInternalException | GitAPIException e) {
                throw new CoreException(Activator.error(e.getMessage(), e));
            } finally {
                if (refreshNeeded()) {
                    ProjectUtil.refreshValidProjects(validProjects, progress.newChild(1));
                }
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) RebaseCommand(org.eclipse.jgit.api.RebaseCommand) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) SubMonitor(org.eclipse.core.runtime.SubMonitor) EclipseGitProgressTransformer(org.eclipse.egit.core.EclipseGitProgressTransformer) IProject(org.eclipse.core.resources.IProject) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) CoreException(org.eclipse.core.runtime.CoreException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 9 with MergeStrategy

use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.

the class SquashCommitsOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor pm) throws CoreException {
            SubMonitor progress = SubMonitor.convert(pm, 2);
            progress.subTask(MessageFormat.format(CoreText.SquashCommitsOperation_squashing, Integer.valueOf(commits.size())));
            InteractiveHandler handler = new InteractiveHandler() {

                @Override
                public void prepareSteps(List<RebaseTodoLine> steps) {
                    RevCommit firstCommit = commits.get(0);
                    for (RebaseTodoLine step : steps) {
                        if (isRelevant(step.getCommit())) {
                            try {
                                if (step.getCommit().prefixCompare(firstCommit) == 0)
                                    step.setAction(RebaseTodoLine.Action.PICK);
                                else
                                    step.setAction(RebaseTodoLine.Action.SQUASH);
                            } catch (IllegalTodoFileModification e) {
                            // shouldn't happen
                            }
                        }
                    }
                }

                private boolean isRelevant(AbbreviatedObjectId id) {
                    for (RevCommit commit : commits) {
                        if (id.prefixCompare(commit) == 0)
                            return true;
                    }
                    return false;
                }

                @Override
                public String modifyCommitMessage(String oldMessage) {
                    return messageHandler.modifyCommitMessage(oldMessage);
                }
            };
            try (Git git = new Git(repository)) {
                RebaseCommand command = git.rebase().setUpstream(commits.get(0).getParent(0)).runInteractively(handler).setOperation(RebaseCommand.Operation.BEGIN);
                MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                if (strategy != null) {
                    command.setStrategy(strategy);
                }
                command.call();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }
            progress.worked(1);
            ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repository), progress.newChild(1));
        }
    };
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
Also used : RebaseTodoLine(org.eclipse.jgit.lib.RebaseTodoLine) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IllegalTodoFileModification(org.eclipse.jgit.errors.IllegalTodoFileModification) RebaseCommand(org.eclipse.jgit.api.RebaseCommand) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) SubMonitor(org.eclipse.core.runtime.SubMonitor) InteractiveHandler(org.eclipse.jgit.api.RebaseCommand.InteractiveHandler) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) TeamException(org.eclipse.team.core.TeamException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 10 with MergeStrategy

use of org.eclipse.jgit.merge.MergeStrategy in project egit by eclipse.

the class StashApplyOperation method execute.

@Override
public void execute(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor pm) throws CoreException {
            SubMonitor progress = SubMonitor.convert(pm, 3);
            try {
                IProject[] validProjects = ProjectUtil.getValidOpenProjects(repository);
                progress.worked(1);
                StashApplyCommand command = Git.wrap(repository).stashApply().setStashRef(commit.name());
                MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                if (strategy != null) {
                    command.setStrategy(strategy);
                }
                command.call();
                progress.worked(1);
                ProjectUtil.refreshValidProjects(validProjects, progress.newChild(1));
            } catch (JGitInternalException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }
        }
    };
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, monitor);
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) TeamException(org.eclipse.team.core.TeamException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) StashApplyCommand(org.eclipse.jgit.api.StashApplyCommand) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) SubMonitor(org.eclipse.core.runtime.SubMonitor) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) IProject(org.eclipse.core.resources.IProject)

Aggregations

MergeStrategy (org.eclipse.jgit.merge.MergeStrategy)10 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)7 SubMonitor (org.eclipse.core.runtime.SubMonitor)7 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 Git (org.eclipse.jgit.api.Git)6 TeamException (org.eclipse.team.core.TeamException)4 Repository (org.eclipse.jgit.lib.Repository)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 IProject (org.eclipse.core.resources.IProject)2 EclipseGitProgressTransformer (org.eclipse.egit.core.EclipseGitProgressTransformer)2 RebaseCommand (org.eclipse.jgit.api.RebaseCommand)2 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1