Skip to content

Commit

Permalink
Fix .gitattributes pattern matching (fix #20)
Browse files Browse the repository at this point in the history
  • Loading branch information
bozaro committed Oct 30, 2016
1 parent 433e393 commit 31edfcd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
11 changes: 1 addition & 10 deletions src/main/java/git/path/WildcardHelper.java
Expand Up @@ -129,7 +129,7 @@ public static List<String> splitPattern(@NotNull String pattern) {
@NotNull
public static List<String> normalizePattern(@NotNull List<String> tokens) {
// By default without slashes using mask for files in all subdirectories
if (tokens.size() == 1 && !tokens.get(0).contains("/")) {
if ((tokens.size() == 1) && !tokens.get(0).startsWith("/")) {
tokens.add(0, "**/");
}
// Normalized pattern always starts with "/"
Expand Down Expand Up @@ -165,15 +165,6 @@ public static List<String> normalizePattern(@NotNull List<String> tokens) {
}
index++;
}
// Remove tailing "**/" and "*"
while (!tokens.isEmpty()) {
final String token = tokens.get(tokens.size() - 1);
if (token.equals("**/")) {
tokens.remove(tokens.size() - 1);
} else {
break;
}
}
return tokens;
}

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/git/path/matcher/path/RecursivePathMatcher.java
Expand Up @@ -42,15 +42,22 @@ public PathMatcher createChild(@NotNull String name, boolean isDir) {
if (index < nameMatchers.length && nameMatchers[index].isMatch(name, isDir)) {
if (nameMatchers[index].isRecursive()) {
childs[count++] = index;
if (nameMatchers[index + 1].isMatch(name, isDir)) {
if (index + 1 == nameMatchers.length) {
if (!exact) {
return AlwaysMatcher.INSTANCE;
}
if (isDir) {
childs[count++] = index + 1;
}
} else if (nameMatchers[index + 1].isMatch(name, isDir)) {
if (index + 2 == nameMatchers.length) {
if (!exact || !isDir) {
return AlwaysMatcher.INSTANCE;
}
}
childs[count++] = index + 2;
changed = true;
}
changed = true;
} else {
if (index + 1 == nameMatchers.length) {
if (!exact || !isDir) {
Expand Down
16 changes: 8 additions & 8 deletions src/test/java/git/path/WildcardTest.java
Expand Up @@ -42,12 +42,12 @@ public static Object[][] normalizePatternData() {
return new Object[][]{
// Simple mask
new Object[]{"/", new String[0]},
new Object[]{"*/", new String[]{"*/"}},
new Object[]{"*/", new String[]{"*/", "**/"}},
new Object[]{"*", new String[]{"**/", "*"}},
new Object[]{"**", new String[]{"**/", "*"}},
new Object[]{"**/", new String[]{}},
new Object[]{"**/", new String[]{"**/"}},
new Object[]{"foo", new String[]{"**/", "foo"}},
new Object[]{"foo/", new String[]{"foo/"}},
new Object[]{"foo/", new String[]{"**/", "foo/"}},
new Object[]{"/foo", new String[]{"foo"}},

// Convert path file mask
Expand All @@ -70,9 +70,9 @@ public static Object[][] normalizePatternData() {
new Object[]{"foo/**", new String[]{"foo/", "**/", "*"}},
new Object[]{"foo/**/*", new String[]{"foo/", "**/", "*"}},
new Object[]{"foo/**/*/*", new String[]{"foo/", "*/", "**/", "*"}},
new Object[]{"foo/**/", new String[]{"foo/"}},
new Object[]{"foo/**/*/", new String[]{"foo/", "*/"}},
new Object[]{"foo/**/*/*/", new String[]{"foo/", "*/", "*/"}},
new Object[]{"foo/**/", new String[]{"foo/", "**/"}},
new Object[]{"foo/**/*/", new String[]{"foo/", "*/", "**/"}},
new Object[]{"foo/**/*/*/", new String[]{"foo/", "*/", "*/", "**/"}},
};
}

Expand Down Expand Up @@ -112,8 +112,8 @@ public static Object[][] pathMatcherData() {
new Object[]{"/bar", "foo/bar", null, null},
new Object[]{"bar/", "foo/bar", null, null},
new Object[]{"b*r/", "foo/bar", null, null},
new Object[]{"bar/", "foo/bar/", null, true},
new Object[]{"b*r/", "foo/bar/", null, true},
new Object[]{"bar/", "foo/bar/", true, true},
new Object[]{"b*r/", "foo/bar/", true, true},
new Object[]{"b[a-z]r", "foo/bar", true, true},
new Object[]{"b[a-z]r", "foo/b0r", null, null},
new Object[]{"b[a-z]r", "foo/b0r/", false, false},
Expand Down

0 comments on commit 31edfcd

Please sign in to comment.