aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/git.bash
blob: 34a6f5efadf3db59751816df0574f10d6bb9afed (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Some simple completion for Git
_git() {
    
    # Try to find the index of the Git subcommand
    local -i sci i
    for ((i = 1; !sci && i <= COMP_CWORD; i++)) ; do
        case ${COMP_WORDS[i]} in

            # Skip --option=value
            --*=*) ;;

            # These ones have arguments, so bump the index up one more
            -C|-c|--exec-path|--git-dir|--work-tree|--namespace) ((i++)) ;;

            # Skip --option
            --?*) ;;

            # We have hopefully found our subcommand
            *) ((sci = i)) ;;
        esac
    done

    # Complete initial subcommand
    if ((sci == COMP_CWORD)) || [[ ${COMP_WORDS[sci]} == 'help' ]] ; then
        local ep
        ep=$(git --exec-path) || return
        local path
        for path in "$ep"/git-"${COMP_WORDS[COMP_CWORD]}"* ; do
            [[ -f $path ]] || continue
            [[ -x $path ]] || continue
            COMPREPLY[${#COMPREPLY[@]}]=${path#"$ep"/git-}
        done
        return
    fi

    # Test subcommand to choose completions
    case ${COMP_WORDS[sci]} in

        # Complete with untracked, unignored files
        add)
            local file
            while IFS= read -rd '' file ; do
                [[ -n $file ]] || continue
                COMPREPLY[${#COMPREPLY[@]}]=$file
            done < <(git ls-files \
                --directory \
                --exclude-standard \
                --no-empty-directory \
                --others \
                -z \
                -- "${COMP_WORDS[COMP_CWORD]}"'*' \
                2>/dev/null)
            ;;

        # Complete with remote subcommands and then remote names
        remote)
            local word
            while IFS= read -r word ; do
                [[ -n $word ]] || continue
                COMPREPLY[${#COMPREPLY[@]}]=$word
            done < <(
                if ((COMP_CWORD - sci > 1)) ; then
                    git remote 2>/dev/null
                else
                    compgen -W '
                        add
                        get-url
                        prune
                        remove
                        rename
                        set-branches
                        set-head
                        set-url
                        show
                        update
                    ' -- "${COMP_WORDS[COMP_CWORD]}"
                fi
            )
            ;;

        # Complete with ref names
        *)
            local ref
            while IFS= read -r ref ; do
                [[ -n $ref ]] || continue
                COMPREPLY[${#COMPREPLY[@]}]=${ref#refs/*/}
            done < <(git for-each-ref \
                --format '%(refname)' \
                -- 'refs/**/'"${COMP_WORDS[COMP_CWORD]}"'*' \
                2>/dev/null)
            return
            ;;
    esac
}

# Defaulting to directory/file completion is important in Git's case
complete -F _git -o bashdefault -o default git