Skip to content

Commit 98b0b1b

Browse files
committed
Lazily initialize GitRatchetGradle to avoid loading JGit at configuration time
GitRatchetGradle is instantiated eagerly when SpotlessTaskService is created, which happens during Gradle configuration. Its static initializer installs a custom JGit SystemReader that reads ~/.gitconfig. This causes Gradle's configuration cache to fingerprint the file even when no Spotless tasks are requested. Defer instantiation to getRatchet(), which is only called during task execution. This avoids loading JGit classes and reading git config files at configuration time.
1 parent 46b8770 commit 98b0b1b

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ void registerApplyAlreadyRan(SpotlessApply task) {
9999
}
100100

101101
// <GitRatchet>
102-
private final GitRatchetGradle ratchet = new GitRatchetGradle();
102+
private GitRatchetGradle ratchet;
103103

104104
GitRatchetGradle getRatchet() {
105+
if (ratchet == null) {
106+
ratchet = new GitRatchetGradle();
107+
}
105108
return ratchet;
106109
}
107110

@@ -112,7 +115,9 @@ public void onFinish(FinishEvent var1) {
112115

113116
@Override
114117
public void close() throws Exception {
115-
ratchet.close();
118+
if (ratchet != null) {
119+
ratchet.close();
120+
}
116121
}
117122
// </GitRatchet>
118123

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigurationCacheTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.IOException;
1919

20+
import org.assertj.core.api.Assertions;
2021
import org.gradle.testkit.runner.GradleRunner;
2122
import org.junit.jupiter.api.Test;
2223

@@ -60,6 +61,32 @@ public void helpConfiguresIfTasksAreCreated() throws IOException {
6061
gradleRunner().withArguments("help").build();
6162
}
6263

64+
@Test
65+
public void jgitNotLoadedDuringConfiguration() throws IOException {
66+
setFile("build.gradle").toLines(
67+
"plugins {",
68+
" id 'com.diffplug.spotless'",
69+
"}",
70+
"repositories { mavenCentral() }",
71+
"apply plugin: 'java'",
72+
"spotless {",
73+
" java {",
74+
" googleJavaFormat()",
75+
" }",
76+
"}",
77+
"",
78+
"// Detect if JGit's SystemReader was replaced during configuration.",
79+
"// GitRatchetGradle.redirectJGitExecutions() replaces it, so if it",
80+
"// is still the default instance, JGit was not loaded.",
81+
"def sr = org.eclipse.jgit.util.SystemReader.getInstance()",
82+
"if (sr.getClass().getName().contains('DelegatingSystemReader')) {",
83+
" throw new GradleException('JGit SystemReader was replaced during configuration')",
84+
"}");
85+
setFile("src/main/java/test.java").toResource("java/googlejavaformat/JavaCodeUnformatted.test");
86+
String output = gradleRunner().withArguments("help").build().getOutput();
87+
Assertions.assertThat(output).doesNotContain("JGit SystemReader was replaced during configuration");
88+
}
89+
6390
@Test
6491
public void multipleRuns() throws IOException {
6592
setFile("build.gradle").toLines(

0 commit comments

Comments
 (0)