Skip to content

Commit b321809

Browse files
committed
do not execute for non root
1 parent 32a8292 commit b321809

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
* Fixed `spotlessInstallGitPrePushHook` task: now compatible with Gradle configuration cache and safe for parallel execution.
67
### Changed
78
* **BREAKING** Bump the required Gradle to `7.3` and required Java to `17`. ([#2375](https://github.com/diffplug/spotless/issues/2375), [#2540](https://github.com/diffplug/spotless/pull/2540))
89
* Bump JGit from `6.10.1` to `7.3.0` ([#2257](https://github.com/diffplug/spotless/pull/2257))

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public SpotlessExtensionImpl(Project project) {
4242
task.setGroup(BUILD_SETUP_TASK_GROUP);
4343
task.setDescription(INSTALL_GIT_PRE_PUSH_HOOK_DESCRIPTION);
4444
task.getRootDir().set(project.getRootDir());
45+
task.getIsRootExecution().set(project.equals(project.getRootProject()));
4546
});
4647

4748
project.afterEvaluate(unused -> {

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessInstallPrePushHookTask.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public abstract class SpotlessInstallPrePushHookTask extends DefaultTask {
3939
@Internal
4040
abstract Property<File> getRootDir();
4141

42+
/**
43+
* Determines whether this task is being executed from the root project.
44+
*/
45+
@Internal
46+
abstract Property<Boolean> getIsRootExecution();
47+
4248
/**
4349
* Executes the task to install the Git pre-push hook.
4450
*
@@ -50,6 +56,11 @@ public abstract class SpotlessInstallPrePushHookTask extends DefaultTask {
5056
*/
5157
@TaskAction
5258
public void performAction() throws Exception {
59+
// if is not root project, skip it
60+
if (!getIsRootExecution().get()) {
61+
return;
62+
}
63+
5364
final var logger = new GitPreHookLogger() {
5465
@Override
5566
public void info(String format, Object... arguments) {

plugin-maven/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
* Improved support for multi-module projects: Git pre-push hook is now always installed in the root `.git/hooks` directory by resolving the top-level project base directory.
67
### Changes
78
* **BREAKING** Bump the required Java to `17`. ([#2375](https://github.com/diffplug/spotless/issues/2375), [#2540](https://github.com/diffplug/spotless/pull/2540))
89
* Bump JGit from `6.10.1` to `7.3.0` ([#2257](https://github.com/diffplug/spotless/pull/2257))

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessInstallPrePushHookMojo.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
*/
1616
package com.diffplug.spotless.maven;
1717

18-
import java.io.File;
19-
2018
import org.apache.maven.plugin.AbstractMojo;
2119
import org.apache.maven.plugin.MojoExecutionException;
2220
import org.apache.maven.plugin.MojoFailureException;
2321
import org.apache.maven.plugins.annotations.Mojo;
2422
import org.apache.maven.plugins.annotations.Parameter;
23+
import org.apache.maven.project.MavenProject;
2524

2625
import com.diffplug.spotless.GitPrePushHookInstaller.GitPreHookLogger;
2726
import com.diffplug.spotless.GitPrePushHookInstallerMaven;
@@ -37,12 +36,8 @@
3736
@Mojo(name = AbstractSpotlessMojo.GOAL_PRE_PUSH_HOOK, threadSafe = true)
3837
public class SpotlessInstallPrePushHookMojo extends AbstractMojo {
3938

40-
/**
41-
* The base directory of the Maven project where the Git pre-push hook will be installed.
42-
* This parameter is automatically set to the root directory of the current project.
43-
*/
44-
@Parameter(defaultValue = "${project.basedir}", readonly = true, required = true)
45-
private File baseDir;
39+
@Parameter(defaultValue = "${project}", readonly = true, required = true)
40+
private MavenProject project;
4641

4742
/**
4843
* Executes the Mojo, installing the Git pre-push hook for the Spotless plugin.
@@ -56,6 +51,11 @@ public class SpotlessInstallPrePushHookMojo extends AbstractMojo {
5651
*/
5752
@Override
5853
public void execute() throws MojoExecutionException, MojoFailureException {
54+
// if is not root project, skip it
55+
if (!project.isExecutionRoot()) {
56+
return;
57+
}
58+
5959
final var logger = new GitPreHookLogger() {
6060
@Override
6161
public void info(String format, Object... arguments) {
@@ -74,7 +74,7 @@ public void error(String format, Object... arguments) {
7474
};
7575

7676
try {
77-
final var installer = new GitPrePushHookInstallerMaven(logger, baseDir);
77+
final var installer = new GitPrePushHookInstallerMaven(logger, project.getBasedir());
7878
installer.install();
7979
} catch (Exception e) {
8080
throw new MojoExecutionException("Unable to install pre-push hook", e);

0 commit comments

Comments
 (0)