blob: 09291d5c460bac2ebb33cba3b25aa7cf785e8c3e [file] [log] [blame]
Mike Frysinger8da7b6f2021-03-05 18:29:09 -05001# Copyright 2021 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Programmable bash completion. https://github.com/scop/bash-completion
16
Mike Frysingercfa00d62021-07-26 15:30:10 -040017# TODO: Handle interspersed options. We handle `repo h<tab>`, but not
18# `repo --time h<tab>`.
19
Mike Frysinger8da7b6f2021-03-05 18:29:09 -050020# Complete the list of repo subcommands.
21__complete_repo_list_commands() {
22 local repo=${COMP_WORDS[0]}
23 (
24 # Handle completions if running outside of a checkout.
25 if ! "${repo}" help --all 2>/dev/null; then
26 repo help 2>/dev/null
27 fi
28 ) | sed -n '/^ /{s/ \([^ ]\+\) .\+/\1/;p}'
29}
30
31# Complete list of all branches available in all projects in the repo client
32# checkout.
33__complete_repo_list_branches() {
34 local repo=${COMP_WORDS[0]}
35 "${repo}" branches 2>/dev/null | \
36 sed -n '/|/{s/[ *][Pp ] *\([^ ]\+\) .*/\1/;p}'
37}
38
39# Complete list of all projects available in the repo client checkout.
40__complete_repo_list_projects() {
41 local repo=${COMP_WORDS[0]}
42 "${repo}" list -n 2>/dev/null
Mike Frysinger54671852021-07-26 15:28:01 -040043 "${repo}" list -p --relative-to=. 2>/dev/null
Mike Frysinger8da7b6f2021-03-05 18:29:09 -050044}
45
46# Complete the repo <command> argument.
47__complete_repo_command() {
48 if [[ ${COMP_CWORD} -ne 1 ]]; then
49 return 1
50 fi
51
52 local command=${COMP_WORDS[1]}
53 COMPREPLY=($(compgen -W "$(__complete_repo_list_commands)" -- "${command}"))
54 return 0
55}
56
57# Complete repo subcommands that take <branch> <projects>.
58__complete_repo_command_branch_projects() {
59 local current=$1
60 if [[ ${COMP_CWORD} -eq 2 ]]; then
61 COMPREPLY=($(compgen -W "$(__complete_repo_list_branches)" -- "${current}"))
62 else
63 COMPREPLY=($(compgen -W "$(__complete_repo_list_projects)" -- "${current}"))
64 fi
65}
66
67# Complete repo subcommands that take only <projects>.
68__complete_repo_command_projects() {
69 local current=$1
70 COMPREPLY=($(compgen -W "$(__complete_repo_list_projects)" -- "${current}"))
71}
72
Mike Frysingerb3803222021-07-26 15:26:22 -040073# Complete `repo help`.
74__complete_repo_command_help() {
75 local current=$1
76 # CWORD=1 is "start".
77 # CWORD=2 is the <subcommand> which we complete here.
78 if [[ ${COMP_CWORD} -eq 2 ]]; then
79 COMPREPLY=(
80 $(compgen -W "$(__complete_repo_list_commands)" -- "${current}")
81 )
82 fi
83}
84
Mike Frysingercfa00d62021-07-26 15:30:10 -040085# Complete `repo forall`.
86__complete_repo_command_forall() {
87 local current=$1
88 # CWORD=1 is "forall".
89 # CWORD=2+ are <projects> *until* we hit the -c option.
90 local i
91 for (( i = 0; i < COMP_CWORD; ++i )); do
92 if [[ "${COMP_WORDS[i]}" == "-c" ]]; then
93 return 0
94 fi
95 done
96
97 COMPREPLY=(
98 $(compgen -W "$(__complete_repo_list_projects)" -- "${current}")
99 )
100}
101
Mike Frysingerb3803222021-07-26 15:26:22 -0400102# Complete `repo start`.
103__complete_repo_command_start() {
104 local current=$1
105 # CWORD=1 is "start".
106 # CWORD=2 is the <branch> which we don't complete.
107 # CWORD=3+ are <projects> which we complete here.
108 if [[ ${COMP_CWORD} -gt 2 ]]; then
109 COMPREPLY=(
110 $(compgen -W "$(__complete_repo_list_projects)" -- "${current}")
111 )
112 fi
113}
114
Mike Frysinger8da7b6f2021-03-05 18:29:09 -0500115# Complete the repo subcommand arguments.
116__complete_repo_arg() {
117 if [[ ${COMP_CWORD} -le 1 ]]; then
118 return 1
119 fi
120
121 local command=${COMP_WORDS[1]}
122 local current=${COMP_WORDS[COMP_CWORD]}
123 case ${command} in
124 abandon|checkout)
125 __complete_repo_command_branch_projects "${current}"
126 return 0
127 ;;
128
129 branch|branches|diff|info|list|overview|prune|rebase|smartsync|stage|status|\
130 sync|upload)
131 __complete_repo_command_projects "${current}"
132 return 0
133 ;;
134
Mike Frysingercfa00d62021-07-26 15:30:10 -0400135 help|start|forall)
Mike Frysingerb3803222021-07-26 15:26:22 -0400136 __complete_repo_command_${command} "${current}"
Mike Frysinger8da7b6f2021-03-05 18:29:09 -0500137 return 0
138 ;;
139
140 *)
141 return 1
142 ;;
143 esac
144}
145
146# Complete the repo arguments.
147__complete_repo() {
148 COMPREPLY=()
149 __complete_repo_command && return 0
150 __complete_repo_arg && return 0
151 return 0
152}
153
Mike Frysinger13d6c942021-07-26 13:57:31 -0400154# Fallback to the default complete methods if we aren't able to provide anything
155# useful. This will allow e.g. local paths to be used when it makes sense.
156complete -F __complete_repo -o bashdefault -o default repo