]> arthur.barton.de Git - bup.git/blob - cmd/margin-cmd.py
cmd/margin: add a new --predict option.
[bup.git] / cmd / margin-cmd.py
1 #!/usr/bin/env python
2 import sys, struct
3 from bup import options, git, _helpers
4 from bup.helpers import *
5
6
7 optspec = """
8 bup margin
9 --
10 predict    Guess object offsets and report the maximum deviation
11 ignore-midx  Don't use midx files; use only plain pack idx files.
12 """
13 o = options.Options('bup margin', optspec)
14 (opt, flags, extra) = o.parse(sys.argv[1:])
15
16 if extra:
17     o.fatal("no arguments expected")
18
19 git.check_repo_or_die()
20 git.ignore_midx = opt.ignore_midx
21
22 mi = git.PackIdxList(git.repo('objects/pack'))
23
24 def do_predict(ix):
25     total = len(ix)
26     maxdiff = 0
27     for count,i in enumerate(ix):
28         prefix = struct.unpack('!Q', i[:8])[0]
29         expected = prefix * total / (1<<64)
30         diff = count - expected
31         maxdiff = max(maxdiff, abs(diff))
32     print '%d of %d (%.3f%%) ' % (maxdiff, len(ix), maxdiff*100.0/len(ix))
33     sys.stdout.flush()
34     assert(count+1 == len(ix))
35
36 if opt.predict:
37     if opt.ignore_midx:
38         for pack in mi.packs:
39             do_predict(pack)
40     else:
41         do_predict(mi)
42 else:
43     # default mode: find longest matching prefix
44     last = '\0'*20
45     longmatch = 0
46     for i in mi:
47         if i == last:
48             continue
49         #assert(str(i) >= last)
50         pm = _helpers.bitmatch(last, i)
51         longmatch = max(longmatch, pm)
52         last = i
53     print longmatch