Search in sources :

Example 11 with AddToIndexOperation

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

the class GitMoveDeleteHookTest method dotestMoveProjectWithinRepo.

private void dotestMoveProjectWithinRepo(String srcParent, String srcProjectName, String dstParent, String dstProjecName, String gitDir, boolean sourceInsideWs, boolean autoStageMoves) throws Exception {
    configureAutoStageMoves(autoStageMoves);
    String gdRelativeSrcParent = srcParent + srcProjectName + "/";
    if (gdRelativeSrcParent.startsWith(gitDir))
        gdRelativeSrcParent = gdRelativeSrcParent.substring(gitDir.length());
    testDirs.add(new File(dstParent));
    String gdRelativeDstParent = dstParent + dstProjecName + "/";
    if (gdRelativeDstParent.startsWith(gitDir))
        gdRelativeDstParent = gdRelativeDstParent.substring(gitDir.length());
    registerWorkspaceRelativeTestDirProject(srcParent, srcProjectName);
    registerWorkspaceRelativeTestDirProject(dstParent, dstProjecName);
    // Old cruft may be laying around
    TestProject project = initRepoAboveProject(srcParent, gitDir, sourceInsideWs);
    IProject project0 = project.getProject().getWorkspace().getRoot().getProject(dstProjecName);
    project0.delete(true, null);
    testUtils.addFileToProject(project.getProject(), "file.txt", "some text");
    AddToIndexOperation addToIndexOperation = new AddToIndexOperation(new IResource[] { project.getProject().getFile("file.txt") });
    addToIndexOperation.execute(null);
    // Check condition before move
    DirCache dirCache = DirCache.read(repository.getIndexFile(), FS.DETECTED);
    assertNotNull(dirCache.getEntry(gdRelativeSrcParent + "file.txt"));
    ObjectId oldContentId = dirCache.getEntry(gdRelativeSrcParent + "file.txt").getObjectId();
    // Modify the content before the move, we want to see the staged content
    // as it was before the move in the index
    testUtils.changeContentOfFile(project.getProject(), project.getProject().getFile("file.txt"), "other text");
    IProjectDescription description = project.getProject().getDescription();
    description.setName(dstProjecName);
    if (sourceInsideWs)
        if (dstParent.length() > 0)
            description.setLocationURI(URIUtil.toURI(project.getProject().getWorkspace().getRoot().getLocation().append(dstParent + dstProjecName)));
        else
            description.setLocationURI(null);
    else
        description.setLocationURI(URIUtil.toURI(new Path(workspaceSupplement + "/" + dstParent + "/" + dstProjecName)));
    project.getProject().move(description, IResource.FORCE | IResource.SHALLOW, null);
    IProject project2 = project.getProject().getWorkspace().getRoot().getProject(dstProjecName);
    assertTrue(project2.exists());
    assertNotNull(RepositoryMapping.getMapping(project2));
    // Check that our file exists on disk has a new location in the index
    dirCache.read();
    assertTrue(project2.getFile("file.txt").exists());
    if (autoStageMoves) {
        assertNotNull(dirCache.getEntry(gdRelativeDstParent + "file.txt"));
        // Same content in index as before the move, i.e. not same as on
        // disk
        assertEquals(oldContentId, dirCache.getEntry(gdRelativeDstParent + "file.txt").getObjectId());
    } else {
        assertNull(dirCache.getEntry(gdRelativeDstParent + "file.txt"));
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DirCache(org.eclipse.jgit.dircache.DirCache) TestProject(org.eclipse.egit.core.test.TestProject) ObjectId(org.eclipse.jgit.lib.ObjectId) IProjectDescription(org.eclipse.core.resources.IProjectDescription) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IProject(org.eclipse.core.resources.IProject) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation)

Example 12 with AddToIndexOperation

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

the class GitMoveDeleteHookTest method testMoveAndRenameProjectContainingGitRepo.

/**
 * Rename and move a project in the workspace containing a Git repository.
 * <p>
 * The repository will be moved with the project.
 * Note that there is no way to rename a project in the workspace without
 * moving it. See https://bugs.eclipse.org/358828 for a discussion.
 *
 * @throws Exception
 */
@Test
public void testMoveAndRenameProjectContainingGitRepo() throws Exception {
    ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").delete(true, null);
    ResourcesPlugin.getWorkspace().getRoot().getProject("P2").delete(true, null);
    TestProject project = initRepoInsideProjectInsideWorkspace();
    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");
    registerWorkspaceRelativeTestDir("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("P2", 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)

Example 13 with AddToIndexOperation

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

the class GitSubscriberMergeContext method markAsMerged.

@Override
public void markAsMerged(IDiff node, boolean inSyncHint, IProgressMonitor monitor) throws CoreException {
    IResource resource = getDiffTree().getResource(node);
    AddToIndexOperation operation = new AddToIndexOperation(new IResource[] { resource });
    operation.execute(monitor);
}
Also used : IResource(org.eclipse.core.resources.IResource) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation)

Example 14 with AddToIndexOperation

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

the class AddToIndexActionHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IResource[] sel = getSelectedResources(event);
    if (sel.length == 0)
        return null;
    IResource[] resourcesInScope;
    try {
        IWorkbenchPart part = getPart(event);
        resourcesInScope = GitScopeUtil.getRelatedChanges(part, sel);
    } catch (InterruptedException e) {
        // cancels the scope operation
        return null;
    }
    final AddToIndexOperation operation = new AddToIndexOperation(resourcesInScope);
    String jobname = UIText.AddToIndexAction_addingFiles;
    Job job = new Job(jobname) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                operation.execute(monitor);
            } catch (CoreException e) {
                return Activator.createErrorStatus(e.getStatus().getMessage(), e);
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            if (JobFamilies.ADD_TO_INDEX.equals(family))
                return true;
            return super.belongsTo(family);
        }
    };
    job.setUser(true);
    job.setRule(operation.getSchedulingRule());
    job.schedule();
    return null;
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) Job(org.eclipse.core.runtime.jobs.Job) IResource(org.eclipse.core.resources.IResource) AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation)

Example 15 with AddToIndexOperation

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

the class EGitUtils method addToRepository.

public static void addToRepository(IProject project, Repository repository, IProgressMonitor monitor) throws CoreException {
    AddToIndexOperation add = new AddToIndexOperation(Collections.singletonList(project));
    add.execute(monitor);
}
Also used : AddToIndexOperation(org.eclipse.egit.core.op.AddToIndexOperation)

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