Files
profile/profile.d/bash-completion/git-completion.sh
2026-05-06 18:20:30 +02:00

221 lines
5.1 KiB
Bash

#!/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") )
}
_profile_git_complete_add_paths()
{
# shellcheck disable=SC2034 # Used indirectly by git-completion helpers via dynamic scope.
local cur words cword prev
local complete_opt="--others --modified --directory --no-empty-directory"
if declare -F __git_complete_index_file >/dev/null 2>&1; then
if declare -F _get_comp_words_by_ref >/dev/null 2>&1; then
_get_comp_words_by_ref -n =: cur words cword prev
else
cur="${COMP_WORDS[COMP_CWORD]}"
if (( COMP_CWORD > 0 )); then
prev="${COMP_WORDS[COMP_CWORD-1]}"
else
prev=""
fi
# shellcheck disable=SC2034 # Used indirectly by git-completion helpers via dynamic scope.
cword="$COMP_CWORD"
# shellcheck disable=SC2034 # Used indirectly by git-completion helpers via dynamic scope.
words=("${COMP_WORDS[@]}")
fi
if [[ -n $(__git_find_on_cmdline "-u --update") ]]; then
complete_opt="--modified"
fi
__git_complete_index_file "$complete_opt"
return 0
fi
COMPREPLY=( $(compgen -f -- "${COMP_WORDS[COMP_CWORD]}") )
}
_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
_profile_git_complete_add_paths
}
_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