Search in sources :

Example 21 with AddToIndexOperation

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

the class GitMoveDeleteHookTest method testMoveFile.

@Theory
public void testMoveFile(boolean autoStageMoves) throws Exception {
    configureAutoStageMoves(autoStageMoves);
    TestProject project = initRepoInsideProjectInsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "file2.txt", "some  more text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] { project.getProject().getFile("file.txt"), project.getProject().getFile("file2.txt") });
    addToIndexOperation.execute(null);
    // Validate pre-conditions
    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry("file.txt"));
    assertNotNull(dirCache.getEntry("file2.txt"));
    assertNull(dirCache.getEntry("data.txt"));
    assertFalse(project.getProject().getFile("data.txt").exists());
    ObjectId oldContentId = dirCache.getEntry("file.txt").getObjectId();
    // Modify the content before the move
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("file.txt"), "other text");
    project.getProject().getFile("file.txt").move(project.getProject().getFile("data.txt").getFullPath(), false, null);
    dirCache.read();
    assertTrue(project.getProject().getFile("data.txt").exists());
    if (autoStageMoves) {
        assertNotNull(dirCache.getEntry("data.txt"));
        // Same content in index as before the move
        assertEquals(oldContentId, dirCache.getEntry("data.txt").getObjectId());
    } else {
        assertNull(dirCache.getEntry("data.txt"));
    }
    // Not moved file still in its old place
    assertNotNull(dirCache.getEntry("file2.txt"));
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) TestProject(org.eclipse.egit.core.test.TestProject) ObjectId(org.eclipse.jgit.lib.ObjectId) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Theory(org.junit.experimental.theories.Theory)

Example 22 with AddToIndexOperation

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

the class AddOperationTest method testTrackFilesInFolder.

@Test
public void testTrackFilesInFolder() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "sub/a.txt", "some text");
    IFile file2 = testUtils.addFileToProject(project.getProject(), "sub/b.txt", "some text");
    assertFalse(testRepository.inIndex(file1.getLocation().toPortableString()));
    assertFalse(testRepository.inIndex(file2.getLocation().toPortableString()));
    resources.add(project.getProject().getFolder("sub"));
    new AddToIndexOperation(resources).execute(null);
    assertTrue(testRepository.inIndex(file1.getLocation().toPortableString()));
    assertTrue(testRepository.inIndex(file2.getLocation().toPortableString()));
}
Also used : IFile(org.eclipse.core.resources.IFile) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Test(org.junit.Test)

Example 23 with AddToIndexOperation

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

the class AddOperationTest method testTrackFile.

@Test
public void testTrackFile() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "a.txt", "some text");
    assertFalse(testRepository.inIndex(file1.getLocation().toPortableString()));
    resources.add(file1);
    new AddToIndexOperation(resources).execute(null);
    assertTrue(testRepository.inIndex(file1.getLocation().toPortableString()));
}
Also used : IFile(org.eclipse.core.resources.IFile) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Test(org.junit.Test)

Example 24 with AddToIndexOperation

use of org.eclipse.egit.core.op.AddToIndexOperation 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 25 with AddToIndexOperation

use of org.eclipse.egit.core.op.AddToIndexOperation 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)

Aggregations

AddToIndexOperation (org.eclipse.egit.core.op.AddToIndexOperation)35 Test (org.junit.Test)26 IFile (org.eclipse.core.resources.IFile)25 TestProject (org.eclipse.egit.core.test.TestProject)9 DirCache (org.eclipse.jgit.dircache.DirCache)9 RemoveFromIndexOperation (org.eclipse.egit.core.op.RemoveFromIndexOperation)7 CommitOperation (org.eclipse.egit.core.op.CommitOperation)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ArrayList (java.util.ArrayList)5 IProject (org.eclipse.core.resources.IProject)5 TestRepository (org.eclipse.egit.core.test.TestRepository)5 IProjectDescription (org.eclipse.core.resources.IProjectDescription)4 IResource (org.eclipse.core.resources.IResource)4 CoreException (org.eclipse.core.runtime.CoreException)4 IPath (org.eclipse.core.runtime.IPath)4 Repository (org.eclipse.jgit.lib.Repository)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 File (java.io.File)3 IFolder (org.eclipse.core.resources.IFolder)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3