# (C) 2011 magicant

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

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

function completion/git::fetch:arg {

	OPTIONS=( #>#
	"--dry-run; don't actually fetch anything"
	"--multiple; allow specifying multiple remotes"
	"p --prune; delete remote-tracking branches that no longer exist on the remote"
	"q --quiet; don't report progress"
	"--submodule-prefix" # not for command line use
	"--recurse-submodules-default::" # not for command line use
	"t --tags; fetch all tags from the remote"
	"v --verbose" # TODO description
	) #<#
	command -f completion/git::fetch:getopt

	command -f completion//parseoptions -n
	case $ARGOPT in
		(-)
			command -f completion//completeoptions
			;;
		(--recurse-submodules|--upload-pack)
			command -f completion/git::$ARGOPT:arg
			;;
		(--recurse-submodules-default)
			;;
#		(--depth)
#			;;
		('')
			typeset i=2 multiple=false
			while [ $i -le ${WORDS[#]} ]; do
				case ${WORDS[i]} in
				(--)
					i=$((i+1))
					break
					;;
				(--multiple)
					multiple=true
					break
					;;
				(-?*)
					i=$((i+1))
					;;
				(*)
					break
					;;
				esac
			done
			if $multiple || [ $i -gt ${WORDS[#]} ]; then
				#TODO complete remote URI
				command -f completion/git::completeremote
				{ command -vf completion/git::completeremotegroup >/dev/null 2>&1 ||
					. -AL completion/git-remote; } &&
					command -f completion/git::completeremotegroup
			elif [ "${WORDS[-1]}" = tag ]; then
				command -f completion/git::completeref --tags
			else
				typeset word="${TARGETWORD#"$PREFIX"}"
				word=${word#+}
				case $word in
				(*:*) # complete local refs
					word=${word#*:}
					PREFIX=${TARGETWORD%"$word"}
					command -f completion/git::completeref
					;;
				(*) # complete remote refs
					PREFIX=${TARGETWORD%"$word"}
					command -f completion/git::completeremoteref "${WORDS[i]}"
					;;
				esac
			fi
			;;
	esac

}

function completion/git::fetch:getopt {
	typeset fetch=
	case ${gitcmd-} in
		fetch) fetch=true;;
	esac
	OPTIONS=("$OPTIONS" #>#
	"--all; fetch all remotes"
	"a --append; append to (not overwrite) existing FETCH_HEAD"
	"--depth:; specify the max number of history to fetch"
	"f --force; allow non-fast-forward update"
	"k --keep; keep downloaded pack"
	"--no-recurse-submodules; don't fetch submodules"
	"${fetch+n} --no-tags; don't fetch tags automatically"
	"--progress; report progress"
	"--recurse-submodules::; specify whether to fetch submodules"
	"u --update-head-ok" # not for command line use
	"--upload-pack:; specify a path for git-upload-pack on the remote host"
	) #<#
}


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