#!/usr/bin/env bash
#
# Add new branch to the GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# Takes the branch name and source location as parameters.
# After you add a branch, you can `cg-pull` it whenever you want and
# it will keep your objects database in sync with it. Its latest
# commit is accessible as `.git/refs/heads/branchname` (or - more
# conveniently - as `$(commit-id branchname)`). For example, to make a
# diff between Linus (after you added him) and your current tree, do:
#
#	$ cg-pull linus
#	$ cg-diff linus
#
# The possible location specifiers are:
#
# 1. Local path - note that pulling will hardlink the objects, not copy them.
# 2. rsync - either `rsync://host/path` or `host:/path` (the latter can change)
# 3. HTTP - `http://host/path`
# 4. SSH - `git+ssh://host/path` or `git+ssh://user@host/path` - note that
#    the path must be absolute.
#
# The URL can have a fragment part, which identifies a branch inside of
# the repository. Therefore, if you have a repository
# `rsync://host/path/repo.git` and you are interested in its 'testing'
# branch, you can e.g.:
#
#	$ cg-branch-add repo-testing rsync://host/path/repo.git#testing
#
# and refer to it as 'repo-testing' anytime later.

USAGE="cg-branch-add BRANCH_NAME LOCATION"

. ${COGITO_LIB}cg-Xlib

name=$1
location=$2

([ "$name" ] && [ "$location" ]) || usage
(echo $name | egrep -qv '[^a-zA-Z0-9_.@!:-]') || \
	die "name contains invalid characters"
if [ "$name" = "this" ] || [ "$name" = "HEAD" ]; then
	die "given branch name is reserved"
fi

mkdir -p $_git/branches
[ -s "$_git/branches/$name" ] && die "branch already exists"
[ -s "$_git/refs/heads/$name" ] && echo "warning: I already have head of this branch" >&2

echo "$location" >$_git/branches/$name
