1 package com
.intellij
.openapi
.vcs
.impl
;
3 import com
.intellij
.openapi
.application
.ApplicationManager
;
4 import com
.intellij
.openapi
.components
.StorageScheme
;
5 import com
.intellij
.openapi
.module
.Module
;
6 import com
.intellij
.openapi
.module
.ModuleManager
;
7 import com
.intellij
.openapi
.module
.ModuleUtil
;
8 import com
.intellij
.openapi
.project
.Project
;
9 import com
.intellij
.openapi
.project
.ex
.ProjectEx
;
10 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
11 import com
.intellij
.openapi
.roots
.ProjectRootManager
;
12 import com
.intellij
.openapi
.util
.Computable
;
13 import com
.intellij
.openapi
.vcs
.*;
14 import com
.intellij
.openapi
.vcs
.changes
.DirtBuilder
;
15 import com
.intellij
.openapi
.vcs
.changes
.FilePathUnderVcs
;
16 import com
.intellij
.openapi
.vcs
.changes
.VcsGuess
;
17 import com
.intellij
.openapi
.vcs
.ex
.ProjectLevelVcsManagerEx
;
18 import com
.intellij
.openapi
.vcs
.impl
.projectlevelman
.NewMappings
;
19 import com
.intellij
.openapi
.vfs
.VfsUtil
;
20 import com
.intellij
.openapi
.vfs
.VirtualFile
;
21 import com
.intellij
.util
.PathUtil
;
22 import org
.jetbrains
.annotations
.Nullable
;
25 import java
.util
.List
;
30 public class ModuleDefaultVcsRootPolicy
extends DefaultVcsRootPolicy
{
31 private final Project myProject
;
32 private final VirtualFile myBaseDir
;
33 private final ModuleManager myModuleManager
;
35 public ModuleDefaultVcsRootPolicy(final Project project
) {
37 myBaseDir
= project
.getBaseDir();
38 myModuleManager
= ModuleManager
.getInstance(myProject
);
41 public void addDefaultVcsRoots(final NewMappings mappingList
, final AbstractVcs vcs
, final List
<VirtualFile
> result
) {
42 if (myBaseDir
!= null && vcs
.getName().equals(mappingList
.getVcsFor(myBaseDir
)) && vcs
.fileIsUnderVcs(new FilePathImpl(myBaseDir
))) {
43 result
.add(myBaseDir
);
45 final StorageScheme storageScheme
= ((ProjectEx
) myProject
).getStateStore().getStorageScheme();
46 if (StorageScheme
.DIRECTORY_BASED
.equals(storageScheme
)) {
47 final VirtualFile ideaDir
= myBaseDir
.findChild(Project
.DIRECTORY_STORE_FOLDER
);
48 if (ideaDir
!= null && ideaDir
.isValid() && ideaDir
.isDirectory()) {
52 // assertion for read access inside
53 final Module
[] modules
= ApplicationManager
.getApplication().runReadAction(new Computable
<Module
[]>() {
54 public Module
[] compute() {
55 return myModuleManager
.getModules();
58 for(Module module
: modules
) {
59 final VirtualFile
[] files
= ModuleRootManager
.getInstanceChecked(module
).getContentRoots();
60 for(VirtualFile file
: files
) {
61 // if we're currently processing moduleAdded notification, getModuleForFile() will return null, so we pass the module
62 // explicitly (we know it anyway)
63 VcsDirectoryMapping mapping
= mappingList
.getMappingFor(file
, module
);
64 final String mappingVcs
= mapping
!= null ? mapping
.getVcs() : null;
65 if (vcs
.getName().equals(mappingVcs
)) {
72 public boolean matchesDefaultMapping(final VirtualFile file
, final Object matchContext
) {
73 if (matchContext
!= null) {
76 if (myBaseDir
!= null && VfsUtil
.isAncestor(myBaseDir
, file
, false)) {
77 return !ProjectRootManager
.getInstance(myProject
).getFileIndex().isIgnored(file
);
83 public Object
getMatchContext(final VirtualFile file
) {
84 return ModuleUtil
.findModuleForFile(file
, myProject
);
88 public VirtualFile
getVcsRootFor(final VirtualFile file
) {
89 if (myBaseDir
!= null && ExcludedFileIndex
.getInstance(myProject
).isValidAncestor(myBaseDir
, file
)) {
92 final VirtualFile contentRoot
= ProjectRootManager
.getInstance(myProject
).getFileIndex().getContentRootForFile(file
);
93 if (contentRoot
!= null) {
96 final StorageScheme storageScheme
= ((ProjectEx
) myProject
).getStateStore().getStorageScheme();
97 if (StorageScheme
.DIRECTORY_BASED
.equals(storageScheme
)) {
98 final VirtualFile ideaDir
= myBaseDir
.findChild(Project
.DIRECTORY_STORE_FOLDER
);
99 if (ideaDir
!= null && ideaDir
.isValid() && ideaDir
.isDirectory()) {
100 if (VfsUtil
.isAncestor(ideaDir
, file
, false)) {
108 public void markDefaultRootsDirty(final DirtBuilder builder
, final VcsGuess vcsGuess
) {
109 final VirtualFile baseDir
= myProject
.getBaseDir();
111 final Module
[] modules
= myModuleManager
.getModules();
112 final StorageScheme storageScheme
= ((ProjectEx
) myProject
).getStateStore().getStorageScheme();
113 if (StorageScheme
.DIRECTORY_BASED
.equals(storageScheme
)) {
114 final File ioFile
= new File(PathUtil
.toPresentableUrl(baseDir
.getPath()), Project
.DIRECTORY_STORE_FOLDER
);
115 final FilePathImpl fp
= new FilePathImpl(ioFile
, true);
116 final AbstractVcs vcs
= vcsGuess
.getVcsForDirty(fp
);
118 builder
.addDirtyDirRecursively(new FilePathUnderVcs(fp
, vcs
));
122 for(Module module
: modules
) {
123 final VirtualFile
[] files
= ModuleRootManager
.getInstanceChecked(module
).getContentRoots();
124 for(VirtualFile file
: files
) {
125 final AbstractVcs vcs
= vcsGuess
.getVcsForDirty(file
);
127 builder
.addDirtyDirRecursively(new VcsRoot(vcs
, file
));
132 final ProjectLevelVcsManager plVcsManager
= ProjectLevelVcsManager
.getInstance(myProject
);
133 final String defaultMapping
= ((ProjectLevelVcsManagerEx
)plVcsManager
).haveDefaultMapping();
134 final boolean haveDefaultMapping
= (defaultMapping
!= null) && (defaultMapping
.length() > 0);
135 if (haveDefaultMapping
) {
136 builder
.addDirtyFile(new VcsRoot(plVcsManager
.findVcsByName(defaultMapping
), baseDir
));
139 final VcsRoot
[] vcsRoots
= plVcsManager
.getAllVcsRoots();
140 for (VcsRoot root
: vcsRoots
) {
141 if (! root
.path
.equals(baseDir
)) {
142 builder
.addDirtyDirRecursively(root
);