#!/usr/bin/env bash
#
# Add files to the GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# Takes a list of file names at the command line, and schedules them
# for addition to the GIT repository at the next commit.
#
# The command will fail if one of the given files does not exist.
#
# Note that directories never have to be added to the repository and any
# attempt in doing so will cause the command to fail. The reason for this
# is that 'Cogito' manages content and empty directories have no content.
# Instead, directories are added automatically when adding files inside
# them.
#
# OPTIONS
# -------
# -N::
#	Only update the cache: do not copy the data into the object database.
#	This is for special purposes when you might not actually _have_ any
#	object database. This option is normally not interesting.

USAGE="cg-add [-N] FILE..."

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

infoonly=
while optparse; do
	if optparse -N; then
		infoonly=--info-only
	else
		optfail
	fi
done

[ "$ARGS" ] || usage

TMPFILE=$(mktemp -t gitadd.XXXXXX) || exit 1
( cd ${_git_relpath:-.} && find "${ARGS[@]}" -type f -print ) > $TMPFILE || {
	die "not all files exist, nothing added"
	rm $TMPFILE
}

cat $TMPFILE | sed 's/^/Adding file /'
cat $TMPFILE | sed "s|^|$_git_relpath|" | normpath | tr '\n' '\0' | xargs -0 git-update-cache --add ${infoonly} --

rm $TMPFILE
