Skip to content

Commit 26423f1

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

File tree

7 files changed

+42
-16
lines changed

7 files changed

+42
-16
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ public void install() throws Exception {
8282
return;
8383
}
8484

85+
// Since hook installation is a manual task triggered by the user,
86+
// using synchronized locking is acceptable. It ensures thread safety
87+
// without affecting overall performance, and provides a simple and reliable solution.
8588
synchronized (LOCK) {
8689
if (installing) {
8790
logger.warn("Parallel Spotless Git pre-push hook installation detected, skipping installation");

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: 12 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,12 @@ 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+
getLogger().debug("Skipping Spotless pre-push hook installation because it is not being executed from the root project.");
62+
return;
63+
}
64+
5365
final var logger = new GitPreHookLogger() {
5466
@Override
5567
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: 10 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,12 @@ 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+
getLog().debug("Skipping Spotless pre-push hook installation for non-root project: " + project.getName());
57+
return;
58+
}
59+
5960
final var logger = new GitPreHookLogger() {
6061
@Override
6162
public void info(String format, Object... arguments) {
@@ -74,7 +75,7 @@ public void error(String format, Object... arguments) {
7475
};
7576

7677
try {
77-
final var installer = new GitPrePushHookInstallerMaven(logger, baseDir);
78+
final var installer = new GitPrePushHookInstallerMaven(logger, project.getBasedir());
7879
installer.install();
7980
} catch (Exception e) {
8081
throw new MojoExecutionException("Unable to install pre-push hook", e);

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,20 @@ private String mavenHookContent(String resourcePath, ExecutorType executorType)
376376

377377
private void parallelRun(ThrowableRun runnable) {
378378
IntStream.range(0, 3)
379-
.mapToObj(i -> new Thread(() -> {
380-
try {
381-
runnable.run();
382-
} catch (Exception e) {
383-
throw new RuntimeException(e);
384-
}
385-
}))
379+
.mapToObj(i -> {
380+
final var t = new Thread(() -> {
381+
try {
382+
runnable.run();
383+
} catch (Exception e) {
384+
throw new RuntimeException(e);
385+
}
386+
});
387+
388+
t.setName("GitPrePushHookInstallerTest-" + i);
389+
t.setDaemon(true);
390+
391+
return t;
392+
})
386393
.peek(Thread::start)
387394
.collect(toList())
388395
.forEach(t -> {

0 commit comments

Comments
 (0)