Browse Source

Fix IgnoreRule for directory-only patterns

Patterns containing only a trailing slash have to be treated
as "global" patterns. For example: "classes/" matches "classes"
as well as "dir/classes" directory.
stable-0.11
Marc Strapetz 14 years ago
parent
commit
c87ae94c70
  1. 10
      org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreMatcherTest.java
  2. 39
      org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreRule.java

10
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 //Test matches for name-only, applies to file name or folder name
pattern = "src"; pattern = "src";
assertMatched(pattern, "/src");
assertMatched(pattern, "/src/");
assertMatched(pattern, "/src/a.c"); assertMatched(pattern, "/src/a.c");
assertMatched(pattern, "/src/new/a.c"); assertMatched(pattern, "/src/new/a.c");
assertMatched(pattern, "/new/src/a.c"); assertMatched(pattern, "/new/src/a.c");
assertMatched(pattern, "/file/src"); assertMatched(pattern, "/file/src");
//Test matches for name-only, applies only to folder names
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");
assertNotMatched(pattern, "/src");
assertNotMatched(pattern, "/file/src");
//Test matches for name-only, applies to file name or folder name //Test matches for name-only, applies to file name or folder name
//With a small wildcard //With a small wildcard

39
org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreRule.java

@ -91,9 +91,9 @@ public class IgnoreRule {
endIndex --; endIndex --;
dirOnly = true; dirOnly = true;
} }
boolean hasSlash = pattern.contains("/");
pattern = pattern.substring(startIndex, endIndex); pattern = pattern.substring(startIndex, endIndex);
boolean hasSlash = pattern.contains("/");
if (!hasSlash) if (!hasSlash)
nameOnly = true; nameOnly = true;
@ -188,8 +188,11 @@ public class IgnoreRule {
if (nameOnly) { if (nameOnly) {
//Iterate through each sub-name //Iterate through each sub-name
for (String folderName : target.split("/")) { final String[] segments = target.split("/");
if (folderName.equals(pattern)) for (int idx = 0; idx < segments.length; idx++) {
final String segmentName = segments[idx];
if (segmentName.equals(pattern) &&
doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
return true; return true;
} }
} }
@ -199,23 +202,29 @@ public class IgnoreRule {
if (matcher.isMatch()) if (matcher.isMatch())
return true; return true;
final String[] segments = target.split("/");
if (nameOnly) { 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 //Iterate through each sub-directory
matcher.reset(); matcher.reset();
matcher.append(folderName); matcher.append(segmentName);
if (matcher.isMatch()) if (matcher.isMatch() &&
doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
return true; return true;
} }
} else { } else {
//TODO: This is the slowest operation //TODO: This is the slowest operation
//This matches e.g. "/src/ne?" to "/src/new/file.c" //This matches e.g. "/src/ne?" to "/src/new/file.c"
matcher.reset(); matcher.reset();
for (String folderName : target.split("/")) { for (int idx = 0; idx < segments.length; idx++) {
if (folderName.length() > 0) final String segmentName = segments[idx];
matcher.append("/" + folderName); if (segmentName.length() > 0) {
matcher.append("/" + segmentName);
}
if (matcher.isMatch()) if (matcher.isMatch() &&
doesMatchDirectoryExpectations(isDirectory, idx, segments.length))
return true; return true;
} }
} }
@ -235,4 +244,14 @@ public class IgnoreRule {
public boolean getResult() { public boolean getResult() {
return !negation; 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;
}
} }
Loading…
Cancel
Save