Skip to content

Commit daabaad

Browse files
committed
Add JGit binary check for plain text acceptance
1 parent 047cb44 commit daabaad

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/text/PlainTextParser.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
import org.openrewrite.Parser;
2121
import org.openrewrite.SourceFile;
2222
import org.openrewrite.internal.EncodingDetectingInputStream;
23+
import org.openrewrite.jgit.diff.RawText;
2324
import org.openrewrite.tree.ParseError;
2425
import org.openrewrite.tree.ParsingEventListener;
2526
import org.openrewrite.tree.ParsingExecutionContextView;
2627

2728
import java.io.File;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.nio.file.Files;
2832
import java.nio.file.Path;
2933
import java.nio.file.PathMatcher;
3034
import java.nio.file.Paths;
@@ -89,7 +93,11 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
8993

9094
@Override
9195
public boolean accept(Path path) {
92-
return true;
96+
try (InputStream is = Files.newInputStream(path)) {
97+
return !RawText.isBinary(is);
98+
} catch (IOException ignored) {
99+
return false;
100+
}
93101
}
94102

95103
@Override
@@ -133,10 +141,11 @@ public boolean accept(Path path) {
133141
}
134142
// PathMather will not evaluate the path "README.md" to be matched by the pattern "**/README.md"
135143
// This is counter-intuitive for most users and would otherwise require separate exclusions for files at the root and files in subdirectories
136-
if(!path.isAbsolute() && !path.startsWith(File.separator)) {
144+
if (!path.isAbsolute() && !path.startsWith(File.separator)) {
137145
return accept(Paths.get("/" + path));
138146
}
139-
return false;
147+
148+
return super.accept(path);
140149
}
141150
};
142151
}

0 commit comments

Comments
 (0)