Search in sources :

Example 6 with AddToIndexOperation

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

the class AddOperationTest method testAddFilesInFolder.

@Test
public void testAddFilesInFolder() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "sub/a.txt", "some text");
    IFile file2 = testUtils.addFileToProject(project.getProject(), "sub/b.txt", "some text");
    resources.add(project.getProject().getFolder("sub"));
    new AddToIndexOperation(resources).execute(null);
    testRepository.commit("first commit");
    assertEquals(file1.getLocalTimeStamp() / 1000, testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 1000);
    Thread.sleep(1000);
    file1.setContents(new ByteArrayInputStream("other text".getBytes(project.project.getDefaultCharset())), 0, null);
    file2.setContents(new ByteArrayInputStream("other text".getBytes(project.project.getDefaultCharset())), 0, null);
    assertFalse(file1.getLocalTimeStamp() == testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()));
    assertFalse(file2.getLocalTimeStamp() == testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()));
    new AddToIndexOperation(resources).execute(null);
    assertTrue(testRepository.inIndex(file1.getLocation().toPortableString()));
    assertTrue(testRepository.inIndex(file2.getLocation().toPortableString()));
    assertEquals(file1.getLocalTimeStamp() / 1000, testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 1000);
    assertEquals(file2.getLocalTimeStamp() / 1000, testRepository.lastModifiedInIndex(file2.getLocation().toPortableString()) / 1000);
}
Also used : IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Test(org.junit.Test)

Example 7 with AddToIndexOperation

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

the class AddOperationTest method testAddFile.

@Test
public void testAddFile() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "a.txt", "some text");
    resources.add(file1);
    new AddToIndexOperation(resources).execute(null);
    testRepository.commit("first commit");
    assertEquals(file1.getLocalTimeStamp() / 1000, testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 1000);
    Thread.sleep(1000);
    file1.setContents(new ByteArrayInputStream("other text".getBytes(project.project.getDefaultCharset())), 0, null);
    assertFalse(file1.getLocalTimeStamp() / 1000 == testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 1000);
    new AddToIndexOperation(resources).execute(null);
    assertTrue(testRepository.inIndex(file1.getLocation().toPortableString()));
    // does not work yet due to the racy git problem: DirCache.writeTo
    // smudges the
    // timestamp of an added file
    assertEquals(file1.getLocalTimeStamp() / 1000, testRepository.lastModifiedInIndex(file1.getLocation().toPortableString()) / 1000);
}
Also used : IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation) Test(org.junit.Test)

Example 8 with AddToIndexOperation

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

the class LocalRepositoryTestCase method stage.

protected static void stage(IFile file) throws Exception {
    ArrayList<IFile> unstaged = new ArrayList<IFile>();
    unstaged.addAll(Arrays.asList(new IFile[] { file }));
    AddToIndexOperation op = new AddToIndexOperation(unstaged);
    op.execute(null);
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation)

Example 9 with AddToIndexOperation

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

the class GitMoveDeleteHookTest method testMoveFolder.

/**
 * Rename "folder" to "dir".
 * @param autoStageMoves
 * @throws Exception
 */
@Theory
public void testMoveFolder(boolean autoStageMoves) throws Exception {
    configureAutoStageMoves(autoStageMoves);
    TestProject project = initRepoInsideProjectInsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "folder/file.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "folder2/file.txt", "some other text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] { project.getProject().getFile("folder/file.txt"), project.getProject().getFile("folder2/file.txt") });
    addToIndexOperation.execute(null);
    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry("folder/file.txt"));
    assertNotNull(dirCache.getEntry("folder2/file.txt"));
    assertNull(dirCache.getEntry("dir/file.txt"));
    assertFalse(project.getProject().getFile("dir/file.txt").exists());
    ObjectId oldContentId = dirCache.getEntry("folder/file.txt").getObjectId();
    // Modify the content before the move
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("folder/file.txt"), "other text");
    project.getProject().getFolder("folder").move(project.getProject().getFolder("dir").getFullPath(), false, null);
    dirCache.read();
    assertTrue(project.getProject().getFile("dir/file.txt").exists());
    if (autoStageMoves) {
        assertNull(dirCache.getEntry("folder/file.txt"));
        assertNotNull(dirCache.getEntry("dir/file.txt"));
        // Same content in index as before the move
        assertEquals(oldContentId, dirCache.getEntry("dir/file.txt").getObjectId());
    } else {
        assertNotNull(dirCache.getEntry("folder/file.txt"));
        assertNull(dirCache.getEntry("dir/file.txt"));
    }
    // Not moved file still there
    assertNotNull(dirCache.getEntry("folder2/file.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 10 with AddToIndexOperation

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

the class GitMoveDeleteHookTest method testRenameProjectOutsideWorkspaceContainingGitRepo.

/**
 * Rename a project outside the workspace containing a Git repository.
 * <p>
 * Note the similarity of the code with {@link #testMoveAndRenameProjectContainingGitRepo()}
 *
 * @throws Exception
 */
@Test
public void testRenameProjectOutsideWorkspaceContainingGitRepo() throws Exception {
    ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").delete(true, null);
    ResourcesPlugin.getWorkspace().getRoot().getProject("P2").delete(true, null);
    TestProject project = initRepoInsideProjectOutsideWorkspace();
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] { project.getProject().getFile("file.txt") });
    addToIndexOperation.execute(null);
    IProjectDescription description = project.getProject().getDescription();
    description.setName("P2");
    project.getProject().move(description, IResource.FORCE | IResource.SHALLOW, null);
    IProject project2 = ResourcesPlugin.getWorkspace().getRoot().getProject("P2");
    assertNotNull(RepositoryMapping.getMapping(project2.getProject()));
    Repository movedRepo = RepositoryMapping.getMapping(project2).getRepository();
    assertEquals("Project-1", movedRepo.getDirectory().getParentFile().getName());
    DirCache dc = movedRepo.readDirCache();
    assertEquals(1, dc.getEntryCount());
    assertEquals("file.txt", dc.getEntry(0).getPathString());
    assertFalse(ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").exists());
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) TestRepository(org.eclipse.egit.core.test.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestProject(org.eclipse.egit.core.test.TestProject) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IProject(org.eclipse.core.resources.IProject) 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