#!/usr/bin/env bash
#
# Mark certain commit by a tag.
# Copyright (c) Petr Baudis, 2005
#
# Creates a tag referencing the given commit (or 'HEAD'). You can then
# use the tag anywhere you specify a commit or tree ID.
#
# Takes the tag name and optionally the associated ID as arguments.

USAGE="cg-tag TAG_NAME [COMMIT_ID]"

. ${COGITO_LIB}cg-Xlib

name=$1
id=$2

[ "$name" ] || usage
[ "$id" ] || id=$(commit-id)

(echo $name | egrep -qv '[^a-zA-Z0-9_.@!:-]') || \
	die "name contains invalid characters"

mkdir -p $_git/refs/tags

[ -s "$_git/refs/tags/$name" ] && die "tag already exists ($name)"
[ "$id" ] || id=$(cat $_git/HEAD)

echo $id >$_git/refs/tags/$name
