fix(githooks): use git rev-parse --absolute-git-dir for repo root
The $0-based dirname trick didn't work because git invokes hooks with just the basename (e.g., 'post-receive'), not the absolute path. Use 'git rev-parse --absolute-git-dir' instead, which works even when CWD is inside .git/.
This commit is contained in:
+9
-14
@@ -19,21 +19,16 @@
|
||||
# 3) **验**证 PATH —— 写日**志**确认 pnpm 可**用**(**不**可用则 build **必**失**败**)
|
||||
|
||||
# ---- 前置:切到 repo 顶 + 加 pnpm 到 PATH ----
|
||||
# 重要:不能**用** `git rev-parse --show-toplevel` 获**取** repo **顶**层
|
||||
# —— git-receive-pack 调 hook 时 CWD **可**能是 `.git/` 内**部**,
|
||||
# 这种情况下 `--show-toplevel` **报**错 `must be run in a work tree`。
|
||||
# 解**法**:**直**接**根**据 hook **本**身路径推**算** repo **顶**层。
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd || echo "")"
|
||||
if [ -z "$SCRIPT_DIR" ]; then
|
||||
# 兜底:**用** git rev-parse(如**果**可**用**)
|
||||
REPO_TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null || true)"
|
||||
if [ -z "$REPO_TOPLEVEL" ]; then
|
||||
# **最**后**兜**底:**直**接**用** PWD
|
||||
REPO_TOPLEVEL="$PWD"
|
||||
fi
|
||||
# 重要:git-receive-pack 调 hook 时 CWD **可**能是 `.git/` 内**部**,
|
||||
# `git rev-parse --show-toplevel` 会**报**错 `must be run in a work tree`。
|
||||
# 解**法**:**用** `git rev-parse --absolute-git-dir` **取**得**绝**对 .git **路**径,
|
||||
# 然后 dirname **得**到 repo **顶**层。**这**两个调**用**在 .git/ 内**部**也都**能**成**功**。
|
||||
GIT_DIR_ABS="$(git rev-parse --absolute-git-dir 2>/dev/null || true)"
|
||||
if [ -n "$GIT_DIR_ABS" ] && [ "$GIT_DIR_ABS" != ".git" ]; then
|
||||
REPO_TOPLEVEL="$(cd "$GIT_DIR_ABS/.." && pwd)"
|
||||
else
|
||||
# .githooks/post-receive 的**父**目录**的****父**目录 = repo **顶**
|
||||
REPO_TOPLEVEL="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
# 兜底:**用** PWD (**非** bare repo 默**认**钩**子**在 **工**作**树**下)
|
||||
REPO_TOPLEVEL="$PWD"
|
||||
fi
|
||||
cd "$REPO_TOPLEVEL" || { echo "FATAL: cannot cd to $REPO_TOPLEVEL" >&2; exit 1; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user