Repository navigation
Use JGit to match .gitignore rules in unlinked files search #17656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
f3825cf
fa4b71f
f071e9e
fe13413
b96dee0
d9f54f3
95df3a2
1b3333e
a1f5ee4
88b72c7
00eb285
636de64
bcc9fd2
558a899
b50de34
d008e24
0b5fb9c
f1075c3
6f8e3e6
2097521
8cf34d8
979e92d
e2b1df8
5627ca3
5ddc8cf
edfcd55
313c926
3312102
dd2db0a
4c8ad28
741ea56
ad9a60a
5cc306c
d0b1241
4fb9b7f
7406231
2217d94
f080d6a
6ae3fb9
f1adf20
1389916
3619f5d
89e538e
9030714
b8da57d
0e73221
a3c867d
758a430
2a0ca2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,80 +1,68 @@ | ||
| package org.jabref.gui.externalfiles; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.file.DirectoryStream; | ||
| import java.nio.file.FileSystems; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.PathMatcher; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.eclipse.jgit.ignore.IgnoreNode; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import static java.util.function.Predicate.not; | ||
|
|
||
| public class GitIgnoreFileFilter implements DirectoryStream.Filter<Path> { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(GitIgnoreFileFilter.class); | ||
|
|
||
| private final Set<PathMatcher> gitIgnorePatterns; | ||
| private final IgnoreNode ignoreNode; | ||
| private final Path baseDir; | ||
|
|
||
| public GitIgnoreFileFilter(Path path) { | ||
| Path currentPath = path; | ||
| while ((currentPath != null) && !Files.exists(currentPath.resolve(".gitignore"))) { | ||
| currentPath = currentPath.getParent(); | ||
| } | ||
|
|
||
| this.ignoreNode = new IgnoreNode(); | ||
|
|
||
| if (currentPath == null) { | ||
| // we did not find any gitignore, set baseDir to provided path and use default ignores | ||
| this.baseDir = path; | ||
| gitIgnorePatterns = Set.of(".git", ".DS_Store", "desktop.ini", "Thumbs.db").stream() | ||
| // duplicate code as below | ||
| .map(line -> "glob:" + line) | ||
| .map(matcherString -> FileSystems.getDefault().getPathMatcher(matcherString)) | ||
| .collect(Collectors.toSet()); | ||
| // Add default ignores | ||
| try (java.io.ByteArrayInputStream input = new java.io.ByteArrayInputStream( | ||
| ".git\n.DS_Store\ndesktop.ini\nThumbs.db\n.gitignore\n".getBytes(java.nio.charset.StandardCharsets.UTF_8))) { | ||
| ignoreNode.parse(input); | ||
| } catch (IOException e) { | ||
| LOGGER.info("Could not parse default ignores", e); | ||
| } | ||
| } else { | ||
| this.baseDir = currentPath; | ||
| Path gitIgnore = currentPath.resolve(".gitignore"); | ||
| Set<PathMatcher> patterns; | ||
| try { | ||
| patterns = Files.readAllLines(gitIgnore).stream() | ||
| .map(String::trim) | ||
| .filter(not(String::isEmpty)) | ||
| .filter(line -> !line.startsWith("#")) | ||
| // expand patterns so that leading "**/" also matches files in the base directory | ||
| .flatMap(line -> { | ||
| if (line.startsWith("**/")) { | ||
| return Stream.of(line, line.substring(3)); | ||
| } | ||
| return Stream.of(line); | ||
| }) | ||
| // convert to Java syntax for Glob patterns | ||
| .map(line -> "glob:" + line) | ||
| .map(matcherString -> FileSystems.getDefault().getPathMatcher(matcherString)) | ||
| .collect(Collectors.toSet()); | ||
| // we want to ignore ".gitignore" itself | ||
| patterns.add(FileSystems.getDefault().getPathMatcher("glob:.gitignore")); | ||
| try (InputStream input = Files.newInputStream(gitIgnore)) { | ||
| ignoreNode.parse(input); | ||
| } catch (IOException e) { | ||
| LOGGER.info("Could not read .gitignore from {}", gitIgnore, e); | ||
| patterns = Set.of(); | ||
| } | ||
| gitIgnorePatterns = patterns; | ||
| // Always ignore .gitignore itself and common defaults | ||
| try (java.io.ByteArrayInputStream input = new java.io.ByteArrayInputStream( | ||
| ".gitignore\n.git\n.DS_Store\ndesktop.ini\nThumbs.db\n".getBytes(java.nio.charset.StandardCharsets.UTF_8))) { | ||
|
Sandeep332005 marked this conversation as resolved.
Resolved
|
||
| ignoreNode.parse(input); | ||
| } catch (IOException e) { | ||
| LOGGER.info("Could not add default ignores", e); | ||
|
Sandeep332005 marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean accept(Path path) throws IOException { | ||
| // Match patterns relative to baseDir because .gitignore patterns are applied relative to their location | ||
| Path relative = safeRelativize(baseDir, path); | ||
| // We assume that git does not stop at a pattern, but tries all. We implement that behavior | ||
| return gitIgnorePatterns.stream().noneMatch(filter -> | ||
| // for patterns like "*.png" or ".gitignore" | ||
| filter.matches(relative.getFileName()) || | ||
| // for patterns like "ignore/*" or "**/*.png" | ||
| filter.matches(relative)); | ||
| String relativeStr = relative.toString().replace('\\', '/'); | ||
|
Sandeep332005 marked this conversation as resolved.
Resolved
|
||
| if (relativeStr.isEmpty()) { | ||
| return true; | ||
| } | ||
| boolean isDirectory = Files.isDirectory(path); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we pass |
||
| Boolean ignored = ignoreNode.checkIgnored(relativeStr, isDirectory); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Windows, |
||
| // checkIgnored returns Boolean.TRUE if ignored, Boolean.FALSE if explicitly not ignored, or null if no rule matched | ||
| return ignored == null || !ignored; | ||
| } | ||
|
|
||
| private static Path safeRelativize(Path base, Path child) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we keep the parent’s !desktop.ini rule active when a subdirectory has its own .gitignore