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
;
12 import java
.io
.IOException
;
13 import java
.util
.LinkedHashSet
;
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
;
27 * This will populate its three {@link IResourceVariantTree} by looking up information within the repository's
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.
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
;
48 * Constructs the resource variant trees by iterating over the given repository's DirCache entries.
51 * The repository which DirCache info we need to cache as IResourceVariantTrees.
53 * Whether we should use local data instead of what's in the index for our side.
55 * if we somehow cannot read the DirCache.
57 public DirCacheResourceVariantTreeProvider(Repository repository
, boolean useWorkspace
)
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()) {
73 switch (entry
.getStage()) {
74 case DirCacheEntry
.STAGE_0
:
76 sourceCache
.setVariant(resource
, new GitLocalResourceVariant(resource
));
78 sourceCache
.setVariant(resource
, IndexResourceVariant
.create(repository
, entry
));
80 baseCache
.setVariant(resource
, IndexResourceVariant
.create(repository
, entry
));
81 remoteCache
.setVariant(resource
, IndexResourceVariant
.create(repository
, entry
));
83 case DirCacheEntry
.STAGE_1
:
84 baseCache
.setVariant(resource
, IndexResourceVariant
.create(repository
, entry
));
86 case DirCacheEntry
.STAGE_2
:
88 sourceCache
.setVariant(resource
, new GitLocalResourceVariant(resource
));
90 sourceCache
.setVariant(resource
, IndexResourceVariant
.create(repository
, entry
));
93 case DirCacheEntry
.STAGE_3
:
94 remoteCache
.setVariant(resource
, IndexResourceVariant
.create(repository
, entry
));
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() {
120 public IResourceVariantTree
getRemoteTree() {
124 public IResourceVariantTree
getSourceTree() {
128 public Set
<IResource
> getKnownResources() {
129 return knownResources
;
132 public Set
<IResource
> getRoots() {