Skip to content

Commit 4409e4a

Browse files
authored
docs: refresh agent guides, README flags, and troubleshooting for v2.11 (#197)
Brings AGENTS.md, CLAUDE.md, README and the docs/ pages in line with what shipped in v2.9 through v2.11. Fixes two errors: AGENTS.md pointed at a nonexistent adapters/ai/Codex.sh, and docs/troubleshooting.md told users to run 'bash -x git gtr', which cannot work because git is a binary. The architecture diagram there also had bin/git-gtr and bin/gtr the wrong way round. Adds --sparse/--no-sparse to the README, the missing GTR_* fallback variables and the direct-read variables to docs/configuration.md, and a side-effect section to docs/agent-usage.md. AGENTS.md and CLAUDE.md now cover pr, trust, clean --closed, sparse inheritance, postCd dispatch and the current test layout.
1 parent 540fa2c commit 4409e4a

7 files changed

Lines changed: 140 additions & 72 deletions

File tree

‎AGENTS.md‎

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AGENTS.md
22

3-
This file provides guidance to Codex (Codex.ai/code) when working with code in this repository.
3+
This file provides guidance to AI coding agents (Codex, Copilot, and similar tools) when working with code in this repository.
44

55
## Project Overview
66

@@ -32,7 +32,7 @@ This project uses **BATS tests** for core functions and **manual smoke tests** f
3232

3333
For exhaustive manual testing (hooks, copy patterns, adapters, `--force`, `--from-current`, etc.), see the full checklist in CONTRIBUTING.md or `.github/instructions/testing.instructions.md`.
3434

35-
**Test files**: `adapters`, `config`, `copy_safety`, `integration_lifecycle`, `parse_args`, `provider`, `resolve_base_dir`, `sanitize_branch_name` (all in `tests/`). Shared fixtures in `tests/test_helper.bash`.
35+
**Test files** (all in `tests/`): one `cmd_*.bats` file per command (`cmd_clean`, `cmd_config`, `cmd_copy`, `cmd_create_integration`, `cmd_go`, `cmd_help`, `cmd_list`, `cmd_pr`, `cmd_remove`, `cmd_rename`, `cmd_run`, `cmd_trust`), library tests (`adapters`, `completion`, `config`, `copy_safety`, `core_create_worktree`, `core_resolve_target`, `hooks`, `init`, `launch`, `parse_args`, `platform`, `provider`, `resolve_base_dir`, `sanitize_branch_name`, `sparse`, `ui_color`), and `integration_lifecycle` for end-to-end flows. Shared fixtures in `tests/test_helper.bash`.
3636

3737
**Tip**: Use a disposable repo for testing to avoid polluting your working tree:
3838

@@ -50,39 +50,41 @@ mkdir -p /tmp/gtr-test && cd /tmp/gtr-test && git init && git commit --allow-emp
5050

5151
### Module Structure
5252

53-
| File | Purpose |
54-
| ------------------- | ----------------------------------------------------------------------------------------------------------- |
55-
| `lib/ui.sh` | Logging (`log_error`, `log_info`, `log_warn`), prompts, formatting |
56-
| `lib/args.sh` | Shared argument parser: flag specs (`--flag`, `--flag: val`, aliases), populates `_arg_*` vars |
57-
| `lib/config.sh` | Git config wrapper with precedence: `cfg_get`, `cfg_default`, `cfg_get_all` |
58-
| `lib/platform.sh` | OS detection, GUI helpers |
59-
| `lib/core.sh` | Worktree CRUD: `create_worktree`, `remove_worktree`, `list_worktrees`, `resolve_target`, `resolve_base_dir` |
60-
| `lib/copy.sh` | File/directory copying with glob patterns: `copy_patterns`, `copy_directories` |
61-
| `lib/hooks.sh` | Hook execution: `run_hooks_in` for postCreate/preRemove/postRemove |
62-
| `lib/provider.sh` | Remote hosting detection (GitHub/GitLab) and CLI integration for `clean --merged` |
63-
| `lib/adapters.sh` | Adapter registry, builder functions, generic fallbacks, loader functions |
64-
| `lib/launch.sh` | Editor/AI launch orchestration: `_open_editor`, `_auto_launch_editor`, `_auto_launch_ai` |
65-
| `lib/commands/*.sh` | One file per subcommand: `cmd_create`, `cmd_remove`, etc. (16 files) |
53+
| File | Purpose |
54+
| ------------------- | --------------------------------------------------------------------------------------------------------------------------- |
55+
| `lib/ui.sh` | Logging (`log_error`, `log_info`, `log_warn`), prompts, formatting |
56+
| `lib/args.sh` | Shared argument parser: flag specs (`--flag`, `--flag: val`, aliases), populates `_arg_*` vars |
57+
| `lib/config.sh` | Git config wrapper with precedence: `cfg_get`, `cfg_default`, `cfg_get_all` |
58+
| `lib/platform.sh` | OS detection, GUI helpers |
59+
| `lib/core.sh` | Worktree CRUD: `create_worktree`, `remove_worktree`, `list_worktrees`, `resolve_target`, `resolve_base_dir` |
60+
| `lib/copy.sh` | File/directory copying with glob patterns: `copy_patterns`, `copy_directories` |
61+
| `lib/hooks.sh` | Hook execution: `run_hooks_in`/`run_hooks` for postCreate, preRemove, postRemove; `run_hooks_export` for postCd (see below) |
62+
| `lib/provider.sh` | Remote hosting detection (GitHub/GitLab) and CLI integration for `clean --merged/--closed` |
63+
| `lib/adapters.sh` | Adapter registry, builder functions, generic fallbacks, loader functions |
64+
| `lib/launch.sh` | Editor/AI launch orchestration: `_open_editor`, `_auto_launch_editor`, `_auto_launch_ai` |
65+
| `lib/commands/*.sh` | One file per subcommand: `cmd_create`, `cmd_remove`, `cmd_pr`, `cmd_trust`, etc. (18 files) |
6666

6767
Libraries are sourced in the order listed above (ui → args → config → ... → launch → commands/\*.sh glob).
6868

69+
`postCd` hooks have two dispatch paths, neither of which is `run_hooks_in`: `run_hooks_export`, called inside a subshell from `lib/launch.sh` and `lib/commands/ai.sh` so that environment changes made by the hooks reach the AI tool launched in that same subshell (the editor paths run no postCd hooks), and the `gtr cd` shell functions generated by `init`, which read `gtr.hook.postCd` (plus `.gtrconfig` `hooks.postCd`) and `eval` each hook directly in the user's shell.
70+
6971
### Adapters
7072

7173
Most adapters are defined declaratively in the **adapter registry** (`lib/adapters.sh`) using pipe-delimited entries. Custom adapters that need special logic remain as override files in `adapters/editor/` and `adapters/ai/`.
7274

7375
**Registry-defined adapters**: antigravity, atom, cursor, emacs, idea, nvim, pycharm, sublime, vim, vscode, webstorm, zed (editors) and aider, auggie, codex, continue, copilot, gemini, opencode (AI).
7476

75-
**Custom adapter files**: `adapters/editor/nano.sh`, `adapters/ai/Codex.sh`, `adapters/ai/cursor.sh` — these implement `editor_can_open()`/`editor_open()` or `ai_can_start()`/`ai_start()` directly.
77+
**Custom adapter files**: `adapters/editor/nano.sh`, `adapters/ai/claude.sh`, `adapters/ai/cursor.sh` — these implement `editor_can_open()`/`editor_open()` or `ai_can_start()`/`ai_start()` directly.
7678

7779
**Loading order**: file override → registry → generic PATH fallback. `GTR_EDITOR_CMD` / `GTR_AI_CMD` env vars allow custom tools without adapters.
7880

7981
### Command Flow
8082

8183
```
82-
bin/gtr main() → case statement → cmd_*() handler → lib/*.sh functions → adapters (if needed)
84+
bin/git-gtr main() → case statement → cmd_*() handler → lib/*.sh functions → adapters (if needed)
8385
```
8486

85-
Key dispatch: `new`→`cmd_create`, `rm`→`cmd_remove`, `mv|rename`→`cmd_rename`, `go`→`cmd_go`, `run`→`cmd_run`, `editor`→`cmd_editor`, `ai`→`cmd_ai`, `copy`→`cmd_copy`, `ls|list`→`cmd_list`, `clean`→`cmd_clean`, `init`→`cmd_init`, `config`→`cmd_config`, `completion`→`cmd_completion`, `doctor`→`cmd_doctor`, `adapter`→`cmd_adapter`.
87+
Key dispatch: `new`→`cmd_create`, `pr`→`cmd_pr`, `rm`→`cmd_remove`, `mv|rename`→`cmd_rename`, `go`→`cmd_go`, `run`→`cmd_run`, `editor`→`cmd_editor`, `ai`→`cmd_ai`, `copy`→`cmd_copy`, `ls|list`→`cmd_list`, `clean`→`cmd_clean`, `init`→`cmd_init`, `config`→`cmd_config`, `completion`→`cmd_completion`, `doctor`→`cmd_doctor`, `adapter|adapters`→`cmd_adapter`, `trust`→`cmd_trust`. `cd` has no `cmd_*` handler: the dispatcher prints shell-integration instructions because `gtr cd` is implemented by the shell function that `init` generates.
8688

8789
**Example: `git gtr new my-feature`**
8890

@@ -114,35 +116,37 @@ cmd_editor() → resolve_target() → load_editor_adapter() → editor_open()
114116

115117
**`init` command**: Outputs shell functions for `gtr cd <branch>` navigation. Output is cached to `~/.cache/gtr/` and auto-invalidates on version change. Users source the cache file directly in their shell rc for fast startup (see `git gtr help init`).
116118

117-
**`clean --merged`**: Removes worktrees whose PRs/MRs are merged. Auto-detects GitHub (`gh`) or GitLab (`glab`) from the `origin` remote URL. Override with `gtr.provider` config for self-hosted instances.
119+
**`clean --merged` / `clean --closed`**: Removes worktrees whose PRs/MRs are merged or closed and deletes their branches. Auto-detects GitHub (`gh`) or GitLab (`glab`) from the `origin` remote URL. Override with `gtr.provider` config for self-hosted instances. `clean` also unlocks and prunes locked registry entries whose directories no longer exist.
120+
121+
**`pr <number|url|branch>`** (lib/commands/pr.sh): Creates a worktree from a GitHub pull request via `gh`. Uses `gh pr checkout --worktree` when the installed `gh` supports it, otherwise fetches `refs/pull/<n>/head` through a compatibility path.
122+
123+
**`new --porcelain`**: Emits exactly three `key<TAB>value` records (`path`, `branch`, `hook_status`) on stdout with everything else on stderr. Contract documented in `docs/agent-usage.md`; keep it stable.
124+
125+
**Sparse-checkout inheritance** (`gtr.sparse.inherit`, default on): On Git 2.36+, `new` copies the base worktree's sparse-checkout patterns instead of materializing a full tree. `--sparse`/`--no-sparse` override per invocation.
118126

119127
## Common Development Tasks
120128

121129
### Adding a New Command
122130

123131
1. Create `lib/commands/<name>.sh` with `cmd_<name>()` function
124-
2. Add case entry in `main()` dispatcher in `bin/gtr`
132+
2. Add case entry in `main()` dispatcher in `bin/git-gtr`
125133
3. Add help text in `lib/commands/help.sh`
126-
4. Update all three completion files: `completions/gtr.bash`, `completions/_git-gtr`, `completions/git-gtr.fish`
134+
4. Add the command and its flags to the `generate_bash`, `generate_zsh`, and `generate_fish` templates in `scripts/generate-completions.sh`, then run `./scripts/generate-completions.sh` (the files under `completions/` are generated; CI runs `--check`)
127135
5. Update README.md
128136

129137
### Adding an Adapter
130138

131-
**Standard adapters** (just a command name + error message): Add an entry to `_EDITOR_REGISTRY` or `_AI_REGISTRY` in `lib/adapters.sh`. Then update: help text in `lib/commands/help.sh`, all three completions, README.md.
139+
**Standard adapters** (just a command name + error message): Add an entry to `_EDITOR_REGISTRY` or `_AI_REGISTRY` in `lib/adapters.sh`. Then update help text in `lib/commands/help.sh` and README.md, and run `./scripts/generate-completions.sh` (registry names feed the completions automatically).
132140

133-
**Custom adapters** (special logic needed): Create `adapters/{editor,ai}/<name>.sh` implementing the two required functions (see `adapters/ai/Codex.sh` for an example). File-based adapters take priority over registry entries.
141+
**Custom adapters** (special logic needed): Create `adapters/{editor,ai}/<name>.sh` implementing the two required functions (see `adapters/ai/claude.sh` for an example). File-based adapters take priority over registry entries.
134142

135143
### Updating the Version
136144

137145
Update `GTR_VERSION` in `bin/git-gtr`.
138146

139147
### Shell Completion Updates
140148

141-
When adding commands or flags, update all three files:
142-
143-
- `completions/gtr.bash` (Bash)
144-
- `completions/_git-gtr` (Zsh)
145-
- `completions/git-gtr.fish` (Fish)
149+
`completions/gtr.bash`, `completions/_git-gtr`, and `completions/git-gtr.fish` are generated by `scripts/generate-completions.sh` and carry a `DO NOT EDIT MANUALLY` header. Adapter names come from `_EDITOR_REGISTRY` / `_AI_REGISTRY`, config keys from `_CFG_KEY_MAP`, and commands and flags from the three `generate_*` templates inside the script. After changing any of those, run `./scripts/generate-completions.sh` and commit the result; CI fails when `--check` finds a difference.
146150

147151
## Critical Gotcha: `set -e`
148152

@@ -171,17 +175,23 @@ All config uses `gtr.*` prefix via `git config`. Key settings:
171175

172176
- `gtr.worktrees.dir` — Base directory (default: `<repo-name>-worktrees` sibling)
173177
- `gtr.worktrees.prefix` — Folder prefix (default: `""`)
178+
- `gtr.defaultBranch` / `gtr.defaultRemote` — Base branch (default: auto-detect) and remote (default: `origin`) for new worktrees
179+
- `gtr.sparse.inherit` — Inherit sparse-checkout from the base worktree on Git 2.36+ (default: `true`)
180+
- `gtr.provider` — Force `github` or `gitlab` for `clean --merged/--closed` (default: detect from `origin` URL)
174181
- `gtr.editor.default` / `gtr.ai.default` — Default editor/AI tool
175182
- `gtr.copy.include` / `gtr.copy.exclude` — File glob patterns (multi-valued, use `--add`)
176183
- `gtr.copy.includeDirs` / `gtr.copy.excludeDirs` — Directory patterns (multi-valued)
177-
- `gtr.hook.postCreate` / `gtr.hook.preRemove` / `gtr.hook.postRemove` — Hook commands (multi-valued)
184+
- `gtr.hook.postCreate` / `gtr.hook.preRemove` / `gtr.hook.postRemove` / `gtr.hook.postCd` — Hook commands (multi-valued; `postCd` runs in the current shell after `gtr cd`, `gtr new --cd`, or `gtr pr --cd`)
185+
186+
Every `cfg_default` key also has an environment-variable fallback (for example `GTR_WORKTREES_DIR`, `GTR_DEFAULT_BRANCH`); the full table is in `docs/configuration.md`.
178187

179188
Hook env vars: `REPO_ROOT`, `WORKTREE_PATH`, `BRANCH`. preRemove hooks run with cwd in worktree; failure aborts removal unless `--force`.
180189

181190
## Debugging
182191

183192
```bash
184193
bash -x ./bin/gtr <command> # Full trace
194+
GTR_DEBUG=1 ./bin/gtr <command> # Report file:line:function on an unguarded failure
185195
declare -f function_name # Check function definition
186196
echo "Debug: var=$var" >&2 # Inspect variable
187197
./bin/gtr doctor # Health check
@@ -195,3 +205,5 @@ echo "Debug: var=$var" >&2 # Inspect variable
195205
- `.github/instructions/*.instructions.md` — File-pattern-specific guidance (testing, shell conventions, lib modifications, adapter contracts, completions)
196206
- `docs/configuration.md` — Complete configuration reference
197207
- `docs/advanced-usage.md` — Advanced workflows
208+
- `docs/agent-usage.md` — `--porcelain` output contract and safety boundaries for coding agents
209+
- `docs/troubleshooting.md` — Common failures and fixes

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- The README documents `--sparse`/`--no-sparse` under `git gtr new` and lists the Maintainers section in its table of contents.
12+
- `docs/agent-usage.md` explains how to suppress hooks, file copying, and fetching for bare automation worktrees.
13+
- `docs/configuration.md` lists the remaining `GTR_*` fallback variables and now separates them from the variables read directly (`GTR_DIR`, `GTR_EDITOR_CMD`, `GTR_AI_CMD`, `GTR_COLOR`, `NO_COLOR`), which do not follow the configuration precedence order.
14+
- `docs/troubleshooting.md` replaces a `bash -x git gtr` instruction that cannot work with a trace of the real script, and documents `GTR_DEBUG`.
15+
- `AGENTS.md` and `CLAUDE.md` now cover the `pr` and `trust` commands, `clean --closed`, sparse-checkout inheritance, `postCd` hooks, and the current test suite layout, and describe the completion files as generated by `scripts/generate-completions.sh`.
16+
917
### Fixed
1018

1119
- `GTR_DEBUG=1` now reports the file, line and function of an unexpected failure. `bin/git-gtr` installed an `ERR` trap but ran under `set -e` alone, so the trap was never inherited by functions; since every command runs inside `main()` and a `cmd_*` handler, the variable had no observable effect. The script now uses `set -eE`, which changes nothing when the trap is not installed.
20+
- `AGENTS.md` referenced a nonexistent `adapters/ai/Codex.sh`; it now points at `adapters/ai/claude.sh`.
21+
- The architecture diagram in `docs/troubleshooting.md` described `bin/git-gtr` as a wrapper around `bin/gtr`; the roles are reversed and the remaining `lib/` modules are listed.
22+
- `AGENTS.md` and `CLAUDE.md` attributed `postCd` hooks to `run_hooks_in`; they now describe `run_hooks_export` and the `init`-generated shell functions, and distinguish the paths: the AI launch path runs them when the tool starts, while the shell functions generated by `init` run them for `gtr cd` and the `--cd` flows.
1223

1324
## [2.11.0] - 2026-08-19
1425

0 commit comments

Comments
 (0)