Search in sources :

Example 16 with CommitOperation

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

the class CommitOperationTest method testCommitAll.

@Test
public void testCommitAll() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "sub/a.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "sub/b.txt", "some text");
    resources.add(project.getProject().getFolder("sub"));
    new AddToIndexOperation(resources).execute(null);
    CommitOperation commitOperation = new CommitOperation(null, null, TestUtils.AUTHOR, TestUtils.COMMITTER, "first commit");
    commitOperation.setCommitAll(true);
    commitOperation.setRepository(repository);
    commitOperation.execute(null);
    Iterator<RevCommit> commits;
    try (Git git = new Git(repository)) {
        commits = git.log().call().iterator();
    }
    RevCommit firstCommit = commits.next();
    assertTrue(firstCommit.getCommitTime() > 0);
    assertEquals("first commit", firstCommit.getFullMessage());
    testUtils.changeContentOfFile(project.getProject(), file1, "changed text");
    commitOperation = new CommitOperation(null, null, TestUtils.AUTHOR, TestUtils.COMMITTER, "second commit");
    commitOperation.setCommitAll(true);
    commitOperation.setRepository(repository);
    commitOperation.execute(null);
    try (Git git = new Git(repository)) {
        commits = git.log().call().iterator();
    }
    RevCommit secondCommit = commits.next();
    assertTrue(secondCommit.getCommitTime() > 0);
    assertEquals("second commit", secondCommit.getFullMessage());
    secondCommit.getParent(0).equals(firstCommit);
    assertEquals("The Author", secondCommit.getAuthorIdent().getName());
    assertEquals("The.author@some.com", secondCommit.getAuthorIdent().getEmailAddress());
    assertEquals("The Commiter", secondCommit.getCommitterIdent().getName());
    assertEquals("The.committer@some.com", secondCommit.getCommitterIdent().getEmailAddress());
}
Also used : IFile(org.eclipse.core.resources.IFile) Git(org.eclipse.jgit.api.Git) CommitOperation(org.eclipse.egit.core.op.CommitOperation) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 17 with CommitOperation

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

the class CommitOperationTest method testCommitStaged.

@Test
public void testCommitStaged() throws Exception {
    IFile fileA = testUtils.addFileToProject(project.getProject(), "foo/a.txt", "some text");
    IFile fileB = testUtils.addFileToProject(project.getProject(), "foo/b.txt", "some text");
    IFile[] filesToCommit = { fileA, fileB };
    CommitOperation commitOperation = new CommitOperation(filesToCommit, Arrays.asList(filesToCommit), TestUtils.AUTHOR, TestUtils.COMMITTER, "first commit");
    commitOperation.execute(null);
    testUtils.changeContentOfFile(project.getProject(), fileA, "new content of A");
    testUtils.changeContentOfFile(project.getProject(), fileB, "new content of B");
    resources.add(fileA);
    resources.add(fileB);
    new AddToIndexOperation(resources).execute(null);
    commitOperation = new CommitOperation(filesToCommit, EMPTY_FILE_LIST, TestUtils.AUTHOR, TestUtils.COMMITTER, "second commit");
    commitOperation.execute(null);
    testUtils.assertRepositoryContainsFilesWithContent(repository, "foo/a.txt", "new content of A", "foo/b.txt", "new content of B");
}
Also used : IFile(org.eclipse.core.resources.IFile) CommitOperation(org.eclipse.egit.core.op.CommitOperation) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Test(org.junit.Test)

Example 18 with CommitOperation

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

the class PushOperationTest method testPush.

/**
 * Push from repository1 "master" into "test" of repository2.
 *
 * @throws Exception
 */
