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
|
#!/bin/sh
"""": # -*-python-*-
bup_exec="$(dirname "$0")/bup-exec" || exit $?
exec "$bup_exec" "$0" ${1+"$@"}
"""
from __future__ import absolute_import, print_function
import os.path, sys
from bup.compat import argv_bytes, get_argvb
from bup.helpers import handle_ctrl_c, readpipe
from bup.io import byte_stream
from bup import options
optspec = """
subtree-hash ROOT_HASH [PATH_ITEM...]
--
"""
handle_ctrl_c()
o = options.Options(optspec)
opt, flags, extra = o.parse_bytes(get_argvb()[1:])
if len(extra) < 1:
o.fatal('must specify a root hash')
tree_hash = argv_bytes(extra[0])
path = [argv_bytes(x) for x in extra[1:]]
while path:
target_name = path[0]
subtree_items = readpipe([b'git', b'ls-tree', b'-z', tree_hash])
target_hash = None
for entry in subtree_items.split(b'\0'):
if not entry:
break
info, name = entry.split(b'\t', 1)
if name == target_name:
_, _, target_hash = info.split(b' ')
break
if not target_hash:
print("Can't find %r in %s" % (target_name, tree_hash.decode('ascii')),
file=sys.stderr)
break
tree_hash = target_hash
path = path[1:]
if path:
sys.exit(1)
sys.stdout.flush()
out = byte_stream(sys.stdout)
out.write(tree_hash + b'\n')
|