use of org.eclipse.egit.core.op.CommitOperation in project egit by eclipse.
the class BranchAndResetActionTest method testCheckoutWithNonDeleted.
@Test
public void testCheckoutWithNonDeleted() throws Exception {
// we need to check if this file system has problems to
// delete a file with an open FileInputStrem
IFile test = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1).getFolder(FOLDER).getFile("temp.txt");
test.create(new ByteArrayInputStream(new byte[0]), false, null);
File testFile = new File(test.getLocation().toString());
assertTrue(testFile.exists());
FileInputStream fis = new FileInputStream(testFile);
try {
FileUtils.delete(testFile);
return;
} catch (IOException e) {
// the test makes sense only if deletion of
// a file with open stream fails
} finally {
fis.close();
if (testFile.exists())
FileUtils.delete(testFile);
}
final Image folderImage = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
final Image projectImage = PlatformUI.getWorkbench().getSharedImages().getImage(SharedImages.IMG_OBJ_PROJECT);
// checkout stable
checkoutAndVerify(new String[] { LOCAL_BRANCHES, "stable" });
// add a file
IFile toBeDeleted = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJ1).getFolder(FOLDER).getFile("ToBeDeleted");
toBeDeleted.create(new ByteArrayInputStream(new byte[0]), false, null);
ArrayList<IFile> untracked = new ArrayList<IFile>();
untracked.add(toBeDeleted);
// commit to stable
CommitOperation op = new CommitOperation(new IFile[] { toBeDeleted }, untracked, TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER, "Add to stable");
op.execute(null);
InputStream is = toBeDeleted.getContents();
try {
checkout(new String[] { LOCAL_BRANCHES, "master" });
final SWTBotShell showUndeleted = bot.shell(UIText.NonDeletedFilesDialog_NonDeletedFilesTitle);
// repo relative path
assertEquals("ToBeDeleted", showUndeleted.bot().tree().getAllItems()[0].getItems()[0].getItems()[0].getText());
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
assertSame(folderImage, showUndeleted.bot().tree().getAllItems()[0].widget.getImage());
}
});
SWTBotToolbarDropDownButton pathButton = showUndeleted.bot().toolbarDropDownButton();
pathButton.menuItem(UIText.NonDeletedFilesTree_FileSystemPathsButton).click();
// see http://www.eclipse.org/forums/index.php/t/159133/ why we need this
pathButton.pressShortcut(KeyStroke.getInstance("ESC"));
// fs path
IPath path = new Path(lookupRepository(repositoryFile).getWorkTree().getPath()).append(PROJ1).append(FOLDER).append("ToBeDeleted");
SWTBotTreeItem[] items = showUndeleted.bot().tree().getAllItems();
for (int i = 0; i < path.segmentCount(); i++) {
boolean found = false;
String segment = path.segment(i);
for (SWTBotTreeItem item : items) if (item.getText().equals(segment)) {
found = true;
items = item.getItems();
}
assertTrue(found);
}
pathButton.menuItem(UIText.NonDeletedFilesTree_ResourcePathsButton).click();
// see http://www.eclipse.org/forums/index.php/t/159133/ why we need this
pathButton.pressShortcut(KeyStroke.getInstance("ESC"));
// resource path
assertEquals("ToBeDeleted", showUndeleted.bot().tree().getAllItems()[0].getItems()[0].getItems()[0].getText());
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
assertSame(projectImage, showUndeleted.bot().tree().getAllItems()[0].widget.getImage());
}
});
ICondition treeEmpty = new ICondition() {
@Override
public boolean test() throws Exception {
return showUndeleted.bot().tree().getAllItems().length == 0;
}
@Override
public void init(SWTBot actBot) {
// nothing
}
@Override
public String getFailureMessage() {
return "Not deleted";
}
};
showUndeleted.bot().button(UIText.NonDeletedFilesDialog_RetryDeleteButton).click();
try {
showUndeleted.bot().waitUntil(treeEmpty, 1000, 100);
fail("Should have failed");
} catch (TimeoutException e) {
// expected
}
is.close();
showUndeleted.bot().button(UIText.NonDeletedFilesDialog_RetryDeleteButton).click();
showUndeleted.bot().waitUntil(treeEmpty, 1000, 100);
showUndeleted.close();
} finally {
is.close();
}
}
use of org.eclipse.egit.core.op.CommitOperation in project egit by eclipse.
the class ShowBlameActionHandlerTest method createJavaProjectAndCommitToRepository.
private IJavaProject createJavaProjectAndCommitToRepository() throws Exception {
Repository myRepository = createLocalTestRepository(REPO1);
File gitDir = myRepository.getDirectory();
IJavaProject jProject = createJavaProject(myRepository, JAVA_PROJECT_NAME);
IProject project = jProject.getProject();
try {
new ConnectProviderOperation(project, gitDir).execute(null);
} catch (Exception e) {
Activator.logError("Failed to connect project to repository", e);
}
assertConnected(project);
// Check in at least the java file
IFolder folder = project.getFolder(SRC_FOLDER_NAME).getFolder(PACKAGE_NAME);
IFile file = folder.getFile(JAVA_FILE_NAME);
IFile[] commitables = new IFile[] { file };
ArrayList<IFile> untracked = new ArrayList<IFile>();
untracked.addAll(Arrays.asList(commitables));
// commit to master
CommitOperation op = new CommitOperation(commitables, untracked, TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER, "Initial commit");
op.execute(null);
// Make sure cache entry is already listening for changes
IndexDiffCache cache = Activator.getDefault().getIndexDiffCache();
cache.getIndexDiffCacheEntry(lookupRepository(gitDir));
return jProject;
}
use of org.eclipse.egit.core.op.CommitOperation in project egit by eclipse.
the class CommitOperationTest method testCommitIndexSubset.
@Test
public void testCommitIndexSubset() 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);
IFile[] filesToCommit2 = { fileA };
commitOperation = new CommitOperation(filesToCommit2, 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", "some text");
}
use of org.eclipse.egit.core.op.CommitOperation in project egit by eclipse.
the class CommitOperationTest method testCommitAddedToIndexDeletedInWorkspace.
@Test
public void testCommitAddedToIndexDeletedInWorkspace() throws Exception {
testUtils.addFileToProject(project.getProject(), "foo/a.txt", "some text");
resources.add(project.getProject().getFolder("foo"));
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);
testUtils.addFileToProject(project.getProject(), "zar/b.txt", "some text");
resources.add(project.getProject().getFolder("zar"));
new AddToIndexOperation(resources).execute(null);
IFile zarFile = project.getProject().getFile("zar/b.txt");
IPath zarFilePath = zarFile.getLocation();
// delete file and refresh. Deleting using the resource would trigger
// GitMoveDeleteHook which removes the file from the index
assertTrue("could not delete file " + zarFilePath.toOSString(), zarFilePath.toFile().delete());
zarFile.refreshLocal(0, null);
assertFalse(project.getProject().getFile("zar/b.txt").exists());
IFile[] filesToCommit = new IFile[] { project.getProject().getFile("zar/b.txt") };
commitOperation = new CommitOperation(filesToCommit, null, TestUtils.AUTHOR, TestUtils.COMMITTER, "first commit");
commitOperation.setRepository(repository);
try {
commitOperation.execute(null);
// TODO this is very ugly. CommitCommand should be extended
// not to throw an JGitInternalException in case of an empty
// commit
fail("expected CoreException");
} catch (CoreException e) {
assertEquals("No changes", e.getCause().getMessage());
}
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(repository.resolve("HEAD^{tree}"));
assertTrue(treeWalk.next());
assertEquals("foo", treeWalk.getPathString());
treeWalk.enterSubtree();
assertTrue(treeWalk.next());
assertEquals("foo/a.txt", treeWalk.getPathString());
assertFalse(treeWalk.next());
}
}
use of org.eclipse.egit.core.op.CommitOperation in project egit by eclipse.
the class CommitOperationTest method testCommitEmptiedTree.
@Test
public void testCommitEmptiedTree() throws Exception {
// Set up a directory structure
testUtils.addFileToProject(project.getProject(), "sub1/a.txt", "some text");
testUtils.addFileToProject(project.getProject(), "sub2/b.txt", "some text");
resources.add(project.getProject().getFolder("sub1"));
resources.add(project.getProject().getFolder("sub2"));
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 secondCommit = commits.next();
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(secondCommit.getTree().getId());
treeWalk.setRecursive(true);
treeWalk.setPostOrderTraversal(true);
assertTrue(treeWalk.next());
assertEquals("sub1/a.txt", treeWalk.getPathString());
assertTrue(treeWalk.next());
assertEquals("sub1", treeWalk.getPathString());
assertTrue(treeWalk.next());
assertEquals("sub2/b.txt", treeWalk.getPathString());
assertTrue(treeWalk.next());
assertEquals("sub2", treeWalk.getPathString());
assertFalse(treeWalk.next());
}
project.getProject().getFolder("sub2").delete(IResource.FORCE, null);
IFile[] filesToCommit = { project.getProject().getFile("sub2/b.txt") };
ArrayList<IFile> notIndexed = new ArrayList<IFile>();
notIndexed.add(filesToCommit[0]);
ArrayList<IFile> notTracked = new ArrayList<IFile>();
commitOperation = new CommitOperation(filesToCommit, notTracked, TestUtils.AUTHOR, TestUtils.COMMITTER, "second commit");
commitOperation.setCommitAll(false);
commitOperation.execute(null);
try (Git git = new Git(repository)) {
commits = git.log().call().iterator();
}
secondCommit = commits.next();
try (TreeWalk treeWalk = new TreeWalk(repository)) {
treeWalk.addTree(secondCommit.getTree().getId());
treeWalk.setRecursive(true);
treeWalk.setPostOrderTraversal(true);
assertTrue(treeWalk.next());
assertEquals("sub1/a.txt", treeWalk.getPathString());
assertTrue(treeWalk.next());
assertEquals("sub1", treeWalk.getPathString());
assertFalse(treeWalk.next());
}
}
Aggregations