2 import os, sys, stat, time
3 import options, git, index, drecurse
7 def _simplify_iter(iters):
8 total = sum([len(it) for it in iters])
9 l = list([iter(it) for it in iters])
11 l = list([(next(it),it) for it in l])
12 l = filter(lambda x: x[0], l)
15 if not (count % 1024):
16 progress('bup: merging indexes (%d/%d)\r' % (count, total))
21 #log('merge: %r %r (%d)\n' % (e.ctime, e.name, len(l)))
22 if e.ctime: # skip auto-generated entries
28 log('bup: merging indexes (%d/%d), done.\n' % (count, total))
31 def merge_indexes(out, r1, r2):
32 for e in _simplify_iter([r1, r2]):
33 #if e.flags & index.IX_EXISTS:
38 def __init__(self, l):
45 self.cur = self.i.next()
51 def check_index(reader):
53 log('check: checking forward iteration...\n')
56 for e in reader.forward_iter():
58 log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n, e.name))
59 assert(e.children_ofs)
60 assert(e.name.endswith('/'))
61 assert(not d.get(e.children_ofs))
63 assert(not e or e.name == '/') # last entry is *always* /
64 log('check: checking normal iteration...\n')
71 log('index error! at %r\n' % e)
73 log('check: passed.\n')
76 def update_index(top):
77 ri = index.Reader(indexfile)
78 wi = index.Writer(indexfile)
79 rig = IterHelper(ri.iter(name=top))
80 tstart = int(time.time())
85 return (0, index.FAKE_SHA)
87 #log('doing: %r\n' % paths)
90 for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev):
91 #log('got: %r\n' % path)
93 progress('Indexing: %d\r' % total)
95 if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
96 sys.stdout.write('%s\n' % path)
98 while rig.cur and rig.cur.name > path: # deleted paths
102 if rig.cur and rig.cur.name == path: # paths that already existed
104 rig.cur.from_stat(pst, tstart)
105 if not (rig.cur.flags & index.IX_HASHVALID):
107 (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
108 rig.cur.flags |= index.IX_HASHVALID
112 #log('adding: %r\n' % path)
113 wi.add(path, pst, hashgen = hashgen)
114 progress('Indexing: %d, done.\n' % total)
122 log('check: before merging: oldfile\n')
124 log('check: before merging: newfile\n')
126 mi = index.Writer(indexfile)
127 merge_indexes(mi, ri, wr)
136 bup index <-p|s|m|u> [options...] <filenames...>
138 p,print print the index entries for the given names (also works with -u)
139 m,modified print only added/deleted/modified files (implies -p)
140 s,status print each filename with a status char (A/M/D) (implies -p)
141 H,hash print the hash for each object next to its name (implies -p)
142 l,long print more information about each file
143 u,update (recursively) update the index entries for the given filenames
144 x,xdev,one-file-system don't cross filesystem boundaries
145 fake-valid mark all index entries as up-to-date even if they aren't
146 check carefully check index file integrity
147 f,indexfile= the name of the index file (default 'index')
148 v,verbose increase log output (can be used more than once)
150 o = options.Options('bup index', optspec)
151 (opt, flags, extra) = o.parse(sys.argv[1:])
153 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
154 log('bup index: supply one or more of -p, -s, -m, -u, or --check\n')
156 if opt.fake_valid and not opt.update:
157 log('bup index: --fake-valid is meaningless without -u\n')
160 git.check_repo_or_die()
161 indexfile = opt.indexfile or git.repo('bupindex')
164 log('check: starting initial check.\n')
165 check_index(index.Reader(indexfile))
167 paths = index.reduce_paths(extra)
171 log('bup index: update (-u) requested but no paths given\n')
173 for (rp,path) in paths:
176 if opt['print'] or opt.status or opt.modified:
177 for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
179 and (ent.flags & index.IX_HASHVALID
181 or stat.S_ISDIR(ent.mode))):
185 if not ent.flags & index.IX_EXISTS:
187 elif not ent.flags & index.IX_HASHVALID:
188 if ent.sha == index.EMPTY_SHA:
195 line += "%7s " % oct(ent.mode)
197 line += ent.sha.encode('hex') + ' '
198 print line + (name or './')
202 log('check: starting final check.\n')
203 check_index(index.Reader(indexfile))
206 log('WARNING: %d errors encountered.\n' % len(saved_errors))