Skip to content

Commit 707f2c4

Browse files
committed
Enhance shell completion documentation and scripts for git gtr
- Updated README.md to clarify prerequisites for Bash and Zsh completions, including the need for git's completion system. - Improved completion instructions in completions.instructions.md to reflect changes in command structure and added details for testing completions. - Modified completion scripts for Bash, Zsh, and Fish to integrate better with their respective completion systems and added instructions for installation and usage.
1 parent 67fe714 commit 707f2c4

File tree

5 files changed

+79
-25
lines changed

5 files changed

+79
-25
lines changed

‎.github/instructions/completions.instructions.md‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,51 @@ Shell completions provide tab-completion for `git gtr` commands, flags, branches
2727

2828
Each completion file implements:
2929

30-
1.**Command completion** - Top-level commands (`new`, `rm`, `open`, `ai`, `list`, etc.)
30+
1.**Command completion** - Top-level commands (`new`, `rm`, `editor`, `ai`, `list`, etc.)
3131
2.**Flag completion** - Command-specific flags (e.g., `--from`, `--force`, `--editor`)
32-
3.**Branch completion** - Dynamic completion of existing worktree branches (via `git gtr list --porcelain`)
32+
3.**Branch completion** - Dynamic completion of git branches plus special ID `1`(via `git branch`)
3333
4.**Adapter completion** - Editor names (`cursor`, `vscode`, `zed`) and AI tool names (`aider`, `claude`, `codex`)
3434

3535
## Testing Completions
3636

3737
**Manual testing** (no automated tests):
3838

3939
```bash
40-
# Bash - source the completion file
40+
# Bash - source the completion file (requires git's bash completion to be loaded)
4141
source completions/gtr.bash
4242
git gtr <TAB># Should show commands
4343
git gtr new <TAB># Should show flags
44-
git gtr open <TAB># Should show branches
45-
git gtr open --editor <TAB># Should show editor names
46-
47-
# Zsh - fpath must include completions directory
48-
fpath=(completions $fpath)
49-
autoload -U compinit && compinit
50-
git gtr <TAB>
44+
git gtr go <TAB># Should show branches + '1'
45+
git gtr editor <TAB># Should show branches + '1'
46+
git gtr editor --editor <TAB># Should show editor names
47+
48+
# Zsh - copy to fpath directory and reload (requires git's zsh completion)
49+
mkdir -p ~/.zsh/completions
50+
cp completions/_gtr ~/.zsh/completions/
51+
# Add to ~/.zshrc: fpath=(~/.zsh/completions $fpath)
52+
# Add to ~/.zshrc: autoload -Uz compinit && compinit
53+
exec zsh # Reload shell
54+
git gtr <TAB># Should show commands
55+
git gtr new <TAB># Should show flags
56+
git gtr go <TAB># Should show branches + '1'
5157

5258
# Fish - symlink to ~/.config/fish/completions/
5359
ln -s "$(pwd)/completions/gtr.fish"~/.config/fish/completions/
54-
git gtr <TAB>
60+
exec fish # Reload shell
61+
git gtr <TAB># Should show commands
62+
git gtr new <TAB># Should show flags
63+
git gtr go <TAB># Should show branches + '1'
5564
```
5665

66+
**Important**: All shell completions require git's own completion system to be enabled for the `git` command. The completions integrate with git's subcommand completion framework.
67+
5768
## Branch Completion Logic
5869

59-
All three completions dynamically fetch current worktree branches:
70+
All three completions dynamically fetch current git branches:
6071

61-
-Parse output of `git gtr list --porcelain` (tab-separated: `path\tbranch\tstatus`)
62-
-Extract branch column (second field)
63-
-Exclude the special ID `1`(main repo) if needed
72+
-Use `git branch --format='%(refname:short)'` to get branch names
73+
-Add special ID `1` for main repo (allows `git gtr go 1`, `git gtr editor 1`, etc.)
74+
-Return combined list of branches + `1`for commands that accept branch arguments (go, editor, ai, rm)
6475

6576
## Adapter Name Updates
6677

‎README.md‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ source ~/.zshrc
123123

124124
### Shell Completions (Optional)
125125

126-
**Bash** (requires `bash-completion` v2):
126+
**Bash** (requires `bash-completion` v2 and git completions):
127127

