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.
18 * Created by IntelliJ IDEA.
23 package com.intellij.openapi.vcs.changes;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.Comparing;
27 import com.intellij.openapi.util.io.FileUtil;
28 import com.intellij.openapi.vfs.LocalFileSystem;
29 import com.intellij.openapi.vfs.VfsUtilCore;
30 import com.intellij.openapi.vfs.VirtualFile;
31 import com.intellij.openapi.vfs.newvfs.impl.NullVirtualFile;
32 import com.intellij.util.PatternUtil;
33 import org.jetbrains.annotations.Nullable;
36 import java.util.regex.Matcher;
38 public class IgnoredFileBean {
39 private final String myPath;
40 private final String myFilenameIfFile;
41 private final String myMask;
42 private final Matcher myMatcher;
43 private final IgnoreSettingsType myType;
44 private final Project myProject;
45 private volatile VirtualFile myCachedResolved;
47 IgnoredFileBean(String path, IgnoreSettingsType type, Project project) {
50 if (IgnoreSettingsType.FILE.equals(type)) {
51 myFilenameIfFile = new File(path).getName();
54 myFilenameIfFile = null;
61 Project getProject() {
65 IgnoredFileBean(String mask) {
66 myType = IgnoreSettingsType.MASK;
72 myMatcher = PatternUtil.fromMask(mask).matcher("");
75 myFilenameIfFile = null;
80 public String getPath() {
85 public String getMask() {
89 public IgnoreSettingsType getType() {
94 public boolean equals(Object o) {
95 if (this == o) return true;
96 if (o == null || getClass() != o.getClass()) return false;
98 IgnoredFileBean that = (IgnoredFileBean)o;
100 if (myPath != null ? !myPath.equals(that.myPath) : that.myPath != null) return false;
101 if (myMask != null ? !myMask.equals(that.myMask) : that.myMask != null) return false;
102 if (myType != that.myType) return false;
108 public int hashCode() {
109 int result = myPath != null ? myPath.hashCode() : 0;
110 result = 31 * result + (myMask != null ? myMask.hashCode() : 0);
111 result = 31 * result + myType.hashCode();
115 public boolean matchesFile(VirtualFile file) {
116 if (myType == IgnoreSettingsType.MASK) {
117 myMatcher.reset(file.getName());
118 return myMatcher.matches();
121 // quick check for 'file' == exact match pattern
122 if (IgnoreSettingsType.FILE.equals(myType) && !myFilenameIfFile.equals(file.getName())) return false;
124 VirtualFile selector = resolve();
125 if (Comparing.equal(selector, NullVirtualFile.INSTANCE)) return false;
127 if (myType == IgnoreSettingsType.FILE) {
128 return Comparing.equal(selector, file);
131 if ("./".equals(myPath)) {
132 // special case for ignoring the project base dir (IDEADEV-16056)
133 return !file.isDirectory() && Comparing.equal(file.getParent(), selector);
135 return VfsUtilCore.isAncestor(selector, file, false);
140 private VirtualFile resolve() {
141 if (myCachedResolved == null) {
142 VirtualFile resolved = doResolve();
143 myCachedResolved = resolved != null ? resolved : NullVirtualFile.INSTANCE;
146 return myCachedResolved;
150 private VirtualFile doResolve() {
151 if (myProject == null || myProject.isDisposed()) {
154 VirtualFile baseDir = myProject.getBaseDir();
156 String path = FileUtil.toSystemIndependentName(myPath);
157 if (baseDir == null) {
158 return LocalFileSystem.getInstance().findFileByPath(path);
161 VirtualFile resolvedRelative = baseDir.findFileByRelativePath(path);
162 if (resolvedRelative != null) return resolvedRelative;
164 return LocalFileSystem.getInstance().findFileByPath(path);
167 public void resetCache() {
168 myCachedResolved = null;