Search in sources :

Example 16 with ResourceMapping

use of org.eclipse.core.resources.mapping.ResourceMapping in project egit by eclipse.

the class GitResourceVariantTreeTest method collectResources.

private static Set<IResource> collectResources(ResourceMapping[] mappings) throws CoreException {
    final Set<IResource> resources = new HashSet<IResource>();
    ResourceMappingContext context = ResourceMappingContext.LOCAL_CONTEXT;
    for (ResourceMapping mapping : mappings) {
        ResourceTraversal[] traversals = mapping.getTraversals(context, new NullProgressMonitor());
        for (ResourceTraversal traversal : traversals) {
            resources.addAll(Arrays.asList(traversal.getResources()));
        }
    }
    return resources;
}
Also used : ResourceTraversal(org.eclipse.core.resources.mapping.ResourceTraversal) ResourceMappingContext(org.eclipse.core.resources.mapping.ResourceMappingContext) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 17 with ResourceMapping

use of org.eclipse.core.resources.mapping.ResourceMapping in project egit by eclipse.

the class GitResourceVariantTreeTest method shouldNotReturnNullOnSameResouceVariant.

@Test
public void shouldNotReturnNullOnSameResouceVariant() throws Exception {
    String modifiedFileName = "changingFile." + SampleModelProvider.SAMPLE_FILE_EXTENSION;
    String unchangedFileName = "notChangingFile." + SampleModelProvider.SAMPLE_FILE_EXTENSION;
    String removedFileName = "toBeRemovedFile." + SampleModelProvider.SAMPLE_FILE_EXTENSION;
    File modifiedFile = testRepo.createFile(iProject, modifiedFileName);
    File unchangedFile = testRepo.createFile(iProject, unchangedFileName);
    File removedFile = testRepo.createFile(iProject, removedFileName);
    testRepo.appendFileContent(modifiedFile, "My content is changing");
    testRepo.appendFileContent(unchangedFile, "My content is constant");
    testRepo.appendFileContent(removedFile, "I will be removed");
    IFile iModifiedFile = testRepo.getIFile(iProject, modifiedFile);
    IFile iUnchangedFile = testRepo.getIFile(iProject, unchangedFile);
    IFile iRemovedFile = testRepo.getIFile(iProject, removedFile);
    testRepo.trackAllFiles(iProject);
    RevCommit firstCommit = testRepo.commit("C1");
    testRepo.appendFileContent(modifiedFile, " My content has changed");
    testRepo.track(modifiedFile);
    testRepo.removeFromIndex(removedFile);
    RevCommit secondCommit = testRepo.commit("C2");
    // @formatter:off
    // History (X means has changed)
    // ------------------------------------------------------------
    // files					   C1 [HEAD]		  	C2
    // changingFile.sample   	|-----X----------|-------X-------|->
    // notChangingFile.sample	|-----X----------|---------------|->
    // toBeRemovedFile.sample	|-----X----------|-------X-------|->
    // -------------------------------------------------------------
    // @formatter:on
    testRepo.checkoutBranch(firstCommit.getName());
    iProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    // Now synchronize the two commits using our logical model provider
    SampleModelProvider provider = new SampleModelProvider();
    // Get the affected resources
    ResourceMapping[] mappings = provider.getMappings(iModifiedFile, ResourceMappingContext.LOCAL_CONTEXT, new NullProgressMonitor());
    Set<IResource> includedResource = collectResources(mappings);
    Set<IResource> expectedIncludedResources = new HashSet<IResource>();
    expectedIncludedResources.add(iModifiedFile);
    expectedIncludedResources.add(iUnchangedFile);
    expectedIncludedResources.add(iRemovedFile);
    assertEquals(expectedIncludedResources, includedResource);
    // Synchronize the data
    final GitSynchronizeData data = new GitSynchronizeData(testRepo.getRepository(), firstCommit.getName(), secondCommit.getName(), true, includedResource);
    GitSynchronizeDataSet gitSynchDataSet = new GitSynchronizeDataSet(data);
    final GitResourceVariantTreeSubscriber subscriber = new GitResourceVariantTreeSubscriber(gitSynchDataSet);
    subscriber.init(new NullProgressMonitor());
    IResourceVariantTree sourceVariantTree = subscriber.getSourceTree();
    assertNotNull(sourceVariantTree);
    IResourceVariantTree remoteVariantTree = subscriber.getRemoteTree();
    assertNotNull(remoteVariantTree);
    // In the use case in which the file has been deleted the source variant is
    // not null whereas the remote variant is null.It seems quite logic.
    // However in the second use case we have the same result, the source variant is
    // not null whereas the remote is null. In both cases the null value does
    // not mean the same thing. In the first case, the null value means that
    // the resource is no longer in the repository and in the second the
    // null value means there is no change between the two versions.
    // Using these values I am not able to distinguish both case.
    // It is in contradiction with test #shouldReturnNullResourceVariant2()
    // and test #shoulReturnSameResourceVariant(). However I haven't found
    // another way to handle this case. Maybe something can be
    // done with ThreeWayDiffEntry.scan(tw) to force including in the cache
    // some entry even if they have not changed. For example,
    // ThreeWayDiffEntry.scan(tw,includedSource) or maybe try preventing the variant
    // tree to return null by walking throught the repository and looking for the file...
    IResourceVariant unchangedSourceVariant = sourceVariantTree.getResourceVariant(iUnchangedFile);
    IResourceVariant unchangedRemoteVariant = remoteVariantTree.getResourceVariant(iUnchangedFile);
    assertNotNull(unchangedSourceVariant);
    assertNotNull(unchangedRemoteVariant);
    IResourceVariant removedSourceVariant = sourceVariantTree.getResourceVariant(iRemovedFile);
    IResourceVariant removedRemoteVariant = remoteVariantTree.getResourceVariant(iRemovedFile);
    assertNotNull(removedSourceVariant);
    assertNull(removedRemoteVariant);
    GitSubscriberResourceMappingContext context = new GitSubscriberResourceMappingContext(subscriber, gitSynchDataSet);
    assertFalse(context.hasLocalChange(iUnchangedFile, new NullProgressMonitor()));
    assertFalse(context.hasRemoteChange(iUnchangedFile, new NullProgressMonitor()));
    assertFalse(context.hasLocalChange(iModifiedFile, new NullProgressMonitor()));
    assertTrue(context.hasRemoteChange(iModifiedFile, new NullProgressMonitor()));
    assertFalse(context.hasLocalChange(iRemovedFile, new NullProgressMonitor()));
    assertTrue(context.hasRemoteChange(iRemovedFile, new NullProgressMonitor()));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) IFile(org.eclipse.core.resources.IFile) SampleModelProvider(org.eclipse.egit.core.test.models.SampleModelProvider) IResourceVariant(org.eclipse.team.core.variants.IResourceVariant) GitSynchronizeDataSet(org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) IResourceVariantTree(org.eclipse.team.core.variants.IResourceVariantTree) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with ResourceMapping

