Search in sources :

Example 1 with InteractiveHandler

use of org.eclipse.jgit.api.RebaseCommand.InteractiveHandler in project egit by eclipse.

the class RewordCommitOperation 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.RewordCommitOperation_rewording, commit.name()));
            InteractiveHandler handler = new InteractiveHandler() {

                @Override
                public void prepareSteps(List<RebaseTodoLine> steps) {
                    for (RebaseTodoLine step : steps) {
                        if (step.getCommit().prefixCompare(commit) == 0) {
                            try {
                                step.setAction(RebaseTodoLine.Action.REWORD);
                            } catch (IllegalTodoFileModification e) {
                            // shouldn't happen
                            }
                        }
                    }
                }

                @Override
                public String modifyCommitMessage(String oldMessage) {
                    return newMessage;
                }
            };
            try (Git git = new Git(repository)) {
                git.rebase().setUpstream(commit.getParent(0)).runInteractively(handler).setOperation(RebaseCommand.Operation.BEGIN).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) 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) IllegalTodoFileModification(org.eclipse.jgit.errors.IllegalTodoFileModification) SubMonitor(org.eclipse.core.runtime.SubMonitor) InteractiveHandler(org.eclipse.jgit.api.RebaseCommand.InteractiveHandler) List(java.util.List)

Example 2 with InteractiveHandler

use of org.eclipse.jgit.api.RebaseCommand.InteractiveHandler in project egit by eclipse.

the class EditCommitOperation 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.EditCommitOperation_editing, commit.name()));
            InteractiveHandler handler = new InteractiveHandler() {

                @Override
                public void prepareSteps(List<RebaseTodoLine> steps) {
                    for (RebaseTodoLine step : steps) {
                        if (step.getCommit().prefixCompare(commit) == 0) {
                            try {
                                step.setAction(RebaseTodoLine.Action.EDIT);
                            } catch (IllegalTodoFileModification e) {
                            // shouldn't happen
                            }
                        }
                    }
                }

                @Override
                public String modifyCommitMessage(String oldMessage) {
                    return oldMessage;
                }
            };
            try (Git git = new Git(repository)) {
                git.rebase().setUpstream(commit.getParent(0)).runInteractively(handler).setOperation(RebaseCommand.Operation.BEGIN).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) 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) IllegalTodoFileModification(org.eclipse.jgit.errors.IllegalTodoFileModification) SubMonitor(org.eclipse.core.runtime.SubMonitor) InteractiveHandler(org.eclipse.jgit.api.RebaseCommand.InteractiveHandler) List(java.util.List)

Example 3 with InteractiveHandler

use of org.eclipse.jgit.api.RebaseCommand.InteractiveHandler in project egit by eclipse.

the class SquashCommitsOperationTest method squash.

