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
Add Gradle pre-commit hook to docs #6892
Open
matejdro
wants to merge
12
commits into
detekt:main
Choose a base branch
from
matejdro:pre-commit-hook-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+488
−38
Open
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e6827f6
Add Gradle pre-commit hook to docs
matejdro 9fa7c76
Move gradle hook docs above CLI hook
matejdro c1dc24f
Reword intro pre-commit paragraph
matejdro 9c8ee8e
Clarify on where to put gradle scripts
matejdro 495268a
Clarify shell script update
matejdro d0e6589
Backport changes to current version
matejdro b2b50c4
Add versioned docs
cortinico 412fbd0
Use cleaner way of getting list of staged files
matejdro 17f4f0e
Document excluding kts files from type resolving pre-commit hook
matejdro d234fd3
Backport changes to old docs
matejdro a5cd4e9
Typo fix
matejdro ac45d72
Remove unused dependsOn
matejdro File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,11 +40,88 @@ A special thanks goes to Mohit Sarveiya for providing this shell script. | |
You can watch his excellent talk about **Static Code Analysis For Kotlin** on | ||
[YouTube](https://www.youtube.com/watch?v=LT6m5_LO2DQ). | ||
|
||
## Only run on staged files | ||
## Only run on staged files - Gradle | ||
|
||
It is also possible to use [the CLI](/docs/gettingstarted/cli) to create a hook that only runs on staged files. This has the advantage of speedier execution, by running on fewer files and avoiding the warm-up time of the gradle daemon. | ||
It is possible to configure Gradle to only run on staged files in pre-commit hook. | ||
This has the advantage of speedier execution, by running on fewer files and | ||
of lowered false positives by not scanning files that are not yet ready to be commited. | ||
|
||
Please note, however, that a handful of checks requiring [type resolution](/docs/gettingstarted/type-resolution) will not work correctly with this approach. If you do adopt a partial hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline. | ||
First, we need to declare a `getGitStagedFiles` function - a function task that will retrieve list of staged files | ||
in a configuration-cache compatible way. Paste following into your project's `build.gradle.kts`: | ||
|
||
```kotlin | ||
fun Project.getGitStagedFiles(rootDir: File): Provider<List<File>> { | ||
return providers.exec { | ||
it.commandLine("git", "--no-pager", "diff", "--name-only", "--cached") | ||
}.standardOutput.asText | ||
.map { outputText -> | ||
outputText.trim() | ||
.split("\n") | ||
.filter { it.isNotBlank() } | ||
.map { File(rootDir, it) } | ||
} | ||
} | ||
``` | ||
|
||
Then we need to configure `Detekt` task and change its `source` from the entire `src` foler (by default) to only set of | ||
matejdro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
files that have been staged by git. Paste following into your project's `build.gradle.kts`: | ||
|
||
```kotlin | ||
tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach { | ||
if (project.hasProperty("precommit")) { | ||
dependsOn(gitPreCommitFileList) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? And, if so, where is this task defined? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, forgot to remove this one. Will delete it. Good catch. |
||
|
||
val rootDir = project.rootDir | ||
val projectDir = projectDir | ||
|
||
val fileCollection = files() | ||
|
||
setSource( | ||
getGitStagedFiles(rootDir) | ||
.map { stagedFiles -> | ||
val stagedFilesFromThisProject = stagedFiles | ||
.filter { it.startsWith(projectDir) } | ||
|
||
fileCollection.setFrom(*stagedFilesFromThisProject.toTypedArray()) | ||
|
||
fileCollection.asFileTree | ||
} | ||
) | ||
} | ||
} | ||
``` | ||
|
||
Additionally, if your project uses `.gradle.kts` files and you want to use type resolution for pre-commit detekt checks, | ||
you must exclude them from pre-commit hook. Otherwise, you will be unable to commit any changes to the | ||
`.gradle.kts` files, since detekt pre-commit check would crash every time due to https://github.com/detekt/detekt/issues/5501: | ||
|
||
```kotlin | ||
afterEvaluate { | ||
tasks.withType(Detekt::class.java).configureEach { | ||
val typeResolutionEnabled = !classpath.isEmpty | ||
if (typeResolutionEnabled && project.hasProperty("precommit")) { | ||
// We must exclude kts files from pre-commit hook to prevent detekt from crashing | ||
// This is a workaround for the https://github.com/detekt/detekt/issues/5501 | ||
exclude("*.gradle.kts") | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Finally, we need to add `-Pprecommit=true` to the pre-commit script to tell Gradle to run detekt in "pre-commit mode". | ||
For example, from above `detekt.sh` | ||
|
||
```bash | ||
... | ||
./gradlew -Pprecommit=true detekt > $OUTPUT | ||
... | ||
``` | ||
|
||
## Only run on staged files - CLI | ||
|
||
It is also possible to use [the CLI](/docs/gettingstarted/cli) to create a hook that only runs on staged files. This has the advantage of speedier execution by avoiding the warm-up time of the gradle daemon. | ||
|
||
Please note, however, that a handful of checks requiring [type resolution](/docs/gettingstarted/type-resolution) will not work correctly with this approach. If you do adopt a partial CLI hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline. | ||
|
||
This example has been put together using [pre-commit](https://pre-commit.com/), but the same principle can be applied to any kind of hook. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part should be moved before the CLI, as the initial script suggests to use
./gradlew
.So this page will look like:
gradlew
on everythinggradlew
only on modified filesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to the top