VCS: correctly mark dirty roots in case project root directory is mapped with non...
[fedora-idea.git] / platform / lang-impl / src / com / intellij / openapi / vcs / impl / ModuleDefaultVcsRootPolicy.java
blobf2ede48b2abef702afccf5dcbb50ac48b4f1d638
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.openapi.vcs.impl;
19 import com.intellij.openapi.application.ApplicationManager;
20 import com.intellij.openapi.components.StorageScheme;
21 import com.intellij.openapi.module.Module;
22 import com.intellij.openapi.module.ModuleManager;
23 import com.intellij.openapi.module.ModuleUtil;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.project.ex.ProjectEx;
26 import com.intellij.openapi.roots.ModuleRootManager;
27 import com.intellij.openapi.roots.ProjectRootManager;
28 import com.intellij.openapi.util.Computable;
29 import com.intellij.openapi.vcs.*;
30 import com.intellij.openapi.vcs.changes.DirtBuilder;
31 import com.intellij.openapi.vcs.changes.FilePathUnderVcs;
32 import com.intellij.openapi.vcs.changes.VcsGuess;
33 import com.intellij.openapi.vcs.ex.ProjectLevelVcsManagerEx;
34 import com.intellij.openapi.vcs.impl.projectlevelman.NewMappings;
35 import com.intellij.openapi.vfs.VfsUtil;
36 import com.intellij.openapi.vfs.VirtualFile;
37 import org.jetbrains.annotations.Nullable;
39 import java.util.List;
41 /**
42 * @author yole
44 public class ModuleDefaultVcsRootPolicy extends DefaultVcsRootPolicy {
45 private final Project myProject;
46 private final VirtualFile myBaseDir;
47 private final ModuleManager myModuleManager;
49 public ModuleDefaultVcsRootPolicy(final Project project) {
50 myProject = project;
51 myBaseDir = project.getBaseDir();
52 myModuleManager = ModuleManager.getInstance(myProject);
55 public void addDefaultVcsRoots(final NewMappings mappingList, final AbstractVcs vcs, final List<VirtualFile> result) {
56 if (myBaseDir != null && vcs.getName().equals(mappingList.getVcsFor(myBaseDir)) && vcs.fileIsUnderVcs(new FilePathImpl(myBaseDir))) {
57 result.add(myBaseDir);
59 final StorageScheme storageScheme = ((ProjectEx) myProject).getStateStore().getStorageScheme();
60 if (StorageScheme.DIRECTORY_BASED.equals(storageScheme)) {
61 final VirtualFile ideaDir = myBaseDir.findChild(Project.DIRECTORY_STORE_FOLDER);
62 if (ideaDir != null && ideaDir.isValid() && ideaDir.isDirectory()) {
63 result.add(ideaDir);
66 // assertion for read access inside
67 final Module[] modules = ApplicationManager.getApplication().runReadAction(new Computable<Module[]>() {
68 public Module[] compute() {
69 return myModuleManager.getModules();
71 });
72 for(Module module: modules) {
73 final VirtualFile[] files = ModuleRootManager.getInstance(module).getContentRoots();
74 for(VirtualFile file: files) {
75 // if we're currently processing moduleAdded notification, getModuleForFile() will return null, so we pass the module
76 // explicitly (we know it anyway)
77 VcsDirectoryMapping mapping = mappingList.getMappingFor(file, module);
78 final String mappingVcs = mapping != null ? mapping.getVcs() : null;
79 if (vcs.getName().equals(mappingVcs)) {
80 result.add(file);
86 public boolean matchesDefaultMapping(final VirtualFile file, final Object matchContext) {
87 if (matchContext != null) {
88 return true;
90 if (myBaseDir != null && VfsUtil.isAncestor(myBaseDir, file, false)) {
91 return !ProjectRootManager.getInstance(myProject).getFileIndex().isIgnored(file);
93 return false;
96 @Nullable
97 public Object getMatchContext(final VirtualFile file) {
98 return ModuleUtil.findModuleForFile(file, myProject);
101 @Nullable
102 public VirtualFile getVcsRootFor(final VirtualFile file) {
103 if (myBaseDir != null && ExcludedFileIndex.getInstance(myProject).isValidAncestor(myBaseDir, file)) {
104 return myBaseDir;
106 final VirtualFile contentRoot = ProjectRootManager.getInstance(myProject).getFileIndex().getContentRootForFile(file);
107 if (contentRoot != null) {
108 return contentRoot;
110 final StorageScheme storageScheme = ((ProjectEx) myProject).getStateStore().getStorageScheme();
111 if (StorageScheme.DIRECTORY_BASED.equals(storageScheme)) {
112 final VirtualFile ideaDir = myBaseDir.findChild(Project.DIRECTORY_STORE_FOLDER);
113 if (ideaDir != null && ideaDir.isValid() && ideaDir.isDirectory()) {
114 if (VfsUtil.isAncestor(ideaDir, file, false)) {
115 return ideaDir;
119 return null;
122 public void markDefaultRootsDirty(final DirtBuilder builder, final VcsGuess vcsGuess) {
123 final VirtualFile baseDir = myProject.getBaseDir();
125 final Module[] modules = myModuleManager.getModules();
126 final StorageScheme storageScheme = ((ProjectEx) myProject).getStateStore().getStorageScheme();
127 if (StorageScheme.DIRECTORY_BASED.equals(storageScheme)) {
128 final FilePathImpl fp = new FilePathImpl(baseDir, Project.DIRECTORY_STORE_FOLDER, true);
129 final AbstractVcs vcs = vcsGuess.getVcsForDirty(fp);
130 if (vcs != null) {
131 builder.addDirtyDirRecursively(new FilePathUnderVcs(fp, vcs));
135 for(Module module: modules) {
136 final VirtualFile[] files = ModuleRootManager.getInstance(module).getContentRoots();
137 for(VirtualFile file: files) {
138 final AbstractVcs vcs = vcsGuess.getVcsForDirty(file);
139 if (vcs != null) {
140 builder.addDirtyDirRecursively(new VcsRoot(vcs, file));
145 final ProjectLevelVcsManager plVcsManager = ProjectLevelVcsManager.getInstance(myProject);
146 final String defaultMapping = ((ProjectLevelVcsManagerEx)plVcsManager).haveDefaultMapping();
147 final boolean haveDefaultMapping = (defaultMapping != null) && (defaultMapping.length() > 0);
148 if (haveDefaultMapping) {
149 builder.addDirtyFile(new VcsRoot(plVcsManager.findVcsByName(defaultMapping), baseDir));
152 final VcsRoot[] vcsRoots = plVcsManager.getAllVcsRoots();
153 for (VcsRoot root : vcsRoots) {
154 //if (! root.path.equals(baseDir)) {
155 builder.addDirtyDirRecursively(root);