From 0e0650a8947a90eb57a104edb75d73c419f08cf0 Mon Sep 17 00:00:00 2001 From: Sergey Nuyanzin Date: Tue, 8 Sep 2026 09:18:41 +0200 Subject: [PATCH] Allow passing args for git hook --- CHANGES.md | 2 + gradle/changelog.gradle | 6 +-- .../spotless/GitPrePushHookInstaller.java | 25 +++++++++-- .../GitPrePushHookInstallerGradle.java | 6 ++- .../GitPrePushHookInstallerMaven.java | 6 ++- plugin-gradle/CHANGES.md | 2 + .../spotless/SpotlessExtensionImpl.java | 3 ++ .../SpotlessInstallPrePushHookTask.java | 10 ++++- plugin-maven/CHANGES.md | 2 + .../maven/SpotlessInstallPrePushHookMojo.java | 10 ++++- .../git_pre_hook/pre-push.created-args-tpl | 11 +++++ .../spotless/GitPrePushHookInstallerTest.java | 45 ++++++++++++++++++- 12 files changed, 115 insertions(+), 13 deletions(-) create mode 100644 testlib/src/main/resources/git_pre_hook/pre-push.created-args-tpl diff --git a/CHANGES.md b/CHANGES.md index e01a596177..392be2543b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +- `GitPrePushHookInstaller` now accepts extra executor arguments that are inserted before the Spotless goals in the generated pre-push hook (e.g. `-T1C` for Maven or `--parallel` for Gradle). ## [4.10.2] - 2026-09-04 ### Fixed diff --git a/gradle/changelog.gradle b/gradle/changelog.gradle index 0fc643a634..5243ccc849 100644 --- a/gradle/changelog.gradle +++ b/gradle/changelog.gradle @@ -29,9 +29,9 @@ spotlessChangelog { if (project == rootProject) { gradle.taskGraph.whenReady { taskGraph -> def changelogPushTasks = taskGraph.allTasks.stream() - .filter { t -> t.name == 'changelogPush' } - .map { t -> t.path } - .toList() + .filter { t -> t.name == 'changelogPush' } + .map { t -> t.path } + .toList() if (changelogPushTasks.size() > 1) { // make sure only one changelog gets published per tag/commit throw new IllegalArgumentException("Run changelogPush one at a time:\n" + changelogPushTasks.join('\n')) diff --git a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java index fe262e8afd..c8a5f3d035 100644 --- a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java +++ b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 DiffPlug + * Copyright 2025-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,6 +51,11 @@ public abstract class GitPrePushHookInstaller { */ protected final File root; + /** + * The extra arguments inserted before the Spotless goals in the generated hook. + */ + protected final String args; + /** * Constructor to initialize the GitPrePushHookInstaller with a logger and repository root path. * @@ -58,8 +63,20 @@ public abstract class GitPrePushHookInstaller { * @param root The root directory of the Git repository. */ protected GitPrePushHookInstaller(GitPreHookLogger logger, File root) { + this(logger, root, ""); + } + + /** + * Constructor to initialize the GitPrePushHookInstaller with a logger, repository root path and extra executor arguments. + * + * @param logger The logger for recording messages. + * @param root The root directory of the Git repository. + * @param args The extra arguments inserted before the Spotless goals in the generated hook. + */ + protected GitPrePushHookInstaller(GitPreHookLogger logger, File root, String args) { this.logger = requireNonNull(logger, "logger can not be null"); this.root = requireNonNull(root, "root file can not be null"); + this.args = args == null ? "" : args.trim(); } /** @@ -240,12 +257,14 @@ private void uninstall(File gitHookFile) throws Exception { protected String preHookTemplate(Executor executor, String commandCheck, String commandApply) { var spotlessHook = ""; + final var argsPrefix = args.isEmpty() ? "" : args + " "; + spotlessHook += "\n"; spotlessHook += "\n" + HOOK_HEADER; spotlessHook += "\nSPOTLESS_EXECUTOR=" + executorPath(executor); - spotlessHook += "\nif ! $SPOTLESS_EXECUTOR " + commandCheck + " ; then"; + spotlessHook += "\nif ! $SPOTLESS_EXECUTOR " + argsPrefix + commandCheck + " ; then"; spotlessHook += "\n echo 1>&2 \"spotless found problems, running " + commandApply + "; commit the result and re-push\""; - spotlessHook += "\n $SPOTLESS_EXECUTOR " + commandApply; + spotlessHook += "\n $SPOTLESS_EXECUTOR " + argsPrefix + commandApply; spotlessHook += "\n exit 1"; spotlessHook += "\nfi"; spotlessHook += "\n" + HOOK_FOOTER; diff --git a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerGradle.java b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerGradle.java index 3cc61e566d..f3a212c267 100644 --- a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerGradle.java +++ b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerGradle.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 DiffPlug + * Copyright 2025-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,10 @@ public GitPrePushHookInstallerGradle(GitPreHookLogger logger, File root) { super(logger, root); } + public GitPrePushHookInstallerGradle(GitPreHookLogger logger, File root, String args) { + super(logger, root, args); + } + /** * {@inheritDoc} */ diff --git a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerMaven.java b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerMaven.java index f8b3943335..b6f9fd63ef 100644 --- a/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerMaven.java +++ b/lib/src/main/java/com/diffplug/spotless/GitPrePushHookInstallerMaven.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 DiffPlug + * Copyright 2025-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,10 @@ public GitPrePushHookInstallerMaven(GitPreHookLogger logger, File root) { super(logger, root); } + public GitPrePushHookInstallerMaven(GitPreHookLogger logger, File root, String args) { + super(logger, root, args); + } + /** * {@inheritDoc} */ diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 7f7b5cbde9..0297a33f3e 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Added +- `spotlessInstallGitPrePushHook` accepts a new `spotless.hook.args` project property, inserted before the Spotless tasks in the generated hook so the hook can run e.g. in parallel: `./gradlew spotlessInstallGitPrePushHook -Pspotless.hook.args=--parallel`. ## [8.10.2] - 2026-09-04 ### Fixed diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java index 34dfa13fd4..056f441d7a 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java @@ -44,6 +44,9 @@ public SpotlessExtensionImpl(Project project) { task.setDescription(INSTALL_GIT_PRE_PUSH_HOOK_DESCRIPTION); task.getRootDir().set(project.getRootDir()); task.getIsRootExecution().set(project.equals(project.getRootProject())); + if (project.hasProperty("spotless.hook.args")) { + task.getArgs().set(String.valueOf(project.property("spotless.hook.args"))); + } }); project.afterEvaluate(unused -> { diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessInstallPrePushHookTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessInstallPrePushHookTask.java index 6cd3d15458..161cfb34b3 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessInstallPrePushHookTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessInstallPrePushHookTask.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 DiffPlug + * Copyright 2025-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,12 @@ public abstract class SpotlessInstallPrePushHookTask extends DefaultTask { @Internal abstract Property getIsRootExecution(); + /** + * The extra arguments inserted before the Spotless tasks in the generated pre-push hook. + */ + @Internal + abstract Property getArgs(); + /** * Executes the task to install the Git pre-push hook. * @@ -79,7 +85,7 @@ public void error(String format, Object... arguments) { } }; - final var installer = new GitPrePushHookInstallerGradle(logger, getRootDir().get()); + final var installer = new GitPrePushHookInstallerGradle(logger, getRootDir().get(), getArgs().getOrElse("")); installer.install(); } } diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index d91c055ea7..a716fa6347 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Added +- `install-git-pre-push-hook` accepts a new `spotless.hook.args` argument, inserted before the Spotless goals in the generated hook so the hook can run e.g. in parallel: `mvn spotless:install-git-pre-push-hook -Dspotless.hook.args=-T1C`. ## [3.10.2] - 2026-09-04 ### Fixed diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojo.java index 51fcb1d7cc..94e41f0f01 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojo.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 DiffPlug + * Copyright 2025-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,12 @@ public class SpotlessInstallPrePushHookMojo extends AbstractMojo { @Parameter(defaultValue = "${project}", readonly = true, required = true) private MavenProject project; + /** + * The extra arguments inserted before the Spotless goals in the generated pre-push hook. + */ + @Parameter(property = "spotless.hook.args", defaultValue = "") + private String hookArgs; + /** * Executes the Mojo, installing the Git pre-push hook for the Spotless plugin. * @@ -75,7 +81,7 @@ public void error(String format, Object... arguments) { }; try { - final var installer = new GitPrePushHookInstallerMaven(logger, project.getBasedir()); + final var installer = new GitPrePushHookInstallerMaven(logger, project.getBasedir(), hookArgs); installer.install(); } catch (Exception e) { throw new MojoExecutionException("Unable to install pre-push hook", e); diff --git a/testlib/src/main/resources/git_pre_hook/pre-push.created-args-tpl b/testlib/src/main/resources/git_pre_hook/pre-push.created-args-tpl new file mode 100644 index 0000000000..04f758b6c2 --- /dev/null +++ b/testlib/src/main/resources/git_pre_hook/pre-push.created-args-tpl @@ -0,0 +1,11 @@ +#!/bin/sh + + +##### SPOTLESS HOOK START ##### +SPOTLESS_EXECUTOR=${executor} +if ! $SPOTLESS_EXECUTOR ${args} ${checkCommand} ; then + echo 1>&2 "spotless found problems, running ${applyCommand}; commit the result and re-push" + $SPOTLESS_EXECUTOR ${args} ${applyCommand} + exit 1 +fi +##### SPOTLESS HOOK END ##### \ No newline at end of file diff --git a/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java b/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java index da07ef0614..8d9ac9f5eb 100644 --- a/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/GitPrePushHookInstallerTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 DiffPlug + * Copyright 2025-2026 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -228,6 +228,41 @@ public void should_create_pre_hook_file_for_maven_when_hook_file_does_not_exists assertFile(".git/hooks/pre-push").hasContent(content); } + @Test + public void should_include_extra_args_in_maven_hook_when_provided() throws Exception { + // given + final var maven = new GitPrePushHookInstallerMaven(logger, rootFolder(), "-T1C"); + setFile("mvnw").toContent(""); + setFile(".git/config").toContent(""); + + // when + maven.install(); + + // then + assertThat(logs).containsExactly( + "Installing git pre-push hook", + "Git pre-push hook not found, creating it", + "Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push").getAbsolutePath()); + + final var content = mavenHookContentWithArgs("git_pre_hook/pre-push.created-args-tpl", ExecutorType.WRAPPER, "-T1C"); + assertFile(".git/hooks/pre-push").hasContent(content); + } + + @Test + public void should_include_extra_args_in_gradle_hook_when_provided() throws Exception { + // given + final var gradle = new GitPrePushHookInstallerGradle(logger, rootFolder(), "--parallel"); + setFile("gradlew").toContent(""); + setFile(".git/config").toContent(""); + + // when + gradle.install(); + + // then + final var content = gradleHookContentWithArgs("git_pre_hook/pre-push.created-args-tpl", ExecutorType.WRAPPER, "--parallel"); + assertFile(".git/hooks/pre-push").hasContent(content); + } + @Test public void should_use_global_maven_when_maven_wrapper_is_not_installed() throws Exception { // given @@ -379,6 +414,14 @@ private String mavenHookContent(String resourcePath, ExecutorType executorType) .replace("${applyCommand}", "spotless:apply"); } + private String mavenHookContentWithArgs(String resourcePath, ExecutorType executorType, String args) { + return mavenHookContent(resourcePath, executorType).replace("${args}", args); + } + + private String gradleHookContentWithArgs(String resourcePath, ExecutorType executorType, String args) { + return gradleHookContent(resourcePath, executorType).replace("${args}", args); + } + private String fileAbsolutePath(File file) { return file.getAbsolutePath().replace("\\", "/"); }