128128
```bash
129129
# Install bash-completion first (if not already installed)
@@ -133,15 +133,27 @@ brew install bash-completion@2
133133
# Ubuntu/Debian:
134134
sudo apt install bash-completion
135135

136+
# Ensure git's bash completion is enabled (usually installed with git)
136137
# Then enable gtr completions:
137138
echo'source /path/to/git-worktree-runner/completions/gtr.bash'>>~/.bashrc
138139
source~/.bashrc
139140
```
140141

141-
**Zsh:**
142+
**Zsh** (requires git's zsh completion):
142143

143144
```bash
144-
echo'source /path/to/git-worktree-runner/completions/_gtr'>>~/.zshrc
145+
# Add completion directory to fpath and enable
146+
mkdir -p ~/.zsh/completions
147+
cp /path/to/git-worktree-runner/completions/_gtr ~/.zsh/completions/
148+
149+
# Add to ~/.zshrc (if not already there):
150+
cat >>~/.zshrc <<'EOF'
151+
# Enable completions
152+
fpath=(~/.zsh/completions $fpath)
153+
autoload -Uz compinit && compinit
154+
EOF
155+
156+
source~/.zshrc
145157
```
146158

147159
**Fish:**

‎completions/_gtr‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
#compdef git-gtr
1+
#compdef _git-gtr git-gtr
2+
3+
# Register gtr as a git subcommand for completion
4+
zstyle ':completion:*:*:git:*' user-commands gtr:'Git worktree management'
5+
26
# Zsh completion for git gtr
37

48
_git-gtr(){
9+
# Note: When called via 'git gtr', the words array is [git, gtr, <command>, ...]
10+
# This matches how git's completion system passes context to subcommand completions
11+
512
local -a commands
613
commands=(
714
'new:Create a new worktree'
@@ -25,10 +32,12 @@ _git-gtr(){
2532
# Add special ID '1' for main repo
2633
all_options=("1" "${branches[@]}")
2734

28-
if (( CURRENT == 2 )); then
35+
# Complete the gtr subcommand (new, go, editor, etc.)
36+
if (( CURRENT == 3 )); then
2937
_describe 'commands' commands
30-
elif (( CURRENT == 3 )); then
31-
case "$words[2]" in
38+
# Complete arguments to the subcommand
39+
elif (( CURRENT == 4 )); then
40+
case "$words[3]" in
3241
go|editor|ai|rm)
3342
_describe 'branch names' all_options
3443
;
@@ -46,16 +55,17 @@ _git-gtr(){
4655
_values 'config action' get set add unset
4756
;
4857
esac
49-
elif (( CURRENT >= 4 )); then
50-
case "$words[2]" in
58+
# Complete subsequent arguments
59+
elif (( CURRENT >= 5 )); then
60+
case "$words[3]" in
5161
rm)
5262
_arguments \
5363
'--delete-branch[Delete branch]' \
5464
'--force[Force removal even if dirty]' \
5565
'--yes[Non-interactive mode]'
5666
;
5767
config)
58-
case "$words[3]" in
68+
case "$words[4]" in
5969
get|set|add|unset)
6070
_values 'config key' \
6171
'gtr.worktrees.dir' \

‎completions/gtr.bash‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#!/bin/bash
22
# Bash completion for git gtr
3+
#
4+
# Prerequisites:
5+
# - bash-completion v2+ must be installed
6+
# - Git's bash completion must be enabled
7+
#
8+
# This completion integrates with git's completion system by defining a _git_<subcommand>
9+
# function, which git's completion framework automatically discovers and calls when
10+
# completing "git gtr ..." commands.
11+
#
12+
# Installation:
13+
# Add to your ~/.bashrc:
14+
# source /path/to/git-worktree-runner/completions/gtr.bash
315

416
_git_gtr(){
517
local cur prev words cword

‎completions/gtr.fish‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Fish completion for git gtr
2+
#
3+
# This completion integrates with fish's completion system by registering completions
4+
# for the "git" command with custom predicates that detect "git gtr" usage.
5+
#
6+
# Installation:
7+
# Symlink to fish's completions directory:
8+
# ln -s /path/to/git-worktree-runner/completions/gtr.fish ~/.config/fish/completions/
9+
# Then reload fish:
10+
# exec fish
211

312
# Helper function to check if we're in 'git gtr' context
413
function __fish_git_gtr_needs_command

0 commit comments

Comments
(0)