-
-
Notifications
You must be signed in to change notification settings - Fork 105
Added pre-commit hooks #421
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2295b44
Pre-commit hook with installation script
Zabuzard 8fb0e65
Bugfixes
Zabuzard f8777a3
decided to remove the unit tests from the hook (use CI/CD instead)
Zabuzard 9836dfa
Removed spotless dependency for compile
Zabuzard d1d9f84
Fixed some issues in hook, +functions, +doc
Zabuzard 7eec923
Bugfix with commit failures (CR marko)
Zabuzard 23a5465
Fixed issues with exit codes (CR marko)
Zabuzard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -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 | ||
|
|
||
| # 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****" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.