2 import sys, math, struct, glob
3 from bup import options, git
4 from bup.helpers import *
7 SHA_PER_PAGE=PAGE_SIZE/200.
10 def merge(idxlist, bits, table):
12 for e in git.idxmerge(idxlist):
14 prefix = git.extract_bits(e, bits)
19 def do_midx(outdir, outfilename, infilenames):
22 sum = Sha1('\0'.join(infilenames)).hexdigest()
23 outfilename = '%s/midx-%s.midx' % (outdir, sum)
27 for name in infilenames:
28 ix = git.PackIdx(name)
32 log('Merging %d indexes (%d objects).\n' % (len(infilenames), total))
33 if (not opt.force and (total < 1024 and len(infilenames) < 3)) \
34 or (opt.force and not total):
35 log('midx: nothing to do.\n')
38 pages = int(total/SHA_PER_PAGE) or 1
39 bits = int(math.ceil(math.log(pages, 2)))
41 log('Table size: %d (%d bits)\n' % (entries*4, bits))
46 os.unlink(outfilename)
49 f = open(outfilename + '.tmp', 'w+')
50 f.write('MIDX\0\0\0\2')
51 f.write(struct.pack('!I', bits))
52 assert(f.tell() == 12)
53 f.write('\0'*4*entries)
55 for e in merge(inp, bits, table):
58 f.write('\0'.join(os.path.basename(p) for p in infilenames))
61 f.write(struct.pack('!%dI' % entries, *table))
63 os.rename(outfilename + '.tmp', outfilename)
65 # this is just for testing
67 p = git.PackMidx(outfilename)
68 assert(len(p.idxnames) == len(infilenames))
70 assert(len(p) == total)
72 for i in merge(inp, total, bits, table):
73 assert(i == pi.next())
79 bup midx [options...] <idxnames...>
81 o,output= output midx filename (default: auto-generated)
82 a,auto automatically create .midx from any unindexed .idx files
83 f,force automatically create .midx from *all* .idx files
85 o = options.Options('bup midx', optspec)
86 (opt, flags, extra) = o.parse(sys.argv[1:])
88 if extra and (opt.auto or opt.force):
89 o.fatal("you can't use -f/-a and also provide filenames")
91 git.check_repo_or_die()
94 do_midx(git.repo('objects/pack'), opt.output, extra)
95 elif opt.auto or opt.force:
96 paths = [git.repo('objects/pack')]
97 paths += glob.glob(git.repo('index-cache/*/.'))
99 log('midx: scanning %s\n' % path)
101 do_midx(path, opt.output, glob.glob('%s/*.idx' % path))
103 m = git.PackIdxList(path)
105 for pack in m.packs: # only .idx files without a .midx are open
106 if pack.name.endswith('.idx'):
107 needed[pack.name] = 1
109 do_midx(path, opt.output, needed.keys())
112 o.fatal("you must use -f or -a or provide input filenames")