IDEADEV-41995
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / connections / CvsRootOnFileSystem.java
blob08d4a106c0517ebd8c1ea8de4e65d04db4ed23ef
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.
16 package com.intellij.cvsSupport2.connections;
18 import com.intellij.cvsSupport2.CvsUtil;
19 import com.intellij.cvsSupport2.application.CvsEntriesManager;
20 import com.intellij.cvsSupport2.application.CvsInfo;
21 import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDate;
22 import com.intellij.cvsSupport2.errorHandling.CannotFindCvsRootException;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
26 import java.io.File;
28 /**
29 * author: lesya
31 public class CvsRootOnFileSystem extends CvsRootProvider {
32 private final CvsConnectionSettings mySettings;
33 private CvsRootOnFileSystem(CvsConnectionSettings cvsRoot, File fileRoot) {
34 super(fileRoot, cvsRoot);
35 mySettings = cvsRoot;
38 public static CvsRootOnFileSystem createMeOn(File file) throws CannotFindCvsRootException {
39 File nearestRoot = getRootFor(file);
40 if (nearestRoot == null) throw new CannotFindCvsRootException(file);
41 CvsConnectionSettings cvsRoot = getCvsRootFor(nearestRoot);
42 if (cvsRoot == CvsInfo.getAbsentSettings()) throw new CannotFindCvsRootException(file);
43 File commonRoot = getCommonRoot(nearestRoot, cvsRoot);
44 return new CvsRootOnFileSystem(getCvsEntriesManager().getCvsConnectionSettingsFor(commonRoot), commonRoot);
47 private static CvsEntriesManager getCvsEntriesManager() {
48 return CvsEntriesManager.getInstance();
52 public int hashCode() {
53 return mySettings.hashCode() ^ getLocalRoot().hashCode();
56 public boolean equals(Object obj) {
57 if (!(obj instanceof CvsRootOnFileSystem)) return false;
58 CvsRootOnFileSystem other = (CvsRootOnFileSystem) obj;
59 return mySettings.equals(other.mySettings) &&
60 getLocalRoot().equals(other.getLocalRoot());
63 private static File getCommonRoot(File nearestRoot, CvsConnectionSettings cvsRoot) {
64 File result = nearestRoot;
65 if (result.getParentFile() == null) return result;
66 while (cvsRoot.equals(getCvsRootFor(result.getParentFile()))) {
67 if (result.getParentFile() == null) return result;
68 result = result.getParentFile();
70 return result;
73 private static CvsConnectionSettings getCvsRootFor(@NotNull File file) {
74 return getCvsEntriesManager().getCvsConnectionSettingsFor(file);
77 @Nullable
78 private static File getRootFor(File file) {
79 if (file == null) return null;
80 if (!file.isDirectory()) return getRootFor(file.getParentFile());
81 if (CvsUtil.fileIsUnderCvs(file)) return file;
82 return getRootFor(file.getParentFile());
85 public String getRepository() {
86 return myCvsEnvironment.getRepository();
89 public RevisionOrDate getRevisionOrDate() {
90 return RevisionOrDate.EMPTY;