-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-hooks.sh
More file actions
executable file
·297 lines (251 loc) · 8.23 KB
/
setup-hooks.sh
File metadata and controls
executable file
·297 lines (251 loc) · 8.23 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash
# 跨平台 Git Hooks 安装脚本
# 支持 macOS、Linux、Windows (Git Bash)
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 获取项目根目录
PROJECT_ROOT="$(cd "$(dirname "$0")" && pwd)"
HOOKS_DIR="$PROJECT_ROOT/.githooks"
GIT_DIR="$PROJECT_ROOT/.git"
echo "========================================"
echo "Git Hooks 跨平台安装工具"
echo "========================================"
echo ""
# 检测操作系统
detect_os() {
local os="unknown"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
os="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="macos"
elif [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
os="windows"
elif [[ -n "$WINDIR" ]]; then
os="windows"
fi
echo "$os"
}
OS=$(detect_os)
echo "检测到操作系统: $OS"
echo ""
# 检查是否在 git 仓库中
if [ ! -d "$GIT_DIR" ]; then
echo -e "${RED}错误:当前目录不是 Git 仓库${NC}"
echo "请在项目根目录运行此脚本"
exit 1
fi
# 安装函数
install_hooks() {
case "$OS" in
"macos"|"linux")
install_unix
;;
"windows")
install_windows
;;
*)
echo -e "${YELLOW}无法识别的操作系统,尝试使用 Unix 方案...${NC}"
install_unix
;;
esac
}
# Unix/Linux/macOS 安装
install_unix() {
echo -e "${BLUE}[1/3] 配置 Git hooks 路径...${NC}"
# 使用 .githooks 目录
git config --local core.hooksPath .githooks
echo -e "${GREEN}✓ Hooks 路径已设置为: .githooks${NC}"
echo ""
echo -e "${BLUE}[2/3] 添加脚本执行权限...${NC}"
if [ -f "$HOOKS_DIR/pre-commit" ]; then
chmod +x "$HOOKS_DIR/pre-commit"
echo -e "${GREEN}✓ pre-commit 已可执行${NC}"
fi
if [ -f "$HOOKS_DIR/commit-msg" ]; then
chmod +x "$HOOKS_DIR/commit-msg"
echo -e "${GREEN}✓ commit-msg 已可执行${NC}"
fi
echo ""
echo -e "${BLUE}[3/3] 验证配置...${NC}"
local configured_path
configured_path=$(git config --local core.hooksPath)
if [ "$configured_path" = ".githooks" ]; then
echo -e "${GREEN}✓ 配置验证成功${NC}"
else
echo -e "${RED}✗ 配置验证失败${NC}"
exit 1
fi
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}安装完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "当前配置:"
echo " Hooks 路径: $configured_path"
echo " 可用 Hooks:"
ls -1 "$HOOKS_DIR"/* 2>/dev/null | grep -v windows | xargs -I {} basename {} | sed 's/^/ - /'
}
# Windows 安装
install_windows() {
echo -e "${BLUE}[1/4] 检测 Windows 环境...${NC}"
local git_hooks_dir="$GIT_DIR/hooks"
echo ""
echo -e "${BLUE}[2/4] 复制 Windows 脚本到 .git/hooks/...${NC}"
local windows_dir="$HOOKS_DIR/windows"
# 复制 PowerShell 脚本
if [ -f "$windows_dir/pre-commit.ps1" ]; then
cp "$windows_dir/pre-commit.ps1" "$git_hooks_dir/pre-commit.ps1"
echo -e "${GREEN}✓ pre-commit.ps1 已复制${NC}"
fi
if [ -f "$windows_dir/commit-msg.ps1" ]; then
cp "$windows_dir/commit-msg.ps1" "$git_hooks_dir/commit-msg.ps1"
echo -e "${GREEN}✓ commit-msg.ps1 已复制${NC}"
fi
# 复制 Batch 脚本作为备用
if [ -f "$windows_dir/pre-commit.bat" ]; then
cp "$windows_dir/pre-commit.bat" "$git_hooks_dir/"
echo -e "${GREEN}✓ pre-commit.bat 已复制${NC}"
fi
echo ""
echo -e "${BLUE}[3/4] 创建 wrapper 脚本...${NC}"
# 创建调用 PowerShell 的 wrapper
cat > "$git_hooks_dir/pre-commit" << 'EOF'
#!/bin/sh
# Wrapper for Windows PowerShell hook
if command -v powershell.exe >/dev/null 2>&1; then
exec powershell.exe -ExecutionPolicy Bypass -File .git/hooks/pre-commit.ps1
elif command -v pwsh >/dev/null 2>&1; then
exec pwsh -ExecutionPolicy Bypass -File .git/hooks/pre-commit.ps1
else
echo "警告:未找到 PowerShell,跳过校验"
exit 0
fi
EOF
chmod +x "$git_hooks_dir/pre-commit"
cat > "$git_hooks_dir/commit-msg" << 'EOF'
#!/bin/sh
# Wrapper for Windows PowerShell hook
MSG_FILE="$1"
if command -v powershell.exe >/dev/null 2>&1; then
exec powershell.exe -ExecutionPolicy Bypass -File .git/hooks/commit-msg.ps1 "$MSG_FILE"
elif command -v pwsh >/dev/null 2>&1; then
exec pwsh -ExecutionPolicy Bypass -File .git/hooks/commit-msg.ps1 "$MSG_FILE"
else
echo "警告:未找到 PowerShell,跳过校验"
exit 0
fi
EOF
chmod +x "$git_hooks_dir/commit-msg"
echo -e "${GREEN}✓ Wrapper 脚本已创建${NC}"
echo ""
echo -e "${BLUE}[4/4] 配置 Git 使用本地 hooks...${NC}"
# Windows 使用 .git/hooks 而不是 core.hooksPath
git config --local --unset core.hooksPath 2>/dev/null || true
echo -e "${GREEN}✓ 已配置为使用 .git/hooks 目录${NC}"
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Windows 安装完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "安装的文件:"
ls -1 "$git_hooks_dir"/*.* 2>/dev/null | xargs -I {} basename {} | sed 's/^/ - /'
echo ""
echo "注意:Windows 需要 PowerShell 5.0 或更高版本"
echo " 如果无法执行,请以管理员身份运行:"
echo " Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"
}
# 卸载函数
uninstall_hooks() {
echo -e "${YELLOW}卸载 Git Hooks...${NC}"
# 重置 hooks 路径
git config --local --unset core.hooksPath 2>/dev/null || true
# 删除 Windows 复制的脚本
if [ -d "$GIT_DIR/hooks" ]; then
rm -f "$GIT_DIR/hooks/pre-commit.ps1"
rm -f "$GIT_DIR/hooks/commit-msg.ps1"
rm -f "$GIT_DIR/hooks/pre-commit.bat"
echo -e "${GREEN}✓ 已清理 Windows 脚本${NC}"
fi
echo -e "${GREEN}✓ Hooks 已卸载${NC}"
}
# 检查依赖
check_dependencies() {
echo -e "${BLUE}检查依赖...${NC}"
if ! command -v git >/dev/null 2>&1; then
echo -e "${RED}错误:未找到 Git${NC}"
exit 1
fi
echo -e "${GREEN}✓ Git 已安装${NC}"
if [ "$OS" = "windows" ]; then
if command -v powershell.exe >/dev/null 2>&1 || command -v pwsh >/dev/null 2>&1; then
echo -e "${GREEN}✓ PowerShell 已安装${NC}"
else
echo -e "${YELLOW}⚠ 未找到 PowerShell,Windows 脚本可能无法执行${NC}"
fi
fi
echo ""
}
# 显示帮助
show_help() {
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " install 安装 Git Hooks (默认)"
echo " uninstall 卸载 Git Hooks"
echo " check 检查当前配置"
echo " help 显示帮助"
echo ""
echo "示例:"
echo " $0 # 安装 hooks"
echo " $0 install # 安装 hooks"
echo " $0 uninstall # 卸载 hooks"
}
# 检查当前配置
check_config() {
echo -e "${BLUE}当前 Git Hooks 配置${NC}"
echo "========================================"
local hooks_path
hooks_path=$(git config --local core.hooksPath 2>/dev/null || echo "未设置")
echo "Hooks 路径: $hooks_path"
echo ""
if [ "$hooks_path" = ".githooks" ]; then
echo -e "${GREEN}✓ 使用项目 .githooks 目录${NC}"
echo "可用 Hooks:"
ls -1 "$HOOKS_DIR"/* 2>/dev/null | grep -v windows | while read -r f; do
if [ -x "$f" ]; then
echo " ✓ $(basename "$f") (可执行)"
else
echo " ✗ $(basename "$f") (无执行权限)"
fi
done
elif [ -d "$GIT_DIR/hooks" ]; then
echo "本地 .git/hooks 目录内容:"
ls -1 "$GIT_DIR/hooks" 2>/dev/null | sed 's/^/ - /' || echo " (空)"
fi
}
# 主逻辑
case "${1:-install}" in
install)
check_dependencies
install_hooks
;;
uninstall)
uninstall_hooks
;;
check)
check_config
;;
help|--help|-h)
show_help
;;
*)
echo -e "${RED}未知选项: $1${NC}"
show_help
exit 1
;;
esac