26
26
import java .util .List ;
27
27
import java .util .Locale ;
28
28
import java .util .Objects ;
29
- import java .util .function .Predicate ;
30
29
import java .util .regex .Pattern ;
31
30
import java .util .stream .Collectors ;
32
31
32
+ import org .eclipse .jgit .lib .ObjectId ;
33
33
import org .gradle .api .DefaultTask ;
34
34
import org .gradle .api .GradleException ;
35
35
import org .gradle .api .file .FileCollection ;
44
44
import org .gradle .api .tasks .incremental .IncrementalTaskInputs ;
45
45
46
46
import com .diffplug .common .base .Errors ;
47
+ import com .diffplug .common .base .Preconditions ;
47
48
import com .diffplug .common .base .StringPrinter ;
49
+ import com .diffplug .common .base .Throwing ;
48
50
import com .diffplug .spotless .FormatExceptionPolicy ;
49
51
import com .diffplug .spotless .FormatExceptionPolicyStrict ;
50
52
import com .diffplug .spotless .Formatter ;
@@ -86,6 +88,13 @@ public void setLineEndingsPolicy(LineEnding.Policy lineEndingsPolicy) {
86
88
this .lineEndingsPolicy = Objects .requireNonNull (lineEndingsPolicy );
87
89
}
88
90
91
+ ObjectId treeSha = ObjectId .zeroId ();
92
+
93
+ @ Input
94
+ public ObjectId getRatchetSha () {
95
+ return treeSha ;
96
+ }
97
+
89
98
@ Deprecated
90
99
@ Internal
91
100
public boolean isPaddedCell () {
@@ -179,10 +188,13 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
179
188
Files .createDirectories (outputDirectory .toPath ());
180
189
}
181
190
182
- Predicate <File > shouldInclude ;
191
+ Throwing . Specific . Predicate <File , IOException > shouldInclude ;
183
192
if (this .filePatterns .isEmpty ()) {
184
193
shouldInclude = file -> true ;
185
194
} else {
195
+ Preconditions .checkArgument (treeSha == ObjectId .zeroId (),
196
+ "Cannot use 'ratchetFrom' and '-PspotlessFiles' at the same time" );
197
+
186
198
// a list of files has been passed in via project property
187
199
final String [] includePatterns = this .filePatterns .split ("," );
188
200
final List <Pattern > compiledIncludePatterns = Arrays .stream (includePatterns )
@@ -197,32 +209,37 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception {
197
209
try (Formatter formatter = buildFormatter ()) {
198
210
inputs .outOfDate (inputDetails -> {
199
211
File input = inputDetails .getFile ();
200
- if ( shouldInclude . test ( input ) && input . isFile ()) {
201
- try {
212
+ try {
213
+ if ( shouldInclude . test ( input ) && input . isFile ()) {
202
214
processInputFile (formatter , input );
203
- } catch (IOException e ) {
204
- throw Errors .asRuntime (e );
205
215
}
216
+ } catch (IOException e ) {
217
+ throw Errors .asRuntime (e );
206
218
}
207
219
});
208
220
}
209
221
210
222
inputs .removed (removedDetails -> {
211
223
File input = removedDetails .getFile ();
212
- if ( shouldInclude . test ( input )) {
213
- try {
224
+ try {
225
+ if ( shouldInclude . test ( input )) {
214
226
deletePreviousResult (input );
215
- } catch (IOException e ) {
216
- throw Errors .asRuntime (e );
217
227
}
228
+ } catch (IOException e ) {
229
+ throw Errors .asRuntime (e );
218
230
}
219
231
});
220
232
}
221
233
222
234
private void processInputFile (Formatter formatter , File input ) throws IOException {
223
235
File output = getOutputFile (input );
224
236
getLogger ().debug ("Applying format to " + input + " and writing to " + output );
225
- PaddedCell .DirtyState dirtyState = PaddedCell .calculateDirtyState (formatter , input );
237
+ PaddedCell .DirtyState dirtyState ;
238
+ if (treeSha != ObjectId .zeroId () && GitRatchet .isClean (getProject (), treeSha , input )) {
239
+ dirtyState = PaddedCell .isClean ();
240
+ } else {
241
+ dirtyState = PaddedCell .calculateDirtyState (formatter , input );
242
+ }
226
243
if (dirtyState .isClean ()) {
227
244
// Remove previous output if it exists
228
245
Files .deleteIfExists (output .toPath ());
0 commit comments