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

if [ ! "$id" ] || [ "$(git-cat-file -t "$id" 2>/dev/null)" != "tree" ]; then
	id=$(git-cat-file commit $(commit-id "$id") | sed -e 's/tree //;q')
fi

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

echo $id
