blob: e556d33a52c11232e9c0b732c9e1d59dd274548c [file] [log] [blame]
Chirayu Desaia8305562013-03-16 20:01:16 +05301# 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 Desaib8e7c152013-03-25 14:13:14 +05303# found in the repo-LICENSE file.
Chirayu Desaia8305562013-03-16 20:01:16 +05304
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
66complete -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
96export -f __gitdir
97export -f __git_ps1
98export -f __git_m_branch
99export -f __git_branch_ps1