update copyright
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / connections / CvsRootOnFileSystem.java
blob14ea23cd304381bc847b69ac558c90ae2b03cd6e
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.Nullable;
25 import java.io.File;
27 /**
28 * author: lesya
30 public class CvsRootOnFileSystem extends CvsRootProvider {
31 private final CvsConnectionSettings mySettings;
32 private CvsRootOnFileSystem(CvsConnectionSettings cvsRoot, File fileRoot) {
33 super(fileRoot, cvsRoot);
34 mySettings = cvsRoot;
37 public static CvsRootOnFileSystem createMeOn(File file) throws CannotFindCvsRootException {
38 File nearestRoot = getRootFor(file);
39 if (nearestRoot == null) throw new CannotFindCvsRootException(file);
40 CvsConnectionSettings cvsRoot = getCvsRootFor(nearestRoot);
41 if (cvsRoot == CvsInfo.getAbsentSettings()) throw new CannotFindCvsRootException(file);
42 File commonRoot = getCommonRoot(nearestRoot, cvsRoot);
43 return new CvsRootOnFileSystem(getCvsEntriesManager().getCvsConnectionSettingsFor(commonRoot), commonRoot);
46 private static CvsEntriesManager getCvsEntriesManager() {
47 return CvsEntriesManager.getInstance();
51 public int hashCode() {
52 return mySettings.hashCode() ^ getLocalRoot().hashCode();
55 public boolean equals(Object obj) {
56 if (!(obj instanceof CvsRootOnFileSystem)) return false;
57 CvsRootOnFileSystem other = (CvsRootOnFileSystem) obj;
58 return mySettings.equals(other.mySettings) &&
59 getLocalRoot().equals(other.getLocalRoot());
62 private static File getCommonRoot(File nearestRoot, CvsConnectionSettings cvsRoot) {
63 File result = nearestRoot;
64 while (cvsRoot.equals(getCvsRootFor(result.getParentFile()))) {
65 if (result.getParentFile() == null) return result;
66 result = result.getParentFile();
68 return result;
71 private static CvsConnectionSettings getCvsRootFor(File file) {
72 return getCvsEntriesManager().getCvsConnectionSettingsFor(file);
75 @Nullable
76 private static File getRootFor(File file) {
77 if (file == null) return null;
78 if (!file.isDirectory()) return getRootFor(file.getParentFile());
79 if (CvsUtil.fileIsUnderCvs(file)) return file;
80 return getRootFor(file.getParentFile());
83 public String getRepository() {
84 return myCvsEnvironment.getRepository();
87 public RevisionOrDate getRevisionOrDate() {
88 return RevisionOrDate.EMPTY;