Skip to content

Commit ece3fba

Browse files
authored
Precommit hook that runs ktlint and detekt (#907)
1 parent 4c7768e commit ece3fba

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

.githooks/pre-push

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash -e
2+
3+
# Prevents pushing any code to the server that does not comply with klint or detekt
4+
# It ignores any unchecked files.
5+
6+
# Ignore any unchecked file
7+
echo "Stashing changes before doing checks"
8+
git stash push -q -u --keep-index
9+
10+
# Pop the stash once the scripts finishes, fails or gets cancelled
11+
function pop_stash() {
12+
echo "Checks completed, popping stash"
13+
git stash pop -q
14+
}
15+
16+
trap "exit" INT TERM ERR
17+
trap pop_stash EXIT
18+
19+
# Do the actual check ups
20+
./gradlew ktlintCheck detekt

CONTRIBUTING.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ We use the offical [style guide](https://kotlinlang.org/docs/reference/coding-co
8181
./gradlew ktlintFormat
8282
```
8383

84-
Note: ktlint does not allow group imports using `.*`. You can configure IntelliJ to disallow this by going to preferences `Editor > Code Style > Kotlin > Imports` and select "Use single name imports".
84+
A pre-push git hook that automatically will perform these checks is available. You can configure it with the following command:
8585

86+
```
87+
git config core.hooksPath .githooks
88+
```
89+
90+
Note: ktlint does not allow group imports using `.*`. You can configure IntelliJ to disallow this by going to preferences `Editor > Code Style > Kotlin > Imports` and select "Use single name imports".
8691

8792
## Multiplatform source layout
8893

0 commit comments

Comments
 (0)