2 import os, sys, stat, time
3 from bup import options, git, index, drecurse
4 from bup.helpers import *
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():
35 log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
37 assert(e.children_ofs)
38 assert(e.name.endswith('/'))
39 assert(not d.get(e.children_ofs))
41 if e.flags & index.IX_HASHVALID:
42 assert(e.sha != index.EMPTY_SHA)
44 assert(not e or e.name == '/') # last entry is *always* /
45 log('check: checking normal iteration...\n')
52 log('index error! at %r\n' % e)
54 log('check: passed.\n')
57 def update_index(top):
58 ri = index.Reader(indexfile)
59 wi = index.Writer(indexfile)
60 rig = IterHelper(ri.iter(name=top))
61 tstart = int(time.time())
66 return (0100644, index.FAKE_SHA)
69 for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev):
70 if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
71 sys.stdout.write('%s\n' % path)
73 progress('Indexing: %d\r' % total)
74 elif not (total % 128):
75 progress('Indexing: %d\r' % total)
77 while rig.cur and rig.cur.name > path: # deleted paths
82 if rig.cur and rig.cur.name == path: # paths that already existed
84 rig.cur.from_stat(pst, tstart)
85 if not (rig.cur.flags & index.IX_HASHVALID):
87 (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
88 rig.cur.flags |= index.IX_HASHVALID
94 wi.add(path, pst, hashgen = hashgen)
95 progress('Indexing: %d, done.\n' % total)
103 log('check: before merging: oldfile\n')
105 log('check: before merging: newfile\n')
107 mi = index.Writer(indexfile)
108 merge_indexes(mi, ri, wr)
117 bup index <-p|m|u> [options...] <filenames...>
119 p,print print the index entries for the given names (also works with -u)
120 m,modified print only added/deleted/modified files (implies -p)
121 s,status print each filename with a status char (A/M/D) (implies -p)
122 H,hash print the hash for each object next to its name (implies -p)
123 l,long print more information about each file
124 u,update (recursively) update the index entries for the given filenames
125 x,xdev,one-file-system don't cross filesystem boundaries
126 fake-valid mark all index entries as up-to-date even if they aren't
127 fake-invalid mark all index entries as invalid
128 check carefully check index file integrity
129 f,indexfile= the name of the index file (default 'index')
130 v,verbose increase log output (can be used more than once)
132 o = options.Options('bup index', optspec)
133 (opt, flags, extra) = o.parse(sys.argv[1:])
135 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
136 o.fatal('supply one or more of -p, -s, -m, -u, or --check')
137 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
138 o.fatal('--fake-{in,}valid are meaningless without -u')
139 if opt.fake_valid and opt.fake_invalid:
140 o.fatal('--fake-valid is incompatible with --fake-invalid')
142 git.check_repo_or_die()
143 indexfile = opt.indexfile or git.repo('bupindex')
146 log('check: starting initial check.\n')
147 check_index(index.Reader(indexfile))
149 paths = index.reduce_paths(extra)
153 o.fatal('update (-u) requested but no paths given')
154 for (rp,path) in paths:
157 if opt['print'] or opt.status or opt.modified:
158 for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
160 and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
166 elif not ent.is_valid():
167 if ent.sha == index.EMPTY_SHA:
174 line += ent.sha.encode('hex') + ' '
176 line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
177 print line + (name or './')
179 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
180 log('check: starting final check.\n')
181 check_index(index.Reader(indexfile))
184 log('WARNING: %d errors encountered.\n' % len(saved_errors))