Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be686a8

Browse files
fix(scripts/githooks): clear all repo-local Git env vars in hooks (#24138)
## Problem In linked worktrees, Git hooks inherit multiple repo-local environment variables: `GIT_DIR`, `GIT_COMMON_DIR`, `GIT_INDEX_FILE`, and others. The pre-commit and pre-push hooks only unset `GIT_DIR`, leaving the rest in place. When `make pre-commit` runs `go build`, Go tries to stamp VCS info by shelling out to `git`. With the leftover partial Git environment, `git` exits 128 and the build fails: ``` error obtaining VCS status: exit status 128 Use -buildvcs=false to disable VCS stamping. ``` This only happens inside hooks in a linked worktree — running `make pre-commit` directly from the terminal works fine because the repo-local vars are not set. ## Fix Replace the bare `unset GIT_DIR` in both hooks with a loop that clears every variable reported by `git rev-parse --local-env-vars`: ```sh while IFS= read -r var; do unset "$var" done < <(git rev-parse --local-env-vars) ``` This covers all 15 repo-local variables Git may inject (`GIT_DIR`, `GIT_COMMON_DIR`, `GIT_INDEX_FILE`, `GIT_OBJECT_DIRECTORY`, etc.) and is forward-compatible — if Git adds new local vars in the future, the loop picks them up automatically.
1 parent 7b7baea commit be686a8

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

scripts/githooks/pre-commit

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414
set -euo pipefail
1515

1616
cd "$(git rev-parse --show-toplevel)"
17-
unset GIT_DIR
17+
18+
# Unset all repo-local Git env vars, not just GIT_DIR. In linked
19+
# worktrees the hook inherits variables like GIT_COMMON_DIR and
20+
# GIT_INDEX_FILE that confuse child processes (notably Go's VCS
21+
# stamping, which shells out to git and gets exit status 128).
22+
# Process substitution (not a pipe) so unset runs in the current shell.
23+
while IFS= read -r var; do
24+
unset "$var"
25+
done < <(git rev-parse --local-env-vars)
1826

1927
# In linked worktrees, set worktree-scoped hooksPath to override shared config.
2028
if [[ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]]; then

scripts/githooks/pre-push

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ ALLOWLIST=(
3030
)
3131

3232
cd "$(git rev-parse --show-toplevel)"
33-
unset GIT_DIR
33+
34+
# Unset all repo-local Git env vars, not just GIT_DIR. In linked
35+
# worktrees the hook inherits variables like GIT_COMMON_DIR and
36+
# GIT_INDEX_FILE that confuse child processes (notably Go's VCS
37+
# stamping, which shells out to git and gets exit status 128).
38+
# Process substitution (not a pipe) so unset runs in the current shell.
39+
while IFS= read -r var; do
40+
unset "$var"
41+
done < <(git rev-parse --local-env-vars)
3442

3543
# In linked worktrees, set worktree-scoped hooksPath to override shared config.
3644
if [[ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]]; then

0 commit comments

Comments
 (0)