Model Merge Tool + classes to do model merges with EGit
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.egit / src / org / eclipse / emf / compare / egit / internal / merge / DirCacheResourceVariantTreeProvider.java
blob121ae378ddae2bdb9963a1029c3e3cf1c7db84cc
1 /*******************************************************************************
2 * Copyright (C) 2015, Obeo.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.emf.compare.egit.internal.merge;
11 //CHECKSTYLE:OFF
12 import java.io.IOException;
13 import java.util.LinkedHashSet;
14 import java.util.Set;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.egit.core.internal.storage.IndexResourceVariant;
18 import org.eclipse.emf.compare.egit.internal.ModelEGitResourceUtil;
19 import org.eclipse.emf.compare.egit.internal.storage.GitLocalResourceVariant;
20 import org.eclipse.jgit.dircache.DirCache;
21 import org.eclipse.jgit.dircache.DirCacheEntry;
22 import org.eclipse.jgit.lib.FileMode;
23 import org.eclipse.jgit.lib.Repository;
24 import org.eclipse.team.core.variants.IResourceVariantTree;
26 /**
27 * This will populate its three {@link IResourceVariantTree} by looking up information within the repository's
28 * DirCache.
29 * <p>
30 * Files that are not located within the workspace will be ignored and thus will not be accessible through the
31 * trees created by this provider.
32 * </p>
34 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
36 public class DirCacheResourceVariantTreeProvider implements GitResourceVariantTreeProvider {
37 private final IResourceVariantTree baseTree;
39 private final IResourceVariantTree sourceTree;
41 private final IResourceVariantTree remoteTree;
43 private final Set<IResource> roots;
45 private final Set<IResource> knownResources;
47 /**
48 * Constructs the resource variant trees by iterating over the given repository's DirCache entries.
50 * @param repository
51 * The repository which DirCache info we need to cache as IResourceVariantTrees.
52 * @param useWorkspace
53 * Whether we should use local data instead of what's in the index for our side.
54 * @throws IOException
55 * if we somehow cannot read the DirCache.
57 public DirCacheResourceVariantTreeProvider(Repository repository, boolean useWorkspace)
58 throws IOException {
59 final DirCache cache = repository.readDirCache();
60 final GitResourceVariantCache baseCache = new GitResourceVariantCache();
61 final GitResourceVariantCache sourceCache = new GitResourceVariantCache();
62 final GitResourceVariantCache remoteCache = new GitResourceVariantCache();
64 for (int i = 0; i < cache.getEntryCount(); i++) {
65 final DirCacheEntry entry = cache.getEntry(i);
66 final IResource resource = ModelEGitResourceUtil.getResourceHandleForLocation(repository, entry
67 .getPathString(), FileMode.fromBits(entry.getRawMode()) == FileMode.TREE);
68 // Resource variants only make sense for IResources. Do not consider
69 // files outside of the workspace or otherwise non accessible.
70 if (resource == null || resource.getProject() == null || !resource.getProject().isAccessible()) {
71 continue;
73 switch (entry.getStage()) {
74 case DirCacheEntry.STAGE_0:
75 if (useWorkspace) {
76 sourceCache.setVariant(resource, new GitLocalResourceVariant(resource));
77 } else {
78 sourceCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
80 baseCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
81 remoteCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
82 break;
83 case DirCacheEntry.STAGE_1:
84 baseCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
85 break;
86 case DirCacheEntry.STAGE_2:
87 if (useWorkspace) {
88 sourceCache.setVariant(resource, new GitLocalResourceVariant(resource));
89 } else {
90 sourceCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
92 break;
93 case DirCacheEntry.STAGE_3:
94 remoteCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
95 break;
96 default:
97 throw new IllegalStateException("Invalid stage: " + entry.getStage()); //$NON-NLS-1$
101 baseTree = new GitCachedResourceVariantTree(baseCache);
102 sourceTree = new GitCachedResourceVariantTree(sourceCache);
103 remoteTree = new GitCachedResourceVariantTree(remoteCache);
105 roots = new LinkedHashSet<IResource>();
106 roots.addAll(baseCache.getRoots());
107 roots.addAll(sourceCache.getRoots());
108 roots.addAll(remoteCache.getRoots());
110 knownResources = new LinkedHashSet<IResource>();
111 knownResources.addAll(baseCache.getKnownResources());
112 knownResources.addAll(sourceCache.getKnownResources());
113 knownResources.addAll(remoteCache.getKnownResources());
116 public IResourceVariantTree getBaseTree() {
117 return baseTree;
120 public IResourceVariantTree getRemoteTree() {
121 return remoteTree;
124 public IResourceVariantTree getSourceTree() {
125 return sourceTree;
128 public Set<IResource> getKnownResources() {
129 return knownResources;
132 public Set<IResource> getRoots() {
133 return roots;
136 // CHECKSTYLE:ON