blob: ba8e5d59f1a4b36ea7e1e58f74517ad2616c00b9 (
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
|
# 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 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 ref names
*)
local ref
while 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
|