Skip to content
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

fix: git check-ignore cannot follow symlinks #108

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,32 @@ public GitHelper(Path baseDir) {
public void filterIgnoredFiles(Collection<String> files) {
final Process p = new ProcessBuilder()
.directory(baseDir.toFile())
.command("git", "check-ignore", "--stdin", "--no-index")
.command("git", "check-ignore", "--stdin")
.start();

final String output;
final Collection<String> localFiles = files.stream()
.filter(file -> noSymbolLink(baseDir.resolve(file)))
.toList();

log.debug("git check-ignore inputs {}", localFiles);
final String output;
try (OutputStream pStdIn = p.getOutputStream()) {
try (InputStream pStdOut = p.getInputStream()) {
IOUtils.writeLines(files, null, pStdIn, StandardCharsets.UTF_8);
IOUtils.writeLines(localFiles, null, pStdIn, StandardCharsets.UTF_8);
pStdIn.flush();
pStdIn.close();
output = IOUtils.toString(pStdOut, StandardCharsets.UTF_8);
}
}
log.debug("git check-ignore outputs {}", output);

if (p.waitFor() != 0) {
final int code = p.waitFor();
// 0 - One or more of the provided paths is ignored.
// 1 - None of the provided paths are ignored.
// 128 - A fatal error was encountered.
if (code != 0 && code != 1) {
final String error = IOUtils.toString(p.getErrorStream(), StandardCharsets.UTF_8);
throw new Exception("git check-ignore failed:\n" + error);
throw new Exception("git check-ignore failed with code " + code + ":\n" + error);
}

final Stream<String> lines = Arrays.stream(output.split(System.lineSeparator()));
Expand All @@ -108,4 +116,13 @@ public void filterIgnoredFiles(Collection<String> files) {
files.removeAll(ignoredFiles);
log.debug("Selected files after filter ignore files: {}", files);
}

@SneakyThrows
private static boolean noSymbolLink(Path path) {
if (path.toAbsolutePath().normalize().equals(path.toRealPath())) {
return true;
}
log.debug("Skip symbol link {}", path);
return false;
}
}