]> arthur.barton.de Git - bup.git/blob - t/subtree-hash
test-restore-map-owner: accommodate python 3 and test there
[bup.git] / t / subtree-hash
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/../cmd/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7
8 from __future__ import absolute_import, print_function
9 import sys
10
11 from bup.compat import argv_bytes
12 from bup.helpers import handle_ctrl_c, readpipe
13 from bup.io import byte_stream
14 from bup import options
15
16
17 optspec = """
18 subtree-hash ROOT_HASH [PATH_ITEM...]
19 --
20 """
21
22 handle_ctrl_c()
23
24 o = options.Options(optspec)
25 (opt, flags, extra) = o.parse(sys.argv[1:])
26
27 if len(extra) < 1:
28     o.fatal('must specify a root hash')
29
30 tree_hash = argv_bytes(extra[0])
31 path = [argv_bytes(x) for x in extra[1:]]
32
33 while path:
34     target_name = path[0]
35     subtree_items = readpipe([b'git', b'ls-tree', b'-z', tree_hash])
36     target_hash = None
37     for entry in subtree_items.split(b'\0'):
38         if not entry:
39             break
40         info, name = entry.split(b'\t', 1)
41         if name == target_name:
42             _, _, target_hash = info.split(b' ')
43             break
44     if not target_hash:
45         print("Can't find %r in %s" % (target_name, tree_hash.decode('ascii')),
46               file=sys.stderr)
47         break
48     tree_hash = target_hash
49     path = path[1:]
50
51 if path:
52     sys.exit(1)
53
54 sys.stdout.flush()
55 out = byte_stream(sys.stdout)
56 out.write(tree_hash + b'\n')