#!/usr/bin/env bash # ------------------------------------------------------------------------------ # Copyright (c) 2013-2026 Geoffray Levasseur # Protected by the BSD3 license. Please read bellow for details. # # * 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 the copyright holder nor the names # * of any other 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, # * OF SUCH DAMAGE. # ------------------------------------------------------------------------------ # 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 mapfile -t 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 mapfile -t 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 __git_cmd_idx=0 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 mapfile -t COMPREPLY < <(compgen -f -- "${COMP_WORDS[COMP_CWORD]}") } _complete_gst() { local cur cur="${COMP_WORDS[COMP_CWORD]}" case "$cur" in -*) mapfile -t COMPREPLY < <(compgen -W "-h --help" -- "$cur") ;; *) mapfile -t 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 mapfile -t COMPREPLY < <(compgen -W "-h --help -n --limit" -- "$cur") } _complete_gsync() { local cur cur="${COMP_WORDS[COMP_CWORD]}" if [[ $cur == -* ]]; then mapfile -t 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 mapfile -t 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 mapfile -t 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 mapfile -t COMPREPLY < <(compgen -W "-h --help" -- "$cur") else COMPREPLY=() fi } _complete_gprune() { local cur cur="${COMP_WORDS[COMP_CWORD]}" if [[ $cur == -* ]]; then mapfile -t COMPREPLY < <(compgen -W "-h --help" -- "$cur") return 0 fi _profile_git_complete_refs } _complete_groot() { local cur cur="${COMP_WORDS[COMP_CWORD]}" mapfile -t 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