Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Avoid check of the ratchetFrom branch when Spotless not invoked #1902

Open
helloncode opened this issue Nov 23, 2023 · 2 comments
Open

Avoid check of the ratchetFrom branch when Spotless not invoked #1902

helloncode opened this issue Nov 23, 2023 · 2 comments

Comments

@helloncode
Copy link

helloncode commented Nov 23, 2023

Currently, Spotless (Gradle Plugin) performs a check for the presence of the ratchetFrom branch on every command execution. This includes instances where Spotless-specific commands aren't called, which can lead to unnecessary checks, and adds the workaround (e.g. git fetch depth:0) for all cases.

Could we refine the ratchetFrom functionality so that it checks for branch presence only when a Spotless command is actually invoked?

I believe this adjustment could be beneficial for users who integrate Spotless into their development workflows, ensuring that the tool remains as efficient and unobtrusive as possible.

@popematt
Copy link

It would also be nice if the ratchetFrom would accept a lazy string as a () -> String, Supplier<String>, or similar and not get the value of the lazy string until running a Spotless task.

In one of my build scripts, I've written some logic that shells out to git to find the name of the remote for the source repository. If the rachetFrom branch name was lazily evaluated only when a Spotless task is actually running, then I could perform the lookup as a Gradle task and cache the result as a task output.

@popematt
Copy link

I managed to come up with a workaround using a Git tag.

Using the Kotlin Gradle DSL, and assuming there is a runCommand() extension function that does exactly what its name implies...

val SPOTLESS_TAG = "spotless-rachet-from-this-commit"
val rachetFromCommitIsh by lazy { TODO("Your logic here") } 

spotless {
    "git tag -f $SPOTLESS_TAG".runCommand()
    rachetFrom(SPOTLESS_TAG)
    // If we delete it now, it's too soon, but we can delete it once the task graph is complete 
    // so that it's cleaned up if no `spotless` tasks are going to be run.
    gradle.taskGraph.addTaskExecutionGraphListener { 
        "git tag -d $SPOTLESS_CHECKPOINT_TAG".runCommand()
    }
}

tasks.withType<SpotlessTask> {
    doFirst { "git tag -f $SPOTLESS_TAG $rachetFromCommitIsh".runCommand() }
    doLast { "git tag -d $SPOTLESS_TAG".runCommand() }
}

The downside is that this creates a new tag in (the local copy of) your Git repository. I can definitely live with that in a CI's local copy of the repo, and I think I can live with that on my local copy too.

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

No branches or pull requests

3 participants