blob: 0b52d29c63e7a7e5ff164869cde366814db5638e [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
17# Complete the list of repo subcommands.
18__complete_repo_list_commands() {
19 local repo=${COMP_WORDS[0]}
20 (
21 # Handle completions if running outside of a checkout.
22 if ! "${repo}" help --all 2>/dev/null; then
23 repo help 2>/dev/null
24 fi
25 ) | sed -n '/^ /{s/ \([^ ]\+\) .\+/\1/;p}'
26}
27
28# Complete list of all branches available in all projects in the repo client
29# checkout.
30__complete_repo_list_branches() {
31 local repo=${COMP_WORDS[0]}
32 "${repo}" branches 2>/dev/null | \
33 sed -n '/|/{s/[ *][Pp ] *\([^ ]\+\) .*/\1/;p}'
34}
35
36# Complete list of all projects available in the repo client checkout.
37__complete_repo_list_projects() {
38 local repo=${COMP_WORDS[0]}
39 "${repo}" list -n 2>/dev/null
40}
41
42# Complete the repo <command> argument.
43__complete_repo_command() {
44 if [[ ${COMP_CWORD} -ne 1 ]]; then
45 return 1
46 fi
47
48 local command=${COMP_WORDS[1]}
49 COMPREPLY=($(compgen -W "$(__complete_repo_list_commands)" -- "${command}"))
50 return 0
51}
52
53# Complete repo subcommands that take <branch> <projects>.
54__complete_repo_command_branch_projects() {
55 local current=$1
56 if [[ ${COMP_CWORD} -eq 2 ]]; then
57 COMPREPLY=($(compgen -W "$(__complete_repo_list_branches)" -- "${current}"))
58 else
59 COMPREPLY=($(compgen -W "$(__complete_repo_list_projects)" -- "${current}"))
60 fi
61}
62
63# Complete repo subcommands that take only <projects>.
64__complete_repo_command_projects() {
65 local current=$1
66 COMPREPLY=($(compgen -W "$(__complete_repo_list_projects)" -- "${current}"))
67}
68
69# Complete the repo subcommand arguments.
70__complete_repo_arg() {
71 if [[ ${COMP_CWORD} -le 1 ]]; then
72 return 1
73 fi
74
75 local command=${COMP_WORDS[1]}
76 local current=${COMP_WORDS[COMP_CWORD]}
77 case ${command} in
78 abandon|checkout)
79 __complete_repo_command_branch_projects "${current}"
80 return 0
81 ;;
82
83 branch|branches|diff|info|list|overview|prune|rebase|smartsync|stage|status|\
84 sync|upload)
85 __complete_repo_command_projects "${current}"
86 return 0
87 ;;
88
89 help)
90 if [[ ${COMP_CWORD} -eq 2 ]]; then
91 COMPREPLY=(
92 $(compgen -W "$(__complete_repo_list_commands)" -- "${current}")
93 )
94 fi
95 return 0
96 ;;
97
98 start)
99 if [[ ${COMP_CWORD} -gt 2 ]]; then
100 COMPREPLY=(
101 $(compgen -W "$(__complete_repo_list_projects)" -- "${current}")
102 )
103 fi
104 return 0
105 ;;
106
107 *)
108 return 1
109 ;;
110 esac
111}
112
113# Complete the repo arguments.
114__complete_repo() {
115 COMPREPLY=()
116 __complete_repo_command && return 0
117 __complete_repo_arg && return 0
118 return 0
119}
120
121complete -F __complete_repo repo