fork-g/bash_completion

85 lines
2.1 KiB
Bash
Raw Normal View History

2015-07-17 01:25:50 -04:00
#!/bin/bash
list_branches() {
git for-each-ref refs/heads --format="%(refname:short)"
}
2015-11-03 01:13:04 -05:00
list_tracked() {
git ls-tree HEAD | grep blob | awk '{print $4}';
git ls-tree HEAD | grep tree | awk '{print $4"/"}';
}
list_untracked() {
git ls-files --exclude-standard --other --directory --no-empty-directory
}
list_modified() {
git ls-files --modified
}
list_staged() {
git diff --name-only --relative --cached
}
2016-02-05 01:56:09 -05:00
list_tags() {
git tag
}
list_remotes() {
git remote
}
2015-07-17 01:25:50 -04:00
_get_complete()
{
# Available variables:
# COMP_LINE COMP_POINT COMP_KEY COMP_TYPE COMP_WORDS COMP_CWORD
# $1 : name of command whose arguments are being completed
# $2 : the word being completed
# $3 : the word preceding the word being completed
case ${COMP_CWORD} in
1)
2016-02-05 01:56:09 -05:00
commands="+ - = ! @ # ? ^ \
clone fetch push remote \
add rm ignore stage unstage reset commit uncommit branch rmbranch tag untag \
branches status diff log review \
squash subrepo submodule"
2015-07-17 01:25:50 -04:00
COMPREPLY=( $(compgen -W "${commands}" "$2") )
;;
2)
2015-11-03 01:13:04 -05:00
compopt -o nospace
#case "$3" in
case "${COMP_WORDS[1]}" in
2015-11-03 01:13:04 -05:00
# <branch>
'@' | branch | rmbranch | fetch)
2015-11-03 01:13:04 -05:00
COMPREPLY=( $(compgen -W "$(list_branches)" "$2") )
;;
# <modified path>
'=' | stage | reset)
2015-11-03 01:13:04 -05:00
COMPREPLY=( $(compgen -W "$(list_modified)" "$2") )
2015-07-17 01:25:50 -04:00
;;
2015-11-03 02:03:54 -05:00
# <staged path>
2015-11-03 01:13:04 -05:00
unstage)
COMPREPLY=( $(compgen -W "$(list_staged)" "$2") )
;;
2015-11-03 02:03:54 -05:00
# <tracked path>
'-' | rm)
2015-11-03 01:13:04 -05:00
COMPREPLY=( $(compgen -W "$(list_tracked)" "$2") )
;;
2016-02-05 01:56:09 -05:00
# <ref>
2015-11-03 02:03:54 -05:00
diff)
COMPREPLY=( $(compgen -W "$(list_tracked)" -W "STAGE" -W "HEAD" "$2") )
;;
2015-11-03 01:13:04 -05:00
# <untracked path>
2016-02-05 01:56:09 -05:00
'+' | add | ignore)
2015-11-03 00:08:33 -05:00
compopt -o nospace
2015-11-03 02:03:54 -05:00
COMPREPLY=( $(compgen -W "$(list_untracked)" "$2") )
2015-11-03 01:13:04 -05:00
;;
esac
;;
3)
case "${COMP_WORDS[COMP_CWORD-2]}" in
2015-07-17 01:25:50 -04:00
diff)
2015-11-03 00:08:33 -05:00
compopt -o nospace
2015-11-03 01:13:04 -05:00
COMPREPLY=( $(compgen -W "$(list_tracked)" "$2") )
2015-07-17 01:25:50 -04:00
;;
esac
;;
*)
COMPREPLY=()
;;
esac
}
2016-02-06 18:34:11 -05:00
complete -F _get_complete g