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

Added pre-commit hooks #421

Merged
merged 7 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ sonarqube {
}
}

// Install git hooks
task installLocalGitHook(type: Copy) {
from new File(rootProject.rootDir, 'scripts/pre-commit')
into new File(rootProject.rootDir, '.git/hooks')
fileMode 0775
}
build.dependsOn installLocalGitHook

subprojects {
apply plugin: "java"
apply plugin: "com.diffplug.spotless"
Expand Down Expand Up @@ -54,9 +62,6 @@ subprojects {
def compileTasks = {
options.encoding = 'UTF-8'

// Makes spotlessApply task run on every compile/build.
dependsOn 'spotlessApply'

// Nails the Java-Version of every Subproject
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
48 changes: 48 additions & 0 deletions scripts/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/sh

create_stash() {
# Stash unstaged changes, if any
previous_stash=$(git rev-parse -q --verify refs/stash)
git stash push -q
new_stash=$(git rev-parse -q --verify refs/stash)
}

pop_stash() {
# Restore unstaged changes, if any
if [ "$previous_stash" != "$new_stash" ]; then
git stash apply -q && git stash drop -q
fi
}

echo "****Running pre-commit hooks****"
# We want to apply spotless on all staged files. Therefore, we have to first draft a
# WIP-commit for the staged changes and put the unstaged changes aside using stash
# (otherwise spotless would apply on them as well, see https://github.com/diffplug/spotless/issues/623).
# There are a few edge cases to handle here; for example if there are no unstaged changes, stash would not create a stash.
# Hence, the following pop of the stash must be guarded (see https://stackoverflow.com/a/20480591/2411243).

# Mini commit for staged changes, so that we can stash away unstaged in the next step (bypass hook with "no-verify" to avoid recursion)
if ! git commit --no-verify --message "WIP"; then
# Commit failed (for example if there is nothing staged), early-out
exit $?
fi
Zabuzard marked this conversation as resolved.
Show resolved Hide resolved

# Put unstaged away
create_stash
# Get back staged changes
git reset --soft HEAD^

# Apply spotless on the staged changes only (rest has been put away)
echo "**Applying spotless**"
if ! ./gradlew spotlessApply; then
# Spotless failed, restore unstaged
pop_stash
exit $?
fi

# Spotless possibly found changes, apply them
git add -A

# Restore back the unstaged changes
pop_stash
echo "****Done pre-commit hooks****"