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