Skip to content

Commit 80a1496

Browse files
authored
git pre push hook, use absolute path to the executor (#2701)
2 parents a0ac600 + 81cf5d8 commit 80a1496

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected String preHookTemplate(Executor executor, String commandCheck, String
266266
private String executorPath(Executor executor) {
267267
final var wrapper = executorWrapperFile(executor);
268268
if (wrapper.exists()) {
269-
return "./" + wrapper.getName();
269+
return wrapper.getAbsolutePath().replace("\\", "/");
270270
}
271271

272272
logger.info("Local %s wrapper (%s) not found, falling back to global command '%s'",

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changes
77
* Bump default `ktfmt` version to latest `0.58` -> `0.59`. ([#2681](https://github.com/diffplug/spotless/pull/2681)
8+
* Use absolute path in the git pre push hook
89
### Fixed
910
- 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))
1011

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changes
77
* Bump default `ktfmt` version to latest `0.58` -> `0.59`. ([#2681](https://github.com/diffplug/spotless/pull/2681)
8+
* Use absolute path in the git pre push hook
89
### Fixed
910
- 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))
1011

plugin-maven/src/test/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ private void writePomWithJavaLicenseHeaderStep() throws IOException {
102102

103103
private String getHookContent(String resourceFile) {
104104
final var executorFile = executorWrapperFile();
105-
final var executorPath = executorFile.exists() ? executorFile.getName() : "mvn";
105+
final var executorPath = executorFile.exists() ? executorFile.getAbsolutePath().replace("\\", "/") : "mvn";
106106
return getTestResource(resourceFile)
107-
.replace("${executor}", "./" + executorPath)
107+
.replace("${executor}", executorPath)
108108
.replace("${checkCommand}", "spotless:check")
109109
.replace("${applyCommand}", "spotless:apply");
110110
}

testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.stream.Collectors.toList;
2121
import static org.assertj.core.api.Assertions.assertThat;
2222

23+
import java.io.File;
2324
import java.util.List;
2425
import java.util.concurrent.CopyOnWriteArrayList;
2526
import java.util.stream.IntStream;
@@ -251,7 +252,7 @@ public void should_use_global_maven_when_maven_wrapper_is_not_installed() throws
251252
public void should_use_maven_bat_wrapper_when_exists_for_windows() {
252253
// given
253254
System.setProperty("os.name", "Windows 10");
254-
setFile("mvnw.bat").toContent("");
255+
final var batFile = setFile("mvnw.bat").toContent("");
255256
setFile("mvnw.cmd").toContent("");
256257

257258
final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder());
@@ -260,22 +261,22 @@ public void should_use_maven_bat_wrapper_when_exists_for_windows() {
260261
final var hook = gradle.preHookTemplate(MAVEN, "spotless:check", "spotless:apply");
261262

262263
// then
263-
assertThat(hook).contains("SPOTLESS_EXECUTOR=./mvnw.bat");
264+
assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(batFile));
264265
}
265266

266267
@Test
267268
public void should_use_maven_cmd_wrapper_when_exists_for_windows() {
268269
// given
269270
System.setProperty("os.name", "Windows 10");
270-
setFile("mvnw.cmd").toContent("");
271+
final var executorFile = setFile("mvnw.cmd").toContent("");
271272

272273
final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder());
273274

274275
// when
275276
final var hook = gradle.preHookTemplate(MAVEN, "spotless:check", "spotless:apply");
276277

277278
// then
278-
assertThat(hook).contains("SPOTLESS_EXECUTOR=./mvnw.cmd");
279+
assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(executorFile));
279280
}
280281

281282
@Test
@@ -297,7 +298,7 @@ public void should_use_maven_global_when_bat_and_cmd_files_not_exists_for_window
297298
public void should_use_gradle_bat_wrapper_when_exists_for_windows() {
298299
// given
299300
System.setProperty("os.name", "Windows 10");
300-
setFile("gradlew.bat").toContent("");
301+
final var executorFile = setFile("gradlew.bat").toContent("");
301302
setFile("gradlew.cmd").toContent("");
302303
setFile("gradlew").toContent("");
303304

@@ -307,14 +308,14 @@ public void should_use_gradle_bat_wrapper_when_exists_for_windows() {
307308
final var hook = gradle.preHookTemplate(GRADLE, "spotlessCheck", "spotlessApply");
308309

309310
// then
310-
assertThat(hook).contains("SPOTLESS_EXECUTOR=./gradlew.bat");
311+
assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(executorFile));
311312
}
312313

313314
@Test
314315
public void should_use_gradle_cmd_wrapper_when_exists_for_windows() {
315316
// given
316317
System.setProperty("os.name", "Windows 10");
317-
setFile("gradlew.cmd").toContent("");
318+
final var executorFile = setFile("gradlew.cmd").toContent("");
318319
setFile("gradlew").toContent("");
319320

320321
final var gradle = new GitPrePushHookInstallerMaven(logger, rootFolder());
@@ -323,7 +324,7 @@ public void should_use_gradle_cmd_wrapper_when_exists_for_windows() {
323324
final var hook = gradle.preHookTemplate(GRADLE, "spotlessCheck", "spotlessApply");
324325

325326
// then
326-
assertThat(hook).contains("SPOTLESS_EXECUTOR=./gradlew.cmd");
327+
assertThat(hook).contains("SPOTLESS_EXECUTOR=" + fileAbsolutePath(executorFile));
327328
}
328329

329330
@Test
@@ -366,18 +367,22 @@ public void should_handle_parallel_installation() {
366367

367368
private String gradleHookContent(String resourcePath, ExecutorType executorType) {
368369
return getTestResource(resourcePath)
369-
.replace("${executor}", executorType == ExecutorType.WRAPPER ? "./" + newFile("gradlew").getName() : "gradle")
370+
.replace("${executor}", executorType == ExecutorType.WRAPPER ? fileAbsolutePath(newFile("gradlew")) : "gradle")
370371
.replace("${checkCommand}", "spotlessCheck")
371372
.replace("${applyCommand}", "spotlessApply");
372373
}
373374

374375
private String mavenHookContent(String resourcePath, ExecutorType executorType) {
375376
return getTestResource(resourcePath)
376-
.replace("${executor}", executorType == ExecutorType.WRAPPER ? "./" + newFile("mvnw").getName() : "mvn")
377+
.replace("${executor}", executorType == ExecutorType.WRAPPER ? fileAbsolutePath(newFile("mvnw")) : "mvn")
377378
.replace("${checkCommand}", "spotless:check")
378379
.replace("${applyCommand}", "spotless:apply");
379380
}
380381

382+
private String fileAbsolutePath(File file) {
383+
return file.getAbsolutePath().replace("\\", "/");
384+
}
385+
381386
private void parallelRun(ThrowableRun runnable) {
382387
IntStream.range(0, 5)
383388
.mapToObj(i -> new Thread(() -> {

0 commit comments

Comments
 (0)