From 5ad2cb5d0365aafb92270081ae02861378942d53 Mon Sep 17 00:00:00 2001 From: Darin Petkov Date: Wed, 18 Aug 2010 14:01:56 -0700 Subject: [PATCH] Add repo bash_completion under chroot. BUG=5807 TEST=enter_chroot, tried repo bash completion Change-Id: Ic90c7bd7748fb8c79b212f465326867bbdca9c80 Review URL: http://codereview.chromium.org/3110024 --- bash_completion | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/bash_completion b/bash_completion index d33a3c67b7..6a3077c64a 100644 --- a/bash_completion +++ b/bash_completion @@ -125,7 +125,7 @@ _autotest_complete() { _complete_board_sysroot_flag && return 0 } -# Complete the cros_workon argument. +# Complete cros_workon's argument. # # TODO(petkov): We should probably extract the list of commands from # cros_workon --help, just like we do for flags (see _flag_complete). @@ -178,7 +178,7 @@ _complete_cros_workon_package() { return 1 } -# Complete the cros_workon arguments. +# Complete cros_workon arguments. _cros_workon() { COMPREPLY=() _flag_complete && return 0 @@ -188,6 +188,65 @@ _cros_workon() { return 0 } +_list_repo_commands() { + local repo=${COMP_WORDS[0]} + "$repo" help --all | grep -E '^ ' | sed 's/ \([^ ]\+\) .\+/\1/' +} + +_list_repo_branches() { + local repo=${COMP_WORDS[0]} + "$repo" branches 2>&1 | grep \| | sed 's/[ *][Pp ] *\([^ ]\+\) .*/\1/' +} + +_list_repo_projects() { + local repo=${COMP_WORDS[0]} + local manifest=$(mktemp) + "$repo" manifest -o "$manifest" >& /dev/null + grep 'project name=' "$manifest" | sed 's/.\+name="\([^"]\+\)".\+/\1/' + rm -f "$manifest" >& /dev/null +} + +# Complete repo's argument. +_complete_repo_command() { + [ ${COMP_CWORD} -eq 1 ] || return 1 + local command=${COMP_WORDS[1]} + COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "$command")) + return 0 +} + +_complete_repo_arg() { + [ ${COMP_CWORD} -gt 1 ] || return 1 + local command=${COMP_WORDS[1]} + local current=${COMP_WORDS[COMP_CWORD]} + if [[ ${command} == "abandon" ]]; then + if [[ ${COMP_CWORD} -eq 2 ]]; then + COMPREPLY=($(compgen -W "$(_list_repo_branches)" -- "$current")) + else + COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "$current")) + fi + return 0 + fi + if [[ ${command} == "help" ]]; then + [ ${COMP_CWORD} -eq 2 ] && \ + COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "$current")) + return 0 + fi + if [[ ${command} == "start" ]]; then + [ ${COMP_CWORD} -gt 2 ] && \ + COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "$current")) + return 0 + fi + return 1 +} + +# Complete repo arguments. +_complete_repo() { + COMPREPLY=() + _complete_repo_command && return 0 + _complete_repo_arg && return 0 + return 0 +} + complete -o bashdefault -o default -F _board_sysroot \ build_autotest.sh \ build_image \ @@ -197,6 +256,7 @@ complete -o bashdefault -o default -F _board_sysroot \ complete -o bashdefault -o default -F _board_overlay setup_board complete -o bashdefault -o default -o nospace -F _autotest_complete autotest complete -F _cros_workon cros_workon +complete -F _complete_repo repo ### Local Variables: ### mode: shell-script