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.
16 package com
.intellij
.openapi
.vcs
.changes
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.components
.StorageScheme
;
20 import com
.intellij
.openapi
.project
.Project
;
21 import com
.intellij
.openapi
.project
.ex
.ProjectEx
;
22 import com
.intellij
.openapi
.util
.Computable
;
23 import com
.intellij
.openapi
.vcs
.AbstractVcs
;
24 import com
.intellij
.openapi
.vcs
.FilePath
;
25 import com
.intellij
.openapi
.vcs
.impl
.ExcludedFileIndex
;
26 import com
.intellij
.openapi
.vcs
.impl
.ProjectLevelVcsManagerImpl
;
27 import com
.intellij
.openapi
.vfs
.VfsUtil
;
28 import com
.intellij
.openapi
.vfs
.VirtualFile
;
29 import org
.jetbrains
.annotations
.Nullable
;
31 public class VcsGuess
{
32 private final Project myProject
;
33 private final ProjectLevelVcsManagerImpl myVcsManager
;
34 private final ExcludedFileIndex myExcludedFileIndex
;
36 VcsGuess(final Project project
) {
38 myVcsManager
= (ProjectLevelVcsManagerImpl
) ProjectLevelVcsManagerImpl
.getInstance(myProject
);
39 myExcludedFileIndex
= ExcludedFileIndex
.getInstance(myProject
);
43 public AbstractVcs
getVcsForDirty(final VirtualFile file
) {
44 if (!file
.isInLocalFileSystem()) {
47 if (myExcludedFileIndex
.isInContent(file
) || isFileInBaseDir(file
) ||
48 myVcsManager
.hasExplicitMapping(file
) || file
.equals(myProject
.getBaseDir())) {
49 if (myExcludedFileIndex
.isExcludedFile(file
)) {
52 return myVcsManager
.getVcsFor(file
);
58 public AbstractVcs
getVcsForDirty(final FilePath filePath
) {
59 if (filePath
.isNonLocal()) {
62 final VirtualFile validParent
= ApplicationManager
.getApplication().runReadAction(new Computable
<VirtualFile
>() {
63 public VirtualFile
compute() {
64 return ChangesUtil
.findValidParent(filePath
);
67 if (validParent
== null) {
70 if (myExcludedFileIndex
.isInContent(validParent
) || isFileInBaseDir(filePath
) ||
71 myVcsManager
.hasExplicitMapping(validParent
) || isInDirectoryBasedRoot(validParent
)) {
72 if (myExcludedFileIndex
.isExcludedFile(validParent
)) {
75 return myVcsManager
.getVcsFor(validParent
);
80 private boolean isInDirectoryBasedRoot(final VirtualFile file
) {
81 if (file
== null) return false;
82 final StorageScheme storageScheme
= ((ProjectEx
) myProject
).getStateStore().getStorageScheme();
83 if (StorageScheme
.DIRECTORY_BASED
.equals(storageScheme
)) {
84 final VirtualFile ideaDir
= myProject
.getBaseDir().findChild(Project
.DIRECTORY_STORE_FOLDER
);
85 return (ideaDir
!= null && ideaDir
.isValid() && ideaDir
.isDirectory() && VfsUtil
.isAncestor(ideaDir
, file
, false));
90 private boolean isFileInBaseDir(final VirtualFile file
) {
91 VirtualFile parent
= file
.getParent();
92 return !file
.isDirectory() && parent
!= null && parent
.equals(myProject
.getBaseDir());
95 private boolean isFileInBaseDir(final FilePath filePath
) {
96 final VirtualFile parent
= filePath
.getVirtualFileParent();
97 return !filePath
.isDirectory() && parent
!= null && parent
.equals(myProject
.getBaseDir());