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