Skip to content

Commit 407d43e

Browse files
committed
Avoid repeated staging change detects
In Git, staging changes serves as a critical step that prepares specific modifications for the next commit. There were unintended detects for staging changes in the pre-commit hook, which can be safely avoided. Change-Id: I361d721dd0d3127327f74f3d7110b45b8acec5f9
1 parent 71a0203 commit 407d43e

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

scripts/pre-commit.hook

+4-5
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ if [ "${#binary_files[@]}" -gt 0 ]; then
179179
printf "${RED}[!]${NC} Binary data found.\n"
180180
fi
181181

182-
FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h)$")
183-
for FILE in $FILES; do
182+
C_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E "\.(c|cpp|h|hpp)$")
183+
for FILE in $C_FILES; do
184184
nf=$(git checkout-index --temp $FILE | cut -f 1)
185185
tempdir=$(mktemp -d) || exit 1
186186
newfile=$(mktemp ${tempdir}/${nf}.XXXXXX) || exit 1
@@ -221,8 +221,7 @@ fi
221221
root=$(git rev-parse --show-toplevel)
222222
banned="([^f]gets\()|(sprintf\()|(strcpy\()"
223223
status=0
224-
for file in $(git diff --staged --name-only | grep -E "\.(c|cc|cpp|h|hh|hpp)\$")
225-
do
224+
for file in $C_FILES; do
226225
filepath="${root}/${file}"
227226
output=$(grep -nrE "${banned}" "${filepath}")
228227
if [ ! -z "${output}" ]; then
@@ -242,7 +241,7 @@ if [ ! -f fmtscan ]; then
242241
throw "Fail to build 'fmtscan' tools"
243242
fi
244243
fi
245-
if git diff --cached --name-only | grep -qiE "\.(c|h|cpp|hpp)$"; then
244+
if [ -n "$C_FILES" ]; then
246245
echo "Running fmtscan..."
247246
./fmtscan
248247
if [ $? -ne 0 ]; then

0 commit comments

Comments
 (0)