Skip to content

Commit

Permalink
fix: git check-ignore cannot follow symlinks (#108)
Browse files Browse the repository at this point in the history
```
java.lang.Exception: git check-ignore failed:
```

For exit code 1 - None of the provided paths are ignored.

```
java.lang.Exception: git check-ignore failed with code 128:
fatal: pathspec 'rust/protos/meta_event.proto' is beyond a symbolic link
```

For cannot follow symlinks.

Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored Dec 20, 2023
1 parent 5921cc6 commit 09c4fa4
Showing 1 changed file with 22 additions and 5 deletions.
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;
}
}

0 comments on commit 09c4fa4

Please sign in to comment.