From 46ff624644888a3e4b345c59dbd87a608764dbad Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Thu, 18 Feb 2016 18:26:36 +0100 Subject: [PATCH 1/2] make the matching in Filter class more predictable fixes #1015 --- .../opensolaris/opengrok/index/Filter.java | 82 ++++++++----------- .../opengrok/index/IgnoredDirs.java | 9 +- .../opengrok/index/IgnoredFiles.java | 22 +++-- .../opengrok/index/IgnoredNames.java | 8 +- .../opengrok/index/IgnoredNamesTest.java | 6 +- 5 files changed, 56 insertions(+), 71 deletions(-) diff --git a/src/org/opensolaris/opengrok/index/Filter.java b/src/org/opensolaris/opengrok/index/Filter.java index 74984018535..87e7dbe37ba 100644 --- a/src/org/opensolaris/opengrok/index/Filter.java +++ b/src/org/opensolaris/opengrok/index/Filter.java @@ -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; @@ -36,11 +36,11 @@ public class Filter implements Serializable { private static final long serialVersionUID = 3L; /** The list of exact filenames */ - private final Set filename; + private final Set filenames; /** The list of filenames with wildcards */ private final List patterns; /** The list of paths */ - private final List path; + private final List paths; /** * The full list of all patterns. This list will be saved in the * configuration file (if used) @@ -48,9 +48,9 @@ public class Filter implements Serializable { private final List items; public Filter() { - filename = new HashSet<>(); + filenames = new HashSet<>(); patterns = new ArrayList<>(); - path = new ArrayList<>(); + paths = new ArrayList<>(); items = new PatternList(this); } @@ -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)); @@ -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); } } diff --git a/src/org/opensolaris/opengrok/index/IgnoredDirs.java b/src/org/opensolaris/opengrok/index/IgnoredDirs.java index 773509854c2..c7465515871 100644 --- a/src/org/opensolaris/opengrok/index/IgnoredDirs.java +++ b/src/org/opensolaris/opengrok/index/IgnoredDirs.java @@ -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; @@ -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", diff --git a/src/org/opensolaris/opengrok/index/IgnoredFiles.java b/src/org/opensolaris/opengrok/index/IgnoredFiles.java index 4cd0f71c8cc..7da6a872c01 100644 --- a/src/org/opensolaris/opengrok/index/IgnoredFiles.java +++ b/src/org/opensolaris/opengrok/index/IgnoredFiles.java @@ -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; @@ -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", }; diff --git a/src/org/opensolaris/opengrok/index/IgnoredNames.java b/src/org/opensolaris/opengrok/index/IgnoredNames.java index 473c7ac3928..468b77ad2ea 100644 --- a/src/org/opensolaris/opengrok/index/IgnoredNames.java +++ b/src/org/opensolaris/opengrok/index/IgnoredNames.java @@ -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; @@ -74,9 +74,9 @@ public void setItems(List 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); } } @@ -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() { diff --git a/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java b/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java index 9c599eb62b5..b894ea4949a 100644 --- a/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java +++ b/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java @@ -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; @@ -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")); + /* cumulative test */ names = new ArrayList<>(); names.add("*.o"); From 5c4d35e68e9dd96047124b7d8fcbd913f618edaf Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Fri, 19 Feb 2016 10:58:16 +0100 Subject: [PATCH 2/2] add test for .git as non final path component --- test/org/opensolaris/opengrok/index/IgnoredNamesTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java b/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java index b894ea4949a..a38f4fceec3 100644 --- a/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java +++ b/test/org/opensolaris/opengrok/index/IgnoredNamesTest.java @@ -73,6 +73,7 @@ public void testIgnoredPatterns() { /* Test handling of special directories. */ assertTrue(instance.ignore("usr/src/.git")); + assertFalse(instance.ignore("usr/src/.git/foo")); assertFalse(instance.ignore("usr/src/foo.git")); /* cumulative test */