001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mercurial.util;
042:
043: import java.io.File;
044: import java.util.Set;
045: import java.util.logging.Level;
046: import org.netbeans.modules.mercurial.Mercurial;
047: import org.netbeans.modules.versioning.spi.VCSContext;
048:
049: /**
050: * A class to encapsulate a Repository and allow us to cache some values
051: *
052: * @author John Rice
053: */
054: public class HgRepositoryContextCache {
055: private static boolean hasHistory;
056: private static String pushDefault;
057: private static String pullDefault;
058: private static File root;
059:
060: private static VCSContext rootCtx;
061: private static Set<File> historyCtxRootFiles;
062: private static Set<File> pushCtxRootFiles;
063: private static Set<File> pullCtxRootFiles;
064:
065: public static boolean hasHistory(VCSContext ctx) {
066: Set<File> files;
067: if (ctx == null)
068: return false;
069: files = ctx.getRootFiles();
070:
071: if (files.equals(historyCtxRootFiles)) {
072: return hasHistory;
073: } else {
074: root = getRoot(ctx);
075: hasHistory = HgCommand.hasHistory(root);
076: historyCtxRootFiles = ctx.getRootFiles();
077: return hasHistory;
078: }
079:
080: }
081:
082: public static void setHasHistory(VCSContext ctx) {
083: historyCtxRootFiles = ctx.getRootFiles();
084: hasHistory = true;
085: }
086:
087: public static void resetPullDefault() {
088: pullCtxRootFiles = null;
089: }
090:
091: public static String getPullDefault(VCSContext ctx) {
092: Set<File> files;
093: if (ctx == null)
094: return null;
095: files = ctx.getRootFiles();
096:
097: if (files.equals(pullCtxRootFiles)) {
098: return pullDefault;
099: } else {
100: root = getRoot(ctx);
101: pullDefault = HgCommand.getPullDefault(root);
102: pullCtxRootFiles = ctx.getRootFiles();
103: return pullDefault;
104: }
105: }
106:
107: public static void resetPushDefault() {
108: pushCtxRootFiles = null;
109: }
110:
111: public static String getPushDefault(VCSContext ctx) {
112: Set<File> files;
113: if (ctx == null)
114: return null;
115: files = ctx.getRootFiles();
116:
117: if (files.equals(pushCtxRootFiles)) {
118: return pushDefault;
119: } else {
120: root = getRoot(ctx);
121: pushDefault = HgCommand.getPushDefault(root);
122: pushCtxRootFiles = ctx.getRootFiles();
123: return pushDefault;
124: }
125: }
126:
127: private static File getRoot(VCSContext ctx) {
128: if (ctx == rootCtx && root != null) {
129: return root;
130: } else {
131: root = HgUtils.getRootFile(ctx);
132: rootCtx = ctx;
133: return root;
134: }
135:
136: }
137: }
|