Skip to content

Commit f2a8151

Browse files
committed
scripts/git: pre-commit: update from test-linux
Signed-off-by: Rong Tao <rongtao@cestc.cn>
1 parent 6230132 commit f2a8151

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

scripts/git/hooks/pre-commit

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
# Copyright (C) 2024-2026 Rong Tao
23
set -e
34

45
readonly GIT_TOPDIR=$(git rev-parse --show-toplevel 2>/dev/null || :)
@@ -7,21 +8,33 @@ readonly GIT_TOPDIR=$(git rev-parse --show-toplevel 2>/dev/null || :)
78
[[ -z ${NR_MOD} ]] && NR_MOD=4
89
[[ -z ${NR_ADD} ]] && NR_ADD=1
910
[[ -z ${NR_DEL} ]] && NR_DEL=1
10-
[[ -z ${FORCE} ]] && FORCE=
11+
[[ -z ${FORCE1} ]] && FORCE1=
1112

1213
if [[ ${V} ]] || [[ ${VERBOSE} ]]; then
1314
set -x
1415
fi
1516

16-
try_add=( $(git status --short | grep ^A | awk '{print $2}' || true) )
17-
try_del=( $(git status --short | grep ^D | awk '{print $2}' || true) )
18-
try_modify=( $(git status --short | grep ^M | awk '{print $2}' || true) )
19-
2017
fatal() {
21-
echo >&2 "FATAL: "${@}
18+
echo >&2 -en "\033[31m"
19+
echo >&2 -e "FATAL: "${@}
20+
echo >&2 -en "\033[0m"
2221
exit 1
2322
}
2423

24+
# Check code format wich git-clang-format
25+
clang_format=$(which git-clang-format 2>/dev/null || :)
26+
if [[ -z ${clang_format} ]] && [[ -z ${FORCE1} ]]; then
27+
fatal "Not found git-clang-format, please install or FORCE1=1"
28+
elif [[ -z ${FORCE1} ]]; then
29+
${clang_format} --diff origin/master --extensions c,cpp,cu,h,hpp || {
30+
fatal "Bad code format, please modify according to the above diff or FORCE1=1"
31+
}
32+
fi
33+
34+
try_add=( $(git status --short | grep ^A | awk '{print $2}' || true) )
35+
try_del=( $(git status --short | grep ^D | awk '{print $2}' || true) )
36+
try_modify=( $(git status --short | grep ^M | awk '{print $2}' || true) )
37+
2538
# Redirect output to stderr.
2639
exec 1>&2
2740

@@ -30,14 +43,14 @@ hint_changelog() {
3043
! [[ -L ${GIT_TOPDIR}/CHANGELOG.md ]]; then
3144
if ! [[ " ${try_modify[@]} " =~ " CHANGELOG.md " ]]; then
3245
git status --short
33-
fatal "${@}" "you forgot modify CHANGELOG!!! Try FORCE???"
46+
fatal "${@}" "you forgot modify CHANGELOG!!! Try FORCE1=1???"
3447
fi
3548
fi
3649
if [[ -f ${GIT_TOPDIR}/docs/en/CHANGELOG.md ]]; then
3750
if [[ ! " ${try_modify[@]} " =~ " docs/en/CHANGELOG.md " ]] ||
3851
[[ ! " ${try_modify[@]} " =~ " docs/zh/CHANGELOG.md " ]]; then
3952
git status --short
40-
fatal "${@}" "you forgot modify docs/{en,zh}/CHANGELOG.md!!! Try FORCE???"
53+
fatal "${@}" "you forgot modify docs/{en,zh}/CHANGELOG.md!!! Try FORCE1=1???"
4154
fi
4255
fi
4356
}
@@ -64,8 +77,8 @@ do
6477
if [[ $(basename $f) == vmlinux.h ]]; then
6578
fatal "vmlinux.h is too big, generate it with bpftool command!!!"
6679
fi
67-
if [[ $(file $f | awk '{print $NF}') == data ]] && [[ -z ${FORCE} ]]; then
68-
fatal "$f is considered a data file!!! try FORCE"
80+
if [[ $(file $f | awk '{print $NF}') == data ]] && [[ -z ${FORCE1} ]]; then
81+
fatal "$f is considered a data file!!! try FORCE1"
6982
fi
7083
done
7184

0 commit comments

Comments
 (0)