1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
#!/usr/bin/env bash
# Recursively dump all blobs in the subtree identified by ID.
set -o pipefail
usage() {
cat <<EOF
Usage: cat-git-tree [--git-dir DIR] ID
EOF
}
cat-item()
{
local hash="$1"
local type="$2"
case "$type" in
blob)
git cat-file blob "$hash" || exit $?
;;
tree)
local tree=$(git ls-tree "$hash") || exit $?
while read -r line; do
local sub_type=$(echo "$line" | cut -d' ' -f 2) || exit $?
local sub_hash=$(echo "$line" | cut -d' ' -f 3) || exit $?
sub_hash=$(echo "$sub_hash" | cut -d' ' -f 1) || exit $?
cat-item "$sub_hash" "$sub_type"
done <<< "$tree"
;;
*)
echo "Unexpected item: $type $hash" 1>&2
exit 1
;;
esac
}
case $# in
1) ;;
3)
if test "$1" != --git-dir; then
usage 1>&2
exit 1
fi
export GIT_DIR="$2"
shift 2
;;
*)
usage 1>&2
exit 1
;;
esac
top="$1"
type=$(git cat-file -t "$top") || exit $?
cat-item "$top" "$type"
|