]> arthur.barton.de Git - bup.git/blob - lib/cmd/list-idx-cmd.py
d9d318f4480361ac03fd27418c71346061f5ed38
[bup.git] / lib / cmd / list-idx-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/bup-python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import, print_function
18 from binascii import hexlify, unhexlify
19 import sys, os
20
21 from bup import compat, git, options
22 from bup.compat import argv_bytes
23 from bup.helpers import add_error, handle_ctrl_c, log, qprogress, saved_errors
24 from bup.io import byte_stream
25
26 optspec = """
27 bup list-idx [--find=<prefix>] <idxfilenames...>
28 --
29 find=   display only objects that start with <prefix>
30 """
31 o = options.Options(optspec)
32 opt, flags, extra = o.parse(compat.argv[1:])
33
34 handle_ctrl_c()
35
36 opt.find = argv_bytes(opt.find) if opt.find else b''
37
38 if not extra:
39     o.fatal('you must provide at least one filename')
40
41 if len(opt.find) > 40:
42     o.fatal('--find parameter must be <= 40 chars long')
43 else:
44     if len(opt.find) % 2:
45         s = opt.find + b'0'
46     else:
47         s = opt.find
48     try:
49         bin = unhexlify(s)
50     except TypeError:
51         o.fatal('--find parameter is not a valid hex string')
52
53 sys.stdout.flush()
54 out = byte_stream(sys.stdout)
55 find = opt.find.lower()
56 count = 0
57 idxfiles = [argv_bytes(x) for x in extra]
58 for name in idxfiles:
59     try:
60         ix = git.open_idx(name)
61     except git.GitError as e:
62         add_error('%r: %s' % (name, e))
63         continue
64     if len(opt.find) == 40:
65         if ix.exists(bin):
66             out.write(b'%s %s\n' % (name, find))
67     else:
68         # slow, exhaustive search
69         for _i in ix:
70             i = hexlify(_i)
71             if i.startswith(find):
72                 out.write(b'%s %s\n' % (name, i))
73             qprogress('Searching: %d\r' % count)
74             count += 1
75
76 if saved_errors:
77     log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))
78     sys.exit(1)