git_cl_completion.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (c) 2016 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. # This adds completion to bash shells for git cl commands. It is
  5. # meant for developers and not needed for inclusion by any automated
  6. # processes that will, of course, specify the full command, not rely
  7. # on or benefit from tab-completion.
  8. #
  9. # Requires:
  10. # Installed git bash completion.
  11. #
  12. # Usage:
  13. # Put something like the following in your .bashrc:
  14. # . $PATH_TO_DEPOT_TOOLS/git cl_completion.sh
  15. #
  16. # Parses commands from git cl -h.
  17. __git_cl_commands () {
  18. git cl -h 2> /dev/null | sed -n 's/^\s*\x1b\[32m\(\S\+\)\s*\x1b\[39m.*$/\1/p'
  19. }
  20. # Caches variables in __git_cl_all_commands.
  21. __git_cl_compute_all_commands () {
  22. test -n "$__git_cl_all_commands" ||
  23. __git_cl_all_commands="$(__git_cl_commands)"
  24. }
  25. _git_cl () {
  26. __git_cl_compute_all_commands
  27. local subcommands=$(echo "$__git_cl_all_commands" | xargs)
  28. local subcommand=$(__git_find_on_cmdline "$subcommands")
  29. if [[ -z "$subcommand" ]]; then
  30. __gitcomp "$subcommands"
  31. return
  32. fi
  33. case "$subcommand,$cur" in
  34. upload,--*)
  35. __gitcomp_builtin cl_upload
  36. ;;
  37. "",*)
  38. __gitcomp_nl "${__git_cl_all_commands}"
  39. ;;
  40. esac
  41. }