Search in sources :

Example 11 with CommitOperation

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();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) SWTBot(org.eclipse.swtbot.swt.finder.SWTBot) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SWTBotToolbarDropDownButton(org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarDropDownButton) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CommitOperation(org.eclipse.egit.core.op.CommitOperation) SWTBotTreeItem(org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem) SWTBotShell(org.eclipse.swtbot.swt.finder.widgets.SWTBotShell) IFile(org.eclipse.core.resources.IFile) File(java.io.File) ICondition(org.eclipse.swtbot.swt.finder.waits.ICondition) TimeoutException(org.eclipse.swtbot.swt.finder.widgets.TimeoutException) Test(org.junit.Test)

Example 12 with CommitOperation

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;
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) IndexDiffCache(org.eclipse.egit.core.internal.indexdiff.IndexDiffCache) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) Repository(org.eclipse.jgit.lib.Repository) IJavaProject(org.eclipse.jdt.core.IJavaProject) CommitOperation(org.eclipse.egit.core.op.CommitOperation) ConnectProviderOperation(org.eclipse.egit.core.op.ConnectProviderOperation) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Example 13 with CommitOperation

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");
}
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 14 with CommitOperation

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());
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) CommitOperation(org.eclipse.egit.core.op.CommitOperation) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Test(org.junit.Test)

Example 15 with CommitOperation

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());
    }
}
Also used : Git(org.eclipse.jgit.api.Git) IFile(org.eclipse.core.resources.IFile) CommitOperation(org.eclipse.egit.core.op.CommitOperation) ArrayList(java.util.ArrayList) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

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