2 import os, sys, stat, time
3 import options, git, index, drecurse
7 def merge_indexes(out, r1, r2):
8 for e in index.MergeIter([r1, r2]):
9 # FIXME: shouldn't we remove deleted entries eventually? When?
14 def __init__(self, l):
21 self.cur = self.i.next()
27 def check_index(reader):
29 log('check: checking forward iteration...\n')
32 for e in reader.forward_iter():
34 log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n, e.name))
35 assert(e.children_ofs)
36 assert(e.name.endswith('/'))
37 assert(not d.get(e.children_ofs))
39 assert(not e or e.name == '/') # last entry is *always* /
40 log('check: checking normal iteration...\n')
47 log('index error! at %r\n' % e)
49 log('check: passed.\n')
52 def update_index(top):
53 ri = index.Reader(indexfile)
54 wi = index.Writer(indexfile)
55 rig = IterHelper(ri.iter(name=top))
56 tstart = int(time.time())
61 return (0100644, index.FAKE_SHA)
64 for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev):
65 if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
66 sys.stdout.write('%-70s\n' % path)
68 progress('Indexing: %d\r' % total)
69 elif not (total % 128):
70 progress('Indexing: %d\r' % total)
72 while rig.cur and rig.cur.name > path: # deleted paths
77 if rig.cur and rig.cur.name == path: # paths that already existed
79 rig.cur.from_stat(pst, tstart)
80 if not (rig.cur.flags & index.IX_HASHVALID):
82 (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
83 rig.cur.flags |= index.IX_HASHVALID
89 wi.add(path, pst, hashgen = hashgen)
90 progress('Indexing: %d, done.\n' % total)
98 log('check: before merging: oldfile\n')
100 log('check: before merging: newfile\n')
102 mi = index.Writer(indexfile)
103 merge_indexes(mi, ri, wr)
112 bup index <-p|m|u> [options...] <filenames...>
114 p,print print the index entries for the given names (also works with -u)
115 m,modified print only added/deleted/modified files (implies -p)
116 s,status print each filename with a status char (A/M/D) (implies -p)
117 H,hash print the hash for each object next to its name (implies -p)
118 l,long print more information about each file
119 u,update (recursively) update the index entries for the given filenames
120 x,xdev,one-file-system don't cross filesystem boundaries
121 fake-valid mark all index entries as up-to-date even if they aren't
122 fake-invalid mark all index entries as invalid
123 check carefully check index file integrity
124 f,indexfile= the name of the index file (default 'index')
125 v,verbose increase log output (can be used more than once)
127 o = options.Options('bup index', optspec)
128 (opt, flags, extra) = o.parse(sys.argv[1:])
130 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
131 o.fatal('supply one or more of -p, -s, -m, -u, or --check')
132 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
133 o.fatal('--fake-{in,}valid are meaningless without -u')
134 if opt.fake_valid and opt.fake_invalid:
135 o.fatal('--fake-valid is incompatible with --fake-invalid')
137 git.check_repo_or_die()
138 indexfile = opt.indexfile or git.repo('bupindex')
141 log('check: starting initial check.\n')
142 check_index(index.Reader(indexfile))
144 paths = index.reduce_paths(extra)
148 o.fatal('update (-u) requested but no paths given')
149 for (rp,path) in paths:
152 if opt['print'] or opt.status or opt.modified:
153 for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
155 and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
159 if not ent.flags & index.IX_EXISTS:
161 elif not ent.flags & index.IX_HASHVALID:
162 if ent.sha == index.EMPTY_SHA:
169 line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
171 line += ent.sha.encode('hex') + ' '
172 print line + (name or './')
175 log('check: starting final check.\n')
176 check_index(index.Reader(indexfile))
179 log('WARNING: %d errors encountered.\n' % len(saved_errors))