fix(githooks): restore cd-to-repo-top before NVM load

Without cd, git rev-parse --show-toplevel fails inside .git/
with 'must be run in a work tree', causing init_log_file to
write to /logs/post-receive.log (root path), and copy_env_by_branch
to fail with 'cannot stat env-example/...'.
This commit is contained in:
2026-06-15 14:18:58 +08:00
parent cc75bed133
commit 4a86e95ecf
+12
View File
@@ -14,6 +14,18 @@
# - **不**用 set -e —— 各步**自**己处理错**误**env 不**存**在 / build 失败 / 旧**进**程**不**存**在)→ 让 deploy 错**误****不**挡 git push # - **不**用 set -e —— 各步**自**己处理错**误**env 不**存**在 / build 失败 / 旧**进**程**不**存**在)→ 让 deploy 错**误****不**挡 git push
# 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
REPO_TOPLEVEL="$PWD"
fi
cd "$REPO_TOPLEVEL" || { echo "FATAL: cannot cd to $REPO_TOPLEVEL" >&2; exit 1; }
# 同 .bashrc **里**的 NVM **加**载逻辑 # 同 .bashrc **里**的 NVM **加**载逻辑
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