Skip to content

Commit 0f3c515

Browse files
CuRahmanjmartinez-silabs
authored andcommitted
Pull request #74: Pre-commit hook to warn against unintended CSA changes - MATTER-705
Merge in WMN_TOOLS/matter from feature/pre-commit-hook to silabs Squashed commit of the following: commit fdff3509f1e85d78ebb923f89d7f63d644848556 Author: Curtis Rahman <curtis.rahman@silabs.com> Date: Thu Sep 8 08:51:39 2022 -0400 Reverted pre-commit script, renamed run-all script commit 01c1ed82b6a5dfa11c7c2f186344be5f09b8bfd3 Author: Curtis Rahman <curtis.rahman@silabs.com> Date: Wed Sep 7 10:01:27 2022 -0400 Cleaned up; added comments/descriptions commit 6febf6cbe5a01bfae11eb7881286ea487ef6450a Author: Curtis Rahman <curtis.rahman@silabs.com> Date: Tue Sep 6 14:22:50 2022 -0400 Added pre-commit SMG checker, modified existing pre-commit
1 parent 7f1a0ec commit 0f3c515

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

.githooks/pre-commit-smg

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!/bin/sh
2+
3+
# This file contains a script to check the modified files within a commit
4+
# against a list of 'untouchable' files that should only be changed within
5+
# the CSA repo.
6+
#
7+
# To set this script to run on each commit, you should locally symlink where
8+
# git looks for this file with this file via a command in project root directory:
9+
# ln -s ../../.githooks/pre-commit-smg .git/hooks/pre-commit
10+
#
11+
# The commit will fail unless checks are passed; to bypass failure you have the
12+
# option to add '--no-verify' tag to your commit.
13+
14+
here=${0%/*}
15+
16+
CHIP_ROOT=$(cd "$here/.." && pwd)
17+
18+
SAVED_UNSTAGED=0
19+
SAVED_UNSTAGED_FILE=$(git rev-parse --short HEAD)-unstaged.diff
20+
21+
save_unstaged() {
22+
if [[ $SAVED_UNSTAGED -ne 0 ]]; then
23+
git diff --output="$SAVED_UNSTAGED_FILE"
24+
git apply -R "$SAVED_UNSTAGED_FILE"
25+
fi
26+
}
27+
28+
revert_unstaged() {
29+
if [[ $SAVED_UNSTAGED -ne 0 ]]; then
30+
git apply "$SAVED_UNSTAGED_FILE"
31+
rm "$SAVED_UNSTAGED_FILE"
32+
fi
33+
SAVED_UNSTAGED=0
34+
}
35+
36+
revert_if_needed() {
37+
revert_unstaged
38+
}
39+
40+
trap "revert_if_needed; exit 1" SIGINT SIGTERM SIGKILL
41+
42+
git diff --quiet
43+
SAVED_UNSTAGED=$?
44+
45+
# If there are unstaged files, save them for now
46+
save_unstaged
47+
revert_if_needed
48+
49+
if git rev-parse --verify HEAD >/dev/null 2>&1; then
50+
against=HEAD
51+
else
52+
# Initial commit: diff against an empty tree object
53+
against=$(git hash-object -t tree /dev/null)
54+
fi
55+
56+
# Redirect output to stderr.
57+
exec 1>&2
58+
59+
# Paths from the repo root of files/directories not to be touched within SMG
60+
MUST_NOT_CHANGE=('.devcontainer'
61+
'build/'
62+
'build_overrides/'
63+
'config/'
64+
'credentials/'
65+
'examples/'
66+
'integrations/'
67+
'scripts/'
68+
'src/'
69+
'third_party/'
70+
'zzz_generated/'
71+
'REVIEWERS.md'
72+
'LICENSE'
73+
'.clang-format'
74+
'.clang-tidy'
75+
'.default-version.min'
76+
'.dir-locals.el'
77+
'.editorconfig'
78+
'.flake8'
79+
'.gitattributes'
80+
'.gitignore'
81+
'.gitmodules'
82+
'.prettierrc.json'
83+
'.pullapprove.yml'
84+
'.restyles.yaml'
85+
'.spellcheck_tree'
86+
'.spellcheck.yml'
87+
'BUILD.gn'
88+
'CODE_OF_CONDUCT.md'
89+
'CONTRIBUTING.md'
90+
'export_examples.sh'
91+
'gn_build.sh'
92+
'lgtm.yml'
93+
'docs/api/'
94+
'docs/discussion/'
95+
'docs/dots/'
96+
'docs/examples/'
97+
'docs/guides'
98+
'docs/images/'
99+
'docs/style/'
100+
'docs/FileBUG_REPORT.md'
101+
'docs/ChipDoxygenLayout.xml'
102+
'docs/Doxyfile'
103+
'docs/namespaces.dox'
104+
'docs/PROJECT_FLOW.md'
105+
'docs/QUICK_START.md'
106+
'docs/README.md'
107+
'docs/STYLE_GUIDE.md'
108+
'docs/VSCODE_DEVELOPMENT.md')
109+
110+
RED='\033[0;31m'
111+
NC='\033[0m'
112+
MATCHING_LIST=()
113+
CHANGED_FILES=$(git diff --cached --name-only $against)
114+
115+
# Search the modified files for changes that should only be made in CSA
116+
# Save these files to be listed along with the commit message
117+
for i in "${MUST_NOT_CHANGE[@]}"; do
118+
MATCH=$(echo "$CHANGED_FILES" | grep "^$i*")
119+
if [[ $MATCH != "" ]]; then
120+
MATCHING_LIST+=$MATCH'\n'
121+
fi
122+
done
123+
124+
# Allow or block commit and list problematic files with warning message
125+
if [ ${#MATCHING_LIST[@]} -eq 0 ]; then
126+
echo "Commit looks good!"
127+
exit 0
128+
else
129+
echo $RED$MATCHING_LIST | tr " " "\n"
130+
echo $NC"Commit failed: You made changes to the listed files which should not be touched within the SMG repo." \
131+
"Are you sure you want to do this?\n"
132+
echo "If so, add justification for making this change in the pull request so the reviewers are aware" \
133+
"and continue with the '--no-verify' tag attached to your commit."
134+
exit 1
135+
fi

.githooks/run-all-pre-commit

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
# This file contains a script to to run all pre-commit scripts within this directoy.
4+
#
5+
# To set this script to run on each commit, you should locally symlink where
6+
# git looks for this file with this file via a command in project root directory:
7+
# ln -s ../../.githooks/run-all-pre-commit .git/hooks/pre-commit
8+
#
9+
# The commit will fail unless checks are passed; to bypass failure you have the
10+
# option to add '--no-verify' tag to your commit.
11+
12+
here=${0%/*}
13+
CHIP_ROOT=$(cd "$here/../.." && pwd)
14+
15+
for PRECOMMITSCRIPT in $CHIP_ROOT/.githooks/pre-commit*; do
16+
$PRECOMMITSCRIPT
17+
done

0 commit comments

Comments
 (0)