-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find Unlinked files should ignore Thumbs.db, etc (#8800)
Co-authored-by: Christoph <siedlerkiller@gmail.com> Co-authored-by: ThiloteE <73715071+ThiloteE@users.noreply.github.com> Co-authored-by: “zhaoqingying123“ <710870305@qq.com> Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
- Loading branch information
1 parent
5990776
commit 2d15917
Showing
10 changed files
with
323 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/org/jabref/gui/externalfiles/ChainedFilters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.jabref.gui.externalfiles; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Chains the given filters - if ALL of them accept, the result is also accepted | ||
*/ | ||
public class ChainedFilters implements DirectoryStream.Filter<Path> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ChainedFilters.class); | ||
|
||
private DirectoryStream.Filter<Path>[] filters; | ||
|
||
public ChainedFilters(DirectoryStream.Filter<Path>... filters) { | ||
this.filters = filters; | ||
} | ||
|
||
@Override | ||
public boolean accept(Path entry) throws IOException { | ||
return Arrays.stream(filters).allMatch(filter -> { | ||
try { | ||
return filter.accept(entry); | ||
} catch (IOException e) { | ||
LOGGER.error("Could not apply filter", e); | ||
return true; | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/jabref/gui/externalfiles/GitIgnoreFileFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.jabref.gui.externalfiles; | ||
|
||
import java.io.IOException; | ||
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.HashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
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 Set<PathMatcher> gitIgnorePatterns; | ||
|
||
public GitIgnoreFileFilter(Path path) { | ||
Path currentPath = path; | ||
while ((currentPath != null) && !Files.exists(currentPath.resolve(".gitignore"))) { | ||
currentPath = currentPath.getParent(); | ||
} | ||
if (currentPath == null) { | ||
// we did not find any gitignore, lets use the default | ||
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()); | ||
} else { | ||
Path gitIgnore = currentPath.resolve(".gitignore"); | ||
try { | ||
Set<PathMatcher> plainGitIgnorePatternsFromGitIgnoreFile = Files.readAllLines(gitIgnore).stream() | ||
.map(line -> line.trim()) | ||
.filter(not(String::isEmpty)) | ||
.filter(line -> !line.startsWith("#")) | ||
// convert to Java syntax for Glob patterns | ||
.map(line -> "glob:" + line) | ||
.map(matcherString -> FileSystems.getDefault().getPathMatcher(matcherString)) | ||
.collect(Collectors.toSet()); | ||
gitIgnorePatterns = new HashSet<>(plainGitIgnorePatternsFromGitIgnoreFile); | ||
// we want to ignore ".gitignore" itself | ||
gitIgnorePatterns.add(FileSystems.getDefault().getPathMatcher("glob:.gitignore")); | ||
} catch (IOException e) { | ||
LOGGER.info("Could not read .gitignore from {}", gitIgnore, e); | ||
gitIgnorePatterns = Set.of(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean accept(Path path) throws IOException { | ||
// We assume that git does not stop at a patern, but tries all. We implement that behavior | ||
return gitIgnorePatterns.stream().noneMatch(filter -> | ||
// we need this one for "*.png" | ||
filter.matches(path.getFileName()) || | ||
// we need this one for "**/*.png" | ||
filter.matches(path)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.