use of org.eclipse.core.resources.mapping.ResourceMapping in project egit by eclipse.

the class GitSubscriberMergeContextTest method mergeModelWithConflict.

@Test
public void mergeModelWithConflict() throws Exception {
    File file1 = testRepo.createFile(iProject, "file1." + SAMPLE_FILE_EXTENSION);
    File file2 = testRepo.createFile(iProject, "file2." + SAMPLE_FILE_EXTENSION);
    String initialContent1 = "some content for the first file";
    String initialContent2 = "some content for the second file";
    testRepo.appendContentAndCommit(iProject, file1, initialContent1, "first file - initial commit");
    testRepo.appendContentAndCommit(iProject, file2, initialContent2, "second file - initial commit");
    IFile iFile1 = testRepo.getIFile(iProject, file1);
    IFile iFile2 = testRepo.getIFile(iProject, file2);
    testRepo.createAndCheckoutBranch(MASTER, BRANCH);
    final String branchChanges = "branch changes\n";
    setContentsAndCommit(testRepo, iFile1, initialContent1 + branchChanges, "branch commit");
    setContentsAndCommit(testRepo, iFile2, initialContent2 + branchChanges, "branch commit");
    testRepo.checkoutBranch(MASTER);
    final String masterChanges = "some changes\n";
    setContentsAndCommit(testRepo, iFile1, initialContent1 + masterChanges, "master commit");
    setContentsAndCommit(testRepo, iFile2, initialContent2 + masterChanges, "master commit");
    iProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    // end setup
    IMergeContext mergeContext = prepareModelContext(repo, iFile1, MASTER, BRANCH);
    IDiff node = mergeContext.getDiffTree().getDiff(iFile1);
    assertNotNull(node);
    node = mergeContext.getDiffTree().getDiff(iFile2);
    assertNotNull(node);
    IResourceMappingMerger merger = createMerger();
    IStatus mergeStatus = merger.merge(mergeContext, new NullProgressMonitor());
    assertEquals(IStatus.ERROR, mergeStatus.getSeverity());
    assertTrue(mergeStatus instanceof IMergeStatus);
    assertEquals(2, ((IMergeStatus) mergeStatus).getConflictingMappings().length);
    Set<IFile> conflictingFiles = new LinkedHashSet<IFile>();
    for (ResourceMapping conflictingMapping : ((IMergeStatus) mergeStatus).getConflictingMappings()) {
        assertTrue(conflictingMapping instanceof SampleResourceMapping && conflictingMapping.getModelObject() instanceof IFile);
        conflictingFiles.add((IFile) conflictingMapping.getModelObject());
    }
    assertTrue(conflictingFiles.contains(iFile1));
    assertTrue(conflictingFiles.contains(iFile2));
    assertContentEquals(iFile1, initialContent1 + masterChanges);
    assertContentEquals(iFile2, initialContent2 + masterChanges);
    Status status = status(repo);
    assertEquals(0, status.getChanged().size());
    assertEquals(0, status.getModified().size());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SampleResourceMapping(org.eclipse.egit.core.test.models.SampleResourceMapping) Status(org.eclipse.jgit.api.Status) IStatus(org.eclipse.core.runtime.IStatus) IMergeStatus(org.eclipse.team.core.mapping.IMergeStatus) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) IFile(org.eclipse.core.resources.IFile) IMergeStatus(org.eclipse.team.core.mapping.IMergeStatus) IMergeContext(org.eclipse.team.core.mapping.IMergeContext) IDiff(org.eclipse.team.core.diff.IDiff) IResourceMappingMerger(org.eclipse.team.core.mapping.IResourceMappingMerger) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) SampleResourceMapping(org.eclipse.egit.core.test.models.SampleResourceMapping) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Test(org.junit.Test)