@Test
public void testPush() throws Exception {
    // push from repository1 to repository2
    PushOperation pop = createPushOperation();
    pop.run(new NullProgressMonitor());
    assertEquals(Status.UP_TO_DATE, getStatus(pop.getOperationResult()));
    // let's add a new file to the project shared with repository1
    IProject proj = importProject(repository1, projectName);
    ArrayList<IFile> files = new ArrayList<IFile>();
    IFile newFile = testUtils.addFileToProject(proj, "folder2/file2.txt", "New file");
    files.add(newFile);
    IFile[] fileArr = files.toArray(new IFile[files.size()]);
    AddToIndexOperation trop = new AddToIndexOperation(files);
    trop.execute(null);
    CommitOperation cop = new CommitOperation(fileArr, files, TestUtils.AUTHOR, TestUtils.COMMITTER, "Added file");
    cop.execute(null);
    proj.delete(false, false, null);
    pop = createPushOperation();
    pop.run(null);
    assertEquals(Status.OK, getStatus(pop.getOperationResult()));
    try {
        // assert that we cannot run this again
        pop.run(null);
        fail("Expected Exception not thrown");
    } catch (IllegalStateException e) {
    // expected
    }
    pop = createPushOperation();
    pop.run(null);
    assertEquals(Status.UP_TO_DATE, getStatus(pop.getOperationResult()));
    String newFilePath = newFile.getFullPath().toOSString();
    File testFile = new File(workdir2, newFilePath);
    assertFalse(testFile.exists());
    testFile = new File(workdir, newFilePath);
    assertTrue(testFile.exists());
    // check out test and verify the file is there
    BranchOperation bop = new BranchOperation(repository2.getRepository(), "refs/heads/test");
    bop.execute(null);
    testFile = new File(workdir2, newFilePath);
    assertTrue(testFile.exists());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) BranchOperation(org.eclipse.egit.core.op.BranchOperation) ArrayList(java.util.ArrayList) PushOperation(org.eclipse.egit.core.op.PushOperation) IProject(org.eclipse.core.resources.IProject) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) CommitOperation(org.eclipse.egit.core.op.CommitOperation) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Test(org.junit.Test)

Example 19 with CommitOperation

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

the class StagingView method commit.

private void commit(boolean pushUpstream) {
    if (!isCommitWithoutFilesAllowed()) {
        MessageDialog md = new MessageDialog(getSite().getShell(), UIText.StagingView_committingNotPossible, null, UIText.StagingView_noStagedFiles, MessageDialog.ERROR, new String[] { IDialogConstants.CLOSE_LABEL }, 0);
        md.open();
        return;
    }
    if (!commitMessageComponent.checkCommitInfo())
        return;
    if (!UIUtils.saveAllEditors(currentRepository, UIText.StagingView_cancelCommitAfterSaving))
        return;
    String commitMessage = commitMessageComponent.getCommitMessage();
    CommitOperation commitOperation = null;
    try {
        commitOperation = new CommitOperation(currentRepository, commitMessageComponent.getAuthor(), commitMessageComponent.getCommitter(), commitMessage);
    } catch (CoreException e) {
        Activator.handleError(UIText.StagingView_commitFailed, e, true);
        return;
    }
    if (amendPreviousCommitAction.isChecked())
        commitOperation.setAmending(true);
    final boolean gerritMode = addChangeIdAction.isChecked();
    commitOperation.setComputeChangeId(gerritMode);
    PushMode pushMode = null;
    if (pushUpstream) {
        pushMode = gerritMode ? PushMode.GERRIT : PushMode.UPSTREAM;
    }
    Job commitJob = new CommitJob(currentRepository, commitOperation).setOpenCommitEditor(openNewCommitsAction.isChecked()).setPushUpstream(pushMode);
    // don't allow to do anything as long as commit is in progress
    enableAllWidgets(false);
    commitJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void done(IJobChangeEvent event) {
            asyncExec(() -> {
                enableAllWidgets(true);
                if (event.getResult().isOK()) {
                    commitMessageText.setText(EMPTY_STRING);
                }
            });
        }
    });
    schedule(commitJob, true);
    CommitMessageHistory.saveCommitHistory(commitMessage);
    clearCommitMessageToggles();
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) CommitOperation(org.eclipse.egit.core.op.CommitOperation) CommitJob(org.eclipse.egit.ui.internal.commit.CommitJob) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) WorkbenchJob(org.eclipse.ui.progress.WorkbenchJob) CommitJob(org.eclipse.egit.ui.internal.commit.CommitJob) Job(org.eclipse.core.runtime.jobs.Job) PushMode(org.eclipse.egit.ui.internal.push.PushMode)

Example 20 with CommitOperation

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

the class CommitUI method commit.

/**
 * Performs a commit
 *
 * @return true if a commit operation was triggered
 */
