diff --git a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java index 0226e409d8..fe262e8afd 100644 --- a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java +++ b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java @@ -266,7 +266,7 @@ protected String preHookTemplate(Executor executor, String commandCheck, String private String executorPath(Executor executor) { final var wrapper = executorWrapperFile(executor); if (wrapper.exists()) { - return "./" + wrapper.getName(); + return wrapper.getAbsolutePath().replace("\\", "/"); } logger.info("Local %s wrapper (%s) not found, falling back to global command '%s'", diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index d9f8e8d06b..987999e6d5 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changes * Bump default `ktfmt` version to latest `0.58` -> `0.59`. ([#2681](https://github.com/diffplug/spotless/pull/2681) +* Use absolute path in the git pre push hook ### Fixed - palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index ee0d73d81b..fc51b506ef 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changes * Bump default `ktfmt` version to latest `0.58` -> `0.59`. ([#2681](https://github.com/diffplug/spotless/pull/2681) +* Use absolute path in the git pre push hook ### Fixed - palantirJavaFormat is no longer arbitrarily set to outdated versions on Java 17, latest available version is always used ([#2686](https://github.com/diffplug/spotless/pull/2686) fixes [#2685](https://github.com/diffplug/spotless/issues/2685)) diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojoTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojoTest.java index 904770c0d8..519f4b7aaa 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojoTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojoTest.java @@ -102,9 +102,9 @@ private void writePomWithJavaLicenseHeaderStep() throws IOException { private String getHookContent(String resourceFile) { final var executorFile = executorWrapperFile(); - final var executorPath = executorFile.exists() ? executorFile.getName() : "mvn"; + final var executorPath = executorFile.exists() ? executorFile.getAbsolutePath().replace("\\", "/") : "mvn"; return getTestResource(resourceFile) - .replace("${executor}", "./" + executorPath) + .replace("${executor}", executorPath) .replace("${checkCommand}", "spotless:check") .replace("${applyCommand}", "spotless:apply"); } diff --git a/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java b/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java index a5be4ea43c..da07ef0614 100644 --- a/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java @@ -20,6 +20,7 @@ import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; +import java.io.File; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.IntStream; @@ -251,7 +252,7 @@ public void should_use_global_maven_when_maven_wrapper_is_not_installed() throws public void should_use_maven_bat_wrapper_when_exists_for_windows() { // given System.setProperty("os.name", "Windows 10"); - setFile("mvnw.bat").toContent(""); + final var batFile = setFile("mvnw.bat").toContent(""); setFile("mvnw.cmd").toContent(""); final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder()); @@ -260,14 +261,14 @@ public void should_use_maven_bat_wrapper_when_exists_for_windows() { final var hook = gradle.preHookTemplate(MAVEN, "spotless:check", "spotless:apply"); // then - assertThat(hook).contains("SPOTLESS_EXECUTOR=./mvnw.bat"); + assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(batFile)); } @Test public void should_use_maven_cmd_wrapper_when_exists_for_windows() { // given System.setProperty("os.name", "Windows 10"); - setFile("mvnw.cmd").toContent(""); + final var executorFile = setFile("mvnw.cmd").toContent(""); final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder()); @@ -275,7 +276,7 @@ public void should_use_maven_cmd_wrapper_when_exists_for_windows() { final var hook = gradle.preHookTemplate(MAVEN, "spotless:check", "spotless:apply"); // then - assertThat(hook).contains("SPOTLESS_EXECUTOR=./mvnw.cmd"); + assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(executorFile)); } @Test @@ -297,7 +298,7 @@ public void should_use_maven_global_when_bat_and_cmd_files_not_exists_for_window public void should_use_gradle_bat_wrapper_when_exists_for_windows() { // given System.setProperty("os.name", "Windows 10"); - setFile("gradlew.bat").toContent(""); + final var executorFile = setFile("gradlew.bat").toContent(""); setFile("gradlew.cmd").toContent(""); setFile("gradlew").toContent(""); @@ -307,14 +308,14 @@ public void should_use_gradle_bat_wrapper_when_exists_for_windows() { final var hook = gradle.preHookTemplate(GRADLE, "spotlessCheck", "spotlessApply"); // then - assertThat(hook).contains("SPOTLESS_EXECUTOR=./gradlew.bat"); + assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(executorFile)); } @Test public void should_use_gradle_cmd_wrapper_when_exists_for_windows() { // given System.setProperty("os.name", "Windows 10"); - setFile("gradlew.cmd").toContent(""); + final var executorFile = setFile("gradlew.cmd").toContent(""); setFile("gradlew").toContent(""); final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder()); @@ -323,7 +324,7 @@ public void should_use_gradle_cmd_wrapper_when_exists_for_windows() { final var hook = gradle.preHookTemplate(GRADLE, "spotlessCheck", "spotlessApply"); // then - assertThat(hook).contains("SPOTLESS_EXECUTOR=./gradlew.cmd"); + assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(executorFile)); } @Test @@ -366,18 +367,22 @@ public void should_handle_parallel_installation() { private String gradleHookContent(String resourcePath, ExecutorType executorType) { return getTestResource(resourcePath) - .replace("${executor}", executorType == ExecutorType.WRAPPER ? "./" + newFile("gradlew").getName() : "gradle") + .replace("${executor}", executorType == ExecutorType.WRAPPER ? fileAbsolutePath(newFile("gradlew")) : "gradle") .replace("${checkCommand}", "spotlessCheck") .replace("${applyCommand}", "spotlessApply"); } private String mavenHookContent(String resourcePath, ExecutorType executorType) { return getTestResource(resourcePath) - .replace("${executor}", executorType == ExecutorType.WRAPPER ? "./" + newFile("mvnw").getName() : "mvn") + .replace("${executor}", executorType == ExecutorType.WRAPPER ? fileAbsolutePath(newFile("mvnw")) : "mvn") .replace("${checkCommand}", "spotless:check") .replace("${applyCommand}", "spotless:apply"); } + private String fileAbsolutePath(File file) { + return file.getAbsolutePath().replace("\\", "/"); + } + private void parallelRun(ThrowableRun runnable) { IntStream.range(0, 5) .mapToObj(i -> new Thread(() -> {