Files
jinshen-website/.husky/commit-msg
R2m1liA c36ab77318 chore(git): 为项目添加Husky & Commitlint用于管理commit message
项目采用Conventional Commit作为Commit Message编写规范:
----
<type>[optional scope]: <description>

[optional body]

[optional footer(s)]
----

详见:https://www.conventionalcommits.org/zh-hans/v1.0.0/
2025-08-30 13:36:35 +08:00

165 lines
4.4 KiB
Plaintext

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
GRAY='\033[0;37m'
NC='\033[0m' # No Color
# 启用调试模式(可选)
DEBUG=${DEBUG:-false}
# 输出函数
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_debug() {
if [ "$DEBUG" = "true" ]; then
echo -e "${GRAY}[DEBUG]${NC} $1"
fi
}
print_section() {
echo -e "${PURPLE}[SECTION]${NC} $1"
}
print_command() {
echo -e "${CYAN}[COMMAND]${NC} $1"
}
print_section "开始执行Commit Message Hook..."
print_info "时间: $(date)"
print_info "工作目录: $(pwd)"
print_info "用户: $(whoami)"
echo
# 调试信息
print_debug "接收到的参数: $*"
print_debug "参数数量: $#"
print_debug "第一个参数 (提交消息文件): $1"
print_debug "Shell: $SHELL"
print_debug "PATH: $PATH"
print_debug "当前进程ID: $"
# 检查 bunx 是否可用
print_section "检查工具可用性..."
print_info "尝试导入用户环境变量..."
export PATH="$HOME/.bun/bin:$PATH"
if command -v bunx >/dev/null 2>&1; then
BUNX_PATH=$(command -v bunx)
print_success "bunx 可用: $BUNX_PATH"
print_debug "bunx 版本信息:"
print_debug "$(bunx --version 2>&1 || echo 'bunx --version 失败')"
else
print_error "bunx 不可用"
print_info "PATH 内容: $PATH"
exit 1
fi
# 检查 commitlint 是否可用
print_section "检查 commitlint..."
if bunx commitlint --version >/dev/null 2>&1; then
COMMITLINT_VERSION=$(bunx commitlint --version)
print_success "commitlint 可用: $COMMITLINT_VERSION"
else
print_error "commitlint 不可用或配置有误"
exit 1
fi
# 检查提交消息文件
echo
print_section "检查提交消息文件..."
if [ -f "$1" ]; then
print_success "文件存在: $1"
FILE_SIZE=$(wc -c < "$1")
print_info "文件大小: $FILE_SIZE 字节"
print_info "文件内容:"
echo -e "${CYAN}========================================${NC}"
cat "$1"
echo
echo -e "${CYAN}========================================${NC}"
# 检查文件权限
FILE_PERMS=$(ls -l "$1" | awk '{print $1}')
print_debug "文件权限: $FILE_PERMS"
# 检查文件内容是否为空
if [ "$FILE_SIZE" -eq 0 ] || [ -z "$(tr -d '[:space:]' < "$1")" ]; then
print_warning "提交消息为空"
fi
else
print_error "找不到提交消息文件: $1"
print_info "当前目录内容:"
ls -la
exit 1
fi
# 检查 commitlint 配置
echo
print_section "检查 commitlint 配置..."
if [ -f ".commitlintrc.js" ] || [ -f ".commitlintrc.json" ] || [ -f "commitlint.config.js" ] || [ -f "commitlint.config.mjs" ]; then
print_success "找到 commitlint 配置文件"
for config_file in .commitlintrc.js .commitlintrc.json commitlint.config.js; do
if [ -f "$config_file" ]; then
print_info "配置文件: $config_file"
print_debug "$config_file 内容预览:"
print_debug "$(head -10 "$config_file" 2>/dev/null || echo '无法读取文件')"
break
fi
done
else
print_warning "未找到 commitlint 配置文件"
print_info "建议创建 .commitlintrc.js 或 commitlint.config.js/commitlint.config.mjs"
fi
# 运行 commitlint
echo
print_section "执行 commitlint 检查..."
print_command "执行命令: bunx commitlint --edit \"$1\""
# 显示详细的执行过程
if [ "$DEBUG" = "true" ]; then
print_debug "详细执行过程:"
bunx commitlint --edit "$1" --verbose
RESULT=$?
else
bunx commitlint --edit "$1"
RESULT=$?
fi
echo
if [ $RESULT -eq 0 ]; then
print_success "提交消息格式检查通过!"
print_info "准备提交到 Git 仓库..."
print_debug "Hook 执行成功,退出代码: 0"
else
print_error "提交消息格式检查失败! (退出代码: $RESULT)"
echo
print_warning "故障排查建议:"
echo "1. 检查提交消息是否符合约定式提交格式"
echo "2. 确认 commitlint 配置是否正确"
echo "3. 运行 'DEBUG=true git commit' 查看详细调试信息"
echo "4. 手动测试: bunx commitlint --edit .git/COMMIT_EDITMSG"
echo
print_debug "Hook 执行失败,退出代码: $RESULT"
fi
print_info "Hook 执行完成: $(date)"
exit $RESULT