From 4a86e95ecfed23deb7169221f3188e5f31411b9b Mon Sep 17 00:00:00 2001 From: chenhang Date: Mon, 15 Jun 2026 14:18:58 +0800 Subject: [PATCH] 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/...'. --- .githooks/post-receive | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.githooks/post-receive b/.githooks/post-receive index 52480672..d53cd9b9 100755 --- a/.githooks/post-receive +++ b/.githooks/post-receive @@ -14,6 +14,18 @@ # - **不**用 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 **加**载逻辑 export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm