Skip to content

Commit a9205b6

Browse files
committed
spotlessApply
1 parent f1f0faf commit a9205b6

File tree

11 files changed

+178
-55
lines changed

11 files changed

+178
-55
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.spotless;
217

318
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -66,7 +81,11 @@ public void install() throws Exception {
6681
final var gitHookFile = root.toPath().resolve(".git/hooks/pre-push").toFile();
6782
if (!gitHookFile.exists()) {
6883
logger.info("Git pre-push hook not found, creating it");
69-
gitHookFile.getParentFile().mkdirs();
84+
if (!gitHookFile.getParentFile().exists() && !gitHookFile.getParentFile().mkdirs()) {
85+
logger.error("Failed to create pre-push hook directory");
86+
return;
87+
}
88+
7089
if (!gitHookFile.createNewFile()) {
7190
logger.error("Failed to create pre-push hook file");
7291
return;
@@ -164,6 +183,7 @@ protected String preHookTemplate(String executor, String commandCheck, String co
164183

165184
public interface GitPreHookLogger {
166185
void info(String format, Object... arguments);
186+
167187
void error(String format, Object... arguments);
168188
}
169189
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.spotless;
217

318
import java.io.File;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.spotless;
217

318
import java.io.File;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.gradle.spotless;
217

318
import org.gradle.api.DefaultTask;
Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.gradle.spotless;
217

318
import static org.assertj.core.api.Assertions.assertThat;
@@ -11,29 +26,29 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
1126
// given
1227
final var gradlew = setFile("gradlew").toContent("");
1328
setFile(".git/config").toContent("");
29+
newFile(".git/hooks").mkdirs();
1430
setFile("build.gradle").toLines(
15-
"plugins {",
16-
" id 'java'",
17-
" id 'com.diffplug.spotless'",
18-
"}",
19-
"repositories { mavenCentral() }"
20-
);
31+
"plugins {",
32+
" id 'java'",
33+
" id 'com.diffplug.spotless'",
34+
"}",
35+
"repositories { mavenCentral() }");
2136

2237
// when
2338
var output = gradleRunner()
24-
.withArguments("spotlessInstallGitPrePushHook")
25-
.build()
26-
.getOutput();
39+
.withArguments("spotlessInstallGitPrePushHook")
40+
.build()
41+
.getOutput();
2742

2843
// then
2944
assertThat(output).contains("Installing git pre-push hook");
3045
assertThat(output).contains("Git pre-push hook not found, creating it");
3146
assertThat(output).contains("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push"));
3247

3348
final var content = getTestResource("git_pre_hook/pre-push.created")
34-
.replace("${executor}", gradlew.getAbsolutePath())
35-
.replace("${checkCommand}", "spotlessCheck")
36-
.replace("${applyCommand}", "spotlessApply");
49+
.replace("${executor}", gradlew.getAbsolutePath())
50+
.replace("${checkCommand}", "spotlessCheck")
51+
.replace("${applyCommand}", "spotlessApply");
3752
assertFile(".git/hooks/pre-push").hasContent(content);
3853
}
3954

@@ -43,28 +58,27 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
4358
final var gradlew = setFile("gradlew").toContent("");
4459
setFile(".git/config").toContent("");
4560
setFile("build.gradle").toLines(
46-
"plugins {",
47-
" id 'java'",
48-
" id 'com.diffplug.spotless'",
49-
"}",
50-
"repositories { mavenCentral() }"
51-
);
61+
"plugins {",
62+
" id 'java'",
63+
" id 'com.diffplug.spotless'",
64+
"}",
65+
"repositories { mavenCentral() }");
5266
setFile(".git/hooks/pre-push").toResource("git_pre_hook/pre-push.existing");
5367

5468
// when
5569
final var output = gradleRunner()
56-
.withArguments("spotlessInstallGitPrePushHook")
57-
.build()
58-
.getOutput();
70+
.withArguments("spotlessInstallGitPrePushHook")
71+
.build()
72+
.getOutput();
5973

