#!/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
# -------
# -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.

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

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

no_delete=
if [ "$1" = "-n" ]; then
	no_delete=1
	shift
fi

[ "$1" ] || usage

TMPFILE=$(mktemp -t gitrm.XXXXXX) || exit 1
( cd ${_git_relpath:-.} && find "$@" -type f -print 2>/dev/null ) > $TMPFILE

cat $TMPFILE | sed 's/^/Removing file /'
[ "$no_delete" ] || ( cd ${_git_relpath:-.} && rm -f "$@" )
cat $TMPFILE | sed "s|^|$_git_relpath|" | normpath | tr '\n' '\0' | xargs -0r git-update-cache --force-remove --

rm $TMPFILE
