Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 33 additions & 49 deletions src/org/opensolaris/opengrok/index/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
*/

package org.opensolaris.opengrok.index;
Expand All @@ -36,21 +36,21 @@ public class Filter implements Serializable {
private static final long serialVersionUID = 3L;

/** The list of exact filenames */
private final Set<String> filename;
private final Set<String> filenames;
/** The list of filenames with wildcards */
private final List<Pattern> patterns;
/** The list of paths */
private final List<String> path;
private final List<String> paths;
/**
* The full list of all patterns. This list will be saved in the
* configuration file (if used)
*/
private final List<String> items;

public Filter() {
filename = new HashSet<>();
filenames = new HashSet<>();
patterns = new ArrayList<>();
path = new ArrayList<>();
paths = new ArrayList<>();
items = new PatternList(this);
}

Expand Down Expand Up @@ -94,68 +94,52 @@ public void add(String pattern) {
*/
public void clear() {
patterns.clear();
filename.clear();
path.clear();
filenames.clear();
paths.clear();
items.clear();
}

/**
* Should the file be ignored or not?
* Does the file match any of the filenames, patterns or paths ?
* @param file the file to check
* @return true if this file should be ignored, false otherwise
* @return true if this file matches, false otherwise
*/
public boolean match(File file) {
boolean ret = false;

String fileName = file.getName();
String fileName = file.getName(); // basename
String absolute = file.getAbsolutePath();

if (filename.contains(fileName)) {
ret = true;
} else {
for (Pattern p : patterns) {
Matcher m = p.matcher(fileName);
if (filenames.contains(fileName)) {
return true;
}

for (Pattern p : patterns) {
// Try to match the basename first.
Matcher m = p.matcher(fileName);
if (m.matches()) {
return true;
}
// Try the full path next.
if (p.pattern().contains("/")) {
m = p.matcher(absolute);
if (m.matches()) {
ret = true;
break;
}
if (p.pattern().contains("/")) {
m = p.matcher(absolute);
if (m.matches()) {
ret = true;
break;
}
return true;
}
}
}

if (!ret) {
for (String s : path) {
if (absolute.endsWith(s)) {
ret = true;
break;
}
for (String s : paths) {
if (absolute.endsWith(s)) {
return true;
}
}

//Check File extension
if (!ret) {
int start = fileName.indexOf(".");
if(start != -1){
String fileExtension = fileName.substring(start,fileName.length());
if (filename.contains(fileExtension)) {
ret = true;
}
}
}

return ret;
return false;
}

/**
* Should the file be ignored or not?
* Does the file name match any of the filenames, patterns or paths ?
* @param name the name of the file to check
* @return true if this pathname should be ignored, false otherwise
* @return true if this pathname matches, false otherwise
*/
public boolean match(String name) {
return match(new File(name));
Expand All @@ -171,12 +155,12 @@ private void addPattern(String pattern) {
patterns.add(compilePattern(pattern));
} else if (pattern.contains(File.separator)) {
if (pattern.charAt(0) == File.separatorChar) {
path.add(pattern);
paths.add(pattern);
} else {
path.add(File.separator + pattern);
paths.add(File.separator + pattern);
}
} else {
filename.add(pattern);
filenames.add(pattern);
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/org/opensolaris/opengrok/index/IgnoredDirs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.index;

Expand All @@ -35,11 +35,10 @@ public final class IgnoredDirs extends Filter {
private static final String[] defaultPatternsDirs = {
"SCCS",
"CVS",
"RCS",
// Teamware
"Codemgr_wsdata",
"deleted_files",
"CVSROOT",
"RCS",
"Codemgr_wsdata", // Teamware
"deleted_files", // Teamware
".svn",
".git",
".hg",
Expand Down
22 changes: 10 additions & 12 deletions src/org/opensolaris/opengrok/index/IgnoredFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.index;

Expand All @@ -40,26 +40,24 @@ public final class IgnoredFiles extends Filter {
"cscope.po.in",
"cscope.files",
"cscope.out",
// CVS
".cvsignore",
".cvsignore", // CVS
".repo",
".hgtags",
".p4config",
".hgtags", // Mercurial
".p4config", // Perforce
"*~",
".make.*",
".del-*",
"_MTN",
// File Extensions for Visual Studio and Mono Projects
".vspscc",
".suo",
".vssscc",
".vspscc", // Visual Studio
".vssscc", // Visual Studio
".suo", // Visual Studio user specific settings
".user",
".ncb",
".gpState",
".gpState", // Guidance automation toolkit (MS)
".snc",
".sln",
".vsmdi",
".dll",
".vsmdi", // Visual Studio tests
"*.dll",
".opengrok_skip_history",
};

Expand Down
8 changes: 4 additions & 4 deletions src/org/opensolaris/opengrok/index/IgnoredNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.index;

Expand Down Expand Up @@ -74,9 +74,9 @@ public void setItems(List<String> item) {
*/
public boolean ignore(File file) {
if (file.isFile()) {
return ignoredFiles.match(file);
return ignoredFiles.ignore(file);
} else {
return ignoredDirs.match(file);
return ignoredDirs.ignore(file);
}
}

Expand All @@ -87,7 +87,7 @@ public boolean ignore(File file) {
* @return true if this pathname should be ignored, false otherwise
*/
public boolean ignore(String name) {
return ignoredFiles.match(name) || ignoredDirs.match(name);
return ignoredFiles.ignore(name) || ignoredDirs.ignore(name);
}

public void clear() {
Expand Down
6 changes: 5 additions & 1 deletion test/org/opensolaris/opengrok/index/IgnoredNamesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/*
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
*/
package org.opensolaris.opengrok.index;

Expand Down Expand Up @@ -71,6 +71,10 @@ public void testIgnoredPatterns() {
assertFalse(instance.ignore("usr/src/foo/bar/usr.lib/main.c"));
assertFalse(instance.ignore("usr/src/foo/bar/usr.lib"));

/* Test handling of special directories. */
assertTrue(instance.ignore("usr/src/.git"));
assertFalse(instance.ignore("usr/src/foo.git"));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test where only FILE .repo will be ignored
but path which has .repo in it won't?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a test but with .git

/* cumulative test */
names = new ArrayList<>();
names.add("*.o");
Expand Down