Skip to content

Commit

Permalink
fix: use commit-msg hook to only lint current commit msg
Browse files Browse the repository at this point in the history
  • Loading branch information
sripwoud committed Jul 9, 2024
1 parent 8ae4261 commit b2e79f3
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions .setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,52 +45,38 @@ install_dev_deps() {
done
}

write_pre_push_hook() {
cat >.git/hooks/pre-push <<'EOF'
write_pre_commit_hook() {
echo "make fmt" >.git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
log " .git/hooks/pre-commit (formatting)"
}

write_commit_msg_hook() {
cat >.git/hooks/commit-msg <<'EOF'
#!/bin/sh
alias convco=.cargo/bin/convco
# https://convco.github.io/check/
z40=0000000000000000000000000000000000000000
while read -r _ local_sha _ remote_sha; do
if [ "$local_sha" != $z40 ]; then
if [ "$remote_sha" = $z40 ]; then
# New branch, examine all commits
range="$local_sha"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# Check only the commits that are not in main
merge_base=$(git merge-base "$local_sha" main)
if [ -n $merge_base ];then
range="$merge_base..$local_sha"
fi
# Check for WIP commit
if ! convco check "$range"; then
exit 1
fi
main() {
if ! cat .git/COMMIT_EDITMSG | convco check --from-stdin --ignore-reverts;then
printf "%s\n" "Please refer to https://www.conventionalcommits.org/en/v1.0.0"
exit 1
fi
done
EOF

chmod +x .git/hooks/pre-push
log " .git/hooks/pre-push (conventional commits linting)"
}
write_pre_commit_hook() {
echo "make fmt" >.git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
log " .git/hooks/pre-commit (formatting)"
main
EOF

chmod +x .git/hooks/commit-msg
log " .git/hooks/commit-msg (conventional commits linting)"
}

write_hooks() {
log "Writing hooks..."
write_pre_commit_hook
write_pre_push_hook
write_commit_msg_hook
}

end_log() {
Expand Down

0 comments on commit b2e79f3

Please sign in to comment.