]> arthur.barton.de Git - bup.git/blob - t/subtree-hash
features: show version number of the Python interpreter
[bup.git] / t / subtree-hash
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/../dev/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 os.path, sys
10
11 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/../lib']
12
13 from bup.compat import argv_bytes
14 from bup.helpers import handle_ctrl_c, readpipe
15 from bup.io import byte_stream
16 from bup import options
17
18
19 optspec = """
20 subtree-hash ROOT_HASH [PATH_ITEM...]
21 --
22 """
23
24 handle_ctrl_c()
25
26 o = options.Options(optspec)
27 (opt, flags, extra) = o.parse(sys.argv[1:])
28
29 if len(extra) < 1:
30     o.fatal('must specify a root hash')
31
32 tree_hash = argv_bytes(extra[0])
33 path = [argv_bytes(x) for x in extra[1:]]
34
35 while path:
36     target_name = path[0]
37     subtree_items = readpipe([b'git', b'ls-tree', b'-z', tree_hash])
38     target_hash = None
39     for entry in subtree_items.split(b'\0'):
40         if not entry:
41             break
42         info, name = entry.split(b'\t', 1)
43         if name == target_name:
44             _, _, target_hash = info.split(b' ')
45             break
46     if not target_hash:
47         print("Can't find %r in %s" % (target_name, tree_hash.decode('ascii')),
48               file=sys.stderr)
49         break
50     tree_hash = target_hash
51     path = path[1:]
52
53 if path:
54     sys.exit(1)
55
56 sys.stdout.flush()
57 out = byte_stream(sys.stdout)
58 out.write(tree_hash + b'\n')