#!/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.

SHA1="[A-Za-z0-9]{40}"
TREE="^tree $SHA1$"

id=$(cat-file commit $(commit-id "$1") | egrep "$TREE" | cut -d ' ' -f 2)

if [ "$(cat-file -t "$id")" != "tree" ]; then
	echo "Invalid id: $id" >&2
	exit 1
fi

echo $id
