11#! /usr/bin/env bash
22
3+ # Resolve a completion asset from supported install layouts.
4+ _completion_asset_path () {
5+ local shell=" $1 "
6+ shift
7+
8+ local candidate
9+ for candidate in " $@ " ; do
10+ if [ -f " $candidate " ]; then
11+ printf ' %s\n' " $candidate "
12+ return 0
13+ fi
14+ done
15+
16+ log_error " Could not find $shell completion asset under: $GTR_DIR "
17+ log_error " Expected either a source checkout (completions/) or a Homebrew install layout."
18+ return 1
19+ }
20+
21+ _completion_bash_path () {
22+ _completion_asset_path " bash" \
23+ " $GTR_DIR /completions/gtr.bash" \
24+ " $GTR_DIR /etc/bash_completion.d/git-gtr"
25+ }
26+
27+ _completion_zsh_path () {
28+ _completion_asset_path " zsh" \
29+ " $GTR_DIR /completions/_git-gtr" \
30+ " $GTR_DIR /share/zsh/site-functions/_git-gtr"
31+ }
32+
33+ _completion_fish_path () {
34+ _completion_asset_path " fish" \
35+ " $GTR_DIR /completions/git-gtr.fish" \
36+ " $GTR_DIR /share/fish/vendor_completions.d/git-gtr.fish"
37+ }
38+
39+ _completion_single_quote () {
40+ printf " '%s'" " $( printf ' %s' " $1 " | sed " s/'/'\\\\ ''/g" ) "
41+ }
42+
343# Completion command (generate shell completions)
444cmd_completion () {
545 if [ " ${1:- } " = " -h" ] || [ " ${1:- } " = " --help" ]; then
@@ -10,9 +50,15 @@ cmd_completion() {
1050
1151 case " $shell " in
1252 bash)
13- cat " $GTR_DIR /completions/gtr.bash"
53+ local bash_path
54+ bash_path=" $( _completion_bash_path) " || return 1
55+ cat " $bash_path "
1456 ;;
1557 zsh)
58+ local zsh_path zsh_dir zsh_dir_quoted
59+ zsh_path=" $( _completion_zsh_path) " || return 1
60+ zsh_dir=" $( dirname " $zsh_path " ) "
61+ zsh_dir_quoted=" $( _completion_single_quote " $zsh_dir " ) "
1662 # Output zstyle registration + completion loading
1763 # The zstyle MUST run before compinit to register gtr as a git subcommand
1864 cat << EOF
@@ -25,7 +71,7 @@ cmd_completion() {
2571zstyle ':completion:*:*:git:*' user-commands gtr:'Git worktree management'
2672
2773# Add completions to fpath and initialize
28- fpath=($GTR_DIR /completions \$ fpath)
74+ fpath=($zsh_dir_quoted \$ fpath)
2975
3076# Note: If you already have compinit in your .zshrc, you may want to
3177# source this file before your existing compinit call and remove the
@@ -34,7 +80,9 @@ autoload -Uz compinit && compinit -C
3480EOF
3581 ;;
3682 fish)
37- cat " $GTR_DIR /completions/git-gtr.fish"
83+ local fish_path
84+ fish_path=" $( _completion_fish_path) " || return 1
85+ cat " $fish_path "
3886 ;;
3987 " " |--help|-h)
4088 echo " Generate shell completions for git gtr"
4694 echo " zsh Generate Zsh completions"
4795 echo " fish Generate Fish completions"
4896 echo " "
97+ echo " Homebrew installs native shell completions automatically."
98+ echo " "
4999 echo " Examples:"
50- echo " # Bash: add to ~/.bashrc"
100+ echo " # Bash: manual setup ( ~/.bashrc) "
51101 echo " source <(git gtr completion bash)"
52102 echo " "
53- echo " # Zsh: add to ~/.zshrc ( BEFORE any existing compinit call)"
103+ echo " # Zsh: manual setup ( ~/.zshrc, BEFORE any existing compinit call)"
54104 echo " eval \"\$ (git gtr completion zsh)\" "
55105 echo " "
56- echo " # Fish: save to completions directory"
106+ echo " # Fish: manual setup"
107+ echo " mkdir -p ~/.config/fish/completions"
57108 echo " git gtr completion fish > ~/.config/fish/completions/git-gtr.fish"
58109 return 0
59110 ;;
64115 return 1
65116 ;;
66117 esac
67- }
118+ }
0 commit comments