#!/usr/bin/env bash
#
# Remove files from a GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# Takes a list of file names at the command line, and schedules them
# for removal from the GIT repository at the next commit.
#
# OPTIONS
# -------
# -f::
#	Also delete the files from the tree physically.
# -n::
#	Do not delete the files from the tree physically, if they are
#	still there. So it effectively just makes Cogito to stop caring
#	about the file. This is the default.

USAGE="cg-rm [-f] [-n] FILE..."

. ${COGITO_LIB}cg-Xlib || exit 1

delete=
while optparse; do
	if optparse -f; then
		delete=1
	elif optparse -n; then
		delete=
	else
		optfail
	fi
done

[ "${ARGS[*]}" ] || usage

TMPFILE=$(mktemp -t gitrm.XXXXXX) || exit 1
error=
for file in "${ARGS[@]}"; do
	echo "$file" >>$TMPFILE
done

cat $TMPFILE | sed 's/^/Removing file /'
[ "$delete" ] && ( cd ${_git_relpath:-.} && rm -f "${ARGS[@]}" )
cat $TMPFILE | sed "s|^|$_git_relpath|" | path_xargs git-update-index --force-remove -- || error=1

rm $TMPFILE

[ "$error" ] && die "warning: not all files could have been removed"
exit 0
