Search in sources :

Example 26 with AddToIndexOperation

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

Example 27 with AddToIndexOperation

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

the class CommitOperationTest method testCommitAll.

@Test
public void testCommitAll() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "sub/a.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "sub/b.txt", "some text");
    resources.add(project.getProject().getFolder("sub"));
    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 firstCommit = commits.next();
    assertTrue(firstCommit.getCommitTime() > 0);
    assertEquals("first commit", firstCommit.getFullMessage());
    testUtils.changeContentOfFile(project.getProject(), file1, "changed text");
    commitOperation = new CommitOperation(null, null, TestUtils.AUTHOR, TestUtils.COMMITTER, "second commit");
    commitOperation.setCommitAll(true);
    commitOperation.setRepository(repository);
    commitOperation.execute(null);
    try (Git git = new Git(repository)) {
        commits = git.log().call().iterator();
    }
    RevCommit secondCommit = commits.next();
    assertTrue(secondCommit.getCommitTime() > 0);
    assertEquals("second commit", secondCommit.getFullMessage());
    secondCommit.getParent(0).equals(firstCommit);
    assertEquals("The Author", secondCommit.getAuthorIdent().getName());
    assertEquals("The.author@some.com", secondCommit.getAuthorIdent().getEmailAddress());
    assertEquals("The Commiter", secondCommit.getCommitterIdent().getName());
    assertEquals("The.committer@some.com", secondCommit.getCommitterIdent().getEmailAddress());
}
Also used : IFile(org.eclipse.core.resources.IFile) Git(org.eclipse.jgit.api.Git) CommitOperation(org.eclipse.egit.core.op.CommitOperation) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 28 with AddToIndexOperation

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

the class CommitOperationTest method testCommitStaged.

@Test
public void testCommitStaged() 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);
    commitOperation = new CommitOperation(filesToCommit, 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", "new content of B");
}
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 29 with AddToIndexOperation

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

the class PushOperationTest method testPush.

/**
 * Push from repository1 "master" into "test" of repository2.
 *
 * @throws Exception
 */
@Test
public void testPush() throws Exception {
    // push from repository1 to repository2
    PushOperation pop = createPushOperation();
    pop.run(new NullProgressMonitor());
    assertEquals(Status.UP_TO_DATE, getStatus(pop.getOperationResult()));
    // let's add a new file to the project shared with repository1
    IProject proj = importProject(repository1, projectName);
    ArrayList<IFile> files = new ArrayList<IFile>();
    IFile newFile = testUtils.addFileToProject(proj, "folder2/file2.txt", "New file");
    files.add(newFile);
    IFile[] fileArr = files.toArray(new IFile[files.size()]);
    AddToIndexOperation trop = new AddToIndexOperation(files);
    trop.execute(null);
    CommitOperation cop = new CommitOperation(fileArr, files, TestUtils.AUTHOR, TestUtils.COMMITTER, "Added file");
    cop.execute(null);
    proj.delete(false, false, null);
    pop = createPushOperation();
    pop.run(null);
    assertEquals(Status.OK, getStatus(pop.getOperationResult()));
    try {
        // assert that we cannot run this again
        pop.run(null);
        fail("Expected Exception not thrown");
    } catch (IllegalStateException e) {
    // expected
    }
    pop = createPushOperation();
    pop.run(null);
    assertEquals(Status.UP_TO_DATE, getStatus(pop.getOperationResult()));
    String newFilePath = newFile.getFullPath().toOSString();
    File testFile = new File(workdir2, newFilePath);
    assertFalse(testFile.exists());
    testFile = new File(workdir, newFilePath);
    assertTrue(testFile.exists());
    // check out test and verify the file is there
    BranchOperation bop = new BranchOperation(repository2.getRepository(), "refs/heads/test");
    bop.execute(null);
    testFile = new File(workdir2, newFilePath);
    assertTrue(testFile.exists());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) BranchOperation(org.eclipse.egit.core.op.BranchOperation) ArrayList(java.util.ArrayList) PushOperation(org.eclipse.egit.core.op.PushOperation) IProject(org.eclipse.core.resources.IProject) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) CommitOperation(org.eclipse.egit.core.op.CommitOperation) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Test(org.junit.Test)

Example 30 with AddToIndexOperation

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

the class RemoveFromIndexOperationTest method shouldUnTrackFilesInFolder.

@Test
public void shouldUnTrackFilesInFolder() throws Exception {
    // given
    IFile file1 = createFileInRepo("sub/a.txt");
    IFile file2 = createFileInRepo("sub/b.txt");
    IFolder container = project.getProject().getFolder("sub");
    new AddToIndexOperation(asList(file1, file2)).execute(null);
    // when
    new RemoveFromIndexOperation(asList(container.getLocation())).execute(null);
    // then
    assertTrue(testRepo.removedFromIndex(file1.getLocation().toPortableString()));
    assertTrue(testRepo.removedFromIndex(file2.getLocation().toPortableString()));
}
Also used : IFile(org.eclipse.core.resources.IFile) RemoveFromIndexOperation(org.eclipse.egit.core.op.RemoveFromIndexOperation) IFolder(org.eclipse.core.resources.IFolder) 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