blob: b16f4739dd83270af64afc5902038f7afd94e53c [file] [log] [blame]
Pankaj Garg3c4576d2021-06-10 23:12:43 -07001#!/bin/sh
2
3here=${0%/*}
4
5CHIP_ROOT=$(cd "$here/.." && pwd)
6
7SAVED_UNSTAGED=0
8SAVED_UNSTAGED_FILE=$(git rev-parse --short HEAD)-unstaged.diff
9
10RESTYLED=0
11
12save_unstaged() {
13 if [[ $SAVED_UNSTAGED -ne 0 ]]; then
14 git diff --output="$SAVED_UNSTAGED_FILE"
15 git apply -R "$SAVED_UNSTAGED_FILE"
16 fi
17}
18
19revert_unstaged() {
20 if [[ $SAVED_UNSTAGED -ne 0 ]]; then
21 git apply "$SAVED_UNSTAGED_FILE"
22 rm "$SAVED_UNSTAGED_FILE"
23 fi
24 SAVED_UNSTAGED=0
25}
26
27revert_restyled() {
28 if [[ $RESTYLED -ne 0 ]]; then
29 # Reset the changes introduced by restyle
30 git stash push -q --keep-index
31 git stash drop -q
32 fi
33 RESTYLED=0
34}
35
36revert_if_needed() {
37 revert_restyled
38 revert_unstaged
39}
40
41trap "revert_if_needed; exit 1" SIGINT SIGTERM SIGKILL
42
43git diff --quiet
44SAVED_UNSTAGED=$?
45
46# If there are unstaged files, save them for now
47save_unstaged
48
49# Try restyling the code
50"$CHIP_ROOT"/scripts/helpers/restyle-diff.sh
51
52git diff --quiet
53RESTYLED=$?
54FAILED_COMMIT="$RESTYLED"
55
56revert_if_needed
57
58if [[ $FAILED_COMMIT -ne 0 ]]; then
59 echo "Commit Failed: Code needs restyling before committing."
60 echo "Restyling can be done by running $CHIP_ROOT/scripts/helpers/restyle-diff.sh"
61 exit 1
62fi
63
64echo "Code doesn't need restyling. Committing."