blob: 053c2b7d0c220301f9f36f2c7bc4788d748b915f [file] [log] [blame]
[email protected]2dca41c2014-01-16 05:16:401# gclient_completion.sh
2#
3# This adds completion to bash shells for gclient commands. It is
4# meant for developers and not needed for inclusion by any automated
5# processes that will, of course, specify the full command, not rely
6# on or benefit from tab-completion.
7#
8# Requires:
9# bash-completion package for _get_comp_words_by_ref.
10# newer versions of sed for the improved regular expression handling.
11#
12# On Mac, this is accomplished by installing fink (www.finkproject.org)
13# then doing sudo apt-get update ; sudo apt-get install sed
14#
15# Usage:
16# Put something like the following in your .bashrc:
17# . $PATH_TO_DEPOT_TOOLS/gclient_completion.sh
18#
19
20
21# Parses commands from gclient -h.
22__gclient_commands () {
23 gclient -h 2> /dev/null | sed -n 's/^\s*\x1b\[32m\(.*\)\x1b\[39m.*$/\1/p'
24}
25
26# Caches variables in __gclient_all_commands.
27# Adds "update" command, which is not listed.
28__gclient_compute_all_commands () {
29 test -n "$__gclient_all_commands" ||
30 __gclient_all_commands="$(__gclient_commands) update"
31}
32
33# Since gclient fetch is a passthrough to git, let the completions
34# come from git's completion if it's defined.
35if [[ -n _git_fetch ]]; then
36 _gclient_fetch=_git_fetch
37fi
38
39# Completion callback for gclient cmdlines.
40_gclient () {
41 local cur prev words cword
42 _get_comp_words_by_ref -n =: cur prev words cword
43
44 # Find the command by ignoring flags.
45 local i c=1 cword_adjust=0 command
46 while [ $c -lt $cword ]; do
47 i="${words[$c]}"
48 case "$i" in
49 -*)
50 ((cword_adjust++))
51 : ignore options ;;
52 *) command="$i"; break ;;
53 esac
54 ((c++))
55 done
56
57 # If there is a completion function for the command, use it and
58 # return.
59 local completion_func="_gclient_${command//-/_}"
60 local -f $completion_func >/dev/null && $completion_func && return
61
62 # If the command or hasn't been given, provide completions for all
63 # commands. Also provide all commands as completion for the help
64 # command.
65 # echo "command=$command" >> /tmp/comp.log
66 case "$command" in
67 ""|help)
68 if [[ "$command" != help || $((cword - cword_adjust)) -le 2 ]]; then
69 __gclient_compute_all_commands
70 COMPREPLY=($(compgen -W "$__gclient_all_commands" $cur))
71 fi
72 ;;
73 *) : just use the default ;;
74 esac
75} &&
76complete -F _gclient -o default gclient