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