#!/usr/bin/env bash
#
# Add new branch to the GIT repository.
# Copyright (c) Petr Baudis, 2005
#
# 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
#
#
# Only rsync scheme URLs are supported for now. An URL can have a fragment
# part, which identifies a branch inside of the repository. So if you have
# a repository rsync://kernel.org/pub/scm/linux/kernel/mj/linux.git and
# you are interested in its 'testing' branch, you can e.g.
#
#	cg-branch-add mj-testing \
#		rsync://kernel.org/pub/scm/linux/kernel/mj/linux.git#testing
#
# and refer to it as 'mj-testing' anytime later.
#
#
# Takes the branch' name and location (local path or rsync URL).

. cg-Xlib

name=$1
location=$2

([ "$name" ] && [ "$location" ]) || die "usage: cg-branch-add NAME SOURCE_LOC"
(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
if [ -s ".git/branches/$name" ] || [ -s ".git/refs/heads/$name" ]; then
	die "branch already exists"
fi

echo "$location" >.git/branches/$name