Example 19 with ResourceMapping

use of org.eclipse.core.resources.mapping.ResourceMapping in project egit by eclipse.

the class LabelEventJob method decorateResourceMapping.

/**
 * Decorates a resource mapping (i.e. a Working Set).
 *
 * @param element the element for which the decoration was initially called
 * @param decoration the decoration
 * @throws CoreException
 */
private void decorateResourceMapping(Object element, IDecoration decoration) throws CoreException {
    @SuppressWarnings("restriction") ResourceMapping mapping = Utils.getResourceMapping(element);
    if (mapping == null) {
        return;
    }
    boolean isWorkingSet = mapping.getModelObject() instanceof IWorkingSet;
    IDecoratableResource decoRes;
    try {
        if (isWorkingSet) {
            decoRes = new DecoratableWorkingSet(mapping);
        } else {
            decoRes = new DecoratableResourceMapping(mapping);
        }
    } catch (IOException e) {
        throw new CoreException(Activator.createErrorStatus(NLS.bind(UIText.Decorator_exceptionMessage, element), e));
    }
    /*
		 *  don't render question marks on working sets. !isTracked() can have two reasons:
		 *   1) nothing is tracked.
		 *   2) no indexDiff for the contained projects ready yet.
		 *  in both cases, don't do anything to not pollute the display of the sets.
		 */
    if (!decoRes.isTracked() && isWorkingSet) {
        return;
    }
    helper.decorate(decoration, decoRes);
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) IOException(java.io.IOException) IWorkingSet(org.eclipse.ui.IWorkingSet)

Example 20 with ResourceMapping

use of org.eclipse.core.resources.mapping.ResourceMapping in project egit by eclipse.

the class GitAdapterFactory method getAdapter.

