Skip to content

Commit 761d14a

Browse files
committed
feat: 处理硬编码嵌套层次,自动动态查找钩子位置
1 parent 7370a03 commit 761d14a

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

.githooks/git-wrapper.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/sh
22
# Git 包装脚本:供 VS Code 通过 git.path 配置使用
33
# push 成功后自动同步父仓库 Lab 的子模块指针
4-
# 适用于所有两级深度的子模块(Lab/<dir>/<name>
4+
# 适用于任意深度的子模块(通过 --show-superproject-working-tree 动态定位父仓库
55

66
REAL_GIT="/usr/bin/git"
77
LOG="/tmp/git-wrapper.log"
@@ -16,9 +16,12 @@ _exit=$?
1616
# 仅在 push 成功时触发同步
1717
if [ "$1" = "push" ] && [ $_exit -eq 0 ]; then
1818
_toplevel=$("$REAL_GIT" rev-parse --show-toplevel 2>/dev/null)
19-
_hook="$_toplevel/../../.githooks/submodule/post-push"
20-
if [ -f "$_hook" ]; then
21-
sh "$_hook"
19+
_parent=$("$REAL_GIT" -C "$_toplevel" rev-parse --show-superproject-working-tree 2>/dev/null)
20+
if [ -n "$_parent" ]; then
21+
_hook="$_parent/.githooks/submodule/post-push"
22+
if [ -f "$_hook" ]; then
23+
sh "$_hook"
24+
fi
2225
fi
2326
fi
2427

.githooks/submodule/post-push

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ set -e
77

88
# 子模块根目录 & 父仓库根目录
99
SUB_DIR=$(git rev-parse --show-toplevel)
10-
# 适用于所有两级深度的子模块(React/source、JS/engine-source 等)
11-
PARENT_DIR=$(cd "$SUB_DIR/../.." && pwd)
10+
# 动态定位父仓库,适用于任意深度的子模块
11+
PARENT_DIR=$(git -C "$SUB_DIR" rev-parse --show-superproject-working-tree 2>/dev/null)
12+
if [ -z "$PARENT_DIR" ]; then
13+
echo "[post-push] ERROR: 无法找到父仓库(非子模块环境)" >&2
14+
exit 1
15+
fi
1216

1317
# 子模块相对于父仓库的路径,例如 React/Ahooks-source
1418
SUB_REL=$(realpath --relative-to="$PARENT_DIR" "$SUB_DIR" 2>/dev/null \

scripts/setup-hooks.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ git() {
2727
if [ "\$1" = "push" ] && [ \$_exit -eq 0 ]; then
2828
local _toplevel
2929
_toplevel=\$(command git rev-parse --show-toplevel 2>/dev/null)
30-
local _hook="\$_toplevel/../../.githooks/submodule/post-push"
31-
if [ -f "\$_hook" ]; then
32-
sh "\$_hook"
30+
local _parent
31+
_parent=\$(command git -C "\$_toplevel" rev-parse --show-superproject-working-tree 2>/dev/null)
32+
if [ -n "\$_parent" ]; then
33+
local _hook="\$_parent/.githooks/submodule/post-push"
34+
if [ -f "\$_hook" ]; then
35+
sh "\$_hook"
36+
fi
3337
fi
3438
fi
3539
return \$_exit

0 commit comments

Comments
 (0)