From c3ba531698ad252fc0baa3907d512d28bb3404a4 Mon Sep 17 00:00:00 2001 From: harshitsinghbhandari <24b4506@iitb.ac.in> Date: Thu, 4 Jun 2026 06:45:19 +0530 Subject: [PATCH] Make pre-commit hook installation and execution work in git worktrees MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In a git worktree, `git rev-parse --show-toplevel` returns the worktree checkout path, and `.git` there is a file containing `gitdir: ...`, not a directory. So both `make pre-commit` (which writes into `.git/hooks`) and the generated hook line (which executes `$(show-toplevel)/.git/hooks/pre-commit.fmt`) fail with "Not a directory" when invoked from inside a worktree. Use `git rev-parse --git-path hooks/...` instead — it resolves to the shared hooks directory in both the main checkout and any worktree. Fixes #1641. --- Makefile | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index cebc907e5..fae34241b 100644 --- a/Makefile +++ b/Makefile @@ -319,12 +319,13 @@ check-licenses: .PHONY: pre-commit pre-commit: - cp scripts/pre-commit.fmt .git/hooks - touch .git/hooks/pre-commit - cat .git/hooks/pre-commit | grep -v 'hooks/pre-commit\.fmt' > /tmp/pre-commit.new || true - echo 'PRECOMMIT_NOFMT=$${PRECOMMIT_NOFMT} $$(git rev-parse --show-toplevel)/.git/hooks/pre-commit.fmt' >> /tmp/pre-commit.new - mv /tmp/pre-commit.new .git/hooks/pre-commit - chmod +x .git/hooks/pre-commit + $(eval HOOKS_DIR := $(shell git rev-parse --git-path hooks)) + cp scripts/pre-commit.fmt $(HOOKS_DIR)/ + touch $(HOOKS_DIR)/pre-commit + cat $(HOOKS_DIR)/pre-commit | grep -v 'hooks/pre-commit\.fmt' > /tmp/pre-commit.new || true + echo 'PRECOMMIT_NOFMT=$${PRECOMMIT_NOFMT} $$(git rev-parse --git-path hooks/pre-commit.fmt)' >> /tmp/pre-commit.new + mv /tmp/pre-commit.new $(HOOKS_DIR)/pre-commit + chmod +x $(HOOKS_DIR)/pre-commit .PHONY: serve-docs serve-docs: