#!/usr/bin/env bash
#
# Restore removed/changed files in the working tree.
# Copyright (c) Petr Baudis, 2005
#
# Restore given files to their original state. Without any parameters,
# it recovers any files removed locally whose removal was not recorded
# by `cg-rm`.
#
# If passed a set of file names, it restores those files to their
# state as of the last commit (including bringing files removed with
# `cg-rm` back to life; FIXME: does not do that part yet).
#
# This command is complementary to the `cg-reset` command, which
# forcefully abandons all the changes in the working tree and
# restores everything to a proper state (including unseeking,
# cancelling merge in progress and rebuilding indexes).
#
# OPTIONS
# -------
# -f::
#	Restore even locally modified files to the version as of
#	the last commit. Take care!

USAGE="cg-restore [-f] [FILE]..."
_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

ret=0

if [ "$ARGS" ]; then
	for file in "${ARGS[@]}"; do
		git-checkout-cache $force "$file" || ret=1
	done
else
	if [ "$(git-ls-files --deleted)" ]; then
		echo "Recovering files:"
		git-ls-files --deleted | sed "s/^/$(echo -e "\t")/"
	fi
	git-checkout-cache -q -a $force
fi

update_index || ret=1

exit $ret
