tools/clang-format.sh: cache clang-format in the common git dir#13302
tools/clang-format.sh: cache clang-format in the common git dir#13302moonchen wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes formatter installation/cache lookup under git worktree setups by updating tools/clang-format.sh to cache/download clang-format under the common Git directory (shared across linked worktrees), aligning the script’s behavior with how CMakeLists.txt computes GIT_COMMON_DIR.
Changes:
- Switch clang-format cache root from
git rev-parse --absolute-git-dirto an absolutegit rev-parse --git-common-dir. - Update the sourced-script path logic (used by the pre-commit hook) to use the common Git dir cache location as well.
clang-format.sh located its download cache with `git rev-parse --absolute-git-dir`, which in a linked worktree is the per-worktree git dir (.git/worktrees/<name>) rather than the shared .git. clang-format is installed once under the common dir, so in a worktree the pre-commit hook and the format target could not find it and failed with "No clang-format found". Resolve the common git dir instead, matching how CMakeLists.txt already computes GIT_COMMON_DIR. It is made absolute with `cd ... && pwd` rather than `git rev-parse --path-format=absolute`, avoiding a dependency on git 2.31+; that --path-format form was removed in apache#11495 over a cmake 3.28 incompatibility. The no-argument default target now uses `git rev-parse --show-toplevel` so a bare run inside a worktree formats that worktree, not the main checkout. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
e6f633a to
65def48
Compare
TestingTested the format workflows from a fresh clone, comparing Does clang-format get found and run? — verified two ways: cache-dir resolution, and a real
clang-format is installed once under the common Everything checked:
Addressing #11495: that PR removed |
Problem
In a linked
git worktree,cmake --build build -t formatand the pre-commit hook fail withNo clang-format found.tools/clang-format.shresolves its download/cache directory withgit rev-parse --absolute-git-dir, which in a worktree is the per-worktree git dir (.git/worktrees/<name>), not the shared.git. clang-format is installed once under the common dir, so the worktree looks in the wrong, empty place.Fix
Resolve the common git dir, which is shared by all worktrees — the same thing
CMakeLists.txtalready does to computeGIT_COMMON_DIR, so the script and CMake now agree on the cache location.--git-common-dircan return a relative path, so it is made absolute withcd … && pwd.I deliberately avoided
git rev-parse --path-format=absolute --git-common-dir: that exact form was removed in #11495 for a cmake 3.28 incompatibility (it needs git 2.31+). This restores the worktree support originally added in #11015 without reverting the #11495 fix.Only
clang-format.shis affected —yapf.sh/cmake-format.shnow useuvand no longer cache under the git dir.Testing (git 2.43.0, cmake 3.28.3)
.git/fmt: the pre-commit hook now finds clang-format and commits succeed (previouslyNo clang-format found) — verified end-to-end with theclangFormat.binaryfallback removed.