← 教程列表
coding

Design: Read-Only Environment Detection

📅 2026/6/12 16:30:02⏱ 约 5 分钟阅读👁 8 阅读

Three read-only git commands detect the environment without side effects: ```bash GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P) GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P) BRANCH=$(git branch --show-current) ``` Two signals derived: - **IN_LINKED_WORKTREE:** `GIT_DIR != GIT_COMMON` — the agent is in a worktree created by something else (Codex App, Claude Code Agent tool, previous skill run, or the user) - **ON_DETACHED_HEAD:** `BRANCH` is empty — no named branch exists Why `git-dir != git-common-dir` instead of checking `show-toplevel`: - In a normal repo, both resolve to the same `.git` directory - In a linked worktree, `git-dir` is `.git/worktrees/` while `git-common-dir` is `.git` - In a submodule, both are equal — avoiding a false positive that `show-toplevel` would produce - Resolving via `cd && pwd -P` handles the relative-path problem (`git-common-dir` returns `.git` relative in normal repos but absolute in worktrees) and symlinks (macOS `/tmp` → `/private/tmp`)

← 返回教程列表 AI8899 · 内容仅供学习参考