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