Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions gradle/changelog.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -51,15 +51,32 @@ 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.
*
* @param logger The logger for recording messages.
* @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();
}

/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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}
*/
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -45,6 +45,12 @@ public abstract class SpotlessInstallPrePushHookTask extends DefaultTask {
@Internal
abstract Property<Boolean> getIsRootExecution();

/**
* The extra arguments inserted before the Spotless tasks in the generated pre-push hook.
*/
@Internal
abstract Property<String> getArgs();

/**
* Executes the task to install the Git pre-push hook.
*
Expand Down Expand Up @@ -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();
}
}
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions testlib/src/main/resources/git_pre_hook/pre-push.created-args-tpl
Original file line number Diff line number Diff line change
@@ -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 #####
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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("\\", "/");
}
Expand Down