Chirayu Desai | a830556 | 2013-03-16 20:01:16 +0530 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
Chirayu Desai | b8e7c15 | 2013-03-25 14:13:14 +0530 | [diff] [blame] | 3 | # found in the repo-LICENSE file. |
Chirayu Desai | a830556 | 2013-03-16 20:01:16 +0530 | [diff] [blame] | 4 | |
| 5 | # Programmable completion for some Chromium OS build scripts. |
| 6 | |
| 7 | _list_repo_commands() { |
| 8 | local repo=${COMP_WORDS[0]} |
| 9 | "${repo}" help --all | grep -E '^ ' | sed 's/ \([^ ]\+\) .\+/\1/' |
| 10 | } |
| 11 | |
| 12 | _list_repo_branches() { |
| 13 | local repo=${COMP_WORDS[0]} |
| 14 | "${repo}" branches 2>&1 | grep \| | sed 's/[ *][Pp ] *\([^ ]\+\) .*/\1/' |
| 15 | } |
| 16 | |
| 17 | _list_repo_projects() { |
| 18 | local repo=${COMP_WORDS[0]} |
| 19 | local manifest=$(mktemp) |
| 20 | "${repo}" manifest -o "${manifest}" >& /dev/null |
| 21 | grep 'project name=' "${manifest}" | sed 's/.\+name="\([^"]\+\)".\+/\1/' |
| 22 | rm -f "${manifest}" >& /dev/null |
| 23 | } |
| 24 | |
| 25 | # Complete the repo <command> argument. |
| 26 | _complete_repo_command() { |
| 27 | [ ${COMP_CWORD} -eq 1 ] || return 1 |
| 28 | local command=${COMP_WORDS[1]} |
| 29 | COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "${command}")) |
| 30 | return 0 |
| 31 | } |
| 32 | |
| 33 | _complete_repo_arg() { |
| 34 | [ ${COMP_CWORD} -gt 1 ] || return 1 |
| 35 | local command=${COMP_WORDS[1]} |
| 36 | local current=${COMP_WORDS[COMP_CWORD]} |
| 37 | if [[ ${command} == "abandon" ]]; then |
| 38 | if [[ ${COMP_CWORD} -eq 2 ]]; then |
| 39 | COMPREPLY=($(compgen -W "$(_list_repo_branches)" -- "${current}")) |
| 40 | else |
| 41 | COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "${current}")) |
| 42 | fi |
| 43 | return 0 |
| 44 | fi |
| 45 | if [[ ${command} == "help" ]]; then |
| 46 | [ ${COMP_CWORD} -eq 2 ] && \ |
| 47 | COMPREPLY=($(compgen -W "$(_list_repo_commands)" -- "${current}")) |
| 48 | return 0 |
| 49 | fi |
| 50 | if [[ ${command} == "start" ]]; then |
| 51 | [ ${COMP_CWORD} -gt 2 ] && \ |
| 52 | COMPREPLY=($(compgen -W "$(_list_repo_projects)" -- "${current}")) |
| 53 | return 0 |
| 54 | fi |
| 55 | return 1 |
| 56 | } |
| 57 | |
| 58 | # Complete the repo arguments. |
| 59 | _complete_repo() { |
| 60 | COMPREPLY=() |
| 61 | _complete_repo_command && return 0 |
| 62 | _complete_repo_arg && return 0 |
| 63 | return 0 |
| 64 | } |
| 65 | |
| 66 | complete -F _complete_repo repo |
| 67 | |
| 68 | # Add a way to get the "m" branch from repo easily; used by __git_branch_ps1() |
| 69 | # |
| 70 | # Repo seems to maintain a phony 'm/' remote and it always seems to be the name |
| 71 | # of the manifest branch. This will retrieve it. |
| 72 | __git_m_branch() { |
| 73 | local git_dir=$(git rev-parse --git-dir 2> /dev/null) |
| 74 | if [ -n "${git_dir}" ]; then |
| 75 | echo $(cd ${git_dir}/refs/remotes/m 2> /dev/null && ls) |
| 76 | fi |
| 77 | } |
| 78 | |
| 79 | # A "subclass" of __git_ps1 that adds the manifest branch name into the prompt. |
| 80 | # ...if you're on manifest branch "0.11.257.B" and local branch "lo" and |
| 81 | # pass " (%s)", we'll output " (0.11.257.B/lo)". Note that we'll never show |
| 82 | # the manifest branch 'master', since it's so common. |
| 83 | __git_branch_ps1() { |
| 84 | local format_str="${1:- (%s)}" |
| 85 | local m_branch=$(__git_m_branch) |
| 86 | if [ "${m_branch}" != "master" -a -n "${m_branch}" ]; then |
| 87 | format_str=$(printf "${format_str}" "${m_branch}/%s") |
| 88 | fi |
| 89 | # for subshells, prefix the prompt with the shell nesting level |
| 90 | local lshlvl="" |
| 91 | [ ! -z "${SHLVL##*[!0-9]*}" ] && [ ${SHLVL} -gt 1 ] && lshlvl="${SHLVL} " |
| 92 | __git_ps1 "${lshlvl}${format_str}" |
| 93 | } |
| 94 | |
| 95 | # Prompt functions should not error when in subshells |
| 96 | export -f __gitdir |
| 97 | export -f __git_ps1 |
| 98 | export -f __git_m_branch |
| 99 | export -f __git_branch_ps1 |