Skip to content

Commit ca026b8

Browse files
author
Dimitrii Lipiridi
committed
feat: Remove checking ratchetFrom from IDE hook
1 parent 1d6b717 commit ca026b8

File tree

4 files changed

+48
-68
lines changed

4 files changed

+48
-68
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void setEncoding(String name) {
124124

125125
/** @see #setRatchetFrom(String) */
126126
public String getRatchetFrom() {
127-
return ratchetFrom.equals(RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL) ? spotless.getRatchetFrom() : ratchetFrom;
127+
return ratchetFrom == RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL ? spotless.getRatchetFrom() : ratchetFrom;
128128
}
129129

130130
/**

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,16 @@ private List<File> collectFiles(FormatterFactory formatterFactory, FormatterConf
264264
}
265265
final String[] includePatterns = this.filePatterns.split(",");
266266
final List<Pattern> compiledIncludePatterns = Arrays.stream(includePatterns)
267-
.map(Pattern::compile)
268-
.collect(Collectors.toList());
267+
.map(Pattern::compile)
268+
.collect(Collectors.toList());
269269
final Predicate<File> shouldInclude = file -> compiledIncludePatterns
270-
.stream()
271-
.anyMatch(filePattern -> filePattern.matcher(file.getAbsolutePath())
272-
.matches());
270+
.stream()
271+
.anyMatch(filePattern -> filePattern.matcher(file.getAbsolutePath())
272+
.matches());
273273
return files
274-
.stream()
275-
.filter(shouldInclude)
276-
.collect(toList());
274+
.stream()
275+
.filter(shouldInclude)
276+
.collect(toList());
277277
} catch (IOException e) {
278278
throw new PluginException("Unable to scan file tree rooted at " + baseDir, e);
279279
}
@@ -384,8 +384,4 @@ private UpToDateChecker createUpToDateChecker(Iterable<Formatter> formatters) {
384384
}
385385
return UpToDateChecker.wrapWithBuildContext(checker, buildContext);
386386
}
387-
388-
protected File getBaseDir() {
389-
return baseDir;
390-
}
391387
}

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

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -15,64 +15,60 @@
1515
*/
1616
package com.diffplug.spotless.maven;
1717

18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
1822
import com.diffplug.common.base.Errors;
1923
import com.diffplug.common.io.ByteStreams;
2024
import com.diffplug.spotless.Formatter;
2125
import com.diffplug.spotless.PaddedCell;
2226

23-
import java.io.File;
24-
import java.io.IOException;
25-
import java.nio.file.Files;
26-
import java.util.List;
27-
2827
class IdeHook {
2928

3029
private static void dumpIsClean() {
3130
System.err.println("IS CLEAN");
3231
}
3332

34-
static void performHook(Iterable<File> projectFiles, String path, Formatter formatter, File projectDir, String ratchetFrom, boolean spotlessIdeHookUseStdIn, boolean spotlessIdeHookUseStdOut) {
33+
//No need to check ratchet (using isClean()) as it is performed in Gradle's IDE hook, since we have already gathered the available git files from ratchet.
34+
static void performHook(Iterable<File> projectFiles, Formatter formatter, String path, boolean spotlessIdeHookUseStdIn, boolean spotlessIdeHookUseStdOut) {
3535
File file = new File(path);
3636
if (!file.isAbsolute()) {
3737
System.err.println("Argument passed to spotlessIdeHook must be an absolute path");
3838
return;
3939
}
40-
if (projectContainsFile(projectFiles, file)) {
41-
try {
42-
if (ratchetFrom != null) {
43-
GitRatchetMaven ratchet = GitRatchetMaven.instance();
44-
if (ratchet.isClean(projectDir, ratchet.rootTreeShaOf(projectDir, ratchetFrom), file)) {
45-
dumpIsClean();
46-
return;
47-
}
48-
}
49-
byte[] bytes;
50-
if (spotlessIdeHookUseStdIn) {
51-
bytes = ByteStreams.toByteArray(System.in);
52-
} else {
53-
bytes = Files.readAllBytes(file.toPath());
54-
}
55-
PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes);
56-
if (dirty.isClean()) {
57-
dumpIsClean();
58-
} else if (dirty.didNotConverge()) {
59-
System.err.println("DID NOT CONVERGE");
60-
System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md");
40+
41+
if (!projectContainsFile(projectFiles, file)) {
42+
return;
43+
}
44+
45+
try {
46+
byte[] bytes;
47+
if (spotlessIdeHookUseStdIn) {
48+
bytes = ByteStreams.toByteArray(System.in);
49+
} else {
50+
bytes = Files.readAllBytes(file.toPath());
51+
}
52+
PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes);
53+
if (dirty.isClean()) {
54+
dumpIsClean();
55+
} else if (dirty.didNotConverge()) {
56+
System.err.println("DID NOT CONVERGE");
57+
System.err.println("See details https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md");
58+
} else {
59+
System.err.println("IS DIRTY");
60+
if (spotlessIdeHookUseStdOut) {
61+
dirty.writeCanonicalTo(System.out);
6162
} else {
62-
System.err.println("IS DIRTY");
63-
if (spotlessIdeHookUseStdOut) {
64-
dirty.writeCanonicalTo(System.out);
65-
} else {
66-
dirty.writeCanonicalTo(file);
67-
}
63+
dirty.writeCanonicalTo(file);
6864
}
69-
} catch (IOException e) {
70-
e.printStackTrace(System.err);
71-
throw Errors.asRuntime(e);
72-
} finally {
73-
System.err.close();
74-
System.out.close();
7565
}
66+
} catch (IOException e) {
67+
e.printStackTrace(System.err);
68+
throw Errors.asRuntime(e);
69+
} finally {
70+
System.err.close();
71+
System.out.close();
7672
}
7773
}
7874

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
import java.io.File;
1919
import java.io.IOException;
2020

21-
import java.util.List;
22-
2321
import org.apache.maven.plugin.MojoExecutionException;
2422
import org.apache.maven.plugins.annotations.Mojo;
23+
import org.apache.maven.plugins.annotations.Parameter;
2524

2625
import com.diffplug.spotless.Formatter;
2726
import com.diffplug.spotless.PaddedCell;
2827
import com.diffplug.spotless.maven.incremental.UpToDateChecker;
2928

30-
import org.apache.maven.plugins.annotations.Parameter;
31-
3229
/**
3330
* Performs formatting of all source files according to configured formatters.
3431
*/
@@ -46,8 +43,8 @@ public class SpotlessApplyMojo extends AbstractSpotlessMojo {
4643

4744
@Override
4845
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
49-
if(isIdeHook()) {
50-
IdeHook.performHook(files, spotlessIdeHook, formatter, getBaseDir(), null, spotlessIdeHookUseStdIn, spotlessIdeHookUseStdOut);
46+
if (isIdeHook()) {
47+
IdeHook.performHook(files, formatter, spotlessIdeHook, spotlessIdeHookUseStdIn, spotlessIdeHookUseStdOut);
5148
return;
5249
}
5350

@@ -91,13 +88,4 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
9188
private boolean isIdeHook() {
9289
return !(spotlessIdeHook == null || spotlessIdeHook.isEmpty());
9390
}
94-
95-
private List<File> fetchFileFromIdeHook() {
96-
File file = new File(spotlessIdeHook);
97-
if (!file.isAbsolute()) {
98-
throw new PluginException("Argument passed to spotlessIdeHook must be an absolute path");
99-
}
100-
101-
return List.of(file);
102-
}
10391
}

0 commit comments

Comments
 (0)