#!/usr/bin/env bash
#
# Get ID of tree associated with given commit or HEAD.
# Copyright (c) Petr Baudis, 2005
#
# Takes ID of the appropriate commit, defaults to HEAD.

id="$1"
normid=$(${COGITO_LIB}cg-Xnormid "$id") || exit 1
type=$(git-cat-file -t "$normid")

if [ "$type" = "commit" ]; then
	normid=$(git-cat-file commit "$normid" | sed -e 's/tree //;q')
	type=$(git-cat-file -t "$normid")
fi

if [ "$type" != "tree" ]; then
	echo "Invalid tree id: $id" >&2
	exit 1
fi

echo $normid
