Cleaned up whitespace, fix DOS line endings
[nbgit.git] / src / org / nbgit / util / exclude / Excludes.java
blob71263f489e0a938b8b1626c24c64abfacf2a778e
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.nbgit.util.exclude;
44 import java.io.File;
45 import java.util.HashMap;
46 import org.nbgit.Git;
47 import org.netbeans.api.project.ProjectManager;
48 import org.netbeans.api.queries.SharabilityQuery;
49 import org.openide.filesystems.FileUtil;
50 import org.spearce.jgit.lib.Repository;
52 /**
53 * Provides support for .gitignore files.
55 * To keep the querying interface fast, a cache of patterns are maintained.
57 public class Excludes {
59 private Excludes() {
62 private static final HashMap<Repository, ExcludeCache> cacheMap
63 = new HashMap<Repository, ExcludeCache>();
65 /**
66 * Check if a file should can be shared between projects as defined by
67 * NetBeans SharabilityQuery interface.
69 * @param file to check
70 * @return true, if the file can be shared.
72 public static boolean isSharable(File file) {
73 return SharabilityQuery.getSharability(file) != SharabilityQuery.NOT_SHARABLE;
76 /**
77 * Checks to see if a file is ignored.
79 * @param file to check
80 * @return true if the file is ignored
82 public static boolean isIgnored(File file) {
83 return isIgnored(file, true);
86 public static boolean isIgnored(File file, boolean checkSharability) {
87 if (file == null) {
88 return false;
91 File topFile = Git.getInstance().getTopmostManagedParent(file);
92 // We assume that the toplevel directory should not be ignored.
93 if (topFile == null || topFile.equals(file)) {
94 return false; // We assume that the Project should not be ignored.
96 if (file.isDirectory()) {
97 ProjectManager projectManager = ProjectManager.getDefault();
98 if (projectManager.isProject(FileUtil.toFileObject(file))) {
99 return false;
103 Repository repo = Git.getInstance().getRepository(topFile);
104 ExcludeCache cache = getCache(repo);
105 if (cache.isExcluded(file))
106 return true;
108 if (file.getName().equals(".gitignore") ||
109 file.getName().equals(".gitattributes") ||
110 file.getName().equals(".gitmodules"))
111 return false;
113 if (checkSharability && !isSharable(file)) {
114 return true;
116 return false;
120 private static ExcludeCache getCache(Repository repo) {
121 ExcludeCache cache = cacheMap.get(repo);
122 if (cache == null) {
123 cache = ExcludeCache.create(repo);
124 cacheMap.put(repo, cache);
126 return cache;