Search in sources :

Example 1 with SquashCommitsOperation

use of org.eclipse.egit.core.op.SquashCommitsOperation 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 2 with SquashCommitsOperation

use of org.eclipse.egit.core.op.SquashCommitsOperation 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

List (java.util.List)2 SquashCommitsOperation (org.eclipse.egit.core.op.SquashCommitsOperation)2 InteractiveHandler (org.eclipse.jgit.api.RebaseCommand.InteractiveHandler)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 Git (org.eclipse.jgit.api.Git)1 LogCommand (org.eclipse.jgit.api.LogCommand)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 Repository (org.eclipse.jgit.lib.Repository)1 Shell (org.eclipse.swt.widgets.Shell)1 Test (org.junit.Test)1