]> arthur.barton.de Git - bup.git/blob - cmd/list-idx-cmd.py
README: add 0.30.x to CI status table
[bup.git] / cmd / list-idx-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/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 from binascii import hexlify, unhexlify
10 import sys, os
11
12 from bup import git, options
13 from bup.compat import argv_bytes
14 from bup.helpers import add_error, handle_ctrl_c, log, qprogress, saved_errors
15 from bup.io import byte_stream
16
17 optspec = """
18 bup list-idx [--find=<prefix>] <idxfilenames...>
19 --
20 find=   display only objects that start with <prefix>
21 """
22 o = options.Options(optspec)
23 (opt, flags, extra) = o.parse(sys.argv[1:])
24
25 handle_ctrl_c()
26
27 opt.find = argv_bytes(opt.find) if opt.find else b''
28
29 if not extra:
30     o.fatal('you must provide at least one filename')
31
32 if len(opt.find) > 40:
33     o.fatal('--find parameter must be <= 40 chars long')
34 else:
35     if len(opt.find) % 2:
36         s = opt.find + b'0'
37     else:
38         s = opt.find
39     try:
40         bin = unhexlify(s)
41     except TypeError:
42         o.fatal('--find parameter is not a valid hex string')
43
44 sys.stdout.flush()
45 out = byte_stream(sys.stdout)
46 find = opt.find.lower()
47 count = 0
48 idxfiles = [argv_bytes(x) for x in extra]
49 for name in idxfiles:
50     try:
51         ix = git.open_idx(name)
52     except git.GitError as e:
53         add_error('%r: %s' % (name, e))
54         continue
55     if len(opt.find) == 40:
56         if ix.exists(bin):
57             out.write(b'%s %s\n' % (name, find))
58     else:
59         # slow, exhaustive search
60         for _i in ix:
61             i = hexlify(_i)
62             if i.startswith(find):
63                 out.write(b'%s %s\n' % (name, i))
64             qprogress('Searching: %d\r' % count)
65             count += 1
66
67 if saved_errors:
68     log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))
69     sys.exit(1)