#!/usr/bin/env bash
#
# Pull and merge changes from a branch to the local repository.
# Copyright (c) Petr Baudis, 2005
#
# Takes the branch name as an argument, defaulting to 'origin'.
#
# If local changes conflict with those of the branch updated from the
# merge will be blocked.
#
# OPTIONS
# -------
# -f::
#	Force the complete pull even if the heads are the same.

USAGE="cg-update [-f] [BRANCH_NAME]"
_git_requires_root=1

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

force=
while optparse; do
	if optparse -f; then
		force=-f
	else
		optfail
	fi
done

name=${ARGS[0]}
[ "$name" ] || { [ -s $_git/refs/heads/origin ] && name=origin; }
[ "$name" ] || die "where to update from?"

if [ -s "$_git/branches/$name" ]; then
	cg-pull $force $name || exit 1
else
	echo "Updating from a local branch."
fi
echo
echo "Applying changes..."
cg-merge $name
