This one is about a git detail I didn’t know, which showed up in a pre-commit hook of a personal iOS project that builds a small Swift command-line tool (the hooks use it for a few lint checks). Git hands its hooks a couple of environment variables that point at the repository being committed, mainly GIT_DIR and GIT_INDEX_FILE, and those win over cd and git -C, so anything inside the hook that runs git on some other directory ends up running it on your repository instead. SwiftPM runs a lot of git, and the failures it produced looked like everything except an environment variable.
What git puts into a hook’s environment
We can see it with two throwaway repositories, app with a linked worktree app-wt, and an unrelated other next to them that has one committed file:
git init -q app && git -C app commit -q --allow-empty -m init
git -C app worktree add -q ../app-wt
git init -q other && touch other/notes.txt
git -C other add notes.txt && git -C other commit -q -m other-init
The pre-commit hook prints the two variables and then asks other for its last commit and its status:
#!/bin/sh
echo "GIT_DIR=$GIT_DIR"
echo "GIT_INDEX_FILE=$GIT_INDEX_FILE"
echo "other's last commit: $(git -C ../other log -1 --format=%s)"
git -C ../other status -s
It goes to app/.git/hooks/pre-commit (and needs chmod +x, otherwise git just prints a hint and skips it), and the worktree uses the same hook, since hooks live in the shared git directory. Then we commit once from each checkout:
(cd app && git commit -q --allow-empty -m m1)
(cd app-wt && git commit -q --allow-empty -m w1)
which prints this with git 2.56.1 and default config (paths shortened):
GIT_DIR=
GIT_INDEX_FILE=.git/index
other's last commit: other-init
GIT_DIR=/tmp/hookdemo/app/.git/worktrees/app-wt
GIT_INDEX_FILE=/tmp/hookdemo/app/.git/worktrees/app-wt/index
other's last commit: init
?? notes.txt
From the main checkout GIT_DIR isn’t set and the index path is relative, so git -C ../other finds other the normal way. From the linked worktree both are absolute paths into the worktree’s git directory. -C only changes the working directory, and git doesn’t look for a repository at all when GIT_DIR is already set, so log answers with init, which is app’s commit. And since there’s no GIT_WORK_TREE, git takes the current directory as the top of the work tree, so status lists other’s file as an untracked file on the app-wt branch.
Linked worktrees are the common way to hit this, but not the only one. Git sets GIT_DIR whenever the repository is reached through a .git file (submodules and --separate-git-dir do that too) or an explicit --git-dir. The main checkout isn’t safe either. With git commit -a or git commit -- <path>, the hook’s GIT_INDEX_FILE is an absolute path to a temporary index (.git/index.lock or .git/next-index-<pid>.lock), and the same git -C ../other status from the main checkout fails with fatal: unable to read <sha>, because it reads app’s index against other’s objects. Even the plain relative .git/index breaks a git -C into a repository whose .git is a file. GIT_INDEX_FILE is set only for the commit hooks (pre-commit, commit-msg and so on), while pre-push or post-checkout get GIT_DIR alone. The githooks man page does say this, the hook “should clear these environment variables so they do not interfere” when it runs git in a foreign repository, but it’s not the first place I’d look when a build fails.
How it looked with SwiftPM
SwiftPM keeps a bare clone of every dependency in its repository cache, and builds a working copy for each one from those clones. Some of its git calls pass --git-dir explicitly and some don’t, and the ones that don’t are the ones that went astray. Depending on which step SwiftPM was at, the same cause came out three different ways.
The first one, seen weeks earlier on Swift 6.3, was version resolution:
error: Dependencies could not be resolved because no versions of
'swift-argument-parser' match the requirement 1.5.0..<2.0.0
With GIT_DIR set, Swift 6.3 was reading the app’s tags instead of the package’s, and there were no semantic versions among them. Resolving by hand outside the commit made it go away, which is why it got blamed on a cold cache for a while. Swift 6.4 passes --git-dir when it lists tags, so on 6.4 resolution goes through and the next step fails instead.
That’s the second one, which came when the tool had to be release-built inside the hook:
error: 'swift-argument-parser': Couldn't check out revision '6a52f32…':
fatal: unable to read tree (6a52f32…)
and the same for swift-syntax. The disk had filled up earlier that day and other processes were fetching into the same shared cache, so a damaged cache looked likely, but git fsck on both cached clones was clean and both trees were readable. Git was looking for the tree in the app’s object store, not in the clone that had it, since the checkout of the working copy runs without --git-dir.
For the third one, the retried commit hung until the hook wrapper’s 30 minute limit killed it, with an empty log, and with a load average somewhere near 500 at the time it was easy to put it down to a slow build. The next commit then failed right away because the worktree’s index.lock existed. lsof on the lock file named the process holding it:
lsof "$(git rev-parse --git-path index.lock)"
ps eww -p <pid> | tr ' ' '\n' | grep '^GIT_'
It was a git -C / status -s left over from the hook’s swift build, re-parented to launchd, and ps eww showed GIT_DIR and GIT_INDEX_FILE pointing at the worktree. With GIT_DIR set, SwiftPM resolves some of its paths to /, and git took / as the work tree of my branch and walked it while holding the worktree’s index lock. With git’s defaults that walk stops early at each untracked directory, but my global config sets status.showUntrackedFiles=all, so git walked the whole disk and kept the lock for about 25 minutes after the hook was gone. A kill <pid> would have ended it sooner (git removes its own lock on a normal TERM), and if lsof names no process at all, the lock is stale and deleting it is safe.
Running the tool without git’s variables
Git can list the variables that make a command repository-local, so we don’t have to type them out (git 2.56 lists 15 of them, and a hand-written list is exactly the kind of thing that misses GIT_CONFIG or GIT_COMMON_DIR one day):
$ git rev-parse --local-env-vars
GIT_ALTERNATE_OBJECT_DIRECTORIES
GIT_CONFIG
...
GIT_DIR
...
GIT_INDEX_FILE
...
GIT_COMMON_DIR
In the hook, the tool gets a subshell with those unset (the unquoted $(...) relies on word splitting, which is fine in sh and bash, not in zsh):
#!/bin/sh
without_git_env() (
unset $(git rev-parse --local-env-vars)
"$@"
)
without_git_env swift build --configuration release --product MyTool --package-path Tools/MyTool
The hook itself keeps its environment, because its own git calls need it whenever the commit uses a temporary index. To see both sides, let’s give the worktree two tracked files a and b, change both, stage only b, and run git commit -a with this hook:
#!/bin/sh
without_git_env() (
unset $(git rev-parse --local-env-vars)
"$@"
)
echo "hook sees staged: $(git diff --cached --name-only | tr '\n' ' ')"
echo "wrapped sees staged: $(without_git_env git diff --cached --name-only | tr '\n' ' ')"
echo "other's last commit: $(without_git_env git -C ../other log -1 --format=%s)"
hook sees staged: a b
wrapped sees staged: b
other's last commit: other-init
The hook sees what is about to be committed, the wrapped call sees the ordinary index (which is why we don’t clear the variables for the whole hook), and the wrapped git -C ../other reaches other. On the day itself I got the commit through by building the tool once outside any commit, which only lasts until the tool changes again. The same cause had also shown up earlier in the project as a test helper in a hook, whose git -C "$scratch" config … wrote core.bare = true into the real repository’s config instead of the scratch one’s and broke every worktree at once.
Anything we run inside a git hook that calls git on a directory other than the repository being committed (SwiftPM, npm with git dependencies, a test that sets up its own repository, a script using git -C) needs git’s repository-local variables cleared first, and git rev-parse --local-env-vars gives us the list to clear. A hook that works from the main checkout with a plain commit doesn’t prove much, since linked worktrees, submodules and commit -a all change what git exports, so the safe default is to wrap every such call. When a tool inside a hook fails in a way that looks like a broken cache or a slow machine, looking at its environment with ps eww is a quicker first check than the cache.