-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathcompletion.sh
More file actions
118 lines (105 loc) · 3.36 KB
/
completion.sh
File metadata and controls
118 lines (105 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
# Resolve a completion asset from supported install layouts.
_completion_asset_path() {
local shell="$1"
shift
local candidate
for candidate in "$@"; do
if [ -f "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
log_error "Could not find $shell completion asset under: $GTR_DIR"
log_error "Expected either a source checkout (completions/) or a Homebrew install layout."
return 1
}
_completion_bash_path() {
_completion_asset_path "bash" \
"$GTR_DIR/completions/gtr.bash" \
"$GTR_DIR/etc/bash_completion.d/git-gtr"
}
_completion_zsh_path() {
_completion_asset_path "zsh" \
"$GTR_DIR/completions/_git-gtr" \
"$GTR_DIR/share/zsh/site-functions/_git-gtr"
}
_completion_fish_path() {
_completion_asset_path "fish" \
"$GTR_DIR/completions/git-gtr.fish" \
"$GTR_DIR/share/fish/vendor_completions.d/git-gtr.fish"
}
_completion_single_quote() {
printf "'%s'" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"
}
# Completion command (generate shell completions)
cmd_completion() {
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
show_command_help
fi
local shell="${1:-}"
case "$shell" in
bash)
local bash_path
bash_path="$(_completion_bash_path)" || return 1
cat "$bash_path"
;;
zsh)
local zsh_path zsh_dir zsh_dir_quoted
zsh_path="$(_completion_zsh_path)" || return 1
zsh_dir="$(dirname "$zsh_path")"
zsh_dir_quoted="$(_completion_single_quote "$zsh_dir")"
# Output zstyle registration + completion loading
# The zstyle MUST run before compinit to register gtr as a git subcommand
cat <<EOF
# git-gtr zsh completion
# Generated by: git gtr completion zsh
# Add to ~/.zshrc (before compinit):
# eval "\$(git gtr completion zsh)"
# Register gtr as a git subcommand (MUST be before compinit)
zstyle ':completion:*:*:git:*' user-commands gtr:'Git worktree management'
# Add completions to fpath and initialize
fpath=($zsh_dir_quoted \$fpath)
# Note: If you already have compinit in your .zshrc, you may want to
# source this file before your existing compinit call and remove the
# autoload line below.
autoload -Uz compinit && compinit -C
EOF
;;
fish)
local fish_path
fish_path="$(_completion_fish_path)" || return 1
cat "$fish_path"
;;
""|--help|-h)
echo "Generate shell completions for git gtr"
echo ""
echo "Usage: git gtr completion <shell>"
echo ""
echo "Shells:"
echo " bash Generate Bash completions"
echo " zsh Generate Zsh completions"
echo " fish Generate Fish completions"
echo ""
echo "Homebrew installs native shell completions automatically."
echo ""
echo "Examples:"
echo " # Bash: manual setup (~/.bashrc)"
echo " source <(git gtr completion bash)"
echo ""
echo " # Zsh: manual setup (~/.zshrc, BEFORE any existing compinit call)"
echo " eval \"\$(git gtr completion zsh)\""
echo ""
echo " # Fish: manual setup"
echo " mkdir -p ~/.config/fish/completions"
echo " git gtr completion fish > ~/.config/fish/completions/git-gtr.fish"
return 0
;;
*)
log_error "Unknown shell: $shell"
log_error "Supported shells: bash, zsh, fish"
log_info "Run 'git gtr completion --help' for usage"
return 1
;;
esac
}