6074
// then
6175
assertThat(output).contains("Installing git pre-push hook");
6276
assertThat(output).contains("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push"));
6377

6478
final var content = getTestResource("git_pre_hook/pre-push.existing-added")
65-
.replace("${executor}", gradlew.getAbsolutePath())
66-
.replace("${checkCommand}", "spotlessCheck")
67-
.replace("${applyCommand}", "spotlessApply");
79+
.replace("${executor}", gradlew.getAbsolutePath())
80+
.replace("${checkCommand}", "spotlessCheck")
81+
.replace("${applyCommand}", "spotlessApply");
6882
assertFile(".git/hooks/pre-push").hasContent(content);
6983
}
7084
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2024 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.spotless.maven;
217

318
import java.io.File;
Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package com.diffplug.spotless.maven;
217

18+
import static org.assertj.core.api.Assertions.assertThat;
19+
320
import java.io.IOException;
421

522
import org.junit.jupiter.api.Test;
623

7-
import static org.assertj.core.api.Assertions.assertThat;
8-
924
class SpotlessInstallPrePushHookMojoTest extends MavenIntegrationHarness {
1025

1126
@Test
@@ -17,20 +32,19 @@ public void should_create_pre_hook_file_when_hook_file_does_not_exists() throws
1732

1833
// when
1934
final var output = mavenRunner()
20-
.withArguments("spotless:install-git-pre-push-hook")
21-
.runNoError()
22-
.stdOutUtf8();
35+
.withArguments("spotless:install-git-pre-push-hook")
36+
.runNoError()
37+
.stdOutUtf8();
2338

2439
// then
2540
assertThat(output).contains("Installing git pre-push hook");
2641
assertThat(output).contains("Git pre-push hook not found, creating it");
2742
assertThat(output).contains("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push"));
2843

29-
3044
final var content = getTestResource("git_pre_hook/pre-push.created")
31-
.replace("${executor}", "mvn")
32-
.replace("${checkCommand}", "spotless:check")
33-
.replace("${applyCommand}", "spotless:apply");
45+
.replace("${executor}", "mvn")
46+
.replace("${checkCommand}", "spotless:check")
47+
.replace("${applyCommand}", "spotless:apply");
3448
assertFile(".git/hooks/pre-push").hasContent(content);
3549
}
3650

@@ -45,25 +59,25 @@ public void should_append_to_existing_pre_hook_file_when_hook_file_exists() thro
4559

4660
// when
4761
final var output = mavenRunner()
48-
.withArguments("spotless:install-git-pre-push-hook")
49-
.runNoError()
50-
.stdOutUtf8();
62+
.withArguments("spotless:install-git-pre-push-hook")
63+
.runNoError()
64+
.stdOutUtf8();
5165

5266
// then
5367
assertThat(output).contains("Installing git pre-push hook");
5468
assertThat(output).contains("Git pre-push hook installed successfully to the file " + newFile(".git/hooks/pre-push"));
5569

5670
final var content = getTestResource("git_pre_hook/pre-push.existing-added")
57-
.replace("${executor}", "mvn")
58-
.replace("${checkCommand}", "spotless:check")
59-
.replace("${applyCommand}", "spotless:apply");
71+
.replace("${executor}", "mvn")
72+
.replace("${checkCommand}", "spotless:check")
73+
.replace("${applyCommand}", "spotless:apply");
6074
assertFile(".git/hooks/pre-push").hasContent(content);
6175
}
6276

6377
private void writePomWithJavaLicenseHeaderStep() throws IOException {
6478
writePomWithJavaSteps(
65-
"<licenseHeader>",
66-
" <file>${basedir}/license.txt</file>",
67-
"</licenseHeader>");
79+
"<licenseHeader>",
80+
" <file>${basedir}/license.txt</file>",
81+
"</licenseHeader>");
6882
}
6983
}

0 commit comments

Comments
 (0)