#!/usr/bin/env bash
#
# Push changes to a remote GIT repository.
# Copyright (c) Petr Baudis, 2005.
#
# It will push your commits to the remote repository, provided that
# your commits follow the last commit in the remote repository.
# Note that if the remote repository is associated with a working
# tree copy, this command won't update that. Use cg-reset at the
# remote side to bring it in sync (but throw away any local changes
# in that tree).
#
# Takes the branch name as an argument, defaulting to "origin".

USAGE="cg-push [BRANCH_NAME]"

. ${COGITO_LIB:-/usr/lib/cogito/}cg-Xlib || exit 1

while optparse; do
	optfail
done

name=${ARGS[0]}

[ "$name" ] || { [ -s $_git/refs/heads/origin ] && name=origin; }
[ "$name" ] || die "where to push from?"
uri=$(cat "$_git/branches/$name" 2>/dev/null) || die "unknown branch: $name"

rembranch=master
if echo "$uri" | grep -q '#'; then
	rembranch=$(echo $uri | cut -d '#' -f 2)
	uri=$(echo $uri | cut -d '#' -f 1)
	die "pushing to a different head not supported yet"
fi

if echo "$uri" | grep -q "^http://"; then
	die "pushing over HTTP not supported yet"
elif echo "$uri" | grep -q "^git+ssh://"; then
	git-send-pack "$(echo "$uri" | sed 's#^git+ssh://\([^/]*\)\(/.*\)$#\1:\2#')" $rembranch
elif echo "$uri" | grep -q ":"; then
	die "pushing over rsync not supported"
else
	remgit="$uri"; [ -d "$remgit/.git" ] && remgit="$remgit/.git"
	if is_same_repo "$_git_objects" "$remgit/objects"; then
		remid="$(cat "$remgit"/refs/heads/$rembranch)" || die "$remgit: no branch $master"
		if [ "$remid" != "$(git-merge-base "$remid" "$(commit-id)")" ]; then
			echo "ERROR: Remote '$rembranch' has some changes you don't have in your '$_git_head'" >&2
			echo "Try to cg-update from it first, then push." >&2
			exit 1
		fi
		commit-id >"$remgit"/refs/heads/$rembranch
	else
		git-send-pack "$uri" $rembranch
	fi
fi
