aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Strapetz2011-01-07 06:52:42 (EST)
committerMarc Strapetz2011-01-07 06:53:14 (EST)
commitc87ae94c70158ba2bcb310aa242102853d221f11 (patch)
treeb685fa1cdab56f4aaa9fc66c3ba950bd6113e9d2
parent7cd812940d25a78f71467ede0b2637211b2a12a1 (diff)
downloadjgit-c87ae94c70158ba2bcb310aa242102853d221f11.zip
jgit-c87ae94c70158ba2bcb310aa242102853d221f11.tar.gz
jgit-c87ae94c70158ba2bcb310aa242102853d221f11.tar.bz2
Fix IgnoreRule for directory-only patternsrefs/changes/16/2216/1
Patterns containing only a trailing slash have to be treated as "global" patterns. For example: "classes/" matches "classes" as well as "dir/classes" directory.
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreMatcherTest.java10
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreRule.java39
2 files changed, 39 insertions, 10 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreMatcherTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreMatcherTest.java
index ce2508a..7e771dc 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreMatcherTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreMatcherTest.java
@@ -242,11 +242,21 @@ public class IgnoreMatcherTest {
//Test matches for name-only, applies to file name or folder name
pattern = "src";
+ assertMatched(pattern, "/src");
+ assertMatched(pattern, "/src/");
assertMatched(pattern, "/src/a.c");
assertMatched(pattern, "/src/new/a.c");
assertMatched(pattern, "/new/src/a.c");
assertMatched(pattern, "/file/src");
+
+ //Test matches for name-only, applies only to folder names
+ pattern = "src/";
assertMatched(pattern, "/src/");
+ assertMatched(pattern, "/src/a.c");
+ assertMatched(pattern, "/src/new/a.c");
+ assertMatched(pattern, "/new/src/a.c");
+ assertNotMatched(pattern, "/src");
+ assertNotMatched(pattern, "/file/src");
//Test matches for name-only, applies to file name or folder name
//With a small wildcard
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreRule.java b/org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreRule.java
index b43b111..3c7fdc6 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreRule.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreRule.java
@@ -91,9 +91,9 @@ public class IgnoreRule {
endIndex --;
dirOnly = true;
}
- boolean hasSlash = pattern.contains("/");
pattern = pattern.substring(startIndex, endIndex);
+ boolean hasSlash = pattern.contains("/");
if (!hasSlash)
nameOnly = true;
@@ -188,8 +188,11 @@ public class IgnoreRule {
if (nameOnly) {
//Iterate through each sub-name
- for (String folderName : target.split("/")) {
- if (folderName.equals(pattern))
+ final String[] segments = target.split("/");
+ for (int idx = 0; idx < segments.length; idx++) {
+ final String segmentName = segments[idx];
+ if (segmentName.equals(pattern) &&
+ doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
return true;
}
}
@@ -199,23 +202,29 @@ public class IgnoreRule {
if (matcher.isMatch())
return true;
+ final String[] segments = target.split("/");
if (nameOnly) {
- for (String folderName : target.split("/")) {
+ for (int idx = 0; idx < segments.length; idx++) {
+ final String segmentName = segments[idx];
//Iterate through each sub-directory
matcher.reset();
- matcher.append(folderName);
- if (matcher.isMatch())
+ matcher.append(segmentName);
+ if (matcher.isMatch() &&
+ doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
return true;
}
} else {
//TODO: This is the slowest operation
//This matches e.g. "/src/ne?" to "/src/new/file.c"
matcher.reset();
- for (String folderName : target.split("/")) {
- if (folderName.length() > 0)
- matcher.append("/" + folderName);
+ for (int idx = 0; idx < segments.length; idx++) {
+ final String segmentName = segments[idx];
+ if (segmentName.length() > 0) {
+ matcher.append("/" + segmentName);
+ }
- if (matcher.isMatch())
+ if (matcher.isMatch() &&
+ doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
return true;
}
}
@@ -235,4 +244,14 @@ public class IgnoreRule {
public boolean getResult() {
return !negation;
}
+
+ private boolean doesMatchDirectoryExpectations(boolean isDirectory, int segmentIdx, int segmentLength) {
+ // The segment we are checking is a directory, expectations are met.
+ if (segmentIdx < segmentLength - 1) {
+ return true;
+ }
+
+ // We are checking the last part of the segment for which isDirectory has to be considered.
+ return !dirOnly || isDirectory;
+ }
} \ No newline at end of file