#!/usr/pkg/bin/bash
#
# Initialize new repository
# Copyright (c) Petr Baudis, 2005
#
# `cg-init` called in a non-empty directory will automatically add its contents
# in the initial commit. (Please note that certain default ignore rules are
# applied during this operation - see `cg-status` for detailed description.
# If any files were not added due to this, cg-init will advise you what to do.)
#
# This command is intended for creating repositories for work on new projects.
# If you want to clone an existing project, see `cg-clone`. If you want to
# set up a public repository not for direct work but only for pushing/pulling,
# see `cg-admin-setuprepo`. It is also possible to import repositories from
# other SCMs to GIT, see `git-cvsimport(1)`, `git-svnimport(1)` and
# `git-archimport(1)`.
#
# OPTIONS
# -------
# -e EXCLUDEPATTERN:: Ignore files matching EXCLUDEPATTERN
#	Ignore files matching this pattern when importing files for the
#	initial commit. Note that if you are importing any .gitignore
#	files, they will be considered as well (see `cg-status` for
#	details on the files ignoring concept). If you want to make
#	an even more custom choice of files to be imported, use the -I
#	parameter and add and perform the initial commit manually.
#
# -I:: No initial commit
#	Do not perform the initial commit. You can perform the initial commit
#	manually later, but you will need to pass cg-commit the -C parameter.
#
# -m MESSAGE:: Specify initial commit message
#	Specify the commit message for the initial commit. See `cg-commit`
#	documentation for details.
#
# -N:: Only update the cache
#	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.

# Testsuite: Almost complete (t9000-init, missing -N test)

USAGE="cg-init [-I] [-N] [-e EXCLUDEPATTERN]... [-m MESSAGE]..."
_git_repo_unneeded=1

. "${COGITO_LIB:-/usr/pkg/lib/cogito/}"cg-Xlib || exit 1

excludes=()
no_initial_commit=
commitargs=()
infoonly=
while optparse; do
	if optparse -e=; then
		excludes[${#excludes[@]}]="$OPTARG"
	elif optparse -I; then
		no_initial_commit=1
	elif optparse -m=; then
		commitargs[${#commitargs[@]}]="-m"
		commitargs[${#commitargs[@]}]="$OPTARG"
	elif optparse -N; then
		infoonly=-N
	else
		optfail
	fi
done
[ "${commitargs[*]}" ] || commitargs=(-m "Initial commit")
if tty -s; then
	commitargs[${#commitargs[@]}]="-e"
fi

[ -e "$_git" ] && die "$_git already exists"

cleanup_trap "rm -rf $_git"

git-init-db

git-read-tree # Seed the dircache
if ! [ "$no_initial_commit" ]; then
	[ "$(ls)" ] && list_untracked_files exclude nosquashdirs "${excludes[@]}" | /usr/bin/xargs -0 cg-add ${infoonly}
	cg-commit -C "${commitargs[@]}" ${infoonly} || die "Initial commit aborted"
fi

if [ "$(cg-status -nwx)" ]; then
	echo "cg-init: Note that some files were not added due to the default ignore rules." >&2
	echo "cg-init: You can list them by invoking 'cg-status -x'." >&2
fi

exit 0
