diff --git a/profile.d/bash-completion/git-completion.sh b/profile.d/bash-completion/git-completion.sh new file mode 100644 index 0000000..df8ce27 --- /dev/null +++ b/profile.d/bash-completion/git-completion.sh @@ -0,0 +1,188 @@ +#!/usr/bin/env bash +# ------------------------------------------------------------------------------ +# Git helper completions for profile.d/git.sh shortcuts. +# ------------------------------------------------------------------------------ + +# Return 0 when current directory is inside a git work tree. +_profile_git_in_repo() +{ + git rev-parse --is-inside-work-tree >/dev/null 2>&1 +} + +# Load git completion helpers on demand if they are available on the system. +_profile_git_load_completion_helpers() +{ + declare -F __git_complete >/dev/null 2>&1 && return 0 + + local completion_file + for completion_file in \ + /usr/share/bash-completion/completions/git \ + /usr/share/git/completion/git-completion.bash \ + /etc/bash_completion.d/git + do + if [[ -r "$completion_file" ]]; then + # shellcheck source=/dev/null + . "$completion_file" + break + fi + done + + declare -F __git_complete >/dev/null 2>&1 +} + +_profile_git_complete_remotes() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + if ! _profile_git_in_repo; then + COMPREPLY=() + return 0 + fi + + COMPREPLY=( $(compgen -W "$(git remote 2>/dev/null)" -- "$cur") ) +} + +_profile_git_complete_refs() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + if ! _profile_git_in_repo; then + COMPREPLY=() + return 0 + fi + + COMPREPLY=( $(compgen -W "$(git for-each-ref --format='%(refname:short)' refs/heads refs/remotes refs/tags 2>/dev/null)" -- "$cur") ) +} + +_complete_gst() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + case "$cur" in + -*) + COMPREPLY=( $(compgen -W "-h --help" -- "$cur") ) + ;; + *) + COMPREPLY=( $(compgen -d -- "$cur") ) + ;; + esac +} + +_complete_ggraph() +{ + local cur prev + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + case "$prev" in + -n|--limit) + COMPREPLY=() + return 0 + ;; + esac + + COMPREPLY=( $(compgen -W "-h --help -n --limit" -- "$cur") ) +} + +_complete_gsync() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + if [[ $cur == -* ]]; then + COMPREPLY=( $(compgen -W "-h --help" -- "$cur") ) + return 0 + fi + + _profile_git_complete_remotes +} + +_complete_gacp() +{ + local cur prev + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + case "$prev" in + -m|--message) + COMPREPLY=() + return 0 + ;; + esac + + if [[ $cur == -* ]]; then + COMPREPLY=( $(compgen -W "-h --help -a --auto -m --message" -- "$cur") ) + return 0 + fi + + COMPREPLY=( $(compgen -f -- "$cur") ) +} + +_complete_greset() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + if [[ $cur == -* ]]; then + COMPREPLY=( $(compgen -W "-h --help -x --with-ignored" -- "$cur") ) + return 0 + fi + + _profile_git_complete_refs +} + +_complete_gwip() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + if [[ $cur == -* ]]; then + COMPREPLY=( $(compgen -W "-h --help" -- "$cur") ) + else + COMPREPLY=() + fi +} + +_complete_gprune() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + if [[ $cur == -* ]]; then + COMPREPLY=( $(compgen -W "-h --help" -- "$cur") ) + return 0 + fi + + _profile_git_complete_refs +} + +_complete_groot() +{ + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + COMPREPLY=( $(compgen -W "-h --help -g --go" -- "$cur") ) +} + +_profile_git_register_completions() +{ + complete -F _complete_gst gst + complete -F _complete_ggraph ggraph + complete -F _complete_gsync gsync + complete -F _complete_gacp gacp + complete -F _complete_greset greset + complete -F _complete_gwip gwip + complete -F _complete_gprune gprune + complete -F _complete_groot groot +} + +# Register completions only in interactive bash sessions. +if [[ $- == *i* && -n ${BASH_VERSION:-} ]]; then + _profile_git_load_completion_helpers >/dev/null 2>&1 || true + _profile_git_register_completions +fi + +# EOF