public boolean commit() {
    // ask the user if they want to save or abort
    if (!UIUtils.saveAllEditors(repo))
        return false;
    BasicConfigurationDialog.show(new Repository[] { repo });
    resetState();
    final IProject[] projects = getProjectsOfRepositories();
    try {
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    buildIndexHeadDiffList(projects, monitor);
                } catch (IOException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        Activator.handleError(UIText.CommitAction_errorComputingDiffs, e.getCause(), true);
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    CommitHelper commitHelper = new CommitHelper(repo);
    if (!commitHelper.canCommit()) {
        MessageDialog.openError(shell, UIText.CommitAction_cannotCommit, commitHelper.getCannotCommitMessage());
        return false;
    }
    boolean amendAllowed = commitHelper.amendAllowed();
    if (files.isEmpty()) {
        if (amendAllowed && commitHelper.getPreviousCommit() != null) {
            boolean result = MessageDialog.openQuestion(shell, UIText.CommitAction_noFilesToCommit, UIText.CommitAction_amendCommit);
            if (!result)
                return false;
            amending = true;
        } else {
            MessageDialog.openWarning(shell, UIText.CommitAction_noFilesToCommit, UIText.CommitAction_amendNotPossible);
            return false;
        }
    }
    CommitDialog commitDialog = new CommitDialog(shell);
    commitDialog.setAmending(amending);
    commitDialog.setAmendAllowed(amendAllowed);
    commitDialog.setFiles(repo, files, indexDiff);
    commitDialog.setPreselectedFiles(getSelectedFiles(repo, files, selectedResources));
    commitDialog.setPreselectAll(preselectAll);
    commitDialog.setAuthor(commitHelper.getAuthor());
    commitDialog.setCommitter(commitHelper.getCommitter());
    commitDialog.setAllowToChangeSelection(!commitHelper.isMergedResolved && !commitHelper.isCherryPickResolved);
    commitDialog.setCommitMessage(commitHelper.getCommitMessage());
    if (commitDialog.open() != IDialogConstants.OK_ID)
        return false;
    final CommitOperation commitOperation;
    try {
        commitOperation = new CommitOperation(repo, commitDialog.getSelectedFiles(), notTracked, commitDialog.getAuthor(), commitDialog.getCommitter(), commitDialog.getCommitMessage());
    } catch (CoreException e1) {
        Activator.handleError(UIText.CommitUI_commitFailed, e1, true);
        return false;
    }
    if (commitDialog.isAmending())
        commitOperation.setAmending(true);
    final boolean gerritMode = commitDialog.getCreateChangeId();
    PushMode pushMode = null;
    if (commitDialog.isPushRequested()) {
        pushMode = gerritMode ? PushMode.GERRIT : PushMode.UPSTREAM;
    }
    commitOperation.setComputeChangeId(gerritMode);
    commitOperation.setCommitAll(commitHelper.isMergedResolved);
    if (commitHelper.isMergedResolved)
        commitOperation.setRepository(repo);
    Job commitJob = new CommitJob(repo, commitOperation).setPushUpstream(pushMode);
    commitJob.schedule();
    return true;
}
Also used : CommitDialog(org.eclipse.egit.ui.internal.dialogs.CommitDialog) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) PushMode(org.eclipse.egit.ui.internal.push.PushMode) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) CommitOperation(org.eclipse.egit.core.op.CommitOperation) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

CommitOperation (org.eclipse.egit.core.op.CommitOperation)22 IFile (org.eclipse.core.resources.IFile)16 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)9 IProject (org.eclipse.core.resources.IProject)7 CoreException (org.eclipse.core.runtime.CoreException)6 AddToIndexOperation (org.eclipse.egit.core.op.AddToIndexOperation)6 File (java.io.File)5 IOException (java.io.IOException)4 Repository (org.eclipse.jgit.lib.Repository)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ConnectProviderOperation (org.eclipse.egit.core.op.ConnectProviderOperation)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IFolder (org.eclipse.core.resources.IFolder)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 Job (org.eclipse.core.runtime.jobs.Job)2 IndexDiffCache (org.eclipse.egit.core.internal.indexdiff.IndexDiffCache)2 BranchOperation (org.eclipse.egit.core.op.BranchOperation)2