-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre-commit hook to test if the code needs restyling (#7506)
* Pre-commit hook to test if the code needs restyling * use diff patch to save unstaged changes * Fix spelling
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/bin/sh | ||
|
||
here=${0%/*} | ||
|
||
CHIP_ROOT=$(cd "$here/.." && pwd) | ||
|
||
SAVED_UNSTAGED=0 | ||
SAVED_UNSTAGED_FILE=$(git rev-parse --short HEAD)-unstaged.diff | ||
|
||
RESTYLED=0 | ||
|
||
save_unstaged() { | ||
if [[ $SAVED_UNSTAGED -ne 0 ]]; then | ||
git diff --output="$SAVED_UNSTAGED_FILE" | ||
git apply -R "$SAVED_UNSTAGED_FILE" | ||
fi | ||
} | ||
|
||
revert_unstaged() { | ||
if [[ $SAVED_UNSTAGED -ne 0 ]]; then | ||
git apply "$SAVED_UNSTAGED_FILE" | ||
rm "$SAVED_UNSTAGED_FILE" | ||
fi | ||
SAVED_UNSTAGED=0 | ||
} | ||
|
||
revert_restyled() { | ||
if [[ $RESTYLED -ne 0 ]]; then | ||
# Reset the changes introduced by restyle | ||
git stash push -q --keep-index | ||
git stash drop -q | ||
fi | ||
RESTYLED=0 | ||
} | ||
|
||
revert_if_needed() { | ||
revert_restyled | ||
revert_unstaged | ||
} | ||
|
||
trap "revert_if_needed; exit 1" SIGINT SIGTERM SIGKILL | ||
|
||
git diff --quiet | ||
SAVED_UNSTAGED=$? | ||
|
||
# If there are unstaged files, save them for now | ||
save_unstaged | ||
|
||
# Try restyling the code | ||
"$CHIP_ROOT"/scripts/helpers/restyle-diff.sh | ||
|
||
git diff --quiet | ||
RESTYLED=$? | ||
FAILED_COMMIT="$RESTYLED" | ||
|
||
revert_if_needed | ||
|
||
if [[ $FAILED_COMMIT -ne 0 ]]; then | ||
echo "Commit Failed: Code needs restyling before committing." | ||
echo "Restyling can be done by running $CHIP_ROOT/scripts/helpers/restyle-diff.sh" | ||
exit 1 | ||
fi | ||
|
||
echo "Code doesn't need restyling. Committing." |