#!/bin/zsh
DO=$0
main() {
	args url! repo_path head_sha1
	rebuild-if-refetch
	eval $(getvars)

	local scheme rest repo_path head_sha1
	scheme=${url%%:*}
	rest=/${${${url#*:}#/}#/}
	repo_path=${DO:h:a}/downloads/git/${scheme}${rest:a}.git
	setvar repo_path $repo_path

	if [[ -d $repo_path ]]; then
		if ! (cd $repo_path && git fetch -u -v -f origin master); then
			rm -rf $repo_path || die "Failed to remove ${(qqq)repo_path}"
			git clone --depth=1 --mirror $url $repo_path \
				|| die "Failed to fetch ${(qqq)url}"
		fi
	else
		git clone --depth=1 --mirror $url $repo_path \
			|| die "Failed to fetch ${(qqq)url}"
	fi
	head_sha1=$(cd $repo_path && git show --format=oneline -s HEAD) ||\
		die "Could not obtain HEAD sha1sum"
	head_sha1=${head_sha1%% *}
	setvar head_sha1 $head_sha1

	__check_deps__+='if { test -d '${(qqq)repo_path}$' }\n'
	__check_deps__+='if { pipeline -d { printf "%s\n" '${(qqq)head_sha1}$' } fdmove 3 0 pipeline -d { pipeline { cd '${(qqq)repo_path}$' git show --format=oneline -s HEAD  } sed "s/ *//" } cmp -s - /proc/self/fd/3 }\n'
}
. ./common.zsh
# vim: fileencoding=utf8 ft=zsh noet sw=4 ts=4 sts=4