Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Mercurial equivalent for ratchetFrom #2116

Open
pinahar opened this issue May 23, 2024 · 2 comments
Open

Mercurial equivalent for ratchetFrom #2116

pinahar opened this issue May 23, 2024 · 2 comments

Comments

@pinahar
Copy link

pinahar commented May 23, 2024

We have a large Kotlin project where we use spotless.
However, it is pretty slow. I came across the ratchetFrom property but seems like it is only for Git.
How can I make spotless run on just modified files to make the build faster?
 

  • Gradle
  • spotless version - 6.23.3
  • operating system and version - Mac OSX 14.4.1

configure<com.diffplug.gradle.spotless.SpotlessExtension> {
kotlin {
target("/*.kt", "/.kts")
targetExclude("$buildDir/**/
", "/build//", "/node_modules//")
ktfmt(findProperty("cbrKtfmtVersion")!! as String)
}
}

tasks.getByName("build") {
group = "build"
dependsOn("spotlessApply")
}

@nedtwigg
Copy link
Member

It's possible. My guess is that it would be best to parse mercurial's console output. If I was implementing it, I would do something along these lines:

class MercurialRatchet {
  String ratchetFrom;
  public String getShaOfHead() { } // or equivalent, used for up-to-date checking
  public boolean isClean(File file) { } // feel free to require more args than just this
}

and it would get integrated in these two places:

  • up-to-date checking
  • /** The sha of the tree at repository root, used for determining if an individual *file* is clean according to git. */
    private transient ObjectId rootTreeSha;
    /**
    * The sha of the tree at the root of *this project*, used to determine if the git baseline has changed within this folder.
    * Using a more fine-grained tree (rather than the project root) allows Gradle to mark more subprojects as up-to-date
    * compared to using the project root.
    */
    private transient ObjectId subtreeSha = ObjectId.zeroId();
    /** Stored so that the configuration cache can recreate the GitRatchetGradle state. */
    protected String ratchetFrom;
    public void setupRatchet(String ratchetFrom) {
    this.ratchetFrom = ratchetFrom;
    if (!ratchetFrom.isEmpty()) {
    GitRatchet ratchet = getTaskService().get().getRatchet();
    File projectDir = getProjectDir().get().getAsFile();
    rootTreeSha = ratchet.rootTreeShaOf(projectDir, ratchetFrom);
    subtreeSha = ratchet.subtreeShaOf(projectDir, rootTreeSha);
    } else {
    subtreeSha = ObjectId.zeroId();
    }
    }
    @Internal
    abstract DirectoryProperty getProjectDir();
    @Internal
    GitRatchetGradle getRatchet() {
    return ObjectId.zeroId().equals(getRatchetSha()) ? null : getTaskService().get().getRatchet();
    }
    @Internal
    ObjectId getRootTreeSha() {
    return rootTreeSha;
    }
    @Input
    public ObjectId getRatchetSha() {
    if (subtreeSha == null) {
    setupRatchet(ratchetFrom);
    }
    return subtreeSha;
    }
  • actual task execution
  • try (Formatter formatter = buildFormatter()) {
    GitRatchetGradle ratchet = getRatchet();
    for (FileChange fileChange : inputs.getFileChanges(target)) {
    File input = fileChange.getFile();
    if (fileChange.getChangeType() == ChangeType.REMOVED) {
    deletePreviousResult(input);
    } else {
    if (input.isFile()) {
    processInputFile(ratchet, formatter, input);
    }
    }
    }
    }
    }
    @VisibleForTesting
    void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException {
    File output = getOutputFile(input);
    getLogger().debug("Applying format to {} and writing to {}", input, output);
    PaddedCell.DirtyState dirtyState;
    if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) {
    dirtyState = PaddedCell.isClean();
    } else {

@pinahar
Copy link
Author

pinahar commented May 23, 2024

Thanks for the response.
How would I do this change locally? Not sure how that part would work.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants