-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathgit-hook-pre-commit
More file actions
executable file
·85 lines (76 loc) · 2.96 KB
/
Copy pathgit-hook-pre-commit
File metadata and controls
executable file
·85 lines (76 loc) · 2.96 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
#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=d)
# Ensure formatting with `stylua` (https://github.com/JohnnyMorganz/StyLua)
LUA_FILES=$(echo "$STAGED_FILES" | grep "\.lua$")
if [ -n "$LUA_FILES" ]; then
printf "Check formatting: "
if command -v stylua >/dev/null 2>&1; then
HASHES_BEFORE=$(echo "$LUA_FILES" | xargs git hash-object)
echo "$LUA_FILES" | xargs stylua --respect-ignores || exit 1
HASHES_AFTER=$(echo "$LUA_FILES" | xargs git hash-object)
# Running `stylua` should not add new changes to staged Lua files
if [ "$HASHES_BEFORE" != "$HASHES_AFTER" ]; then
echo "FAIL"
echo " StyLua modified some files."
echo " Stage formatted changes and re-commit."
exit 1
fi
echo "OK"
else
echo "WARN (stylua executable not found)"
fi
fi
# Ensure documentation was generated and is good quality
GENDOC_FILES=$(echo "$STAGED_FILES" | grep -E "^(doc|lua)/")
if [ -n "$GENDOC_FILES" ]; then
printf "Check gendoc: "
# Running `gendoc` should not add new changes to any "doc/" files
DOCDIFF_BEFORE=$(git diff -- doc/*)
make --silent gendoc >/dev/null 2>&1
DOCDIFF_AFTER=$(git diff -- doc/*)
if [ "$DOCDIFF_BEFORE" != "$DOCDIFF_AFTER" ]; then
echo "FAIL"
echo " Documentation was not properly generated."
echo " Review generated changes, stage relevant ones, re-commit."
exit 1
fi
# There should not be unstaged doc if its source file has staged changes
GEN_FILES=$(echo "$STAGED_FILES" | grep -E '^lua/mini/.+\.lua$')
if [ -n "$GEN_FILES" ]; then
DOC_TARGET=$(echo "$GEN_FILES" | sed -E 's|^lua/mini/(.+)\.lua$|doc/mini-\1.txt|')
DOC_TARGET_UNSTAGED=$(echo "$DOC_TARGET" | xargs git diff --name-only --)
if [ -n "$DOC_TARGET_UNSTAGED" ]; then
echo "FAIL"
echo " Documentation for not all staged lua/mini/* files was staged."
echo " Stage relevant ones and re-commit."
exit 1
fi
fi
echo "OK"
fi
DOC_FILES=$(echo "$STAGED_FILES" | grep -E "^doc/")
if [ -n "$DOC_FILES" ]; then
printf "Check lintdoc: "
make --silent lintdoc || exit 1
# NOTE: OK/FAIL status comes from `make --sillent lintdoc` itself
fi
# Ensure formatting with `typos` (https://github.com/crate-ci/typos)
# Condition: running it should not add new changes to staged Lua files
SPELLED_FILES=$(echo "$STAGED_FILES" | grep -E '\.(lua|txt|md)$')
if [ -n "$SPELLED_FILES" ]; then
printf "Check spelling: "
if command -v typos >/dev/null 2>&1; then
# Intercept `typos` output in case of errors for pretty printing
TYPOS_OUT=$(echo "$SPELLED_FILES" | xargs typos --color=never 2>&1)
if [ $? -eq 0 ]; then
echo "OK"
else
echo "FAIL"
echo "$TYPOS_OUT"
exit 1
fi
else
echo "WARN (typos executable not found)"
fi
fi
exit 0