Skip to content

Commit 8dd8073

Browse files
committed
chore: manage git hooks by husky
1 parent aea864a commit 8dd8073

5 files changed

Lines changed: 91 additions & 0 deletions

File tree

.husky/commit-msg

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/sh
2+
3+
# Purpose: check commit messages (typos and conventional commits)
4+
# Usage: make this script the git commit-msg hook
5+
# Dependencies: typos, cocogitto
6+
# Date: 2025-09-02
7+
# Author: Yusong
8+
9+
set -e
10+
11+
# The first argument is the path to the file containing the commit message.
12+
commit_msg_file="$1"
13+
# Read the commit message excluding comments (lines starting with #).
14+
commit_message=$(grep -v '^#' "$commit_msg_file")
15+
# Check if the commit message is empty.
16+
if [ -z "$commit_message" ]; then
17+
# Git will handle the empty message when exiting with 0.
18+
exit 0
19+
fi
20+
21+
# Run typos on the commit message.
22+
typos_output=$(echo "$commit_message" | typos --format brief -)
23+
if [ -n "$typos_output" ]; then
24+
echo "Error: Spelling issues found in the commit message."
25+
echo "Please fix the typos or use 'git push --no-verify' to bypass."
26+
echo
27+
echo "Spelling issues:"
28+
echo "$typos_output"
29+
echo
30+
echo "==================== Commit message ======================"
31+
echo "$commit_message"
32+
echo "==================== Commit message ======================"
33+
exit 1
34+
fi
35+
36+
# Check if the commit message aligns with conventional commits.
37+
if ! verify_message=$(cog verify "$commit_message" 2>&1); then
38+
echo "Error: Commit message violates conventional commits."
39+
echo "$verify_message"
40+
exit 1
41+
fi

.husky/post-commit

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
# Purpose: check conventional commits
4+
# Usage: make this script the git post-commit hook
5+
# Dependencies: cocogitto
6+
# Date: 2025-09-02
7+
# Author: Yusong
8+
9+
set -e
10+
11+
# Check the latest commit message.
12+
if ! cog check HEAD..HEAD >/dev/null 2>&1; then
13+
echo "Error: Commit message violates conventional commits."
14+
cog check HEAD..HEAD
15+
exit 1
16+
fi

.husky/pre-commit

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# Purpose: check typos in all files excluding ./.git
4+
# Usage: make this script the git pre-commit hook
5+
# Dependencies: typos
6+
# Date: 2025-09-02
7+
# Author: Yusong
8+
9+
set -e
10+
11+
# Check spelling.
12+
typos --hidden --exclude .git --format brief

.husky/pre-push

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/sh
2+
3+
# Purpose: check conventional commits for those not pushed
4+
# Usage: make this script the git pre-push hook
5+
# Dependencies: cocogitto
6+
# Date: 2025-09-02
7+
# Author: Yusong
8+
9+
set -e
10+
11+
# Check commit messages that haven't been pushed.
12+
if ! cog check origin/HEAD..HEAD >/dev/null 2>&1; then
13+
echo "Error: Commit message(s) violates conventional commits."
14+
cog check origin/HEAD..HEAD
15+
exit 1
16+
fi

flake.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@
2828
cocogitto # conventional commit toolkit
2929
git-cliff # changelog generator
3030
trivy # scan security issues
31+
husky # managing git hooks
32+
typos # check misspelling
3133
];
3234
# The shell script executed when the environment is activated.
3335
shellHook = /* sh */ ''
3436
# Print the last modified date of "flake.lock".
3537
git log -1 --format="%cd" --date=format:"%Y-%m-%d" -- flake.lock |
3638
awk '{printf "\"flake.lock\" last modified on: %s", $1}' &&
3739
echo " ($((($(date +%s) - $(git log -1 --format="%ct" -- flake.lock)) / 86400)) days ago)"
40+
# Install git hooks managed by husky.
41+
if [ ! -e "./.husky/_" ]; then
42+
husky install
43+
fi
3844
'';
3945
};
4046
}

0 commit comments

Comments
 (0)