aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/git.bash
blob: 8c0335c5db595e0644705566143467896c9a24b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Completion for git(1) local branch names
_git() {

    # Bail if not a git repo (or no git!)
    git rev-parse --git-dir >/dev/null 2>&1 || return 1

    # Switch on the previous word
    case ${COMP_WORDS[1]} in

        # If the first word is appropriate, complete with branch/tag names
        checkout|merge|rebase)
            local branch
            while read -r _ _ branch ; do
                branch=${branch##*/}
                [[ $branch == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
                COMPREPLY[${#COMPREPLY[@]}]=$branch
            done < <(git for-each-ref refs/heads refs/tags 2>/dev/null)
            return
            ;;

        # Bail if it isn't
        *)
            return 1
            ;;
    esac
}
complete -F _git -o default git