From 31edfcd65064468d35e66f4018c3c91af728601d Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Sun, 30 Oct 2016 21:40:12 +0300 Subject: [PATCH] Fix .gitattributes pattern matching (fix #20) --- src/main/java/git/path/WildcardHelper.java | 11 +---------- .../path/matcher/path/RecursivePathMatcher.java | 11 +++++++++-- src/test/java/git/path/WildcardTest.java | 16 ++++++++-------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/main/java/git/path/WildcardHelper.java b/src/main/java/git/path/WildcardHelper.java index 325b876..d566d51 100644 --- a/src/main/java/git/path/WildcardHelper.java +++ b/src/main/java/git/path/WildcardHelper.java @@ -129,7 +129,7 @@ public static List splitPattern(@NotNull String pattern) { @NotNull public static List normalizePattern(@NotNull List 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 "/" @@ -165,15 +165,6 @@ public static List normalizePattern(@NotNull List 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; } diff --git a/src/main/java/git/path/matcher/path/RecursivePathMatcher.java b/src/main/java/git/path/matcher/path/RecursivePathMatcher.java index b1e44ac..3afde3c 100644 --- a/src/main/java/git/path/matcher/path/RecursivePathMatcher.java +++ b/src/main/java/git/path/matcher/path/RecursivePathMatcher.java @@ -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) { diff --git a/src/test/java/git/path/WildcardTest.java b/src/test/java/git/path/WildcardTest.java index 4b6024e..eff8c5f 100644 --- a/src/test/java/git/path/WildcardTest.java +++ b/src/test/java/git/path/WildcardTest.java @@ -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 @@ -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/", "*/", "*/", "**/"}}, }; } @@ -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},