-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·105 lines (103 loc) · 2.71 KB
/
pre-commit
File metadata and controls
executable file
·105 lines (103 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
set -e
SCRIPT="$(basename "$0")"
if [ -t 1 ]
then
ncolors=$(tput colors)
if [ -n "$ncolors" -a "$ncolors" -ge 8 ]
then
blue="$(tput setaf 4)"
green="$(tput setaf 2)"
red="$(tput setaf 1)"
yellow="$(tput setaf 3)"
normal="$(tput sgr0)"
fi
fi
_fix() {
local FMT="$1"
local FLS="$2"
if [ -z "$FLS" ]
then
return
fi
if ! type "$FMT" >/dev/null
then
>&2 echo "${blue}[$SCRIPT] ${red}Program $normal'$FMT' ${red}not found$normal"
exit 2
fi
case "$FMT" in
biome)
printf '%s' "$FLS" | xargs -d '\n' biome check --apply || true
printf '%s' "$FLS" | xargs -d '\n' biome format --write
;;
ruff)
printf '%s' "$FLS" | xargs -d '\n' ruff check --fix --exit-zero
printf '%s' "$FLS" | xargs -d '\n' ruff format
;;
php-cs-fixer)
printf '%s' "$FLS" | xargs -d '\n' php-cs-fixer fix
;;
clang-format)
printf '%s' "$FLS" | xargs -d '\n' clang-format -i
;;
rustfmt)
# cargo clippy --fix
printf '%s' "$FLS" | xargs -d '\n' rustfmt --edition 2021
;;
*)
>&2 echo "${blue}[$SCRIPT] ${red}Unknown program$normal '$FMT'"
exit 2
esac
local CODE=$?
echo "${blue}[$SCRIPT]$normal Fixed $(echo -n "$FLS"|wc -l) files with '$FMT' (exit code $CODE)"
}
PARENT_PID=$PPID
while [ "$PARENT_PID" -gt 1 ]
do
IFS=' ' read -r PARENT_PID PARENT_COM <<-EOF
$(ps -p $PARENT_PID -o ppid=,comm=)
EOF
if [ "$PARENT_COM" = git ]
then
GIT_ARG=--cached
break
fi
done
FILES="$(git diff $GIT_ARG --name-only --diff-filter=AM)"
IFS='
'
for FILE in $FILES
do
case "$FILE" in
*.cts|*.mts|*.ts|*.tsx|*.cjs|*.js|*.jsx|*.mjs|*.json)
FILES_BIOME="$FILES_BIOME$FILE$IFS"
;;
*.py|*.pyi)
FILES_RUFF="$FILES_RUFF$FILE$IFS"
;;
*.php|*.php3|*.php4|*.php5|*.php7|*.php8|*.pht|*.phtml)
FILES_PHP_CS_FIXER="$FILES_PHP_CS_FIXER$FILE$IFS"
;;
*.[cChH]|*.[cChH].in|*.[ch]pp|*.[ch]pp.in|*.[ch]xx|*.[ch]xx.in|*.cc|*.cc.in|*.hh|*.hh.in|*.inl|*.cats)
FILES_CLANG_FORMAT="$FILES_CLANG_FORMAT$FILE$IFS"
;;
*.java|*.jsp|*.jspx|*.properties|*.cs)
FILES_CLANG_FORMAT="$FILES_CLANG_FORMAT$FILE$IFS"
;;
*.rs)
FILES_RUSTFMT="$FILES_RUSTFMT$FILE$IFS"
;;
*)
>&2 echo "${blue}[$SCRIPT] ${yellow}No support for:$normal '$FILE'"
;;
esac
done
_fix 'biome' "$FILES_BIOME"
_fix 'ruff' "$FILES_RUFF"
_fix 'php-cs-fixer' "$FILES_PHP_CS_FIXER"
_fix 'clang-format' "$FILES_CLANG_FORMAT"
_fix 'rustfmt' "$FILES_RUSTFMT"
if [ "$GIT_ARG" = '--cached' ]
then
printf '%s' "$FILES" | xargs -d '\n' git add
fi