chore(githooks): simplify post-receive hook repo root detection

Remove the workaround that derived REPO_TOPLEVEL from
`git rev-parse --absolute-git-dir` and fall back to
`git rev-parse --show-toplevel` directly. Also drop the
related comments and debug echo. The simpler approach is
sufficient for the hook's needs and makes the script easier
to maintain.
This commit is contained in:
2026-06-16 15:33:22 +08:00
parent 20cb9e80ed
commit bab731bd21
+1 -12
View File
@@ -1,25 +1,14 @@
#!/bin/sh #!/bin/sh
# Post-receive hook: push 后自动 build + start # Post-receive hook: push 后自动 build + start
# #
# 重要:git 调钩子时 cwd=GIT_DIR(不是工作树根),所以必须先把 cwd
# 切到工作树根,下面的所有路径(env-example/、logs/、pnpm)才正确。
# pnpm 环境变量 # pnpm 环境变量
export NVM_DIR="$HOME/.nvm" export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# 关键:git 调钩子时 cwd=GIT_DIR,且`git rev-parse --show-toplevel`会拒返回。 REPO_TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null || echo "$PWD")"
# 用 `git rev-parse --absolute-git-dir` 拿到绝对 git-dir 路径,
# 其父目录就是工作树根。
GIT_DIR_ABS="$(git rev-parse --absolute-git-dir 2>/dev/null)"
if [ -n "$GIT_DIR_ABS" ] && [ -d "$GIT_DIR_ABS" ]; then
REPO_TOPLEVEL="$(cd "$GIT_DIR_ABS/.." && pwd)"
else
REPO_TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null || echo "$PWD")"
fi
cd "$REPO_TOPLEVEL" || { echo "FATAL: cannot cd to $REPO_TOPLEVEL" >&2; exit 1; } cd "$REPO_TOPLEVEL" || { echo "FATAL: cannot cd to $REPO_TOPLEVEL" >&2; exit 1; }
echo "=== REPO_TOPLEVEL=$REPO_TOPLEVEL (GIT_DIR=$GIT_DIR_ABS) ===" >&2
# ============================================================ # ============================================================
# 0. sync_worktree —— 将**工**作**树**同步到**新** push 的 HEAD # 0. sync_worktree —— 将**工**作**树**同步到**新** push 的 HEAD