Permalink
Browse files

add bash completion script for repo

  • Loading branch information...
1 parent aba306f commit 5395872dae55663f65f45a34ea8f1560d1e95030 @chirayudesai chirayudesai committed with aaronpoweruser Mar 25, 2013
Showing with 126 additions and 0 deletions.
  1. +27 −0 bash_completion/LICENSE
  2. +99 −0 bash_completion/repo.bash
View
@@ -0,0 +1,27 @@
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
@@ -0,0 +1,99 @@
+# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# Programmable completion for some Chromium OS build scripts.
+
+_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 the repo <command> 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 the repo arguments.
+_complete_repo() {
+ COMPREPLY=()
+ _complete_repo_command && return 0
+ _complete_repo_arg && return 0
+ return 0
+}
+
+complete -F _complete_repo repo
+
+# Add a way to get the "m" branch from repo easily; used by __git_branch_ps1()
+#
+# Repo seems to maintain a phony 'm/' remote and it always seems to be the name
+# of the manifest branch. This will retrieve it.
+__git_m_branch() {
+ local git_dir=$(git rev-parse --git-dir 2> /dev/null)
+ if [ -n "${git_dir}" ]; then
+ echo $(cd ${git_dir}/refs/remotes/m 2> /dev/null && ls)
+ fi
+}
+
+# A "subclass" of __git_ps1 that adds the manifest branch name into the prompt.
+# ...if you're on manifest branch "0.11.257.B" and local branch "lo" and
+# pass " (%s)", we'll output " (0.11.257.B/lo)". Note that we'll never show
+# the manifest branch 'master', since it's so common.
+__git_branch_ps1() {
+ local format_str="${1:- (%s)}"
+ local m_branch=$(__git_m_branch)
+ if [ "${m_branch}" != "master" -a -n "${m_branch}" ]; then
+ format_str=$(printf "${format_str}" "${m_branch}/%s")
+ fi
+ # for subshells, prefix the prompt with the shell nesting level
+ local lshlvl=""
+ [ ! -z "${SHLVL##*[!0-9]*}" ] && [ ${SHLVL} -gt 1 ] && lshlvl="${SHLVL} "
+ __git_ps1 "${lshlvl}${format_str}"
+}
+
+# Prompt functions should not error when in subshells
+export -f __gitdir
+export -f __git_ps1
+export -f __git_m_branch
+export -f __git_branch_ps1

0 comments on commit 5395872

Please sign in to comment.