From d7efa2f6a08edccc032c2c1561b26097b2aea7e1 Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 21 Dec 2023 00:24:28 +0800 Subject: [PATCH] fix: git check-ignore cannot follow symlinks Signed-off-by: tison --- .../io/korandoru/hawkeye/core/GitHelper.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/GitHelper.java b/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/GitHelper.java index 8e599a5..1809462 100644 --- a/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/GitHelper.java +++ b/hawkeye-core/src/main/java/io/korandoru/hawkeye/core/GitHelper.java @@ -81,14 +81,18 @@ public GitHelper(Path baseDir) { public void filterIgnoredFiles(Collection 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 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); @@ -96,9 +100,13 @@ public void filterIgnoredFiles(Collection files) { } 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 lines = Arrays.stream(output.split(System.lineSeparator())); @@ -108,4 +116,13 @@ public void filterIgnoredFiles(Collection 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; + } }