#!/usr/bin/env bash
#
# Initialize a GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# Takes an optional parameter which will make it "clone" a specified
# remote repository.

. cg-Xlib

uri=$1

[ -e .git ] && die ".git already exists"

init-db
mkdir .git/branches .git/refs .git/refs/heads .git/refs/tags
touch .git/refs/heads/master
ln -s refs/heads/master .git/HEAD

if [ "$uri" ]; then
	echo "$uri" >.git/branches/origin
	cg-pull origin || die "pull failed"

	cp .git/refs/heads/origin .git/refs/heads/master
	read-tree $(tree-id)
	checkout-cache -a
	update-cache --refresh

	echo "Cloned (origin $uri available as branch \"origin\")"
fi