@Override
public Object getAdapter(Object adaptableObject, Class adapterType) {
    if (adapterType.isAssignableFrom(IHistoryPageSource.class)) {
        return GitHistoryPageSource.INSTANCE;
    }
    if (IWorkbenchAdapter.class == adapterType) {
        // property page names for git repository tree nodes
        if (adaptableObject instanceof RepositoryTreeNode) {
            return getRepositoryTreeNodeWorkbenchAdapter((RepositoryTreeNode) adaptableObject);
        }
        if (gitModelWorkbenchAdapter == null) {
            gitModelWorkbenchAdapter = new GitModelWorkbenchAdapter();
        }
        return gitModelWorkbenchAdapter;
    }
    if (adaptableObject instanceof IHistoryView && IShowInSource.class == adapterType) {
        IHistoryView historyView = (IHistoryView) adaptableObject;
        IHistoryPage historyPage = historyView.getHistoryPage();
        if (historyPage instanceof GitHistoryPage) {
            return historyPage;
        }
    }
    if (adaptableObject instanceof IURIEditorInput && adapterType == Repository.class) {
        return getRepository((IURIEditorInput) adaptableObject);
    }
    if (adaptableObject instanceof IURIEditorInput && adapterType == File.class) {
        return getFile((IURIEditorInput) adaptableObject);
    }
    if (adaptableObject instanceof GitModelObject && adapterType == ResourceMapping.class) {
        return GitObjectMapping.create((GitModelObject) adaptableObject);
    }
    if (adaptableObject instanceof GitModelObject && adapterType == IResource.class) {
        GitModelObject obj = (GitModelObject) adaptableObject;
        if (obj instanceof GitModelBlob) {
            IResource res = ResourceUtil.getFileForLocation(obj.getLocation(), false);
            if (res == null) {
                // Deleted resource?
                res = getWorkspaceResourceFromGitPath(obj.getLocation());
            }
            return res;
        }
        if (obj instanceof GitModelTree) {
            IResource res = root.getContainerForLocation(obj.getLocation());
            if (res == null) {
                res = root.getFolder(obj.getLocation());
            }
            return res;
        }
    }
    if (adapterType == Repository.class) {
        ResourceMapping m = AdapterUtils.adapt(adaptableObject, ResourceMapping.class);
        if (m != null) {
            return SelectionUtils.getRepository(new StructuredSelection(m));
        }
    }
    return null;
}
Also used : IURIEditorInput(org.eclipse.ui.IURIEditorInput) GitModelTree(org.eclipse.egit.ui.internal.synchronize.model.GitModelTree) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) GitModelObject(org.eclipse.egit.ui.internal.synchronize.model.GitModelObject) IHistoryPage(org.eclipse.team.ui.history.IHistoryPage) Repository(org.eclipse.jgit.lib.Repository) GitHistoryPage(org.eclipse.egit.ui.internal.history.GitHistoryPage) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) GitModelWorkbenchAdapter(org.eclipse.egit.ui.internal.synchronize.mapping.GitModelWorkbenchAdapter) ResourceMapping(org.eclipse.core.resources.mapping.ResourceMapping) IShowInSource(org.eclipse.ui.part.IShowInSource) IHistoryView(org.eclipse.team.ui.history.IHistoryView) File(java.io.File) IResource(org.eclipse.core.resources.IResource) GitModelBlob(org.eclipse.egit.ui.internal.synchronize.model.GitModelBlob)

Aggregations

ResourceMapping (org.eclipse.core.resources.mapping.ResourceMapping)28 IResource (org.eclipse.core.resources.IResource)14 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 JavaElementResourceMapping (org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping)7 CoreException (org.eclipse.core.runtime.CoreException)6 GitSynchronizeDataSet (org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 GitSynchronizeData (org.eclipse.egit.core.synchronize.dto.GitSynchronizeData)5 IFile (org.eclipse.core.resources.IFile)4 ResourceTraversal (org.eclipse.core.resources.mapping.ResourceTraversal)4 IAdaptable (org.eclipse.core.runtime.IAdaptable)4 Change (org.eclipse.ltk.core.refactoring.Change)4 File (java.io.File)3 IProject (org.eclipse.core.resources.IProject)3 ResourceMappingContext (org.eclipse.core.resources.mapping.ResourceMappingContext)3 GitResourceVariantTreeSubscriber (org.eclipse.egit.core.synchronize.GitResourceVariantTreeSubscriber)3 GitSubscriberMergeContext (org.eclipse.egit.core.synchronize.GitSubscriberMergeContext)3 GitSubscriberResourceMappingContext (org.eclipse.egit.core.synchronize.GitSubscriberResourceMappingContext)3 ResourceChange (org.eclipse.ltk.core.refactoring.resource.ResourceChange)3