Search in sources :

Example 1 with DiscardChangesOperation

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

the class DiscardChangesOperationTest method testDiscardChanges.

@Test
public void testDiscardChanges() throws Exception {
    IFile file1 = project.getFile(new Path("folder1/file1.txt"));
    String contents = testUtils.slurpAndClose(file1.getContents());
    assertEquals("Hello world 1", contents);
    setNewFileContent(file1, "changed 1");
    IFile file2 = project.getFile(new Path("folder1/file2.txt"));
    contents = testUtils.slurpAndClose(file2.getContents());
    assertEquals("Hello world 2", contents);
    setNewFileContent(file2, "changed 2");
    DiscardChangesOperation dcop = new DiscardChangesOperation(new IResource[] { file1, file2 });
    dcop.execute(new NullProgressMonitor());
    contents = testUtils.slurpAndClose(file1.getContents());
    assertEquals("Hello world 1", contents);
    contents = testUtils.slurpAndClose(file2.getContents());
    assertEquals("Hello world 2", contents);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) DiscardChangesOperation(org.eclipse.egit.core.op.DiscardChangesOperation) Test(org.junit.Test)

Example 2 with DiscardChangesOperation

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

the class DiscardChangesOperationTest method shouldWorkWhenProjectIsRootOfRepository.

@Test
public void shouldWorkWhenProjectIsRootOfRepository() throws Exception {
    IFile file = project2.getFile(new Path("file.txt"));
    String contents = testUtils.slurpAndClose(file.getContents());
    assertEquals("initial", contents);
    setNewFileContent(file, "changed");
    DiscardChangesOperation dcop = new DiscardChangesOperation(new IResource[] { project2 });
    dcop.execute(new NullProgressMonitor());
    String replacedContents = testUtils.slurpAndClose(file.getContents());
    assertEquals("initial", replacedContents);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) DiscardChangesOperation(org.eclipse.egit.core.op.DiscardChangesOperation) Test(org.junit.Test)

Example 3 with DiscardChangesOperation

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

the class DiscardChangesActionHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    // capture selection from active part as long as we have context
    mySelection = getSelection(event);
    try {
        IWorkbenchPart part = getPart(event);
        String question = UIText.DiscardChangesAction_confirmActionMessage;
        ILaunchConfiguration launch = LaunchFinder.getRunningLaunchConfiguration(Arrays.asList(getRepositories()), null);
        if (launch != null) {
            question = MessageFormat.format(question, "\n\n" + // $NON-NLS-1$
            MessageFormat.format(UIText.LaunchFinder_RunningLaunchMessage, launch.getName()));
        } else {
            // $NON-NLS-1$
            question = MessageFormat.format(question, "");
        }
        boolean performAction = openConfirmationDialog(event, question);
        if (!performAction) {
            return null;
        }
        final DiscardChangesOperation operation = createOperation(part, event);
        if (operation == null) {
            return null;
        }
        String jobname = UIText.DiscardChangesAction_discardChanges;
        Job job = new WorkspaceJob(jobname) {

            @Override
            public IStatus runInWorkspace(IProgressMonitor monitor) {
                try {
                    operation.execute(monitor);
                } catch (CoreException e) {
                    return Activator.createErrorStatus(e.getStatus().getMessage(), e);
                }
                return Status.OK_STATUS;
            }

            @Override
            public boolean belongsTo(Object family) {
                if (JobFamilies.DISCARD_CHANGES.equals(family)) {
                    return true;
                }
                return super.belongsTo(family);
            }
        };
        job.setUser(true);
        job.setRule(operation.getSchedulingRule());
        job.schedule();
        return null;
    } finally {
        // cleanup mySelection to avoid side effects later after execution
        mySelection = null;
    }
}
Also used : ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) DiscardChangesOperation(org.eclipse.egit.core.op.DiscardChangesOperation) Job(org.eclipse.core.runtime.jobs.Job) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob)

Example 4 with DiscardChangesOperation

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

the class DiscardChangesOperationTest method testDiscardChangesWithPath.

@Test
public void testDiscardChangesWithPath() throws Exception {
    IFile file1 = project.getFile(new Path("folder1/file1.txt"));
    setNewFileContent(file1, "changed 1");
    DiscardChangesOperation operation = new DiscardChangesOperation(Arrays.asList(file1.getLocation()));
    operation.execute(new NullProgressMonitor());
    assertEquals("Hello world 1", testUtils.slurpAndClose(file1.getContents()));
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) DiscardChangesOperation(org.eclipse.egit.core.op.DiscardChangesOperation) Test(org.junit.Test)

Example 5 with DiscardChangesOperation

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

the class DiscardChangesOperationTest method testDiscardChangesWithStage.

@Test
public void testDiscardChangesWithStage() throws Exception {
    Git git = Git.wrap(repository1.getRepository());
    File file = new File(repository1.getRepository().getWorkTree(), "conflict.txt");
    repository1.appendFileContent(file, "base", false);
    git.add().addFilepattern("conflict.txt").call();
    git.commit().setMessage("commit").call();
    git.checkout().setCreateBranch(true).setName("side").call();
    repository1.appendFileContent(file, "side", false);
    git.add().addFilepattern("conflict.txt").call();
    RevCommit side = git.commit().setMessage("commit on side").call();
    git.checkout().setName("master").call();
    repository1.appendFileContent(file, "master", false);
    git.add().addFilepattern("conflict.txt").call();
    git.commit().setMessage("commit on master").call();
    git.merge().include(side).call();
    DirCache dirCache = repository1.getRepository().readDirCache();
    assertEquals(1, dirCache.getEntry("conflict.txt").getStage());
    IPath path = new Path(file.getAbsolutePath());
    DiscardChangesOperation operation = new DiscardChangesOperation(Arrays.asList(path));
    operation.setStage(Stage.THEIRS);
    operation.execute(new NullProgressMonitor());
    DirCache dirCacheAfter = repository1.getRepository().readDirCache();
    assertEquals("Expected index to be unmodified", 1, dirCacheAfter.getEntry("conflict.txt").getStage());
    assertEquals("side", new String(IO.readFully(file), "UTF-8"));
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DirCache(org.eclipse.jgit.dircache.DirCache) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Git(org.eclipse.jgit.api.Git) IPath(org.eclipse.core.runtime.IPath) DiscardChangesOperation(org.eclipse.egit.core.op.DiscardChangesOperation) File(java.io.File) IFile(org.eclipse.core.resources.IFile) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

DiscardChangesOperation (org.eclipse.egit.core.op.DiscardChangesOperation)5 IFile (org.eclipse.core.resources.IFile)4 IPath (org.eclipse.core.runtime.IPath)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 Path (org.eclipse.core.runtime.Path)4 Test (org.junit.Test)4 File (java.io.File)1 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Job (org.eclipse.core.runtime.jobs.Job)1 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)1 Git (org.eclipse.jgit.api.Git)1 DirCache (org.eclipse.jgit.dircache.DirCache)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)1