# (C) 2011 magicant

# Completion script for the "git-stash" command.
# Supports Git 1.7.7.

function completion/git-stash {
	WORDS=(git stash "${WORDS[2,-1]}")
	command -f completion//reexecute
}

function completion/git::stash:arg
	if [ ${WORDS[#]} -le 1 ]; then #>>#
		complete -P "$PREFIX" -D "restore stash contents" apply
		complete -P "$PREFIX" -D "restore stash contents in a new branch" branch
		complete -P "$PREFIX" -D "remove all stashes" clear
		complete -P "$PREFIX" -D "create a stash commit without adding it to the stash list" create
		complete -P "$PREFIX" -D "remove stash contents without restoring it" drop
		complete -P "$PREFIX" -D "list existing stashes" list
		complete -P "$PREFIX" -D "restore stash contents and remove it from the stash list" pop
		complete -P "$PREFIX" -D "save local changes in a new stash and reset to HEAD" save
		complete -P "$PREFIX" -D "show stash contents" show
	else #<<#
		WORDS=("${WORDS[2,-1]}")
		if command -vf "completion/git::stash:${WORDS[1]}:arg" >/dev/null 2>&1; then
			command -f "completion/git::stash:${WORDS[1]}:arg"
		fi
	fi

function completion/git::stash:apply:arg {
	command -f completion/git::stash:pop:arg
}

function completion/git::stash:branch:arg {
	if [ ${WORDS[#]} -le 1 ]; then
		command -f completion/git::completeref --branches
	else
		command -f completion/git::stash:completestash
	fi
}

#function completion/git::stash:clear:arg {
#}

#function completion/git::stash:create:arg {
#}

function completion/git::stash:drop:arg {
	command -f completion/git::stash:pop:arg
}

function completion/git::stash:list:arg {

	OPTIONS=()
	if command -vf completion/git::log:getopt >/dev/null 2>&1 ||
			. -AL completion/git-log; then
		command -f completion/git::log:getopt
	fi

	command -f completion//parseoptions -n
	case $ARGOPT in
		(-)
			command -f completion//completeoptions
			;;
		('')
			;;
		(*)
			command -f completion/git::log:compopt
			;;
	esac

}

function completion/git::stash:pop:arg {

	OPTIONS=( #>#
	"q --quiet; print error messages only"
	) #<#
	case ${WORDS[1]} in (apply|pop)
		OPTIONS=("$OPTIONS" #>#
		"--index; restore the index as well as the working copy"
		) #<#
	esac

	command -f completion//parseoptions
	case $ARGOPT in
		(-)
			command -f completion//completeoptions
			;;
		('')
			command -f completion/git::stash:completestash
			;;
	esac

}

function completion/git::stash:save:arg {

	OPTIONS=( #>#
	"a --all; stash all files including ignored/untracked files"
	"--keep-index; keep the index intact"
	"p --patch; interactively choose patch hunks to stash"
	"q --quiet; print error messages only"
	"--no-keep-index; stash and reset the index"
	"u --include-untracked; stash untracked files as well as tracked files"
	) #<#

	command -f completion//parseoptions
	case $ARGOPT in
		(-)
			command -f completion//completeoptions
			;;
#		('')
#			;;
	esac

}

function completion/git::stash:show:arg {

	OPTIONS=()
	if command -vf completion/git::diff:getopt >/dev/null 2>&1 ||
			. -AL completion/git-diff; then
		command -f completion/git::diff:getopt
	fi

	command -f completion//parseoptions -n
	case $ARGOPT in
		(-)
			command -f completion//completeoptions
			;;
		('')
			command -f completion/git::stash:completestash
			;;
		(*)
			command -f completion/git::diff:compopt
			;;
	esac

}

function completion/git::stash:completestash {
	typeset name message
	while read -r name message; do
		name=${name%:}
		complete -P "$PREFIX" -D "$message" -- "$name"
	done 2>/dev/null <(git stash list)
}


# vim: set ft=sh ts=8 sts=8 sw=8 noet:
