]> arthur.barton.de Git - bup.git/blobdiff - t/subtree-hash
Given bup-python, rm vestigial PYTHONPATH settings
[bup.git] / t / subtree-hash
index e85b37fff5ffd2382fb5ec0e9f359a5fb6dc960c..062f4858be60cfe65e521143415f1da338df67b2 100755 (executable)
@@ -1,29 +1,51 @@
-#!/usr/bin/env bash
-
-# Usage: subtree-hash ROOT_HASH [SUBDIR ...]
-
-subtree_hash()
-{
-    root_hash="$1"
-    if test "$#" -eq 1; then
-        echo $root_hash
-    else
-        subdir="$2"
-        subtree_info="$(git ls-tree "$root_hash" | grep -E "   $subdir\$")" || true
-        if test "$(echo "$subtree_info" | wc -l)" -ne 1; then
-            echo "Didn't find just one matching line in subtree $root_hash" 1>&2
-            return 1
-        fi
-
-        subtree_hash="$(echo "$subtree_info" | cut -d' ' -f 3 | cut -d$'\t' -f 1)" || true
-        if test -z "$subtree_hash"; then
-            echo "Unable to find subtree hash in git output: $subtree_info" 1>&2
-            return 1
-        fi
-
-        shift 2 || exit $?
-        subtree_hash "$subtree_hash" "$@" || exit $?
-    fi
-}
-
-subtree_hash "$@"
+#!/bin/sh
+"""": # -*-python-*-
+bup_python="$(dirname "$0")/../cmd/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
+
+from __future__ import absolute_import
+import os, sys
+
+from bup.helpers import handle_ctrl_c, readpipe
+from bup import options
+
+
+optspec = """
+subtree-hash ROOT_HASH [PATH_ITEM...]
+--
+"""
+
+handle_ctrl_c()
+
+o = options.Options(optspec)
+(opt, flags, extra) = o.parse(sys.argv[1:])
+
+if len(extra) < 1:
+    o.fatal('must specify a root hash')
+
+tree_hash = extra[0]
+path = extra[1:]
+
+while path:
+    target_name = path[0]
+    subtree_items = readpipe(['git', 'ls-tree', '-z', tree_hash])
+    target_hash = None
+    for entry in subtree_items.split('\0'):
+        if not entry:
+            break
+        info, name = entry.split('\t', 1)
+        if name == target_name:
+            _, _, target_hash = info.split(' ')
+            break
+    if not target_hash:
+        print >> sys.stderr, "Can't find %r in %s" % (target_name, tree_hash)
+        break
+    tree_hash = target_hash
+    path = path[1:]
+
+if path:
+    sys.exit(1)
+
+print tree_hash