@Test
public void squash() throws Exception {
    InteractiveHandler messageHandler = new InteractiveHandler() {

        @Override
        public void prepareSteps(List<RebaseTodoLine> steps) {
        // not used
        }

        @Override
        public String modifyCommitMessage(String commit) {
            return "squashed";
        }
    };
    List<RevCommit> commits = Arrays.asList(commit1, commit2, commit3);
    SquashCommitsOperation op = new SquashCommitsOperation(testRepository.getRepository(), commits, messageHandler);
    op.execute(new NullProgressMonitor());
    assertEquals(2, countCommitsInHead());
    LogCommand log;
    try (Git git = new Git(testRepository.getRepository())) {
        log = git.log();
    }
    Iterable<RevCommit> logCommits = log.call();
    RevCommit latestCommit = logCommits.iterator().next();
    assertEquals("squashed", latestCommit.getFullMessage());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Git(org.eclipse.jgit.api.Git) LogCommand(org.eclipse.jgit.api.LogCommand) SquashCommitsOperation(org.eclipse.egit.core.op.SquashCommitsOperation) InteractiveHandler(org.eclipse.jgit.api.RebaseCommand.InteractiveHandler) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 4 with InteractiveHandler

use of org.eclipse.jgit.api.RebaseCommand.InteractiveHandler in project egit by eclipse.

the class RebaseCurrentRefCommand method createRebaseOperation.

@Override
protected RebaseOperation createRebaseOperation(Repository repository) throws ExecutionException {
    if (LaunchFinder.shouldCancelBecauseOfRunningLaunches(repository, null)) {
        return null;
    }
    InteractiveHandler handler = interactive ? RebaseInteractiveHandler.INSTANCE : null;
    RebaseOperation operation = new RebaseOperation(repository, ref, handler);
    operation.setPreserveMerges(preserveMerges);
    return operation;
}
Also used : RebaseOperation(org.eclipse.egit.core.op.RebaseOperation) RebaseInteractiveHandler(org.eclipse.egit.ui.internal.rebase.RebaseInteractiveHandler) InteractiveHandler(org.eclipse.jgit.api.RebaseCommand.InteractiveHandler)

Example 5 with InteractiveHandler

use of org.eclipse.jgit.api.RebaseCommand.InteractiveHandler in project egit by eclipse.

the class SquashHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    List<RevCommit> commits = getSelectedItems(RevCommit.class, event);
    if ((commits == null) || commits.isEmpty())
        return null;
    Repository repo = getSelectedItem(Repository.class, event);
    if (repo == null)
        return null;
    commits = CommitUtil.sortCommits(commits);
    final Shell shell = getPart(event).getSite().getShell();
    try {
        if (!UIRepositoryUtils.handleUncommittedFiles(repo, shell))
            return null;
    } catch (GitAPIException e) {
        Activator.logError(e.getMessage(), e);
        return null;
    }
    InteractiveHandler messageHandler = new InteractiveHandler() {

        @Override
        public void prepareSteps(List<RebaseTodoLine> steps) {
        // not used
        }

        @Override
        public String modifyCommitMessage(String oldMessage) {
            return promptCommitMessage(shell, oldMessage);
        }
    };
    final SquashCommitsOperation op = new SquashCommitsOperation(repo, commits, messageHandler);
    Job job = new Job(MessageFormat.format(UIText.SquashHandler_JobName, Integer.valueOf(commits.size()))) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                op.execute(monitor);
            } catch (CoreException e) {
                Activator.logError(UIText.SquashHandler_InternalError, e);
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            if (JobFamilies.SQUASH.equals(family))
                return true;
            return super.belongsTo(family);
        }
    };
    job.setUser(true);
    job.setRule(op.getSchedulingRule());
    job.schedule();
    return null;
}
Also used : SquashCommitsOperation(org.eclipse.egit.core.op.SquashCommitsOperation) InteractiveHandler(org.eclipse.jgit.api.RebaseCommand.InteractiveHandler) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) List(java.util.List) Job(org.eclipse.core.runtime.jobs.Job) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

InteractiveHandler (org.eclipse.jgit.api.RebaseCommand.InteractiveHandler)6 List (java.util.List)5 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)4 Git (org.eclipse.jgit.api.Git)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)3 SubMonitor (org.eclipse.core.runtime.SubMonitor)3 IllegalTodoFileModification (org.eclipse.jgit.errors.IllegalTodoFileModification)3 RebaseTodoLine (org.eclipse.jgit.lib.RebaseTodoLine)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 TeamException (org.eclipse.team.core.TeamException)3 SquashCommitsOperation (org.eclipse.egit.core.op.SquashCommitsOperation)2 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 RebaseOperation (org.eclipse.egit.core.op.RebaseOperation)1 RebaseInteractiveHandler (org.eclipse.egit.ui.internal.rebase.RebaseInteractiveHandler)1 LogCommand (org.eclipse.jgit.api.LogCommand)1 RebaseCommand (org.eclipse.jgit.api.RebaseCommand)1 AbbreviatedObjectId (org.eclipse.jgit.lib.AbbreviatedObjectId)1