Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quality(style): add task that installs a precommit hook that format code
`./gradlew installPrecommitHook` backs up existing hook The hook formats all changed files (staged or unstaged) and outputs a warning about other files that do not conform to google code style
- Loading branch information
1 parent
8cc3dc6
commit 429e02a
Showing
2 changed files
with
49 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/bin/sh | ||
|
||
# Git pre commit hook that reformats all staged and unstaged java files before commit. | ||
# Files that do not comply to the code format but were staged or unstagd warned but ignored.. | ||
# The rationale is to enforce code style without creating "format all files" commits | ||
# accidentally because that cleanup should be separate in a separate commit. | ||
|
||
staged_files=$(git diff --name-only --cached) | ||
unstaged_files=$(git diff --name-only) | ||
|
||
# echo $unstaged_files | xargs git stash push -q | ||
./gradlew reformatJavaSources | ||
echo $staged_files | xargs git add # add all touched & reformatted files | ||
|
||
changed_files=$(git ls-files -m) | ||
# offenders = changed_files - unstaged_files | ||
union="$(echo "$changed_files" ; echo "$unstaged_files")" | ||
offenders="$(echo "$union" | sort | uniq -u)" | ||
if [[ $offenders ]]; then | ||
echo "😬 The following files are not conforming to our style guide, commit them separately after running ./gradlew reformatJavaSources\n" | ||
echo "$offenders" | ||
echo "" | ||
fi | ||
|
||
echo $offenders | xargs git checkout # discard reformat of untouched files | ||
|
||
exit 0 |