Commit | Line | Data |
---|---|---|
8ebae56b KH |
1 | # bash completion support for StGIT -*- shell-script -*- |
2 | # | |
3 | # Copyright (C) 2006, Karl Hasselström <kha@treskal.com> | |
4 | # Based on git-completion.sh | |
5 | # | |
6 | # To use these routines: | |
7 | # | |
8 | # 1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash). | |
9 | # | |
10 | # 2. Add the following line to your .bashrc: | |
11 | # . ~/.stgit-completion.bash | |
12 | ||
13 | # The path to .git, or empty if we're not in a repository. | |
14 | _gitdir () | |
15 | { | |
16 | echo "$(git rev-parse --git-dir 2>/dev/null)" | |
17 | } | |
18 | ||
19 | # Name of the current branch, or empty if there isn't one. | |
20 | _current_branch () | |
21 | { | |
22 | local b=$(git symbolic-ref HEAD 2>/dev/null) | |
23 | echo ${b#refs/heads/} | |
24 | } | |
25 | ||
26 | # List of all applied patches. | |
27 | _applied_patches () | |
28 | { | |
29 | local g=$(_gitdir) | |
30 | [ "$g" ] && cat "$g/patches/$(_current_branch)/applied" | |
31 | } | |
32 | ||
33 | # List of all unapplied patches. | |
34 | _unapplied_patches () | |
35 | { | |
36 | local g=$(_gitdir) | |
37 | [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied" | |
38 | } | |
39 | ||
40 | # List of all patches. | |
41 | _all_patches () | |
42 | { | |
43 | local b=$(_current_branch) | |
44 | local g=$(_gitdir) | |
45 | [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" | |
46 | } | |
47 | ||
48 | # List of all patches except the current patch. | |
49 | _all_other_patches () | |
50 | { | |
51 | local b=$(_current_branch) | |
52 | local g=$(_gitdir) | |
53 | [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \ | |
54 | | grep -v "^$(< $g/patches/$b/current)$" | |
55 | } | |
56 | ||
57 | # Generate completions for patches and patch ranges from the given | |
58 | # patch list function, and options from the given list. | |
59 | _complete_patch_range () | |
60 | { | |
61 | local patchlist="$1" options="$2" | |
62 | local pfx cur="${COMP_WORDS[COMP_CWORD]}" | |
63 | case "$cur" in | |
64 | *..*) | |
65 | pfx="${cur%..*}.." | |
66 | cur="${cur#*..}" | |
67 | COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur")) | |
68 | ;; | |
69 | *) | |
70 | COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur")) | |
71 | ;; | |
72 | esac | |
73 | } | |
74 | ||
75 | # Generate completions for options from the given list. | |
76 | _complete_options () | |
77 | { | |
78 | local options="$1" | |
79 | COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}")) | |
80 | } | |
81 | ||
82 | _stg_delete () | |
83 | { | |
84 | _complete_patch_range _all_patches "--branch --help" | |
85 | } | |
86 | ||
87 | _stg_goto () | |
88 | { | |
89 | _complete_patch_range _all_other_patches "--help" | |
90 | } | |
91 | ||
92 | _stg_mail () | |
93 | { | |
94 | _complete_patch_range _all_patches \ | |
95 | "--all --to --cc --bcc --auto --noreply --version --prefix --template \ | |
96 | --cover --edit-cover --edit-patches --sleep --refid --smtp-user \ | |
97 | --smtp-password --branch --mbox --help" | |
98 | } | |
99 | ||
100 | _stg_new () | |
101 | { | |
102 | _complete_options "--message --showpatch --author --authname --authemail \ | |
103 | --authdate --commname --commemail --help" | |
104 | } | |
105 | ||
106 | _stg_pop () | |
107 | { | |
108 | _complete_patch_range _applied_patches "--all --number --keep --help" | |
109 | } | |
110 | ||
111 | _stg_push () | |
112 | { | |
113 | _complete_patch_range _unapplied_patches "--all --number --reverse \ | |
114 | --merged --undo --help" | |
115 | } | |
116 | ||
117 | _stg_status () | |
118 | { | |
119 | _complete_options "--modified --new --deleted --conflict --unknown \ | |
120 | --noexclude --reset --help" | |
121 | } | |
122 | ||
123 | _stg () | |
124 | { | |
125 | local i c=1 command | |
126 | ||
127 | while [ $c -lt $COMP_CWORD ]; do | |
128 | if [ $c == 1 ]; then | |
129 | command="${COMP_WORDS[c]}" | |
130 | fi | |
131 | c=$((++c)) | |
132 | done | |
133 | ||
134 | # Complete name of subcommand. | |
135 | if [ $c -eq $COMP_CWORD -a -z "$command" ]; then | |
136 | COMPREPLY=($(compgen \ | |
137 | -W "--help --version \ | |
138 | $(stg help|grep '^ '|sed 's/ *\([^ ]\) .*/\1/')" \ | |
139 | -- "${COMP_WORDS[COMP_CWORD]}")) | |
140 | return; | |
141 | fi | |
142 | ||
143 | # Complete arguments to subcommands. | |
144 | case "$command" in | |
145 | delete) _stg_delete ;; | |
146 | goto) _stg_goto ;; | |
147 | mail) _stg_mail ;; | |
148 | new) _stg_new ;; | |
149 | pop) _stg_pop ;; | |
150 | push) _stg_push ;; | |
151 | status) _stg_status ;; | |
152 | *) COMPREPLY=() ;; | |
153 | esac | |
154 | } | |
155 | ||
156 | complete -o default -F _stg stg |