Fix post-checkout hook installation failing in git worktrees#57
Merged
albertodebortoli merged 1 commit intomainfrom Apr 10, 2026
Merged
Fix post-checkout hook installation failing in git worktrees#57albertodebortoli merged 1 commit intomainfrom
albertodebortoli merged 1 commit intomainfrom
Conversation
In a git worktree, `.git` is a regular file (containing a gitdir pointer) rather than a directory. The previous code used `fileExists(atPath:)` to detect whether the current directory was a git repository, but that method returns true for both files and directories. As a result, `GitHookInstaller` would proceed past the guard and attempt to copy the hook to `.git/hooks/post-checkout` — a path that cannot exist when `.git` is a file — causing an error. Additionally, when the post-checkout hook fires in a worktree context (git shares the main repo's hooks across all worktrees), it invokes `luca install`, which would re-trigger the same failing installation. Fix: after confirming `.git` exists, check that it is a directory using `attributesOfItem(atPath:)`. If it is a file (worktree), return early — hooks are owned by the main repository. Add `attributesOfItem(atPath:)` to `GitHookInstallerFileManaging` and a new test covering the worktree case.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes
luca installfailing when run inside a git worktree.Root Cause
Git worktrees do not have a
.gitdirectory. Instead, each worktree gets a.gitfile containing a pointer likegitdir: /path/to/main/.git/worktrees/<name>. The actual hooks directory exists only in the main repository at<main>/.git/hooks/.GitHookInstaller.installPostCheckoutHook()usedfileExists(atPath:)to detect whether the current directory was a git repository.FileManager.fileExists(atPath:)returnstruefor both files and directories, so in a worktree the guard passed even though.gitwas a file. The code then tried to copy the hook to.git/hooks/post-checkout— a path that is unreachable when.gitis a file — causing an error.This also triggered a cascading problem: git shares hooks across all worktrees (there is only one hooks directory per repository, in the main repo). So when the post-checkout hook fires in a worktree after a branch switch, it calls
luca install, which re-enters the same failing code path.The issue was reported by a colleague who found
luca installand any tool that calls it (e.g. Swiftlane) would fail when working in a worktree setup. The only workaround at the time was passing--no-install-post-checkout-git-hookto everyluca installinvocation.Fix
After confirming
.gitexists, check that it is a directory usingattributesOfItem(atPath:)[.type]. If it is a file (worktree), return early — the hook is owned by the main repository and does not need to be installed per-worktree.attributesOfItem(atPath:)was added toGitHookInstallerFileManaging(it was already part ofFileManaging/FileManagerWrapper/FileManagerWrapperMock).Type of Change
How Has This Been Tested?
test_installPostCheckoutHook_whenGitWorktree_doesNothingcreates a.gitfile (simulating a worktree) and asserts the installer silently returns without printing anything or modifying the filesystem.Checklist
Breaking Changes?
Additional Notes
A fully complete fix LucaTools/LucaScripts#12 updates the
post-checkoutshell script (in the LucaScripts repo) to pass--no-install-post-checkout-git-hookwhen callingluca install. That prevents the hook from needlessly attempting to reinstall itself on every checkout. With this fix in place that is harmless (the attempt just returns early), but it is a nice follow-up.