Mike Frysinger | 8da7b6f | 2021-03-05 18:29:09 -0500 | [diff] [blame] | 1 | # 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 Frysinger | cfa00d6 | 2021-07-26 15:30:10 -0400 | [diff] [blame] | 17 | # TODO: Handle interspersed options. We handle `repo h<tab>`, but not |
| 18 | # `repo --time h<tab>`. |
| 19 | |
Mike Frysinger | 8da7b6f | 2021-03-05 18:29:09 -0500 | [diff] [blame] | 20 | # 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 Frysinger | 5467185 | 2021-07-26 15:28:01 -0400 | [diff] [blame] | 43 | "${repo}" list -p --relative-to=. 2>/dev/null |
Mike Frysinger | 8da7b6f | 2021-03-05 18:29:09 -0500 | [diff] [blame] | 44 | } |
| 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 Frysinger | b380322 | 2021-07-26 15:26:22 -0400 | [diff] [blame] | 73 | # 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 Frysinger | cfa00d6 | 2021-07-26 15:30:10 -0400 | [diff] [blame] | 85 | # 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 Frysinger | b380322 | 2021-07-26 15:26:22 -0400 | [diff] [blame] | 102 | # 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 Frysinger | 8da7b6f | 2021-03-05 18:29:09 -0500 | [diff] [blame] | 115 | # 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 Frysinger | cfa00d6 | 2021-07-26 15:30:10 -0400 | [diff] [blame] | 135 | help|start|forall) |
Mike Frysinger | b380322 | 2021-07-26 15:26:22 -0400 | [diff] [blame] | 136 | __complete_repo_command_${command} "${current}" |
Mike Frysinger | 8da7b6f | 2021-03-05 18:29:09 -0500 | [diff] [blame] | 137 | 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 Frysinger | 13d6c94 | 2021-07-26 13:57:31 -0400 | [diff] [blame] | 154 | # 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. |
| 156 | complete -F __complete_repo -o bashdefault -o default repo |