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);
}
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);
}
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;
}
}
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()));
}
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"));
}
Aggregations